Assume I have a linear system of $A x = b$ which I want to solve with Jacobi iteration. Matrix $A$ is given in CSR format. The vectors are dense.
The code for Jacobi iteration is quite clear and can be looked up fast in the internet. Now I want to vectorize the code but I do not really find a good way to handle the if condition where it is decided if we have a diagonal value or a non-diagonal value of the matrix.
Any ideas or maybe code examples?

  • 1
    $\begingroup$ You can store diagonal value as first element of each row data in the CSR format. $\endgroup$ – cfdlab Oct 1 '19 at 4:31
  • $\begingroup$ Sounds good and it works quite well. Would you mind to put the comment as an answer to close this question? $\endgroup$ – vydesaster Oct 4 '19 at 14:41

In CSR format, data need not be arranged in a contiguous way. The elements in a row can be arranged in any order since their indices are also included in the data. This allows us to store the diagonal element as the first element for easy access which can be useful for many iterative schemes. This strategy makes sense if the diagonal element is always non-zero.

For example, consider this matrix

d00 d01 0   0   d04
0   d11 0   d13 d14
0   d21 d22 0   d24
0   0   d32 d33 0
d40 0   d42 0   d44

The non-zero elements can be stored as


For others ways, see section 3.4 of

Y. Saad, Iterative methods for sparse linear systems PDF

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.