32bit/64bit issue when working with Numpy and petsc4py

When indexing PETSc.Mat A with an array c ( numpy.ndarray with dtype('int64')), I run into the following error:

  File "/Users/cls/workspace/LAMG-Python/src/lamg/amg/setup.py", line 945, in buildInternal
Acc = A[c, c]
File "Mat.pyx", line 185, in petsc4py.PETSc.Mat.__getitem__ (src/petsc4py.PETSc.c:71407)
File "petscmat.pxi", line 862, in petsc4py.PETSc.mat_getitem (src/petsc4py.PETSc.c:22442)
File "petscmat.pxi", line 773, in petsc4py.PETSc.matgetvalues (src/petsc4py.PETSc.c:21226)
File "arraynpy.pxi", line 123, in petsc4py.PETSc.iarray_i (src/petsc4py.PETSc.c:5284)
File "arraynpy.pxi", line 117, in petsc4py.PETSc.iarray (src/petsc4py.PETSc.c:5192)
TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'


I assume there is a simple solution (allowing the cast, making everything 32 or 64 bit...). Can you point me in the right direction? Thanks.

• oops, just noticed this question languishing on the petsc4py mailing list :( May 26 '12 at 13:06
• Yeah, I thought it must be a really stupid question if nobody cares to answer. ;) May 26 '12 at 13:32

If you always want 64-bit indices, then you should configure and build PETSc with 64-bit indices (pass --with-64-bit-indices to configure) and everything will work fine.
On the other hand, if you would like to explicitly force a copy if needed every time you hand a data structure to PETSc, you can do this by c.astype(petsc4py.PETSc.IntType). See this question on StackOverflow for more on the intricacies of doing/avoiding a Numpy copy.
• I'd instantiate the index arrays in numpy using the petsc4py.PETSc.IntType data type, which will increase your code coupling to petsc4py, but avoid the need for doing a copy if you otherwise don't care about integer type. May 26 '12 at 14:49