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.

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

There are two solutions, depending on what you want. As a point of information, PETSc defaults to 32-bit indexing on normal builds.

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.

| cite | improve this answer | |
  • $\begingroup$ What would you recommend? If numpy defaults to 64-bit , I think I should rebuild PETSc with 64 bit indices. I doubt that my matrices will be large enough that the integer size becomes a concern. $\endgroup$ – clstaudt May 26 '12 at 13:35
  • 2
    $\begingroup$ 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. $\endgroup$ – Aron Ahmadia May 26 '12 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.