[petsc-users] petsc4py and numpy

Christian Staudt christian.staudt at ira.uka.de
Tue May 8 08:50:25 CDT 2012


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. )


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?


Regards & thank you for your answers,
Chris

[1] http://www.scipy.org/NumPy_for_Matlab_Users


More information about the petsc-users mailing list