# How to parallelize the following code by OpenMP?

Dear all,

The following code is used to calculate y = A * x as the function mkl_?coogemv:

do i=1,n ! matrix A is the size of n*n
y(i)=0.0d0
enddo
do i=1,nnz  ! nnz is the number of entries of A
ii = irow(i) ! irow is the row number index of entries
jj = jcol(i) ! jcol is the column number index of entries
y(ii) = y(ii) + A(i)*x(jj)
enddo

Could anyone give me some suggestion about how to parallelize the code by OpenMP?

If the matrix is symmetric, additional operations are needed. The sequential version of code is as follows:

do i=1,n ! matrix A is the size of n*n
y(i)=0.0d0
enddo
do i=1,nnz  ! nnz is the number of entries of A, only half of the elements
! of the matrix are stored
ii = irow(i) ! irow is the row number index of entries
jj = jcol(i) ! jcol is the column number index of entries
y(ii) = y(ii) + A(i)*x(jj)
if(issymmetric.and.ii/=jj)y(jj)=y(jj)+A(i)*x(ii)
enddo

Thanks,
Zhanghong Tang[/code]

Hi Zhanghong Tang,

Unless you can guarantee that all values of irow and jcol are unique, then the code is not parallelizable. You could use the “critical” or “atomic” clauses when operating on the “y(ii)” variable to ensure the read and write of “y(ii)” is only performed by one thread at a time. However, your performance will be very poor.

• Mat

Dear Mat,

Thank you very much for your kindly reply. What I want to do is sparse matrix-vector product, there are similar function mkl_?coogemv in Intel MKL. I am wondering how does Intel MKL parallelize this function.

Thanks,
Zhanghong Tang