This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

PETSc matrix convertion

+1 vote

Hi,

I'm having trouble converting a FEniCS matrix into a PETSc matrix. For the smaller dimensional I have no problems but for the larger dimensional problems I get the following errors

As = AA.sparray()
A = PETSc.Mat().createAIJ(size=As.shape, csr=(As.indptr, As.indices, As.data))

gives the error

     24     else:
     25         if AA.size(1) == AA.size(0):
---> 26             As = AA.sparray()
     27             A = PETSc.Mat().createAIJ(size=As.shape, csr=(As.indptr, As.indices, As.data))
     28             #row, col, value = AA.data()

/ubc/cs/research/scl/people/mwathen/.hastdist/bld/profile/3kneeoad7jpp/lib/python2.7/site-packages/dolfin/cpp/la.py in sparray(self)
    649         from scipy.sparse import csr_matrix
    650         data = self.data(deepcopy=True)
--> 651         C = csr_matrix((data[2], data[1], data[0]))
    652         return C
    653

/ubc/cs/research/scl/people/mwathen/install/lib/python2.7/site-packages/scipy/sparse/compressed.pyc in __init__(self, arg1, shape, dtype, copy)
     86             self.data = np.asarray(self.data, dtype=dtype)
     87
---> 88         self.check_format(full_check=False)
     89
     90     def getnnz(self, axis=None):

/ubc/cs/research/scl/people/mwathen/install/lib/python2.7/site-packages/scipy/sparse/compressed.pyc in check_format(self, full_check)
    167             raise ValueError("indices and data should have the same size")
    168         if (self.indptr[-1] > len(self.indices)):
--> 169             raise ValueError("Last value of index pointer should be less than "
    170                                 "the size of index and data arrays")
    171

ValueError: Last value of index pointer should be less than the size of index and data arrays

.

row, col, value = AA.data()
A = PETSc.Mat().createAIJ(size=(AA.size(0),AA.size(1)),csr=(row.astype('int32'), col.astype('int32'), value))

gives the error

Segmentation fault

.

A = as_backend_type(AA).mat()

[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run
[0]PETSC ERROR: to get more information on the crash.
application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0

Is there any other way to convert the matrix into a PETSc format? The machine I am running this on has 256G of RAM so memory isn't an issue.

asked Apr 29, 2015 by mwathen FEniCS Novice (710 points)

Is there any particular reason to not start with a PETSc matrix right away?

Do you mean using

A = PETScMatrix()

This function is not available in FEniCS 1.5....

Hi, maybe you could use the mat() method of PETScMatrix. Works fine for 1.5.0

from dolfin import *

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)

M = PETScMatrix()
assemble(inner(u, v)*dx, tensor=M)

Mmat = M.mat()
print type(Mmat)

Mcopy = Mmat.duplicate()
Mcopy.zeroEntries()

print Mcopy.getRow(3)
print Mmat.getRow(3)
print M.getrow(3)
...