[petsc-users] petsc4py and numpy

Matthew Knepley knepley at gmail.com
Tue May 8 08:59:07 CDT 2012


On Tue, May 8, 2012 at 9:50 AM, Christian Staudt <
christian.staudt at ira.uka.de> wrote:

> I am currently getting to know PETSc via petsc4py.  Because I am writing a
> Python offshoot of a project originally in MATLAB, I have to
> come up with equivalents of built-in MATLAB functions. I am discovering
> that numpy already provides many equivalents (this page documents some
> numpy equivalents for common MATLAB expressions: [1])
>
> However, the goal is shared-memory parallelism, so numpy is not
> sufficient. This is where PETSc comes in, and the petsc4py bindings are
> very convenient. As far as I know, petsc4py is based on numpy.
>
> My questions to users of petsc4py and numpy:
>
> a) Is there an easy and efficient way to convert a numpy.ndarray to a
> PETSc.Vec or PETSc.Mat (and vice-versa)?  (A PETSc.Vec.getArray() returns a
> numpy.ndarray, though I have found no such method for PETSc.Mat yet. )
>

If you are using dense matrices (MATDENSE) then MatGetArray() works.


> b) In MATLAB, arrays are used everywhere, also for small collections,
> where one would use tuples or lists in Python (e.g. multiple return values
> from a function). When I encounter an array in the original MATLAB code, I
> have to decide whether a tuple, a list, a numpy.ndarray or a
> PETSc.Vec/PETSc.Mat is appropriate. One approach would be to use PETSc
> wherever the array can be larger than a typical tuple. Is there a
> performance penalty associated with using PETSc Vec/Mat for rather small
> arrays?
>

The opposite. However, it is not as flexible because the type is
constrained.

   Matt


>
> Regards & thank you for your answers,
> Chris
>
> [1] http://www.scipy.org/NumPy_for_Matlab_Users




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


More information about the petsc-users mailing list