 ## Example code for solving a sparse linear system Ax=d

General CULA Sparse support and troubleshooting. Use this forum if you are having a general problem or have encountered a bug.

### Example code for solving a sparse linear system Ax=d

Hello,

I am interesting in solving the linear system A.x = d using cudatools (the specific example is actually sparse and tri-diagonal matrix). Is there any tutorials or example code which I can follow which will show how to set-up the matices and perform the solution?

Best wishes,

Dan
boyfarrell

Posts: 3
Joined: Tue Mar 12, 2013 5:37 pm

### Re: Example code for solving a sparse linear system Ax=d

Are you interested in solving dense matrices? If so look into CULA Dense. Otherwise, we'll need a little more information to provide some help.
kyle

Posts: 301
Joined: Fri Jun 12, 2009 7:47 pm

### Re: Example code for solving a sparse linear system Ax=d

Hello Kyle,

Thanks for the reply. No I am explicitly interested in solving sparse systems, which are tridiagonal. For example take the Poisson equation, .

The matrix and vector on the left are A and x, respectively. The vector on the right is d and all the terms are known. Therefore the system has the form A.x=d, I wish to solve this system of equations to find the (only) unknown, vector x. This is quite a standard linear system.

Best wishes,

Dan.
boyfarrell

Posts: 3
Joined: Tue Mar 12, 2013 5:37 pm

### Re: Example code for solving a sparse linear system Ax=d

Ah, I misunderstood your question. We have an example in the SDK demonstrating how to solve a traditional system using CULA Sparse. The bulk of the example is in a single call to:

Which uses the Cuda platform, takes double precision (D) coordinate matrix (Coo) as input, and uses the conjugate gradient solver (Cg) to solve the system. There are also parameters to further specify options such as tolerance and maximum number of iterations.
kyle

Posts: 301
Joined: Fri Jun 12, 2009 7:47 pm

### Re: Example code for solving a sparse linear system Ax=d

Hello Kyle,

Fantastic! I will give it a go.

Is there any example code I could follow for solving sparse system? For example, I imagine there is plenty of boilerplate that will need to be setup etc.

Best wishes,
boyfarrell

Posts: 3
Joined: Tue Mar 12, 2013 5:37 pm

### Re: Example code for solving a sparse linear system Ax=d

Here is an incomplete example showing a large portion of the boilerplate needed:

Code: Select all
`    // cula-specific items    culaStatus status;    culaIterativeConfig config;    culaIterativeResult result;    culaCgOptions cgOptions;    culaEmptyOptions emptyOptions;    // enable sparse solvers and select a GPU device    status = culaSparseInitialize();    // check status    // ...    // set configuration options to defaults    culaIterativeConfigInit(&config);    culaCgOptionsInit(&cgOptions);    culaEmptyOptionsInit(&emptyOptions);    // change configuration options if needed    // ...    // run routine    status = culaDcooCg(&config, &cgOptions, &emptyOptions, N, nnz, an, colInd, rowInd, x, b, &result);    // check status    // ...    // clean up    culaSparseShutdown();`
kyle 