Hello; <div>I had a quick question about using PETSc inside parallel for loop in C language. </div><div><span>In more detail, I have couple of lines of matrix algebra which is written by using PETSc inside a for loop that I would like to parallelize it. </span></div>
<div><span>Here is the code that I have written:</span><br>
</div><div><span><div><br></div><div><div>MPI_Comm_size(MPI_COMM_WORLD,&Np);</div><div>MPI_Comm_rank(MPI_COMM_WORLD,&myid);</div><div>for (j=myid*(nw/Np);j<(myid+1)*(nw/Np);j++)</div><div>{</div><div> MatCreate(PETSC_COMM_WORLD,&Ypcq);</div>
<div> MatSetSizes(Ypcq,PETSC_DECIDE,PETSC_DECIDE,ns*tindex_f,1);</div><div> MatSetFromOptions(Ypcq);</div><div> for (k=0; k<ns*tindex_f; ++k)</div><div> {</div><div> tmp=*(Ymat +j*nstindexf + k);</div>
<div> MatSetValues(Ypcq,1,&k,1,&col,&tmp,INSERT_VALUES);</div><div> }</div><div> MatAssemblyBegin(Ypcq,MAT_FINAL_ASSEMBLY);</div><div> MatAssemblyEnd(Ypcq,MAT_FINAL_ASSEMBLY);</div>
<div> MatAXPY(Ypcq,-1,Mean,DIFFERENT_NONZERO_PATTERN);</div><div> // Evaluation of Ypcq Transpose:</div><div> MatTranspose(Ypcq,MAT_INITIAL_MATRIX,&YpcqT);</div><div> MatAssemblyBegin(YpcqT,MAT_FINAL_ASSEMBLY);</div>
<div> MatAssemblyEnd(YpcqT,MAT_FINAL_ASSEMBLY);</div><div><br></div><div> MatCreate(PETSC_COMM_WORLD,&Para);</div><div> MatSetSizes(Para,PETSC_DECIDE,PETSC_DECIDE,np,1);</div><div> MatSetFromOptions(Para);</div>
<div><br></div><div> for (i=0; i<np; ++i)</div><div> {</div><div> MatSetValues(Para,1,&i,1,&col,&rhsp[np*j+i],INSERT_VALUES);</div><div> }</div><div> MatAssemblyBegin(Para,MAT_FINAL_ASSEMBLY);</div>
<div> MatAssemblyEnd(Para,MAT_FINAL_ASSEMBLY);</div><div> MatAXPY(Para,-1,XYmat,DIFFERENT_NONZERO_PATTERN);</div><div> MatAssemblyBegin(Para,MAT_FINAL_ASSEMBLY);</div><div> MatAssemblyEnd(Para,MAT_FINAL_ASSEMBLY);</div>
<div> MatMatMult(Para,YpcqT,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&InnProd);</div><div> MatAssemblyBegin(InnProd,MAT_FINAL_ASSEMBLY);</div><div> MatAssemblyEnd(InnProd,MAT_FINAL_ASSEMBLY);</div><div>
<br></div><div> MatAssemblyBegin(Pzy,MAT_FINAL_ASSEMBLY);</div><div> MatAssemblyEnd(Pzy,MAT_FINAL_ASSEMBLY);</div><div> MatAXPY(Pzy,rhsw[j],InnProd,DIFFERENT_NONZERO_PATTERN);</div><div> MatAssemblyBegin(Pzy,MAT_FINAL_ASSEMBLY);</div>
<div> MatAssemblyEnd(Pzy,MAT_FINAL_ASSEMBLY);</div><div><br></div><div> MatDestroy(&InnProd);</div><div> MatDestroy(&Ypcq);</div><div> MatDestroy(&YpcqT);</div><div> MatDestroy(&Para);</div>
<div>}</div><div>MPI_Reduce(&Pzy,&Pzy,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);</div><div>MatView(Pzy,PETSC_VIEWER_STDOUT_WORLD);</div></div><div><br></div></span></div><div><span><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">
I am trying to parallelize this loop using MPI, but I don't get the right result. I appreciate if anyone could help me on that?</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br></div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Thanks in advance</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Reza</div></span></div>