Excellent. Thanks<div><br></div><div>I think the "with X as x, Y as y" syntax only works with python 2.7 and up and one computer I run this on still has 2.6, so I'll leave them nested. In my particular application I'm using petsc4py to solve a coupled problem with two codes that never talk directly to each other. Each code takes care of its own topology, communication etc, so my petsc4py objects just have the owned rows with no halos. </div>
<div><br></div><div>Thanks for all your help</div><div><br></div><div>Gaetan<br><br><div class="gmail_quote">On Wed, Mar 7, 2012 at 4:08 PM, Matthew Knepley <span dir="ltr"><<a href="mailto:knepley@gmail.com">knepley@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Wed, Mar 7, 2012 at 3:04 PM, Gaetan Kenway <span dir="ltr"><<a href="mailto:kenway@utias.utoronto.ca" target="_blank">kenway@utias.utoronto.ca</a>></span> wrote:<br>
</div><div class="gmail_quote"><div class="im"><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><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 class="im"><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></div><br><br clear="all"><div class="HOEnZb"><div class="h5"><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>
</div></div></blockquote></div><br></div>