Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Meng__Xiangyi
Novice
64 Views

spblas memory question

program main
    use mkl_spblas
    use iso_c_binding
    implicit none
    type(SPARSE_MATRIX_T) A, B 
    type(MATRIX_DESCR) descA
    integer :: indexing = SPARSE_INDEX_BASE_ONE
    integer m, n, nnz, stat
    integer, dimension(:), allocatable :: row_ptr, col_indx
    real(8), dimension(:), allocatable :: values
    type(c_ptr) rows_start_c, rows_end_c, col_indx_c, values_c
    integer, pointer :: rows_start_f(:), rows_end_f(:), col_indx_f(:)
    real(8), pointer :: values_f(:)
    integer i, j
    
    m = 5
    n = 5
    nnz = 13
    allocate(row_ptr(m + 1))
    allocate(col_indx(nnz))
    allocate(values(nnz))
    row_ptr = (/ 1, 4, 6, 9, 12, 14 /)
    col_indx = (/ 1, 2, 4, 1, 2, 3, 4, 5, 1, 3, 4, 2, 5 /)
    values = (/ 1.d0, -1.d0, -3.d0, -2.d0, 5.d0, 4.d0, 6.d0, 4.d0, -4.d0, 2.d0, 7.d0, 8.d0, -5.d0 /)      
    stat = mkl_sparse_d_create_csr(A, indexing, m, n, row_ptr, row_ptr(2), col_indx, values)


    descA%type = SPARSE_MATRIX_TYPE_GENERAL
    stat = mkl_sparse_copy(A, descA, B)    
    stat = mkl_sparse_destroy(A)
    deallocate(row_ptr, col_indx, values)

    stat = mkl_sparse_d_export_csr(B, indexing, m, n, rows_start_c, rows_end_c, col_indx_c, values_c)
    stat = mkl_sparse_destroy(B)

    call c_f_pointer(rows_start_c, rows_start_f, )
    call c_f_pointer(rows_end_c  , rows_end_f  , )
    call c_f_pointer(col_indx_c  , col_indx_f  , [rows_end_f(m) - 1])
    call c_f_pointer(values_c    , values_f    , [rows_end_f(m) - 1])
    print *, '---------------------------------------------------'
    do i = 1, m
        print *, 'row #', i 
        do j = rows_start_f(i), rows_end_f(i) - 1
            print *, col_indx_f(j), values_f(j)
        end do 
    end do 
    print *, '---------------------------------------------------'

end program main

As the fortran code above, I construct a sparse matrix A , then I destroy A and free the memory allocated for row indices, column indices and values of A after I copy A to B. When I get the pointer for row indices, column indices and values of B, I again destroy B. The result is that I can still get the data of B after destroying it.

My question is that how mkl_sparse_copy allocate memory for indices and values of the destination matrix? Dose it use the function allocate?If so, mkl_sparse_destroy seems not to free those memories. If this will case memory leak?

 

 ---------------------------------------------------
 row #           1
           1   1.00000000000000     
           2  -1.00000000000000     
           4  -3.00000000000000     
 row #           2
           1  -2.00000000000000     
           2   5.00000000000000     
 row #           3
           3   4.00000000000000     
           4   6.00000000000000     
           5   4.00000000000000     
 row #           4
           1  -4.00000000000000     
           3   2.00000000000000     
           4   7.00000000000000     
 row #           5
           2   8.00000000000000     
           5  -5.00000000000000     
 --------------------------------------------------- 

0 Kudos
1 Reply
Kirill_V_Intel
Employee
64 Views

Hello,

The routine mkl_sparse_copy uses mkl_malloc for allocating memory.

It is not a memory leak. I am not sure why exactly the data are still printable after you destroy the matrix B and I believe there should be cases when it won't, but there is no memory leak (checked with valgrind and also with mkl_mem_stat).

Hope this helps!

Best,
Kirill

Reply