[英]Wrap C++ function that returns MPI communicator using Cython
我正在嘗試包裝不接受任何輸入並返回MPI通信器的C ++函數。
foo.h中
class Foo{
public:
Foo(MPI_Comm _comm){
_comm = comm;
}
MPI_Comm getMPIComm(){
return comm
}
virtual void Foo1() = 0
我已經嘗試過以下方法:
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
這段代碼已編譯,我能夠編寫一個繼承此類的python代碼。 但是,當我嘗試在python級別訪問getMPIComm時,遇到了分段錯誤,並且valgrind給出的錯誤是:
Access not within mapped region at address 0x8
這是否意味着我沒有正確包裝getMPIComm()? 有人知道我應該怎么做嗎?
我認為您永遠不會分配*thisptr
,從而在嘗試對其調用getMPIComm
時遇到訪問沖突。 嘗試如下創建Foo
實例:
cdef class pyFoo:
cdef Foo *thisptr
def __cinit__(self,MPI.Comm _comm):
self.thisptr = new Foo(_comm)
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.