Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Intel Community
- Software
- Software Development SDKs and Libraries
- Intel® oneAPI Math Kernel Library
- How to access the number of non-zero elements in sparse_matrix_t?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Nishimura__Akihiko

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-09-2019
04:31 PM

422 Views

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

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')?

1 Solution

Spencer_P_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-12-2019
02:40 PM

422 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

Link Copied

6 Replies

Spencer_P_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-11-2019
12:03 PM

422 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

Nishimura__Akihiko

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-12-2019
11:33 AM

422 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

Spencer_P_Intel

Employee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-12-2019
02:40 PM

423 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

Nishimura__Akihiko

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-12-2019
04:06 PM

422 Views

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

Aki

Nishimura__Akihiko

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-14-2019
10:33 AM

422 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

Connor_Wong

Beginner

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-26-2020
06:48 AM

332 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!

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

For more complete information about compiler optimizations, see our Optimization Notice.