[petsc-users] petsc4py KSP Question

Gaetan Kenway kenway at utias.utoronto.ca
Wed Mar 7 15:04:00 CST 2012


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?

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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120307/e5dbbb1d/attachment.htm>


More information about the petsc-users mailing list