<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0; margin-bottom:0">Hi Mark,</p>
<p style="margin-top:0; margin-bottom:0"> Will the parameter <span>MatMPIAIJSetPreallocation </span>in <span style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols; font-size:16px">influence
the following part</span></p>
<div> <span style="color:rgb(208,92,18)"> do i=mystart,nelem,nproc</span></div>
<div><span style="color:rgb(208,92,18)"> call ptSystem%getElementalMAT(i, Ae, auxRHSe, idx)</span></div>
<div><span style="color:rgb(208,92,18)"> ne=size(idx)</span></div>
<div><span style="color:rgb(208,92,18)"> idx=idx-1 !-1 since PETSC index starts from zero </span></div>
<div><span style="color:rgb(208,92,18)"> if (allocated(auxRHSe)) call VecSetValues(bvec,ne,idx,auxRHSe,ADD_VALUES,ierr)</span></div>
<div><span style="color:rgb(208,92,18)"> call MatSetValues(Amat,ne,idx,ne,idx,Ae,ADD_VALUES,ierr)</span></div>
<div><span style="color:rgb(208,92,18)"> end do</span></div>
<p></p>
<p style="margin-top:0; margin-bottom:0">I found this part will impede my assembling process. In my case ,the total DOF is 20800. I estimated the upper bound of number of nonzero entries in each row as 594. So I set f9 to be 20206 and f6 to be 10103 in the
following command:</p>
<p style="margin-top:0; margin-bottom:0"><span style="color: rgb(208, 92, 18);"> call MatMPIAIJSetPreallocation(Amat,f9,PETSC_NULL_INTEGER,f6,PETSC_NULL_INTEGER, ierr)</span><br>
</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<p style="margin-top:0; margin-bottom:0">Running the code in sequential mode with -info I got the following information.And I can get the result even thought the assembling process is kind of slow(several times slower than using Mumps).</p>
<p style="margin-top:0; margin-bottom:0"></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyBegin_MPIBAIJ(): Stash has 0 entries,uses 0 mallocs.</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyBegin_MPIBAIJ(): Block-Stash has 0 entries, uses 0 mallocs.</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Matrix size: 20800 X 20800, block size 1; storage space: 13847 unneeded, 1030038 used</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Number of mallocs during MatSetValues is 62659</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Most nonzeros blocks in any row is 70</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatCheckCompressedRow(): Found the ratio (num_zerorows 0)/(num_localrows 20800) < 0.6. Do not use CompressedRow routines.</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PetscCommDuplicate(): Using internal PETSc communicator 4462889472 140509341618528</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PetscCommDuplicate(): Using internal PETSc communicator 4462889472 140509341618528</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PetscCommDuplicate(): Using internal PETSc communicator 4462889472 140509341618528</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] VecScatterCreate_Seq(): Special case: sequential copy</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Matrix size: 20800 X 0, block size 1; storage space: 104000 unneeded, 0 used</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Number of mallocs during MatSetValues is 0</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyEnd_SeqBAIJ(): Most nonzeros blocks in any row is 0</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatCheckCompressedRow(): Found the ratio (num_zerorows 20800)/(num_localrows 20800) > 0.6. Use CompressedRow routines.</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>aseembel time
<span class="Apple-converted-space"> </span>282.54871800000001<span class="Apple-converted-space"> </span></span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PetscCommDuplicate(): Using internal PETSc communicator 4462888960 140509341442256</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Setting up PC for first time</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] PCSetUp(): Leaving PC with identical preconditioner since operator is unchanged</span></p>
<br>
<p></p>
<p style="margin-top:0; margin-bottom:0">However, when I use the parallel mode,I got the following information:</p>
<p style="margin-top:0; margin-bottom:0"></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyBegin_MPIBAIJ(): Stash has 707965 entries,uses 6 mallocs.</span></p>
<p style="margin: 0px; font: 11px Menlo;"><span style="font-variant-ligatures: no-common-ligatures;">[0] MatAssemblyBegin_MPIBAIJ(): Block-Stash has 0 entries, uses 0 mallocs.</span></p>
<br>
<p></p>
<p style="margin-top:0; margin-bottom:0">it seems it never went to <span> <span style="color: rgb(208, 92, 18);">call MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY,ierr)</span></span></p>
<p style="margin-top:0; margin-bottom:0">Is there anything I am doing wrong?</p>
<p style="margin-top:0; margin-bottom:0">Thanks</p>
<p style="margin-top:0; margin-bottom:0"><br>
</p>
<div id="Signature">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:Calibri,Arial,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p><span style="color:rgb(33,33,33); font-size:13.3333330154419px">Yaxiong Chen, </span><br style="color:rgb(33,33,33); font-size:13.3333330154419px">
<span style="color:rgb(33,33,33); font-size:13.3333330154419px">Ph.D. Student </span><br style="color:rgb(33,33,33); font-size:13.3333330154419px">
<br style="color:rgb(33,33,33); font-size:13.3333330154419px">
<span style="font-size:13.3333330154419px"></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">School of Mechanical Engineering, 3171 </span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px"><span style="color:rgb(34,34,34); font-family:Roboto,arial,sans-serif; font-size:13px">585 Purdue Mall</span></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">West Lafayette, IN 47907</span></p>
<div><span style="font-size:13.3333px"><br>
</span></div>
<p></p>
<p><br style="color:rgb(33,33,33); font-size:13.3333330154419px">
<br>
</p>
</div>
</div>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Mark Adams <mfadams@lbl.gov><br>
<b>Sent:</b> Tuesday, January 29, 2019 10:02 PM<br>
<b>To:</b> Yaxiong Chen; PETSc users list<br>
<b>Subject:</b> Re: [petsc-users] PETSC matrix assembling super slow</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">Optimized is a configuration flag not a versions.</div>
<div dir="ltr"><br>
</div>
You need to figure out your number of non-zeros per row of you global matrix, or a bound on it, and supply that in MatMPIAIJSetPreallocation. Otherwise it has to allocate and copy memory often.
<div><br>
</div>
<div>You could increase your f9 on a serial run and see what runs best and then move to parallel with a value in f6 of about 1/2 of f9.<br>
<div><br>
</div>
<div>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Tue, Jan 29, 2019 at 9:13 PM Yaxiong Chen <<a href="mailto:chen2018@purdue.edu" target="_blank" id="LPlnk217381" class="OWAAutoLink" previewremoved="true">chen2018@purdue.edu</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0px; margin-bottom:0px">Thanks Mark, </p>
<p style="margin-top:0px; margin-bottom:0px"> I use PETSC 3.9.4, is this the optimized version you called?</p>
<p style="margin-top:0px; margin-bottom:0px"> Actually f9 and f6 are from the PETSC example. I don't know how to set the value correctly so I commend them. The size of my elemental matrix may vary. For 2D problem, the size of elemental matrix can be 24*24
or 32*32 or some other sizes. And the index is not continuous. In this case, the elemental matrix may interlace with each other in the global matrix, and I may have thousands of elemental matrix to be assembled. Does the preallocating suitable for this?</p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007Signature">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:Calibri,Arial,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p><span style="color:rgb(33,33,33); font-size:13.3333px">Yaxiong Chen, </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="color:rgb(33,33,33); font-size:13.3333px">Ph.D. Student </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="font-size:13.3333px"></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">School of Mechanical Engineering, 3171 </span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px"><span style="color:rgb(34,34,34); font-family:Roboto,arial,sans-serif; font-size:13px">585 Purdue Mall</span></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">West Lafayette, IN 47907</span></p>
<div><span style="font-size:13.3333px"><br>
</span></div>
<p></p>
<p><br style="color:rgb(33,33,33); font-size:13.3333px">
<br>
</p>
</div>
</div>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block; width:98%">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007divRplyFwdMsg" dir="ltr">
<font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Mark Adams <<a href="mailto:mfadams@lbl.gov" target="_blank" id="LPlnk439924" class="OWAAutoLink" previewremoved="true">mfadams@lbl.gov</a>><br>
<b>Sent:</b> Tuesday, January 29, 2019 8:25 PM<br>
<b>To:</b> Yaxiong Chen<br>
<b>Cc:</b> Song Gao; <a href="mailto:petsc-users@mcs.anl.gov" target="_blank" id="LPlnk864745" class="OWAAutoLink" previewremoved="true">
petsc-users@mcs.anl.gov</a><br>
<b>Subject:</b> Re: [petsc-users] PETSC matrix assembling super slow</font>
<div> </div>
</div>
<div>
<div dir="ltr">Slow assembly is often from not preallocating correctly. I am guessing that you are using Q1 element and f9==9, and thus the preallocation should be OK if this is a scalar problem on a regular grid and f6-==6 should be OK for the off processor
allocation, if my assumptions are correct.
<div><br>
</div>
<div>You can run with -info, which will tell you how many allocation were done in assembly. Make sure that it is small (eg, 0).</div>
<div><br>
I see you use f90 array stuff 'idx-1'. Compilers can sometimes do crazy things with seeming simple code. You could just do this manually if you can find anything else.</div>
<div><br>
</div>
<div>And I trust you are running an optimized version of PETSc.<br>
<div><br>
</div>
</div>
</div>
<br>
<div class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail_quote">
<div dir="ltr" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail_attr">
On Tue, Jan 29, 2019 at 6:22 PM Yaxiong Chen via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" id="LPlnk65224" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" previewremoved="true">petsc-users@mcs.anl.gov</a>>
wrote:<br>
</div>
<blockquote class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0px; margin-bottom:0px">Hi Song,</p>
<p style="margin-top:0px; margin-bottom:0px">I don't quite understand how I can use this command. I don't partition the gloabl matrix. If I add my elemental matrix to the global system it will be like this. And in my parallel part, I use each core to generate
the elemental matrix in turn. In this case, I guess each core will be assigned the space for global matrix and finally be assembled.But according to the manual, it seems each core will store a part of the global matrix. I<span style="font-size:12pt">s the </span><span style="font-size:12pt; font-family:Times">local
submatrix in the </span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatMPIAIJSetPreallocation.html#MatMPIAIJSetPreallocation" id="LPlnk2672" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">MatMPIAIJSetPreallocation</a><span style="font-family:Times; font-size:12pt">(</span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/Mat.html#Mat" id="LPlnk348188" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">Mat</a><span style="font-family:Times; font-size:12pt">
B,</span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscInt.html#PetscInt" id="LPlnk515249" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">PetscInt</a><span style="font-family:Times; font-size:12pt">
d_nz,const </span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscInt.html#PetscInt" id="LPlnk93764" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">PetscInt</a><span style="font-family:Times; font-size:12pt">
d_nnz[],</span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscInt.html#PetscInt" id="LPlnk449820" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">PetscInt</a><span style="font-family:Times; font-size:12pt">
o_nz,const </span><a href="https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscInt.html#PetscInt" id="LPlnk136081" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" style="font-family:Times; font-size:12pt" previewremoved="true">PetscInt</a><span style="font-family:Times; font-size:12pt">
o_nnz[])the same as my elemental matrix? </span></p>
<p style="margin-top:0px; margin-bottom:0px"><span style="font-family:Times"><a name="x_m_789429084943478815_m_3717110541932564007_x_m_9052320459455506416_MatMPIAIJSetPreallocation" id="LPlnk635186" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" style="font-family:Times" previewremoved="true"></a></span></p>
<h1><img size="3840" id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416img659" tabindex="0" style="max-width: 100%; user-select: none;" data-outlook-trace="F:1|T:1" src="cid:1689caf2cd3f456b1e51"><br>
</h1>
<div><br>
</div>
<div>Thanks</div>
<p></p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416Signature">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:Calibri,Arial,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p><span style="color:rgb(33,33,33); font-size:13.3333px">Yaxiong Chen, </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="color:rgb(33,33,33); font-size:13.3333px">Ph.D. Student </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="font-size:13.3333px"></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">School of Mechanical Engineering, 3171 </span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px"><span style="color:rgb(34,34,34); font-family:Roboto,arial,sans-serif; font-size:13px">585 Purdue Mall</span></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">West Lafayette, IN 47907</span></p>
<div><span style="font-size:13.3333px"><br>
</span></div>
<p></p>
<p><br style="color:rgb(33,33,33); font-size:13.3333px">
<br>
</p>
</div>
</div>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block; width:98%">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416divRplyFwdMsg" dir="ltr">
<font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Song Gao <<a href="mailto:song.gao2@mail.mcgill.ca" id="LPlnk126994" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" previewremoved="true">song.gao2@mail.mcgill.ca</a>><br>
<b>Sent:</b> Tuesday, January 29, 2019 1:22 PM<br>
<b>To:</b> Yaxiong Chen<br>
<b>Cc:</b> Matthew Knepley; <a href="mailto:petsc-users@mcs.anl.gov" id="LPlnk671573" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" previewremoved="true">
petsc-users@mcs.anl.gov</a><br>
<b>Subject:</b> Re: [petsc-users] PETSC matrix assembling super slow</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div dir="ltr">I think you would prefer to preallocate the matrix<br>
<br>
</div>
<div>uncomment this line <br>
</div>
<div dir="ltr">
<div>! call MatMPIAIJSetPreallocation(Amat,f9,PETSC_NULL_INTEGER,f6,PETSC_NULL_INTEGER, ierr)<br>
<br>
<br>
</div>
</div>
</div>
<br>
<div class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail_quote">
<div dir="ltr" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail-m_-5873209276696769746gmail_attr">
Le mar. 29 janv. 2019, à 12 h 40, Yaxiong Chen via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" id="LPlnk313606" class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416OWAAutoLink x_gmail-m_789429084943478815gmail-m_3717110541932564007OWAAutoLink OWAAutoLink" target="_blank" previewremoved="true">petsc-users@mcs.anl.gov</a>>
a écrit :<br>
</div>
<blockquote class="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail-m_-5873209276696769746gmail-m_-9202678646466270310divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p style="margin-top:0px; margin-bottom:0px">Hello,</p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<p style="margin-top:0px; margin-bottom:0px">I have a 2D system which is assembled by each elemental matrix. Ae is my elemental matrix, auxRHSe(:) and RHSe(:) and corresponding right hand side, idx is the global index. My code is as follow, however ,the assembling
rate is super slow(Marked red in the code). I am not sure whether the assembling type is right or not. Since for each element, idx are not continuous numbers. Do you have any idea what is the better way to assemble the matrix?</p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<p style="margin-top:0px; margin-bottom:0px">Thanks</p>
<p style="margin-top:0px; margin-bottom:0px"></p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<p style="margin-top:0px; margin-bottom:0px"></p>
<div>block</div>
<div> PetscErrorCode ierr</div>
<div> PetscMPIInt rank,nproc, mystart</div>
<div> PetscInt nelem</div>
<div> integer,allocatable ::info(:)</div>
<div> real(wp), allocatable :: Ae(:,:), auxRHSe(:),RHSe(:)</div>
<div> integer, allocatable :: idx(:)</div>
<div> PetscScalar, pointer :: xx_v(:)</div>
<div> </div>
<div> PC prec</div>
<div> PetscScalar :: val</div>
<div> Vec xvec,bvec,uvec</div>
<div> Mat Amat</div>
<div> KSP ksp</div>
<div> PetscViewer viewer</div>
<div> PetscInt geq,j,k,ne,M,Istart,Iend</div>
<div> PetscBool flg</div>
<div> KSPConvergedReason reason</div>
<div> Vec dummyVec, dummyVecs(1)</div>
<div> MatNullSpace nullspace</div>
<div><br>
</div>
<div> call PetscInitialize( PETSC_NULL_CHARACTER, ierr )</div>
<div><br>
</div>
<div> if (ierr .ne. 0) then</div>
<div> print*,'Unable to initialize PETSc'</div>
<div> stop</div>
<div> endif</div>
<div> call MPI_Comm_size(PETSC_COMM_WORLD,nproc,ierr)</div>
<div> call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)</div>
<div> mystart=rank+1</div>
<div><br>
</div>
<div> ! Parameter set</div>
<div> info=ptsystem%getInitInfo()</div>
<div> nelem = info(Info_EleMatNum)+info(Info_FixedDOFNum)+info(Info_NumConstrain) </div>
<div> print*,'nelem',nelem</div>
<div> !-------------------------------------</div>
<div> ! Create Matrix</div>
<div> call MatCreate(PETSC_COMM_WORLD,Amat,ierr)</div>
<div> call MatSetSizes( Amat,PETSC_DECIDE, PETSC_DECIDE, info(1), info(1), ierr )</div>
<div> call MatSetType( Amat, MATMPIBAIJ, ierr )</div>
<div> ! call MatMPIAIJSetPreallocation(Amat,f9,PETSC_NULL_INTEGER,f6,PETSC_NULL_INTEGER, ierr)</div>
<div> call MatSetFromOptions( Amat, ierr )</div>
<div> call MatSetUp( Amat, ierr )</div>
<div> call MatGetOwnershipRange( Amat, Istart, Iend, ierr )</div>
<div> </div>
<div> xvec = tVec(0)</div>
<div> call MatCreateVecs( Amat, PETSC_NULL_VEC, xvec, ierr )</div>
<div> call VecSetFromOptions( xvec, ierr )</div>
<div> call VecDuplicate( xvec, bvec, ierr )</div>
<div> call VecDuplicate( xvec, uvec, ierr )</div>
<div><br>
</div>
<div> t1 = MPI_WTime(); </div>
<div><br>
</div>
<div> do i=mystart,nelem,nproc</div>
<div> call ptSystem%getElementalMAT(i, Ae, auxRHSe, idx)</div>
<div> ne=size(idx)</div>
<div> if (allocated(auxRHSe)) call VecSetValues(bvec,ne,idx-1,auxRHSe,ADD_VALUES,ierr)</div>
<div> call MatSetValues(Amat,ne,idx-1,ne,idx-1,Ae,ADD_VALUES,ierr)</div>
<div> end do</div>
<div><br>
</div>
<div> nelem = info(Info_EleRHSNum)</div>
<div> mystart = rank+1</div>
<div><br>
</div>
<div> do i = mystart, nelem, nproc </div>
<div> call ptSystem%getElementalRHS(i, RHSe, idx)</div>
<div> print*,'idx',idx</div>
<div> ne=size(idx)</div>
<div> if (allocated(RHSe)) call VecSetValues(bvec,ne,idx-1,RHSe,ADD_VALUES,ierr)</div>
<div> end do </div>
<div><span style="color:rgb(255,0,0)"> call MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY,ierr)</span></div>
<div><span style="color:rgb(255,0,0)"> call MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY,ierr) ! this part is slow, the for loop above is done but here it may get stuck</span></div>
<div><span style="color:rgb(255,0,0)"> call VecAssemblyBegin(bvec,ierr) ! For a 2500 DOF system, assembling only takes over 2 seconds</span></div>
<div><span style="color:rgb(255,0,0)"> call VecAssemblyEnd(bvec,ierr) ! But for a 10000 DOF system , it gets stuck</span></div>
<div> t2 = MPI_WTime(); </div>
<div><span style="font-family:Calibri,Helvetica,sans-serif"> </span>print*,'assembeling time',t2-t1</div>
<div> ! Solve</div>
<div> call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)</div>
<div>! Set operators. Here the matrix that defines the linear system</div>
<div>! also serves as the preconditioning matrix.</div>
<div> call KSPSetOperators(ksp,Amat,Amat,ierr)</div>
<div></div>
<div> call KSPSetFromOptions(ksp,ierr)</div>
<div>! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</div>
<div>! Solve the linear system</div>
<div>! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</div>
<div> call KSPSetType(ksp,KSPCG,ierr)</div>
<div> call KSPGetPC(ksp,prec,ierr)</div>
<div> ! call KSPSetPCSide(ksp,PC_SYMMETRIC,ierr)</div>
<div> call PCSetType(prec,PCJACOBI,ierr)</div>
<div> call KSPSolve(ksp,bvec,xvec,ierr)</div>
<div></div>
<div> call PetscFinalize(ierr)</div>
<div><br>
</div>
<div>end block</div>
<br>
<p></p>
<p style="margin-top:0px; margin-bottom:0px"><br>
</p>
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail-m_-5873209276696769746gmail-m_-9202678646466270310Signature">
<div id="x_gmail-m_789429084943478815gmail-m_3717110541932564007x_gmail-m_9052320459455506416x_gmail-m_-5873209276696769746gmail-m_-9202678646466270310divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255); font-family:Calibri,Arial,Helvetica,sans-serif,EmojiFont,"Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols">
<p><span style="color:rgb(33,33,33); font-size:13.3333px">Yaxiong Chen, </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="color:rgb(33,33,33); font-size:13.3333px">Ph.D. Student </span><br style="color:rgb(33,33,33); font-size:13.3333px">
<br style="color:rgb(33,33,33); font-size:13.3333px">
<span style="font-size:13.3333px"></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">School of Mechanical Engineering, 3171 </span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px"><span style="color:rgb(34,34,34); font-family:Roboto,arial,sans-serif; font-size:13px">585 Purdue Mall</span></span></p>
<p style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:16px"><span style="font-size:13.3333px">West Lafayette, IN 47907</span></p>
<div><span style="font-size:13.3333px"><br>
</span></div>
<p></p>
<p><br style="color:rgb(33,33,33); font-size:13.3333px">
<br>
</p>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>