Exemple #1
0
        public void Test_BLAS2_TPSV()
        {
            // Solve Ax = y, and x overwrites y.

            ClearBuffer(hiMatrixANN);
            ClearBuffer(hiMatrixAPS);
            ClearBuffer(hiVectorXN);
            ClearBuffer(hiVectorYN);

            diMatrixA  = _gpu.Allocate(hiMatrixAPS);
            diVectorYN = _gpu.Allocate(hiVectorYN);

            // Because of there is no singular test, set matrix A to simple maindiagonal only matrix.
            CreateMainDiagonalOnlyMatrix(hiMatrixANN, N);
            FillBuffer(hiVectorYN);

            // Lower fill mode
            PackSymmetricMatrix(hiMatrixANN, hiMatrixAPS, N, cublasFillMode.Lower);
            _gpu.CopyToDevice(hiMatrixAPS, diMatrixA);

            // Test without transpose
            _gpu.CopyToDevice(hiVectorYN, diVectorYN);

            _blas.TPSV(N, diMatrixA, diVectorYN);

            _gpu.CopyFromDevice(diVectorYN, hiVectorXN);

            // Check solution error : y - Ax by elemental wise.
            double maxError = 0.0;

            for (int i = 0; i < N; i++)
            {
                double ax = 0.0;

                for (int j = 0; j < N; j++)
                {
                    ax += hiMatrixANN[GetIndexColumnMajor(i, j, N)] * hiVectorXN[j];
                }

                double error = Math.Abs(ax - hiVectorYN[i]);

                if (maxError < error)
                {
                    maxError = error;
                }
            }

            Console.WriteLine("max error : {0} (lower fill mode, without transpose)", maxError);

            // Test with transpose
            _gpu.CopyToDevice(hiVectorYN, diVectorYN);

            _blas.TPSV(N, diMatrixA, diVectorYN, cublasOperation.T);

            _gpu.CopyFromDevice(diVectorYN, hiVectorXN);

            // Check solution error : y - Ax by elemental wise.
            maxError = 0.0;

            for (int j = 0; j < N; j++)
            {
                double ax = 0.0;

                for (int i = 0; i < N; i++)
                {
                    ax += hiMatrixANN[GetIndexColumnMajor(i, j, N)] * hiVectorXN[i];
                }

                double error = Math.Abs(ax - hiVectorYN[j]);

                if (maxError < error)
                {
                    maxError = error;
                }
            }

            Console.WriteLine("max error : {0} (lower fill mode, with transpose)", maxError);

            // Upper fill mode
            PackSymmetricMatrix(hiMatrixANN, hiMatrixAPS, N, cublasFillMode.Upper);
            _gpu.CopyToDevice(hiMatrixAPS, diMatrixA);

            // Test without transpose
            _gpu.CopyToDevice(hiVectorYN, diVectorYN);

            _blas.TPSV(N, diMatrixA, diVectorYN, cublasOperation.N, cublasFillMode.Upper);

            _gpu.CopyFromDevice(diVectorYN, hiVectorXN);

            // Check solution error : y - Ax by elemental wise.
            maxError = 0.0;

            for (int i = 0; i < N; i++)
            {
                double ax = 0.0;

                for (int j = 0; j < N; j++)
                {
                    ax += hiMatrixANN[GetIndexColumnMajor(i, j, N)] * hiVectorXN[j];
                }

                double error = Math.Abs(ax - hiVectorYN[i]);

                if (maxError < error)
                {
                    maxError = error;
                }
            }

            Console.WriteLine("max error : {0} (upper fill mode, without transpose)", maxError);

            // Test with transpose
            _gpu.CopyToDevice(hiVectorYN, diVectorYN);

            _blas.TPSV(N, diMatrixA, diVectorYN, cublasOperation.T, cublasFillMode.Upper);

            _gpu.CopyFromDevice(diVectorYN, hiVectorXN);

            // Check solution error : y - Ax by elemental wise.
            maxError = 0.0;

            for (int j = 0; j < N; j++)
            {
                double ax = 0.0;

                for (int i = 0; i < N; i++)
                {
                    ax += hiMatrixANN[GetIndexColumnMajor(i, j, N)] * hiVectorXN[i];
                }

                double error = Math.Abs(ax - hiVectorYN[j]);

                if (maxError < error)
                {
                    maxError = error;
                }
            }

            Console.WriteLine("max error : {0} (upper fill mode, with transpose)", maxError);

            _gpu.FreeAll();
        }