<div dir="ltr">Hi Matt,<div>   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><br></div><div>Thanks,</div><div>Sam</div></div><br><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">knepley@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"><div dir="ltr">On Mon, Sep 28, 2020 at 3:43 PM Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank">sam.guo@cd-adapco.com</a>> wrote:<br></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">Hi Stefano and PETSc dev team,<div>   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><br></div><div>I do not think that will work as you expect. I will try to explain below.</div><div> </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"><div>   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><br></div><div>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>MatRealPart sets the imaginary parts of all the matrix elements to zero.</div><div> </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"><div> 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><br></div><div>What you suggest would mean rewriting the matrix multiplication algorithm by hand after extracting the values. I am not sure if this</div><div>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>from temporaries would be very hard to control.</div><div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><div> </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"><div>Thanks,</div><div>Sam</div><div><br></div><div>PetscScalar *px = nullptr;</div><div>VecGetArrayRead(x, &px);</div><div>PetscScalar *py = nullptr;</div><div>VecGetArray(y, &py);</div><div>int localSize = 0;</div><div>VecGetLocalSize(x, &localSize);</div><div>std::vector<PetasReal> realX(localSize); // I am using c++ to call PETSc</div><div><br></div><div>//retrieve real part </div><div>for(int i = 0; i < localSize; i++)  realX[i] = PetscRealPart(px[i]);</div><div><br></div><div>// do real matrix-vector multiplication </div><div>// realY=A*realX </div><div>// here where realY is std::vector<PetscReal></div><div><br></div><div>//put real part back to py</div><div>  for(int i = 0; i < localSize; i++)  pv[i] = realY[i];  <br></div><div>VecRestoreArray(y,&py);</div></div><br><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">sam.guo@cd-adapco.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">Thanks<br><br>On Tuesday, May 26, 2020, Stefano Zampini <<a href="mailto:stefano.zampini@gmail.com" target="_blank">stefano.zampini@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>All the solvers/matrices/vectors works for PetscScalar types (i.e. in your case complex)<div>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>You should not be worried about doubling the memory for a matrix (i.e. real and imaginary part)</div><div><br><div><br><blockquote type="cite"><div>On May 26, 2020, at 11:28 PM, Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank">sam.guo@cd-adapco.com</a>> wrote:</div><br><div>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><br>On Tuesday, May 26, 2020, Zhang, Hong <<a href="mailto:hzhang@mcs.anl.gov" target="_blank">hzhang@mcs.anl.gov</a>> wrote:<br><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">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt">
You can build PETSc with complex <span style="color:rgb(50,49,48);background-color:rgb(255,255,255);display:inline">
version</span>, and declare some variables as 'PETSC_REAL'.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt">
Hong</div>
<div></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt">
<br>
</div>
<hr style="display:inline-block;width:98%">
<div dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt"><b>From:</b> petsc-users <<a href="mailto:petsc-users-bounces@mcs.anl.gov" target="_blank">petsc-users-bounces@mcs.anl.gov</a>> on behalf of Sam Guo <<a href="mailto:sam.guo@cd-adapco.com" target="_blank">sam.guo@cd-adapco.com</a>><br>
<b>Sent:</b> Tuesday, May 26, 2020 1:00 PM<br>
<b>To:</b> PETSc <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
<b>Subject:</b> [petsc-users] using real and complex together</font>
<div> </div>
</div>
<div>
<div dir="ltr">Dear PETSc dev team,
<div>   Can I use both real and complex versions together?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Sam</div>
</div>
</div>
</div>

</blockquote>
</div></blockquote></div><br></div></div></blockquote>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>
</blockquote></div>