예제 #1
0
        public void Multiply()
        {
            ColumnWiseMatrix m1 = new ColumnWiseMatrix(10, 10, 1.2345f);

            DeviceManager.CheckDeviceSanity();
            var _m1 = m1.GetMatrix <float>();

            ColumnWiseMatrix m2 = new ColumnWiseMatrix(10, 10, 9.8765f);

            DeviceManager.CheckDeviceSanity();
            var _m2 = m2.GetMatrix <float>();

            var m3 = m1 * m2;

            DeviceManager.CheckDeviceSanity();
            var _m3 = m3.GetMatrix <float>();

            for (int i = 0; i < m1.nRows; ++i)
            {
                for (int j = 0; j < m1.nCols; ++j)
                {
                    double m1m2 = 0.0;
                    for (int k = 0; k < m1.nCols; ++k)
                    {
                        m1m2 += _m1[i, k] * _m2[k, j];
                    }
                    Assert.IsTrue(Math.Abs(m1m2 - _m3[i, j]) <= 5e-5);
                }
            }
        }
예제 #2
0
        public void Allocation()
        {
            var m1 = new ColumnWiseMatrix(10, 5, 2, MemorySpace.Device, MathDomain.Int);

            m1.Print();
            DeviceManager.CheckDeviceSanity();

            var m2 = new ColumnWiseMatrix(10, 5, 1.234f, MemorySpace.Device, MathDomain.Float);

            m2.Print();
            DeviceManager.CheckDeviceSanity();

            var m3 = new ColumnWiseMatrix(10, 5, 1.2345, MemorySpace.Device, MathDomain.Double);

            m3.Print();
            DeviceManager.CheckDeviceSanity();

            var m4 = new ColumnWiseMatrix(10, 5, 1, MemorySpace.Host, MathDomain.Int);

            m4.Print();
            DeviceManager.CheckDeviceSanity();

            var m5 = new ColumnWiseMatrix(10, 5, 1.234f, MemorySpace.Host, MathDomain.Float);

            m5.Print();
            DeviceManager.CheckDeviceSanity();

            var m6 = new ColumnWiseMatrix(10, 5, 1.2345, MemorySpace.Host, MathDomain.Double);

            m6.Print();
            DeviceManager.CheckDeviceSanity();
        }
예제 #3
0
        public void Dot()
        {
            ColumnWiseMatrix m1 = new ColumnWiseMatrix(10, 10, 1.2345f);

            DeviceManager.CheckDeviceSanity();
            var _m1 = m1.GetMatrix <float>();

            Vector v1 = new Vector(10, 9.8765f);

            DeviceManager.CheckDeviceSanity();
            var _v1 = v1.Get <float>();

            var v2 = m1 * v1;

            DeviceManager.CheckDeviceSanity();
            var _v2 = v2.Get <float>();

            for (int i = 0; i < m1.nRows; ++i)
            {
                double m1v1 = 0.0;
                for (int j = 0; j < m1.nCols; ++j)
                {
                    m1v1 += _m1[i, j] * _v1[j];
                }
                Assert.IsTrue(Math.Abs(m1v1 - _v2[i]) <= 5e-5);
            }
        }
예제 #4
0
        public void KroneckerProduct()
        {
            Vector u = new Vector(128, 0.01);

            DeviceManager.CheckDeviceSanity();
            var _u = u.Get <float>();

            Vector v = new Vector(64, 0.02);

            DeviceManager.CheckDeviceSanity();
            var _v = v.Get <float>();

            var A = ColumnWiseMatrix.KroneckerProduct(u, v, 2.0);

            DeviceManager.CheckDeviceSanity();
            var _A = A.GetMatrix <float>();

            for (int i = 0; i < A.nRows; ++i)
            {
                for (int j = 0; j < A.nCols; ++j)
                {
                    double expected = 2.0 * _u[i] * _v[j];
                    double err      = Math.Abs(expected - _A[i, j]);
                    Assert.IsTrue(err <= 5e-4, String.Format("i({0}) j({1}) err({2})", i, j, err));
                }
            }
        }
예제 #5
0
        private void Compress <T>(ColumnWiseMatrix denseMatrix) where T : struct, IEquatable <T>, IFormattable
        {
            var hostDenseMatrix = denseMatrix.GetMatrix <T>();

            List <T>   nonZeroValues        = new List <T>();
            List <int> nonZeroColumnIndices = new List <int>();
            List <int> nNonZeroRows         = new List <int>();
            int        nNonZeros            = 0;

            for (int i = 0; i < denseMatrix.nRows; i++)
            {
                for (int j = 0; j < denseMatrix.nCols; j++)
                {
                    if (Math.Abs(Convert.ToDouble(hostDenseMatrix[i, j])) > 1e-7)
                    {
                        nNonZeros++;
                        nonZeroValues.Add(hostDenseMatrix[i, j]);
                        nonZeroColumnIndices.Add(j);
                    }
                }

                nNonZeroRows.Add(nNonZeros);
            }

            Buffer.size = (uint)nNonZeros;

            values = new Vector(nonZeroValues.Count, memorySpace, mathDomain);
            values.ReadFrom(nonZeroValues, nonZeroValues.Count);

            this.nonZeroColumnIndices = new Vector(nonZeroColumnIndices.Count, memorySpace, MathDomain.Int);
            this.nonZeroColumnIndices.ReadFrom(nonZeroColumnIndices, nonZeroColumnIndices.Count);

            this.nNonZeroRows = new Vector(nNonZeroRows.Count, memorySpace, MathDomain.Int);
            this.nNonZeroRows.ReadFrom(nNonZeroRows, nNonZeroRows.Count);
        }
예제 #6
0
        /// <summary>
        /// Copy denseVector to host, numerically finds the non-zero indices, and then copy back to device
        /// </summary>
        public CompressedSparseRowMatrix(ColumnWiseMatrix denseMatrix)
            : base(false, denseMatrix.memorySpace, denseMatrix.mathDomain)
        {
            _buffer = new SparseMemoryTile(0, 0, 0, 0, (uint)denseMatrix.nRows, (uint)denseMatrix.nCols, denseMatrix.memorySpace, denseMatrix.mathDomain);

            switch (denseMatrix.mathDomain)
            {
            case MathDomain.Int:
                Compress <int>(denseMatrix);
                break;

            case MathDomain.Float:
                Compress <float>(denseMatrix);
                break;

            case MathDomain.Double:
                Compress <double>(denseMatrix);
                break;

            default:
                break;
            }

            SyncPointers();
        }
예제 #7
0
        public void Invert()
        {
            ColumnWiseMatrix A = GetInvertibleMatrix(128);

            DeviceManager.CheckDeviceSanity();

            ColumnWiseMatrix AMinus1 = new ColumnWiseMatrix(A);

            AMinus1.Invert();
            DeviceManager.CheckDeviceSanity();

            var eye      = A.Multiply(AMinus1);
            var _eye     = eye.GetMatrix <float>();
            var _A       = A.GetMatrix <float>();
            var _AMinus1 = AMinus1.GetMatrix <float>();

            for (int i = 0; i < A.nRows; ++i)
            {
                for (int j = 0; j < A.nCols; ++j)
                {
                    double expected = i == j ? 1.0 : 0.0;
                    Assert.IsTrue(Math.Abs(_eye[i, j] - expected) <= 5e-5);
                }
            }
        }
예제 #8
0
        public void Solve()
        {
            ColumnWiseMatrix A = GetInvertibleMatrix(128);

            DeviceManager.CheckDeviceSanity();
            var _A = A.GetMatrix <float>();

            ColumnWiseMatrix B = GetInvertibleMatrix(128, 2345);

            DeviceManager.CheckDeviceSanity();
            var _B = B.GetMatrix <float>();

            A.Solve(B);
            DeviceManager.CheckDeviceSanity();
            var _x = B.Get <float>();

            var BSanity  = A.Multiply(B);
            var _BSanity = BSanity.GetMatrix <float>();

            for (int i = 0; i < A.nRows; ++i)
            {
                for (int j = 0; j < A.nCols; ++j)
                {
                    double expected = _B[i, j];
                    Assert.IsTrue(Math.Abs(_BSanity[i, j] - expected) <= 5e-5);
                }
            }
        }
예제 #9
0
        public ColumnWiseMatrix Multiply(ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None, MatrixOperation rhsOperation = MatrixOperation.None, double alpha = 1.0)
        {
            ColumnWiseMatrix ret = new ColumnWiseMatrix(nRows, rhs.nCols, memorySpace, rhs.mathDomain);

            Multiply(ret, rhs, lhsOperation, rhsOperation, alpha);

            return(ret);
        }
예제 #10
0
        public void Copy()
        {
            var m1 = new ColumnWiseMatrix(10, 5, 1, MemorySpace.Device, MathDomain.Int);

            m1.Print();
            var m1Copy = new ColumnWiseMatrix(m1);

            DeviceManager.CheckDeviceSanity();
            m1Copy.Print();
            Assert.AreEqual(m1, m1Copy);

            var m2 = new ColumnWiseMatrix(10, 5, 1.234f, MemorySpace.Device, MathDomain.Float);

            m2.Print();
            var m2Copy = new ColumnWiseMatrix(m2);

            DeviceManager.CheckDeviceSanity();
            m2Copy.Print();
            Assert.AreEqual(m2, m2Copy);

            var m3 = new ColumnWiseMatrix(10, 5, 1.2345, MemorySpace.Device, MathDomain.Double);

            m3.Print();
            var m3Copy = new ColumnWiseMatrix(m3);

            DeviceManager.CheckDeviceSanity();
            m3Copy.Print();
            Assert.AreEqual(m3, m3Copy);

            var m4 = new ColumnWiseMatrix(10, 5, 1, MemorySpace.Host, MathDomain.Int);

            m4.Print();
            var m4Copy = new ColumnWiseMatrix(m4);

            DeviceManager.CheckDeviceSanity();
            m4Copy.Print();
            Assert.AreEqual(m4, m4Copy);

            var m5 = new ColumnWiseMatrix(10, 5, 1.234f, MemorySpace.Host, MathDomain.Float);

            m5.Print();
            var m5Copy = new ColumnWiseMatrix(m5);

            DeviceManager.CheckDeviceSanity();
            m5Copy.Print();
            Assert.AreEqual(m5, m5Copy);

            var m6 = new ColumnWiseMatrix(10, 5, 1.2345, MemorySpace.Host, MathDomain.Double);

            m6.Print();
            var m6Copy = new ColumnWiseMatrix(m6);

            DeviceManager.CheckDeviceSanity();
            m6Copy.Print();
            Assert.AreEqual(m6, m6Copy);
        }
예제 #11
0
        public void MatrixSerializationInversion()
        {
            ColumnWiseMatrix A = new ColumnWiseMatrix(10, 20, 5.0);

            A.ToBinaryFile <float>("A.zf");

            ColumnWiseMatrix B = new ColumnWiseMatrix("A.zf", MathDomain.Float);

            Assert.AreEqual(A, B);
        }
예제 #12
0
        public void LinSpace()
        {
            var m = ColumnWiseMatrix.LinSpace(10, 5, 0.0, 1.0);

            DeviceManager.CheckDeviceSanity();

            var _m = m.Get <float>();

            Assert.IsTrue(System.Math.Abs(_m[0] - 0.0) <= 1e-7);
            Assert.IsTrue(System.Math.Abs(_m[_m.Count - 1] - 1.0) <= 1e-7);
        }
예제 #13
0
        public void RandomUniform()
        {
            var m = ColumnWiseMatrix.RandomUniform(10, 5, 1234);

            DeviceManager.CheckDeviceSanity();

            var _m = m.Get <float>();

            foreach (var iter in _m)
            {
                Assert.IsTrue(iter >= 0.0 && iter <= 1.0);
            }
        }
예제 #14
0
        public void RandomGaussian()
        {
            var m = ColumnWiseMatrix.RandomGaussian(10, 5, 1234);

            DeviceManager.CheckDeviceSanity();

            var _m = m.Get <float>();

            for (int i = 0; i < _m.Count / 2; ++i)
            {
                Assert.IsTrue(System.Math.Abs(_m[2 * i] + _m[2 * i + 1]) <= 1e-7);
            }
        }
예제 #15
0
        static ColumnWiseMatrix GetInvertibleMatrix(int nRows, int seed = 1234)
        {
            var A  = ColumnWiseMatrix.RandomUniform(nRows, nRows, seed);
            var _A = A.GetMatrix <float>();

            for (int i = 0; i < nRows; ++i)
            {
                _A[i, i] += 2;
            }

            A.ReadFrom(_A);
            return(A);
        }
예제 #16
0
        public static ColumnWiseMatrix operator *(CompressedSparseRowMatrix lhs, ColumnWiseMatrix rhs)
        {
            Debug.Assert(rhs.nRows == lhs.nCols);
            Debug.Assert(lhs.memorySpace == rhs.memorySpace);
            Debug.Assert(lhs.mathDomain == rhs.mathDomain);
            Debug.Assert(lhs.Buffer.pointer != 0);
            Debug.Assert(rhs.Buffer.pointer != 0);

            ColumnWiseMatrix ret = new ColumnWiseMatrix(lhs.nRows, rhs.nCols, lhs.memorySpace, lhs.mathDomain);

            CuSparseApi.SparseMultiply(ret.Buffer as MemoryTile, lhs._buffer, rhs.Buffer as MemoryTile, lhs.nRows, rhs.nRows, MatrixOperation.None, MatrixOperation.None, 1.0);

            return(ret);
        }
예제 #17
0
        /// <summary>
        /// Same version as above, but gives the possibility of reusing the output buffer
        /// </summary>
        /// <param name="output"></param>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        /// <param name="rhsOperation"></param>
        /// <param name="alpha"></param>
        public void Multiply(ColumnWiseMatrix output, ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None, MatrixOperation rhsOperation = MatrixOperation.None, double alpha = 1.0)
        {
            Debug.Assert(rhs.nRows == nCols);
            Debug.Assert(output.nRows == nRows);
            Debug.Assert(output.nCols == rhs.nCols);
            Debug.Assert(memorySpace == rhs.memorySpace);
            Debug.Assert(memorySpace == output.memorySpace);
            Debug.Assert(mathDomain == rhs.mathDomain);
            Debug.Assert(mathDomain == output.mathDomain);
            Debug.Assert(Buffer.pointer != 0);
            Debug.Assert(rhs.Buffer.pointer != 0);
            Debug.Assert(output.Buffer.pointer != 0);

            CuSparseApi.SparseMultiply(output.Buffer as MemoryTile, _buffer, rhs.Buffer as MemoryTile, nRows, rhs.nRows, lhsOperation, rhsOperation, alpha);
        }
예제 #18
0
        public void Eye()
        {
            var m = ColumnWiseMatrix.Eye(10);

            m.Print();
            DeviceManager.CheckDeviceSanity();

            var _m = m.GetMatrix <float>();

            for (int i = 0; i < m.nRows; ++i)
            {
                for (int j = 0; j < m.nCols; ++j)
                {
                    Assert.AreEqual(j == i ? 1.0 : 0.0, _m[i, j]);
                }
            }
        }
예제 #19
0
        public void ColumnWiseArgAbsoluteMinMax()
        {
            ColumnWiseMatrix A = ColumnWiseMatrix.LinSpace(128, 64, -1, 1);
            var _A             = A.GetMatrix <float>();

            var min = A.ColumnWiseAbsoluteMinimumIndex().GetRaw <int>();
            var max = A.ColumnWiseAbsoluteMaximumIndex().GetRaw <int>();

            int[] _min = new int[A.nCols];
            int[] _max = new int[A.nCols];
            for (int j = 0; j < A.nCols; j++)
            {
                _min[j] = _A.Column(j).AbsoluteMinimumIndex();
                _max[j] = _A.Column(j).AbsoluteMaximumIndex();

                Assert.AreEqual(min[j] - 1, _min[j]);
                Assert.AreEqual(max[j] - 1, _max[j]);
            }
        }
예제 #20
0
        public void GetColumn()
        {
            var m1 = new ColumnWiseMatrix(10, 5, 1.2345f);

            m1.Print();
            DeviceManager.CheckDeviceSanity();

            for (int j = 0; j < m1.nCols; ++j)
            {
                var col = m1.Get <float>(j);
                DeviceManager.CheckDeviceSanity();

                Assert.AreEqual(col.Count, m1.nRows);
                for (int i = 0; i < m1.nRows; ++i)
                {
                    Assert.IsTrue(Math.Abs(col[i] - 1.2345f) <= 1e-7);
                }
            }
        }
예제 #21
0
        public void SetColumn()
        {
            var m1 = new ColumnWiseMatrix(10, 5, 1.2345f);

            m1.Print();
            DeviceManager.CheckDeviceSanity();
            var _m1 = m1.Get <float>();

            var v1 = new Vector(10, 2.3456f);

            v1.Print();
            DeviceManager.CheckDeviceSanity();
            var _v1 = v1.Get <float>();

            m1.Set(v1, 3);

            for (int j = 0; j < m1.nCols; ++j)
            {
                var col = m1.Get <float>(j);
                DeviceManager.CheckDeviceSanity();

                Assert.AreEqual(col.Count, m1.nRows);
                if (j != 3)
                {
                    for (int i = 0; i < m1.nRows; ++i)
                    {
                        Assert.IsTrue(Math.Abs(col[i] - _m1[i + m1.nRows * j]) <= 1e-7);
                    }
                }
                else
                {
                    for (int i = 0; i < m1.nRows; ++i)
                    {
                        Assert.IsTrue(Math.Abs(col[i] - _v1[i]) <= 1e-7);
                    }
                }
            }
        }
예제 #22
0
        public void AddMatrix()
        {
            ColumnWiseMatrix m1 = ColumnWiseMatrix.LinSpace(100, 20, -1.0, 1.0);

            DeviceManager.CheckDeviceSanity();
            var _m1 = m1.GetMatrix <float>();

            ColumnWiseMatrix m2 = ColumnWiseMatrix.RandomGaussian(m1.nRows, m1.nCols, 1234);

            DeviceManager.CheckDeviceSanity();
            var _m2 = m2.GetMatrix <float>();

            var m3 = m1 + m2;

            DeviceManager.CheckDeviceSanity();
            var _m3 = m3.GetMatrix <float>();

            for (int i = 0; i < m1.nRows; ++i)
            {
                for (int j = 0; j < m1.nCols; ++j)
                {
                    Assert.IsTrue(Math.Abs(_m3[i, j] - _m1[i, j] - _m2[i, j]) <= 3e-7, String.Format("i({0}) j({1}) err({2})", i, j, Math.Abs(_m3[i, j] - _m1[i, j] - _m2[i, j])));
                }
            }

            var m4 = ColumnWiseMatrix.Add(m1, m2, alpha: 2.0);

            DeviceManager.CheckDeviceSanity();
            var _m4 = m4.GetMatrix <float>();

            for (int i = 0; i < m1.nRows; ++i)
            {
                for (int j = 0; j < m1.nCols; ++j)
                {
                    Assert.IsTrue(Math.Abs(_m4[i, j] - _m1[i, j] - 2.0 * _m2[i, j]) <= 5e-7, String.Format("i({0}) j({1}) err({2})", i, j, Math.Abs(_m4[i, j] - _m1[i, j] - 2.0 * _m2[i, j])));
                }
            }
        }
예제 #23
0
        public void Multiply()
        {
            int[]  _NonZeroCols   = new int[] { 0, 1, 1, 3, 2, 3, 4, 5 };
            Vector gpuNonZeroCols = new Vector(_NonZeroCols);

            int[]  _NonZeroRows   = new int[] { 0, 2, 4, 7, 8 };
            Vector gpuNonZeroRows = new Vector(_NonZeroRows);

            CompressedSparseRowMatrix m1 = new CompressedSparseRowMatrix(4, 6, gpuNonZeroCols, gpuNonZeroRows, 1.2345f, MathDomain.Float);

            DeviceManager.CheckDeviceSanity();
            var _m1 = m1.GetMatrix <float>();

            ColumnWiseMatrix m2 = new ColumnWiseMatrix(6, 8, 9.8765f, MemorySpace.Device, MathDomain.Float);

            DeviceManager.CheckDeviceSanity();
            var _m2 = m2.GetMatrix <float>();

            var m3 = m1 * m2;

            DeviceManager.CheckDeviceSanity();
            var _m3 = m3.GetMatrix <float>();

            for (int i = 0; i < m1.nRows; ++i)
            {
                for (int j = 0; j < m2.nCols; ++j)
                {
                    double m1m2 = 0.0;
                    for (int k = 0; k < m1.nCols; ++k)
                    {
                        m1m2 += _m1[i, k] * _m2[k, j];
                    }
                    Assert.IsTrue(Math.Abs(m1m2 - _m3[i, j]) <= 5e-5);
                }
            }
        }