<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div><br></div> Here is the code for MPIBAIJ with SAME_NONZERO_STRUCTURE. (the other formats are similar)<div><br></div><div>PetscErrorCode MatAXPY_SeqBAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)</div><div>{</div><div> Mat_SeqBAIJ *x = (Mat_SeqBAIJ *)X->data, *y = (Mat_SeqBAIJ *)Y->data;</div><div> PetscInt bs = Y->rmap->bs, bs2 = bs * bs;</div><div> PetscBLASInt one = 1;</div><div>....</div><div>if (str == SAME_NONZERO_PATTERN) {</div><div> PetscScalar alpha = a;</div><div> PetscBLASInt bnz;</div><div> PetscCall(PetscBLASIntCast(x->nz * bs2, &bnz));</div><div> PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, x->a, &one, y->a, &one));</div><div> PetscCall(PetscObjectStateIncrease((PetscObject)Y));</div><div><br></div><div>It directly adds the nonzero values from the two matrices together (the nonzero structure plays no role) and it uses the BLAS so it should perform as "fast as possible" given the hardware (are you configured --with-debugging=0 ?, are you using binding with mpiexec to ensure MPI is using the best combination of cores? <a href="https://petsc.org/release/faq/#what-kind-of-parallel-computers-or-clusters-are-needed-to-use-petsc-or-why-do-i-get-little-speedup">https://petsc.org/release/faq/#what-kind-of-parallel-computers-or-clusters-are-needed-to-use-petsc-or-why-do-i-get-little-speedup</a>)</div><div><br></div><div> Can you run with -log_view and send the output so we can see directly the performance?</div><div><br></div><div><br><div><br id="lineBreakAtBeginningOfMessage"><div><br><blockquote type="cite"><div>On Nov 11, 2023, at 4:25 PM, Donald Rex Planalp <Donald.Planalp@colorado.edu> wrote:</div><br class="Apple-interchange-newline"><div><div dir="ltr"><div dir="ltr">Hello, and thank you for the quick reply.<div><br></div><div>For some context, all of these matrices are produced from a kronecker product in parallel between an "angular" and a "radial" matrix. In this case S_total and H_atom use the same angular matrix which is the identity, while their </div><div>radial matrices obey different sparsity.</div><div><br></div><div> Meanwhile H_mix and H_ang are constructed from two different angular matrices, however both are only nonzero along the two off diagonals, and the radial matrices have the same sparse structure. </div><div><br></div><div>So S_total and H_atom have the same block sparse structure, while H_mix and H_ang have the same block sparse structure. However even just adding H_mix and H_ang for the simplest case takes around half a second which is still too slow even when using SAME_NONZERO_PATTERN . </div><div><br></div><div>This confuses me the most because another researcher wrote a similar code a few years back using finite difference, so his matrices are on the order 250kx250k and larger, yet the matrix additions are far faster for him. </div><div><br></div><div>If you would like I can show more code snippets but im not sure what you would want to see.</div><div><br></div><div>Thank you for your time</div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Nov 11, 2023 at 11:22 AM Barry Smith <<a href="mailto:bsmith@petsc.dev">bsmith@petsc.dev</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><div><br></div><div><br></div><div> DIFFERENT_NONZERO_PATTERN will always be slow because it needs to determine the nonzero pattern of the result for each computation.</div><div><br></div><div> SAME_NONZERO_PATTERN can obviously run fast but may not be practical for your problem. </div><div><br></div><div> SUBSET_NONZERO_PATTERN (which means in A += b*B we know that B has NO nonzero locations that A does not have) does not need to reallocate anything in A so it can be reasonably fast (we can do more optimizations on the current code to improve it for you).</div><div><br></div><div> So, can you construct the S nonzero structure initially to have the union of the nonzero structures of all the matrices that accumulate into it? This is the same</div><div>nonzero structure that it "ends up with" in the current code. With this nonzero structure, SUBSET_NONZERO_PATTERN can be used. Depending on how sparse all the matrices that get accumulated into S are, you could perhaps go further and make sure they all have the same nonzero structure (sure, it uses more memory and will do extra operations, but the actual computation will be so fast it may be worth the extra computations.</div><div><br></div><div> Barry</div><div><br></div><br id="m_-8108465918442253411lineBreakAtBeginningOfMessage"><div><br><blockquote type="cite"><div>On Nov 10, 2023, at 9:53 PM, Donald Rex Planalp <<a href="mailto:Donald.Planalp@colorado.edu" target="_blank">Donald.Planalp@colorado.edu</a>> wrote:</div><br><div><div dir="ltr"><div dir="ltr">Hello,<div><br></div><div>I am trying to use petsc4py to conduct a quantum mechanics simulation. I've been able to construct all of the relevant matrices, however I am reaching a gigantic bottleneck. </div><div><br></div><div>For the simplest problem I am running I have a few matrices each about 5000x5000. In order to begin time propagation I need to add these matrices together. However, on 6 cores of my local machine it is taking approximately 1-2 seconds per addition. Since I need to do this for each time step in my simulation it is prohibitively slow since there could be upwards of 10K time steps. </div><div><br></div><div>Below is the relevant code:</div><div><br></div><div><div style="color:rgb(166,172,205);background-color:rgb(41,45,62);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre-wrap"><div>structure <span style="color:rgb(199,146,234)">=</span> structure<span style="color:rgb(199,146,234)">=</span>PETSc<span style="color:rgb(137,221,255)">.</span>Mat<span style="color:rgb(137,221,255)">.</span>Structure<span style="color:rgb(137,221,255)">.</span>DIFFERENT_NONZERO_PATTERN</div><div> <span style="color:rgb(137,221,255);font-style:italic">if</span> test2<span style="color:rgb(137,221,255)">:</span></div><div> <span style="color:rgb(199,146,234)">def</span> <span style="color:rgb(130,170,255)">makeLeft</span><span style="color:rgb(137,221,255)">(</span><span style="color:rgb(255,83,112)">S</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">MIX</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">ANG</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">ATOM</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">i</span><span style="color:rgb(137,221,255)">):</span></div><div> </div><br><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(199,146,234)">-</span>Field<span style="color:rgb(137,221,255)">.</span>pulse<span style="color:rgb(137,221,255)">[</span>i<span style="color:rgb(137,221,255)">],</span>MIX<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(199,146,234)">-</span>Field<span style="color:rgb(137,221,255)">.</span>pulse<span style="color:rgb(137,221,255)">[</span>i<span style="color:rgb(137,221,255)">],</span>ANG<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(199,146,234)">-</span><span style="color:rgb(247,140,108)">1</span><span style="color:rgb(137,221,255)">,</span>ATOM<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> <span style="color:rgb(137,221,255);font-style:italic">return</span> S</div><div> <span style="color:rgb(199,146,234)">def</span> <span style="color:rgb(130,170,255)">makeRight</span><span style="color:rgb(137,221,255)">(</span><span style="color:rgb(255,83,112)">S</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">MIX</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">ANG</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">ATOM</span><span style="color:rgb(137,221,255)">,</span><span style="color:rgb(255,83,112)">i</span><span style="color:rgb(137,221,255)">):</span></div><div> </div><div> </div><div> </div><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span>Field<span style="color:rgb(137,221,255)">.</span>pulse<span style="color:rgb(137,221,255)">[</span>i<span style="color:rgb(137,221,255)">],</span>MIX<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span>Field<span style="color:rgb(137,221,255)">.</span>pulse<span style="color:rgb(137,221,255)">[</span>i<span style="color:rgb(137,221,255)">],</span>ANG<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> S<span style="color:rgb(137,221,255)">.</span>axpy<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(247,140,108)">1</span><span style="color:rgb(137,221,255)">,</span>ATOM<span style="color:rgb(137,221,255)">,</span>structure<span style="color:rgb(137,221,255)">)</span></div><div> </div><div> <span style="color:rgb(137,221,255);font-style:italic">return</span> S</div><br><div> H_mix <span style="color:rgb(199,146,234)">=</span> Int<span style="color:rgb(137,221,255)">.</span>H_mix</div><div> H_mix<span style="color:rgb(137,221,255)">.</span>scale<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(247,140,108)">1</span><span style="color:rgb(199,146,234)">j</span> <span style="color:rgb(199,146,234)">*</span> dt <span style="color:rgb(199,146,234)">/</span><span style="color:rgb(247,140,108)">2</span><span style="color:rgb(137,221,255)">)</span></div><div> </div><div> H_ang <span style="color:rgb(199,146,234)">=</span> Int<span style="color:rgb(137,221,255)">.</span>H_ang</div><div> H_ang<span style="color:rgb(137,221,255)">.</span>scale<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(247,140,108)">1</span><span style="color:rgb(199,146,234)">j</span> <span style="color:rgb(199,146,234)">*</span> dt <span style="color:rgb(199,146,234)">/</span><span style="color:rgb(247,140,108)">2</span><span style="color:rgb(137,221,255)">)</span></div><br><div> H_atom <span style="color:rgb(199,146,234)">=</span> Int<span style="color:rgb(137,221,255)">.</span>H_atom</div><div> H_atom<span style="color:rgb(137,221,255)">.</span>scale<span style="color:rgb(137,221,255)">(</span><span style="color:rgb(247,140,108)">1</span><span style="color:rgb(199,146,234)">j</span> <span style="color:rgb(199,146,234)">*</span> dt <span style="color:rgb(199,146,234)">/</span><span style="color:rgb(247,140,108)">2</span><span style="color:rgb(137,221,255)">)</span></div><br><div> S <span style="color:rgb(199,146,234)">=</span> Int<span style="color:rgb(137,221,255)">.</span>S_total</div><br><div> psi_initial <span style="color:rgb(199,146,234)">=</span> psi<span style="color:rgb(137,221,255)">.</span>psi_initial<span style="color:rgb(137,221,255)">.</span>copy<span style="color:rgb(137,221,255)">()</span></div><div> ksp <span style="color:rgb(199,146,234)">=</span> PETSc<span style="color:rgb(137,221,255)">.</span>KSP<span style="color:rgb(137,221,255)">().</span>create<span style="color:rgb(137,221,255)">(</span>PETSc<span style="color:rgb(137,221,255)">.</span>COMM_WORLD<span style="color:rgb(137,221,255)">)</span></div><br><div> </div><div> <span style="color:rgb(137,221,255);font-style:italic">for</span> i<span style="color:rgb(137,221,255)">,</span>t <span style="color:rgb(137,221,255);font-style:italic">in</span> <span style="color:rgb(130,170,255)">enumerate</span><span style="color:rgb(137,221,255)">(</span>box<span style="color:rgb(137,221,255)">.</span>t<span style="color:rgb(137,221,255)">):</span></div><div> <span style="color:rgb(130,170,255)">print</span><span style="color:rgb(137,221,255)">(</span>i<span style="color:rgb(137,221,255)">,</span>L<span style="color:rgb(137,221,255)">)</span></div><br><div> </div><br><div> O_L <span style="color:rgb(199,146,234)">=</span> makeLeft<span style="color:rgb(137,221,255)">(</span>S<span style="color:rgb(137,221,255)">,</span>H_mix<span style="color:rgb(137,221,255)">,</span>H_ang<span style="color:rgb(137,221,255)">,</span>H_atom<span style="color:rgb(137,221,255)">,</span>i<span style="color:rgb(137,221,255)">)</span></div><div> O_R <span style="color:rgb(199,146,234)">=</span> makeRight<span style="color:rgb(137,221,255)">(</span>S<span style="color:rgb(137,221,255)">,</span>H_mix<span style="color:rgb(137,221,255)">,</span>H_ang<span style="color:rgb(137,221,255)">,</span>H_atom<span style="color:rgb(137,221,255)">,</span>i<span style="color:rgb(137,221,255)">)</span></div><br><div> </div><br><div> <span style="color:rgb(137,221,255);font-style:italic">if</span> i <span style="color:rgb(199,146,234)">==</span> <span style="color:rgb(247,140,108)">0</span><span style="color:rgb(137,221,255)">:</span></div><div> known <span style="color:rgb(199,146,234)">=</span> O_R<span style="color:rgb(137,221,255)">.</span>getVecRight<span style="color:rgb(137,221,255)">()</span></div><div> sol <span style="color:rgb(199,146,234)">=</span> O_L<span style="color:rgb(137,221,255)">.</span>getVecRight<span style="color:rgb(137,221,255)">()</span></div><div> </div><div> O_R<span style="color:rgb(137,221,255)">.</span>mult<span style="color:rgb(137,221,255)">(</span>psi_initial<span style="color:rgb(137,221,255)">,</span>known<span style="color:rgb(137,221,255)">)</span></div><br><div> ksp<span style="color:rgb(137,221,255)">.</span>setOperators<span style="color:rgb(137,221,255)">(</span>O_L<span style="color:rgb(137,221,255)">)</span></div><div> </div><div> </div><div> ksp<span style="color:rgb(137,221,255)">.</span>solve<span style="color:rgb(137,221,255)">(</span>known<span style="color:rgb(137,221,255)">,</span>sol<span style="color:rgb(137,221,255)">)</span></div><br><div> </div><div> </div><div> </div><div> psi_initial<span style="color:rgb(137,221,255)">.</span>copy<span style="color:rgb(137,221,255)">(</span>sol<span style="color:rgb(137,221,255)">)</span></div><div><br></div></div></div><div><br></div><div>I need to clean it up a bit, but the main point is that I need to add the matrices many times for a single time step. I can't preallocate memory very well since some of the matrices aren't the most sparse either. It seems if I cannot speed up the addition it will be difficult to continue so I was wondering if you had any insights.</div><div><br></div><div>Thank you for your time</div></div></div>
</div></blockquote></div><br></div></blockquote></div>
</div></blockquote></div><br></div></div></body></html>