1

I am trying to wrap a C++ function that doesn't take any input and returns the MPI communicator.

Foo.h

class Foo{
 public:
  Foo(MPI_Comm _comm){
   _comm = comm;
  }
  MPI_Comm getMPIComm(){
   return comm
  }
  virtual void Foo1() = 0 

I have tried that following:

source.pyx

cimport mpi4py.MPI as MPI
from mpi4py.libmpi cimport *

cdef extern from  Foo.h:
   cdef cppclass Foo:
      Foo(MPI_Comm _comm)
      MPI_Comm getMPIComm()
      void Foo1() 

cdef class pyFoo:
   cdef Foo *thisptr
   def __cinit__(self,MPI.Comm _comm):
      pass
   def get MPIComm(self):
      c_comm = self.thisptr.getMPIComm()
      return <MPI.Comm> c_comm
   def Foo1(self):
      pass       

This code compiled and I was able to write a python code that inherits this class. However, when I tried to access getMPIComm at the python level, I encountered a segmentation fault and the error given by valgrind is:

Access not within mapped region at address 0x8

Does that mean that I did not wrap getMPIComm() properly? Does anyone know how I should approach it?

4

1 回答 1

0

我认为您从不分配*thisptr,因此在尝试调用getMPIComm它时会遇到访问冲突。尝试Foo如下创建一个实例:

cdef class pyFoo:
   cdef Foo *thisptr
   def __cinit__(self,MPI.Comm _comm):
      self.thisptr = new Foo(_comm)
   # ...
于 2015-03-11T15:07:52.597 回答