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

## export dense matrix to CSR format, how to optimized?

New Contributor I
350 Views

Hello everyone,

I have a dense matrix A. I know the structure of A is sparse and I don't know the number or the location of non-zero elements in A.

I want to cover A to the sparse matrix by CSR format. The array of pointerB, pointerE,  column index, and nonzero value is the target output.

Here is my program:

``````
program Console24

implicit none
double precision A(5,5)
INTEGER nnz_A,i,j,t,k
integer, allocatable, dimension (:)::c_A
INTEGER ia(6),pB_A(5),pE_A(5)
double precision, allocatable, dimension (:)::v_A

A=0d0
A(3,4)=1.2d0
A(3,1)=1.2d0
A(3,3)=1.3d0
A(5,1)=1.7d0
A(2,4)=0.2d0
A(2,1)=4.2d0
A(2,3)=5.3d0
A(2,1)=3.7d0

nnz_A=count(A/=0)
allocate (c_A(nnz_A),v_A(nnz_A))
k=1
t=1
ia(1)=1
do j=1,5
do i=1,5
if (A(j,i)/=0) then
v_A(t)=A(j,i)
c_A(t)=i
t=t+1
endif
enddo
ia(j+1)=ia(j)+count(A(j,:)/=0)
enddo
pB_A=ia(1:5)
pE_A=ia(2:6)

end program Console24

``````

The code runs well but it takes so long time for the large size of A, for example A(100000,100000).  Is there any function in mkl library similar to my program or is there any way to speed up the calculation?