<div dir="ltr">This is the recommended call sequence to create a matrix:<div><br></div><div><pre class="gmail-code gmail-highlight" lang="c"><span id="gmail-LC20" lang="c" class="gmail-line"><span class="gmail-"> PetscCall</span><span class="gmail-">(MatCreate</span><span class="gmail-">(comm, &A)</span><span class="gmail-">)</span><span class="gmail-">;</span></span>
<span id="gmail-LC21" lang="c" class="gmail-line"><span class="gmail-">  PetscCall</span><span class="gmail-">(MatSetSizes</span><span class="gmail-">(A, PETSC_DECIDE, PETSC_DECIDE, N, N)</span><span class="gmail-">)</span><span class="gmail-">;</span></span>
<span id="gmail-LC22" lang="c" class="gmail-line"><span class="gmail-">  PetscCall</span><span class="gmail-">(MatSetFromOptions</span><span class="gmail-">(A)</span><span class="gmail-">)</span><span class="gmail-">;</span></span>
<span id="gmail-LC23" lang="c" class="gmail-line"><span class="gmail-">  PetscCall</span><span class="gmail-">(MatSeqAIJSetPreallocation</span><span class="gmail-">(A, </span><span class="gmail-hljs-number">3</span><span class="gmail-">, </span><span class="gmail-hljs-literal">NULL</span><span class="gmail-">)</span><span class="gmail-">)</span><span class="gmail-">;</span></span>
<span id="gmail-LC24" lang="c" class="gmail-line"><span class="gmail-">  PetscCall</span><span class="gmail-">(MatMPIAIJSetPreallocation</span><span class="gmail-">(A, </span><span class="gmail-hljs-number">3</span><span class="gmail-">, </span><span class="gmail-hljs-literal">NULL</span><span class="gmail-">, </span><span class="gmail-hljs-number">2</span><span class="gmail-">, </span><span class="gmail-hljs-literal">NULL</span><span class="gmail-">)</span><span class="gmail-">)</span><span class="gmail-">;</span></span>
</pre>You need to look at <a href="https://petsc.org/main/docs/manualpages/Mat/MatMPIAIJSetPreallocation">https://petsc.org/main/docs/manualpages/Mat/MatMPIAIJSetPreallocation</a></div><div>And set the preallocation correctly.</div><div>Note, you can run with -info (very noisy so filter with grep or something) and look for messages about the number of mallocs in the matrix assembly. </div><div>You want to preallocate enough to have zero mallocs.</div><div><br></div><div>Mark</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Dec 1, 2022 at 2:32 AM 김성익 <<a href="mailto:ksi2443@gmail.com">ksi2443@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div><br></div><div>I set a large-sized global matrix through matsetsize and proceed with the process of assembling multiple local-sized matrices into a global matrix (By using MatSetValue).<br></div><div><br></div><div>However, only using matsetsize much less than expected performance.</div><div>In this case, what can be done to speed up assembly?</div><div><br></div><div>Thanks,</div><div>Hyung Kim</div></div>
</blockquote></div>