Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6975 Discussions

operate on diagonal of a sparse matrix in csr format

Robert_E_
Beginner
668 Views
hi, I want to efficiently operate only on the diagonal of a large non-singular sparse_status_t matrix (in csr format). More precisely, if 'A' is my matrix then I want to do: A - alpha*I where 'alpha' is a positive scalar and 'I' is the Identity. I could create the matrix alpha*I and do a sparse matrix - sparse matrix - multiplication, but that would take too much space and time. Is there a better way to do this? best regards, Franz
0 Kudos
1 Solution
mecej4
Honored Contributor III
668 Views

First of all, the CSR representation of A is a compact representation, and may have only a few or no diagonal elements at all. Therefore, if you wish to modify the diagonal, you must already have a full diagonal, possibly many or all elements of which are filled with zero values.

If you wish to do the diagonal update repeatedly, it would be efficient to pre-compute and store a separate array, say, ID(1:N), containing the column indices of the diagonal elements -- the subset of JA corresponding to the diagonal.

View solution in original post

0 Kudos
4 Replies
mecej4
Honored Contributor III
669 Views

First of all, the CSR representation of A is a compact representation, and may have only a few or no diagonal elements at all. Therefore, if you wish to modify the diagonal, you must already have a full diagonal, possibly many or all elements of which are filled with zero values.

If you wish to do the diagonal update repeatedly, it would be efficient to pre-compute and store a separate array, say, ID(1:N), containing the column indices of the diagonal elements -- the subset of JA corresponding to the diagonal.

0 Kudos
Robert_E_
Beginner
668 Views
another question: I observed some strange behavior from the sparse_matrix_t handle. here is what I did: I exported the pointers to the internal structure via mkl_sparse_?_export_csr and changed some values. To check if the changes were successful I exported the internal data again. The values seem to have changed. But when I call mkl_sparse_?_mv afterwards the old values are used. I have to create a new handle right before the call to mkl_sparse_?_mv so that the new values are used. I couldn't find any detailed information on sparse_matrix_t handles, I've searched the C-reference manual and the user guide. I can also provide some code if necessary.
0 Kudos
MariaZh
Employee
668 Views

Hi Franz,

It is an expected behavior in your case. 

IE SpBLAS perform a lot of different optimizations on a user's data. However, the data itself will not be changed during calls (one exception here is mkl_sparse_order routine).
mkl_sparse_?_export_csr allows one to export the initial pointers or to get CSR representation of resulting matrix (e.g. after mkl_sparse_spmm).
If you need to change several values in the handle you can use mkl_sparse_?_set_value routine. Please, refer to https://software.intel.com/en-us/mkl-developer-reference-c-mkl-sparse-set-value

Hope, this will help!
Best regards,
Maria

0 Kudos
Robert_E_
Beginner
668 Views
Hi Maria, Thanks for the advice. I tried the set_value routine but that didn't change anything. The problem was that my tiny 9x9 test-matrix had too many non-zero entries (only 2-3 zeros). So I guess the mkl library is expecting the matrix to be sparse independent of its size. Using a sparser matrix resolved the issue even without incorporating the set_value routine. Kind regards, Franz
0 Kudos
Reply