[petsc-users] petsc4py KSP Question

Gaetan Kenway kenway at utias.utoronto.ca
Wed Mar 7 15:32:43 CST 2012


Excellent. Thanks

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.

Thanks for all your help

Gaetan

On Wed, Mar 7, 2012 at 4:08 PM, Matthew Knepley <knepley at gmail.com> wrote:

> 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/b6c4d290/attachment.htm>


More information about the petsc-users mailing list