This project is read-only.

Error of AccessViolationException at second call of CSRSV_SOLVE

Jun 11, 2014 at 3:54 AM
I made a very simple code with CSRSV_SOLVE();
First call of CSRSV_SOLVE was successfully finished and the solution is correct.
However, I got "AccessViolationException" at second call of the method.

I attach my code and error messages as follows.

Does Anybody have ideas to solve it?
    class TestSparseSolve
    {
        static public void Execute()
        {
            CudafyModule km = CudafyTranslator.Cudafy();
            GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
            GPGPUBLAS blas = GPGPUBLAS.Create(gpu);
            GPGPUSPARSE sparse = GPGPUSPARSE.Create(gpu);

            /*
             * Solve L*x=b
             * L = |1 0|,  b= |1| ------> x = |1|
             *     |2 3|      |5|             |1|
            */

            //Matrix L
            double[] val = new double[] { 1, 2, 3 };
            int[] col = new int[] { 0, 0, 1 };
            int[] row = new int[] { 0, 1, 3 };
            int m = 2;
            int nnz = 3;
            double[] devVal = gpu.Allocate<double>(val);
            int[] devCol = gpu.Allocate<int>(col);
            int[] devRow = gpu.Allocate<int>(row);
            gpu.CopyToDevice<double>(val, devVal);
            gpu.CopyToDevice<int>(col, devCol);
            gpu.CopyToDevice<int>(row, devRow);

            //Vector b
            double[] b = new double[] { 1, 5 };
            double[] devB = gpu.Allocate<double>(b);
            gpu.CopyToDevice<double>(b, devB);

            //Vector x
            double[] x0 = new double[] { 0, 0 };
            double[] devX = gpu.Allocate<double>(x0);
            gpu.CopyToDevice<double>(x0, devX);
            
            double one = 1.0;
            cusparseSolveAnalysisInfo info = new cusparseSolveAnalysisInfo();
            sparse.CreateSolveAnalysisInfo(ref info);
            cusparseMatDescr descr = cusparseMatDescr.DefaultTriangular();
            cusparseOperation op = cusparseOperation.NonTranspose;
            sparse.CSRSV_ANALYSIS(m, nnz, devVal, devRow, devCol, op, info, descr);
            
            //step 1 ---> success
            sparse.CSRSV_SOLVE(m, ref one, devVal, devRow, devCol, devB, devX, op, info, descr);

            //step 2 ---> failed with "AccessViolationException"
            sparse.CSRSV_SOLVE(2, ref one, devVal, devRow, devCol, devB, devX, op, info, descr);

            double[] x = new double[2];
            gpu.CopyFromDevice<double>(devX, x);

            Console.WriteLine("Solution: x = (" + x[0] + ", " + x[1] + ")");

        }
    }
<<error message>> Sorry, the message includes some Japanese.
System.AccessViolationException はハンドルされませんでした。
HResult=-2147467261
Message=保護されているメモリに読み取りまたは書き込み操作を行おうとしました。他のメモリが壊れていることが考えられます。
Source=Cudafy.NET
StackTrace:
   場所 Cudafy.Maths.SPARSE.CUSPARSEDriver64.cusparseDcsrsv_solve_v2(cusparseHandle handle, cusparseOperation transA, Int32 m, Double& alpha, cusparseMatDescr descrA, IntPtr csrValA, IntPtr csrRowPtrA, IntPtr csrColIndA, cusparseSolveAnalysisInfo info, IntPtr x, IntPtr y)
   場所 Cudafy.Maths.SPARSE.CUSPARSEDriver64.CusparseDcsrsv_solve(cusparseHandle handle, cusparseOperation transA, Int32 m, Double& alpha, cusparseMatDescr descrA, IntPtr csrValA, IntPtr csrRowPtrA, IntPtr csrColIndA, cusparseSolveAnalysisInfo info, IntPtr x, IntPtr y)
   場所 Cudafy.Maths.SPARSE.CudaSPARSE.CSRSV_SOLVE(Int32 m, Double& alpha, Double[] csrValA, Int32[] csrRowA, Int32[] csrColA, Double[] x, Double[] y, cusparseOperation op, cusparseSolveAnalysisInfo info, cusparseMatDescr descrA)
   場所 PoissonSolver.TestSparseSolve.Execute() 場所 C:\Users\takagawa\Documents\Visual Studio 2010\Projects\PoissonSolver\PoissonSolver\Program12_.cs:行 88
   場所 PoissonSolver.Program.Main(String[] args) 場所 C:\Users\takagawa\Documents\Visual Studio 2010\Projects\PoissonSolver\PoissonSolver\Program12_.cs:行 28
   場所 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   場所 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   場所 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   場所 System.Threading.ThreadHelper.ThreadStart()
InnerException:
Jun 14, 2014 at 10:32 AM
imo, the call failed on the 1st invocation, it just seems it did in the 2nd. and the msg seems to imply that the mem for the matrix wasn't properly allocated with the correct sizes.
Jun 17, 2014 at 1:07 PM
Thank you pedritolo!

I agree with your opinion, but I don't know how to correct the code.
Is there any sample code of CSRSV_SOLVE?
It must be helpful not only for me, but also other users.