On Wed, Mar 7, 2012 at 3:04 PM, Gaetan Kenway <span dir="ltr"><<a href="mailto:kenway@utias.utoronto.ca">kenway@utias.utoronto.ca</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks for the info. I've recoded the functions using the 'with' statements and I agree is makes it more explicit the scope of the temporary numpy array. Would the following snippet of code be the preferred way to use the petsc vectors in python?</blockquote>
<div><br></div><div>1) You can use with X as x, Y as y:</div><div><br></div><div>2) For linear algebra, this is fine. For things with topology, you generally want "ghost" regions, so you</div><div> first get a local vector, instead of the global vector, and then pull out the array.</div>
<div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Thanks,</div><div>Gaetan</div><div><br></div><div><div>class ASPC(object):</div>
<div> def apply(self, pc, X, Y):</div><div> """y <-- M^-1 * x"""</div><div><br></div>
<div> # Extract Vector Pointers</div><div> with X as x:</div><div> with Y as y:</div><div><br></div><div> # Apply preconditioners in parallel</div><div> if self.AS.isAero:</div>
<div> y = self.AS.solver.globalNKPreCon(x, y)</div><div><br></div><div> if self.AS.isStruct:</div><div> y = self.AS.solver.globalNKPreCon(x, y)</div><div> </div><div> </div>
</div><div><br><div class="gmail_quote">On Wed, Mar 7, 2012 at 10:14 AM, Jed Brown <span dir="ltr"><<a href="mailto:jedbrown@mcs.anl.gov" target="_blank">jedbrown@mcs.anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="gmail_quote"><div>On Wed, Mar 7, 2012 at 08:49, Gaetan Kenway <span dir="ltr"><<a href="mailto:kenway@utias.utoronto.ca" target="_blank">kenway@utias.utoronto.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
What is the 'with block' option. I don't see anything related to that when I run help(PETSc.Vec()). Does it return a (N/bs,bs) array instead of an (N) array? </blockquote><div><br></div></div><div>with X as x:</div>
<div> print(numpy.sin(x)) # x is a Numpy array</div><div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Also in petsc4py the call for resetting the pointer from getArray() is actually resetArray() which is different from C/Fortran where it is restoreArray(). I've got them confused as well. </blockquote>
</div></div><br><div>No, resetArray() is VecResetArray() which is a different thing, getArray() in Python does not need an explicit restore. (I believe the restore is called when the "gotten" array falls out of scope.) I recommend using the 'with' statement, it's much clearer and more explicit about resource management.</div>
</blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>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<br>