Esempio n. 1
0
        //[Test]
        public void TestBiCGSTABSolver()
        {
            Stopwatch sw = new Stopwatch();

            float one  = 1.0f;
            float zero = 0.0f;

            _hiMatrixMN = new float[N * N];
            _hoVectorN  = new float[N];
            CreateDiagonalMatrix(_hiMatrixMN, N, 6);


            _hiVectorN  = new float[N];
            _hiVectorN2 = new float[N];
            FillBuffer(_hiVectorN2, 6);

            _diMatrixMN = _gpu.CopyToDevice(_hiMatrixMN);
            _diVectorN  = _gpu.Allocate(_hiVectorN);
            _diVectorN2 = _gpu.CopyToDevice(_hiVectorN2);

            _diPerRow   = _gpu.Allocate <int>(N);
            _diVectorP  = _gpu.Allocate <float>(N);
            _diVectorAX = _gpu.Allocate <float>(N);

            int nnz = _sparse.NNZ(N, N, _diMatrixMN, _diPerRow);

            _diCSRVals = _gpu.Allocate <float>(nnz);
            _diCSRCols = _gpu.Allocate <int>(nnz);
            _diCSRRows = _gpu.Allocate <int>(N + 1);

            // For temporary memory.
            float[] r0 = _gpu.Allocate <float>(N);
            float[] r  = _gpu.Allocate <float>(N);
            float[] v  = _gpu.Allocate <float>(N);
            float[] s  = _gpu.Allocate <float>(N);
            float[] t  = _gpu.Allocate <float>(N);

            _sparse.Dense2CSR(N, N, _diMatrixMN, _diPerRow, _diCSRVals, _diCSRRows, _diCSRCols);

            sw.Start();
            SolveResult result = _solver.BiCGSTAB(N, nnz, _diCSRVals, _diCSRRows, _diCSRCols, _diVectorN, _diVectorN2, _diVectorAX, r0, r, v, _diVectorP, s, t, 0.00001f, 1000);
            long        time   = sw.ElapsedMilliseconds;

            _sparse.CSRMV(N, N, nnz, ref one, _diCSRVals, _diCSRRows, _diCSRCols, _diVectorN, ref zero, _diVectorN2);

            _gpu.CopyFromDevice(_diVectorN2, _hoVectorN);

            float maxError = 0.0f;

            for (int i = 0; i < N; i++)
            {
                float error = Math.Abs(_hoVectorN[i] - _hiVectorN2[i]);

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

            Console.WriteLine("Time : {0} ms", time);
            Console.WriteLine("Iterate Count : {0}", result.IterateCount);
            Console.WriteLine("Residual : {0}", result.LastError);
            Console.WriteLine("max error : {0}", maxError);

            _gpu.FreeAll();
        }