<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Sam, you can solve a complex matrix using a real version of PETSc by doubling the size of your matrix and spitting out real/imaginary parts.<div class=""><br class=""><div class="">See this paper:</div><div class=""><br class=""></div><div class=""><a href="https://epubs.siam.org/doi/abs/10.1137/S1064827500372262?mobileUi=0" class="">https://epubs.siam.org/doi/abs/10.1137/S1064827500372262?mobileUi=0</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Randy M.</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 28, 2020, at 5:12 PM, Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" class="">sam.guo@cd-adapco.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">All I want is to solve both real and complex matrix using either real or complex version of PETSc. If I load complex version of PETSc, I waste memory solving real matrix. If I can solve complex matrix using real version of PETSc, I will accept it.<br class=""><br class="">On Monday, September 28, 2020, Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If I load complex version of PETSc, how can Vec be real?<br class=""><br class="">On Monday, September 28, 2020, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank" class="">knepley@gmail.com</a>> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class="">On Mon, Sep 28, 2020 at 7:44 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I want to make sure I understand you. I load real version of PETSc. If my input matrix is complex, </blockquote><div class=""><br class=""></div><div class="">You said that your matrix was real.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">just get real and imagine parts of PETSc Vec</blockquote><div class=""><br class=""></div><div class="">No, the PETSc Vec would be real. You would have two vectors.</div><div class=""><br class=""></div><div class=""> Thanks,</div><div class=""><br class=""></div><div class=""> Matt</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> and do the matrix vector multiplication. Right?<br class=""><br class="">On Monday, September 28, 2020, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank" class="">knepley@gmail.com</a>> wrote:<br class=""><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" class=""><div dir="ltr" class="">On Mon, Sep 28, 2020 at 6:29 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">“<span style="color:rgb(34,34,34);font-size:14px" class=""> I think it would be much easier to just decompose your complex work into real and imaginary parts and use PETSc with real scalars to compute them separately.</span><div style="color:rgb(34,34,34);font-size:14px" class="">Since you know your matrices have 0 imaginary part, this becomes very straightforward.”</div><div style="color:rgb(34,34,34);font-size:14px" class=""><br class=""></div><div style="color:rgb(34,34,34);font-size:14px" class="">I think this is what I am trying to do. But since I have to provide matrix-vector operator(since I am using shell matrix), the Vec I receive is complex. I need to convert complex vec to real one and then convert it back(that’s the code I shown before).</div></blockquote><div class=""><br class=""></div><div class="">No, this is not what I am advocating. Keep your vectors real, just keep one for the real part and one for the imaginary part. Then you can just call MatMult() twice with your matrix.</div><div class=""><br class=""></div><div class=""> Thanks,</div><div class=""><br class=""></div><div class=""> Matt</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Monday, September 28, 2020, Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank" class="">knepley@gmail.com</a>> wrote:<br class=""><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" class=""><div dir="ltr" class="">On Mon, Sep 28, 2020 at 5:01 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""></div><div class="gmail_quote"><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" class="">Hi Matt,<div class=""> Since I use MUMPS as preconditioner, complex uses too much memory if my input matrix is real. Ideally if I can compile real and complex into different symbols (like MUMPS) , I can load both version without conflict. </div></div></blockquote><div class=""><br class=""></div><div class="">What I mean to say is that it would be great if it were as simple as using two different symbols, but unfortunately the problem is more difficult. I was trying to use</div><div class="">the example of templates. This would be a very intrusive change no matter what technology you are using.</div><div class=""><br class=""></div><div class="">So your main memory usage is from the MUMPS factorization, and you cannot afford to double that usage?</div><div class=""><br class=""></div><div class="">You could consider writing a version of AIJ that stores real entries, but allows complex vector values. It would promote to complex for the row dot product.</div><div class="">However, you would also have to do the same work for all the solves you do with MUMPS.</div><div class=""><br class=""></div><div class="">I think it would be much easier to just decompose your complex work into real and imaginary parts and use PETSc with real scalars to compute them separately.</div><div class="">Since you know your matrices have 0 imaginary part, this becomes very straightforward.</div><div class=""><br class=""></div><div class=""> Thanks,</div><div class=""><br class=""></div><div class=""> Matt</div><div class=""> </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" class=""><div class="">Thanks,</div><div class="">Sam</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Sep 28, 2020 at 12:52 PM Matthew Knepley <<a href="mailto:knepley@gmail.com" target="_blank" class="">knepley@gmail.com</a>> wrote:<br class=""></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" class=""><div dir="ltr" class="">On Mon, Sep 28, 2020 at 3:43 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""></div><div class="gmail_quote"><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" class="">Hi Stefano and PETSc dev team,<div class=""> I want to try your suggestion to always load complex version of PETSc but if my input matrix A is real, I want to create shell matrix to matrix-vector and factorization using real only.</div></div></blockquote><div class=""><br class=""></div><div class="">I do not think that will work as you expect. I will try to explain below.</div><div class=""> </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" class=""><div class=""> I still need to understand how MatRealPart works. Does it just zero out the image numerical values or does it delete the image memory?</div></div></blockquote><div class=""><br class=""></div><div class="">When we have complex values, we use the "complex" type to allocate and store them. Thus you cannot talk about just the memory to store imaginary parts.</div><div class="">MatRealPart sets the imaginary parts of all the matrix elements to zero.</div><div class=""> </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" class=""><div class=""> If my input matrix A is real, how do I create a shell matrix to matrix -vector multiplication y=A*x where A is real, PestcScalar = complex, x and y are Vec? I notice there is a VecRealPart but it seems it just zeros the image numerical values. It seems I still have to create a PetscReal pointer to copy the real part of PetacScalar pointers like following. Can you comment on it?</div></div></blockquote><div class=""><br class=""></div><div class="">What you suggest would mean rewriting the matrix multiplication algorithm by hand after extracting the values. I am not sure if this</div><div class="">is really what you want to do. Is the matrix memory really your limiting factor? Even if you tried to do this with templates, the memory</div><div class="">from temporaries would be very hard to control.</div><div class=""><br class=""></div><div class=""> Thanks,</div><div class=""><br class=""></div><div class=""> Matt</div><div class=""> </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" class=""><div class="">Thanks,</div><div class="">Sam</div><div class=""><br class=""></div><div class="">PetscScalar *px = nullptr;</div><div class="">VecGetArrayRead(x, &px);</div><div class="">PetscScalar *py = nullptr;</div><div class="">VecGetArray(y, &py);</div><div class="">int localSize = 0;</div><div class="">VecGetLocalSize(x, &localSize);</div><div class="">std::vector<PetasReal> realX(localSize); // I am using c++ to call PETSc</div><div class=""><br class=""></div><div class="">//retrieve real part </div><div class="">for(int i = 0; i < localSize; i++) realX[i] = PetscRealPart(px[i]);</div><div class=""><br class=""></div><div class="">// do real matrix-vector multiplication </div><div class="">// realY=A*realX </div><div class="">// here where realY is std::vector<PetscReal></div><div class=""><br class=""></div><div class="">//put real part back to py</div><div class=""> for(int i = 0; i < localSize; i++) pv[i] = realY[i]; <br class=""></div><div class="">VecRestoreArray(y,&py);</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, May 26, 2020 at 1:49 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Thanks<br class=""><br class="">On Tuesday, May 26, 2020, Stefano Zampini <<a href="mailto:stefano.zampini@gmail.com" target="_blank" class="">stefano.zampini@gmail.com</a>> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="">All the solvers/matrices/vectors works for PetscScalar types (i.e. in your case complex)<div class="">If you need to solve for the real part only, you can duplicate the matrix and call MatRealPart to zero out the imaginary part. But the solve will always run in the complex space</div><div class="">You should not be worried about doubling the memory for a matrix (i.e. real and imaginary part)</div><div class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On May 26, 2020, at 11:28 PM, Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>> wrote:</div><br class=""><div class="">complex version is needed since matrix sometimes is real and sometimes is complex. I want to solve real matrix without allocating memory for imaginary part((except eigen pairs).<br class=""><br class="">On Tuesday, May 26, 2020, Zhang, Hong <<a href="mailto:hzhang@mcs.anl.gov" target="_blank" class="">hzhang@mcs.anl.gov</a>> wrote:<br class=""><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" class="">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt" class="">
You can build PETSc with complex <span style="color:rgb(50,49,48);background-color:rgb(255,255,255);display:inline" class="">
version</span>, and declare some variables as 'PETSC_REAL'.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt" class="">
Hong</div>
<div class=""></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt" class="">
<br class="">
</div>
<hr style="display:inline-block;width:98%" class="">
<div dir="ltr" class=""><font face="Calibri, sans-serif" style="font-size:11pt" class=""><b class="">From:</b> petsc-users <<a href="mailto:petsc-users-bounces@mcs.anl.gov" target="_blank" class="">petsc-users-bounces@mcs.anl.g<wbr class="">ov</a>> on behalf of Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank" class="">sam.guo@cd-adapco.com</a>><br class="">
<b class="">Sent:</b> Tuesday, May 26, 2020 1:00 PM<br class="">
<b class="">To:</b> PETSc <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:</b> [petsc-users] using real and complex together</font>
<div class=""> </div>
</div>
<div class="">
<div dir="ltr" class="">Dear PETSc dev team,
<div class=""> Can I use both real and complex versions together?</div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Sam</div>
</div>
</div>
</div>
</blockquote>
</div></blockquote></div><br class=""></div></div></blockquote>
</blockquote></div>
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br class="">-- Norbert Wiener</div><div class=""><br class=""></div><div class=""><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" class="">https://www.cse.buffalo.edu/~k<wbr class="">nepley/</a><br class=""></div></div></div></div></div></div></div></div>
</blockquote></div>
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br class="">-- Norbert Wiener</div><div class=""><br class=""></div><div class=""><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" class="">https://www.cse.buffalo.edu/~k<wbr class="">nepley/</a><br class=""></div></div></div></div></div></div></div></div>
</blockquote>
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br class="">-- Norbert Wiener</div><div class=""><br class=""></div><div class=""><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" class="">https://www.cse.buffalo.edu/~k<wbr class="">nepley/</a><br class=""></div></div></div></div></div></div></div></div>
</blockquote>
</blockquote></div><br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class="">What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br class="">-- Norbert Wiener</div><div class=""><br class=""></div><div class=""><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" class="">https://www.cse.buffalo.edu/~k<wbr class="">nepley/</a><br class=""></div></div></div></div></div></div></div></div>
</blockquote>
</blockquote>
</div></blockquote></div><br class=""></div></div></body></html>