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

How to access the number of non-zero elements in sparse_matrix_t?

Nishimura__Akihiko
1,950 Views

I am writing a Python wrapper for calling the 'mkl_sparse_spmm' function. 

In order to export the result of matrix-matrix multiplication to a Python object, I need to know the size of the 'col_idx' or 'values' array in the MKL export routines. How could I get a hold of it?

Incidentally, the documentation for the 'mkl_sparse_spmm' function does not state the format of the returned matrix 'C'. Is it the same format as the matrix 'A' (or 'B')?

0 Kudos
1 Solution
Spencer_P_Intel
Employee
1,950 Views

Hi Aki,

Please note that sparse_matrix_t should be viewed as a black box for users.  We do not advertise what is in it and it allows us the flexibility to change internal structures to enable better performance at our convenience without breaking user code.  The only way to extract anything from it is through using the provided mkl_sparse_?_export_XXX routines (for instance XXX=csr or csc or coo or bsr).  

For example, if you are using CSR matrix format, once you have extracted the rows_start, rows_end, col_indx, and values arrays using the CSR export routines, as well as the indexing, rows and cols integers, you can determine the number of non zeros from the rows_start array:

int nnz = rows_start[rows];

Now, the arrays for matrix C would be returned in the format that the export function uses, ie,  if you use mkl_sparse_?_export_csr, then it is csr format.

Please note that these arrays provided from export CSR routine are the actual internal CSR arrays in the matrix handle.  That is, they still belong to the matrix handle and would be deleted if mkl_sparse_destroy(C) is called. Think of the export routine as a window into the data.  The best use case, if you truly need this data for use outside of mkl functions is to copy the data from them to your own arrays.  At this point you already know the indexing of the internal data and can also convert it to your desired indexing (0 or 1 based) format while copying.  Please note that since these arrays are internal data, if they are modified, it could break future calls to mkl sparse routines using C, so modify at your own risk :).  See the Inspector Executor Sparse BLAS data types currently supported: https://software.intel.com/en-us/mkl-developer-reference-c-sparse-matrix-storage-formats-for-inspector-executor-sparse-blas-routines. 

I hope this helps!

Spencer

View solution in original post

0 Kudos
6 Replies
Spencer_P_Intel
Employee
1,950 Views

Hi Akihiko,

In order to access the matrix 'C', you must use the mkl_sparse_?_export_csr  type exporters routines.  This extracts the internal data from sparse_matrix_t, the matrix handle, ( note that mkl_sparse_?_create_csr() creates a matrix handle (sparse_matrix_t) which internally can be defined in any manner that makes things simple for the desired library functions.  This internal representation is however not externally defined. The export routines are provided to extract the desired values and arrays from that handle.) and returns you the desired values and arrays in the CSR format.  See for example the "mkl/examples/spblasc/source/sparse_spmm.c" example C code for using the export routine after a call to spmm. 

I hope this helps!

Best,

Spencer

 

0 Kudos
Nishimura__Akihiko
1,950 Views

Thank you for the reply, but it did not answer my question.

I have already used 'mkl_sparse_?_export_csr' to extract the pointers to all the data I need. The problem is, I don't know the lengths of the contiguous memories because I don't know the number of non-zero elements in the matrix 'C'. How could I extract this information from, say, 'sparse_matrix_t'?

Also, the documentation for the 'mkl_sparse_spmm' function does not state the format of the returned matrix 'C'. Is it the same format as the matrix 'A' (or 'B')?

Thank you for your help,
Aki

0 Kudos
Spencer_P_Intel
Employee
1,951 Views

Hi Aki,

Please note that sparse_matrix_t should be viewed as a black box for users.  We do not advertise what is in it and it allows us the flexibility to change internal structures to enable better performance at our convenience without breaking user code.  The only way to extract anything from it is through using the provided mkl_sparse_?_export_XXX routines (for instance XXX=csr or csc or coo or bsr).  

For example, if you are using CSR matrix format, once you have extracted the rows_start, rows_end, col_indx, and values arrays using the CSR export routines, as well as the indexing, rows and cols integers, you can determine the number of non zeros from the rows_start array:

int nnz = rows_start[rows];

Now, the arrays for matrix C would be returned in the format that the export function uses, ie,  if you use mkl_sparse_?_export_csr, then it is csr format.

Please note that these arrays provided from export CSR routine are the actual internal CSR arrays in the matrix handle.  That is, they still belong to the matrix handle and would be deleted if mkl_sparse_destroy(C) is called. Think of the export routine as a window into the data.  The best use case, if you truly need this data for use outside of mkl functions is to copy the data from them to your own arrays.  At this point you already know the indexing of the internal data and can also convert it to your desired indexing (0 or 1 based) format while copying.  Please note that since these arrays are internal data, if they are modified, it could break future calls to mkl sparse routines using C, so modify at your own risk :).  See the Inspector Executor Sparse BLAS data types currently supported: https://software.intel.com/en-us/mkl-developer-reference-c-sparse-matrix-storage-formats-for-inspector-executor-sparse-blas-routines. 

I hope this helps!

Spencer

0 Kudos
Nishimura__Akihiko
1,950 Views

Your answer solved all my problems, thank you very much!

Aki

0 Kudos
Nishimura__Akihiko
1,950 Views

A few things thing that are not clear from Spencer's answer and I discovered myself: 

- `mkl_sparse_spmm`, when calling it with CSR (or CSC) matrices, seem to return the `C` matrix in the CSR (or CSC) format.

- `mkl_sparse_spmm` seems to fail when the matices `A` and `B` are in different formats.

- `mkl_sparse_?_export_XXX` does not carry out an automatic conversion of matrix formats, e.g. your matrix needs to be already in the CSR format if calling `mkl_sparse_?_export_csr`.

Hope these tips are helpful to other users in the future!

Aki

0 Kudos
Connor_Wong
Beginner
1,860 Views

Hi @Nishimura__Akihiko , 

Could you teach me how to bind the sparse blas MKL routines in Python? Do you have any example Python script? I tried to use ctypes but do not know how to handle "sparse_matrix_t" in Python.

Thanks in advance!

0 Kudos
Reply