# Vectorization of Jacobi iteration

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?

• You can store diagonal value as first element of each row data in the CSR format. – cfdlab Oct 1 '19 at 4:31
• Sounds good and it works quite well. Would you mind to put the comment as an answer to close this question? – 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

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


For others ways, see section 3.4 of

Y. Saad, Iterative methods for sparse linear systems PDF