Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Question about mkl_sparse_d_export_csr


I am using mkl_sparse_d_export_csr to access the arrays rows_start and rows_end of the csr representation of my matrix. I have a question about memory allocation. Before call mkl_sparse_d_export_csr, I am allocating the 4 arrays for the matrix by using new. My programm crashes when I am trying to delete these arrays after using mkl_sparse_d_export_csr. I would like to know if the memory is allocated inside the function, as it looks like, or if it needs to be allocated before calling the function. I am assuming the answer is that it is allocated by the function and freed when I call mkl_sparse_destroy. Am I right?


0 Kudos
2 Replies

Hi Marc,

You correct, the arrays returned by export function allocated internally and deallocated by mkl_sparse_destroy




Hello Marc,

There is no need to allocate memory for mkl_sparse_._export output arrays. Routines such as mkl_sparse_._mm, mkl_sparse_._add, mkl_sparse_._spmm, mkl_sparse_._convert compute size of output sparse matrix arrays and allocate memory internally. To free this memory you simply call mkl_sparse_destroy. User only allocates input matrices for the above routines before passing them to mkl_sparse_create routine. The example of usage is the following:

/* Allocate input CSR arrays */
double *values_A = (double *)mkl_malloc(sizeof(double) * NNZ, ALIGN);
MKL_INT *columns_A = (MKL_INT *)mkl_malloc(sizeof(MKL_INT) * NNZ, ALIGN);
MKL_INT *rowIndex_A = (MKL_INT *)mkl_malloc(sizeof(MKL_INT) * (M + 1), ALIGN);

double *values_B = (double *)mkl_malloc(sizeof(double) * M, ALIGN);
MKL_INT *columns_B = (MKL_INT *)mkl_malloc(sizeof(MKL_INT) * M, ALIGN);
MKL_INT *rowIndex_B = (MKL_INT *)mkl_malloc(sizeof(MKL_INT) * (M + 1), ALIGN);

double *values_C;
MKL_INT *columns_C;
MKL_INT *rowIndex_C;

//some code here, preparing arrays, related to matrices.

/* Create handles for matrices A and B stored in CSR format */
mkl_sparse_d_create_csr( &csrA, SPARSE_INDEX_BASE_ZERO, M, M, rowIndex_A, rowIndex_A+1, columns_A, values_A );
mkl_sparse_d_create_csr( &csrB, SPARSE_INDEX_BASE_ZERO, M, M, rowIndex_B, rowIndex_B+1, columns_B, values_B );

// C = A*B
mkl_sparse_spmm( SPARSE_OPERATION_NON_TRANSPOSE, csrA, csrB, &csrC );

mkl_sparse_d_export_csr( csrC, &indexing, &rows, &cols, &pointerB_C, &pointerE_C, &columns_C, &values_C );
//some code here

 /* Release matrix handle. Not necessary to deallocate arrays for which we don't allocate memory: values_C, columns_C, pointerB_C, and pointerE_C.
These arrays will be deallocated together with csrC structure. */
mkl_sparse_destroy( csrC );

 //Release matrix handle and deallocate arrays for which we allocate memory ourselves.
mkl_sparse_destroy( csrA );
mkl_free(values_A); mkl_free(columns_A); mkl_free(rowIndex_A);

mkl_sparse_destroy( csrB );
mkl_free(values_B); mkl_free(columns_B); mkl_free(rowIndex_B);