[petsc-users] petsc4py and numpy

Lisandro Dalcin dalcinl at gmail.com
Tue May 8 14:11:22 CDT 2012


On 8 May 2012 12:25, Matthew Knepley <knepley at gmail.com> wrote:
> On Tue, May 8, 2012 at 11:15 AM, Lisandro Dalcin <dalcinl at gmail.com> wrote:
>>
>> On 8 May 2012 12:04, Matthew Knepley <knepley at gmail.com> wrote:
>> > On Tue, May 8, 2012 at 11:00 AM, Christian Staudt
>> > <christian.staudt at ira.uka.de> wrote:
>> >>
>> >> Matthew Knepley <knepley at gmail.com> wrote:
>> >>
>> >> 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.
>> >>
>> >>
>> >> I don't think this is implemented in petsc4py: At least, there's no
>> >> method
>> >> PETSc.Mat.getArray()
>> >
>> >
>> > If you ask Lisandro for it, he will put it in.
>> >
>>
>> Matt, I've never put that in just because I'm not sure how to make it
>> general. PETSc have many different matrix formats, and MatGetArray()
>> call is like a hack, where you can get very different things (the "1D"
>> values of an AIJ matrix, the "2D" values of a dense matrix in Fortran
>> order). What about MPIAIJ, what should I return to users? Can you
>> suggest some approach or API to implement this (I mean, how to get
>> arrays for the various matrix types)?
>
>
> I think the whole point is that you should not do it. However, that has
> never stopped us from giving users
> what they ask for. We are not int he business of reforming computing
> practices, which is why we have
> MatGetArray(). I would make a wrapper that failed unless the type was
> MATDENSE.
>

Yea, that's really easy.

However, Is there anything better for AIJ matrices? Right now, we have
mat.getValuesCSR()... it works by calling MatGetRow() row by row twice
(first pass to count and allocate, next to get values and copy to
NumPy arrays).


-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169


More information about the petsc-users mailing list