[petsc-users] petsc4py KSP Question

Matthew Knepley knepley at gmail.com
Wed Mar 7 15:08:38 CST 2012


On Wed, Mar 7, 2012 at 3:04 PM, Gaetan Kenway <kenway at utias.utoronto.ca>wrote:

> 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?


1) You can use with X as x, Y as y:

2) For linear algebra, this is fine. For things with topology, you
generally want "ghost" regions, so you
    first get a local vector, instead of the global vector, and then pull
out the array.

   Matt


> Thanks,
> Gaetan
>
> class ASPC(object):
>     def apply(self, pc, X, Y):
>         """y <-- M^-1 * x"""
>
>         # Extract Vector Pointers
>         with X as x:
>             with Y as y:
>
>                 # Apply preconditioners in parallel
>                 if self.AS.isAero:
>                     y = self.AS.solver.globalNKPreCon(x, y)
>
>                 if self.AS.isStruct:
>                     y = self.AS.solver.globalNKPreCon(x, y)
>
>
>
> On Wed, Mar 7, 2012 at 10:14 AM, Jed Brown <jedbrown at mcs.anl.gov> wrote:
>
>> On Wed, Mar 7, 2012 at 08:49, Gaetan Kenway <kenway at utias.utoronto.ca>wrote:
>>
>>> 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?
>>
>>
>> with X as x:
>>     print(numpy.sin(x)) # x is a Numpy array
>>
>>
>>> 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.
>>
>>
>> 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.
>>
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120307/468d518a/attachment.htm>


More information about the petsc-users mailing list