Dear Barry:<br><br>Thank you for your reply. Generally, for security, after I do some matrix operations by calling some functions in PETSc, I should call the assembly functions based on your advice. Whether is there a mechanism/method that the users should always call assembly functions but the code will judge whether it should do the assembly? If there is, I think that it should save some time :).<br>
<br>Regards,<br>Yujie<br><br><div><span class="gmail_quote">On 3/3/08, <b class="gmail_sendername">Barry Smith</b> <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
<div><div><br></div> Based on your comments I have simplified and improved the code slightly it is now<div><span class="q"><div>PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)</div></span><div>{</div>
<div> PetscErrorCode ierr;</div><div> Mat At,Bt,Ct;</div><div><br></div><div> PetscFunctionBegin;</div><span class="q"><div> ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);</div></span><span class="q"><div>
ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);</div></span><span class="q"><div> ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr);</div></span><span class="q"><div> ierr = MatDestroy(At);CHKERRQ(ierr);</div>
</span><span class="q"><div> ierr = MatDestroy(Bt);CHKERRQ(ierr);</div></span><span class="q"><div> ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);</div></span><span class="q"><div> ierr = MatDestroy(Ct);CHKERRQ(ierr);</div>
</span><div> PetscFunctionReturn(0);</div><div>}</div><div><br></div><div>#undef __FUNCT__</div><div>#define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ"</div><div>PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C)</div>
<div>{</div><div> PetscErrorCode ierr;</div><div> PetscInt m=A->rmap.n,n=B->cmap.n;</div><div> Mat Cmat;</div><div><br></div><div> PetscFunctionBegin;</div><div> if (A->cmap.n != B->rmap.n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap.n %d != B->rmap.n %d\n",A->cmap.n,B->rmap.n);</div>
<div> ierr = MatCreate(A->hdr.comm,&Cmat);CHKERRQ(ierr);</div><div> ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);</div><div> ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr);</div>
<div> ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);</div><div> ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);</div><div> ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);</div>
<div> *C = Cmat;</div><div> PetscFunctionReturn(0);</div><div>}</div><div><br></div><div>As you note there was no need to call the assembly after the transpose. It is, needed, in general where I put it</div><div>in the symbolic portion of the code. (You could avoid it by directly setting some flags in the Cmat object</div>
<div>but it is better to do the right thing and call the assembly (which is free in terms of time anyways compare to the</div><div>numerical calculations).</div><div><br></div><span class="sg"><div> Barry</div></span><div>
<span class="e" id="q_11875a469d88cd5b_12"><div><br></div><div><div>On Mar 3, 2008, at 10:59 AM, Yujie wrote:</div><br><blockquote type="cite">Dear Barry:<br><br>Why do you call these two functions in your code? Because I have realized the same method with yours to calculate MPIDENSE_MPIAIJ. However, I didn't call MatAssemblyBegin and MatAssemblyEnd after I call MatTranspose for matrix C. <br>
In application, sometimes I don't know what do the functions I call in PETSc do? So, I don't know whether I should call these two functions although you give me these conditions. Regarding this example, which condition should it belong to? thanks a lot.<br>
<br>Regards,<br>Yujie <br><br><div><span class="gmail_quote">On 3/3/08, <b class="gmail_sendername">Barry Smith</b> <<a href="mailto:bsmith@mcs.anl.gov" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">bsmith@mcs.anl.gov</a>> wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
<br> MatAssembly does<br> 1) moves any values set on one process that belongs on another<br> 2) finalizes the sparse matrix data structure (for example takes any<br> holes out of the CSR format)<br> doesn't need to do this for dense matrices<br>
3) sets up whatever VecScatters that are need to do matrix vector<br> products<br> 4) marks the matrix as assembled and ready to be used<br><br><br> Barry<br><br><br> On Mar 2, 2008, at 11:47 PM, Yujie wrote:<br><br> > Hi, everyone<br>
><br> > I just check the codes added by Barry for MPIDENSE_MPIAIJ. The codes<br> > is as follows. I am wondering what context one may call<br> > MatAssemblyBegin and MatAssemblyEnd?<br> > From the webpage, I can find some information about two functions:<br>
> "Begins assembling the matrix. This routine should be called after<br> > completing all calls to MatSetValues()."<br> > I mean that I don't know how to judge whether I should call two<br> > functions. Could you give me some advice? thanks a lot.<br>
><br> > 4723 */<br> > 4724 PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat<br> > B,Mat C)<br> > 4725 {<br> > 4726 PetscErrorCode ierr;<br> > 4727 Mat_MPIDense *sub_c = (Mat_MPIDense*)C->data;<br>
> 4728 Mat At,Bt,Ct;<br> > 4729<br> > 4730 PetscFunctionBegin;<br> > 4731 ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr);<br> > 4732 ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr);<br>
> 4733 ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,<br> > 1.0,&Ct);CHKERRQ(ierr);<br> > 4734 ierr = MatDestroy(At);CHKERRQ(ierr);<br> > 4735 ierr = MatDestroy(Bt);CHKERRQ(ierr);<br> > 4736 ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr);<br>
> 4737 ierr = MatDestroy(Ct);CHKERRQ(ierr);<br> > 4738<br> > 4739 C->assembled = PETSC_TRUE;<br> > 4740 ierr = MatAssemblyBegin(sub_c-<br> > >A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br> > 4741 ierr = MatAssemblyEnd(sub_c->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);<br>
> 4742 if (!C->was_assembled) {<br> > 4743 ierr = MatSetUpMultiply_MPIDense(C);CHKERRQ(ierr);<br> > 4744 }<br> > 4745 PetscFunctionReturn(0);<br> > 4746 }<br> ><br> ><br> ><br> > Regards,<br>
> Yujie<br><br></blockquote></div><br></blockquote></div><br></span></div></div></div></blockquote></div><br>