我正试图包装一个c函数
MPI_Comm
通信器通过Cython作为参数处理。因此,我希望能够从python调用该函数,并将其传递给
mpi4py.MPI.Comm
对象。我想知道的是,如何从
MPI4PY.MPI.COMM
到
MPIICOM
。
为了演示,我使用了一个简单的“你好世界!”型功能:
helloworld.h
:
#ifndef HELLOWORLD
#define HELLOWORLD
#include <mpi.h>
void sayhello(MPI_Comm comm);
#endif
helloworld.c
:
#include <stdio.h>
#include "helloworld.h"
void sayhello(MPI_Comm comm){
int size, rank;
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
printf("Hello, World! "
"I am process %d of %d.\n",
rank, size);
}
现在我想从python调用这个函数,如下所示:
from_python.py
:
import mpi4py
import helloworld_wrap
helloworld_wrap.py_sayhello(mpi4py.MPI.COMM_WORLD)
意思是
mpirun -np 4 python2 from_python.py
应该给一些类似的东西:
你好,世界!我是第0个进程,共4个。
你好,世界!我是4个过程中的1个。
你好,世界!我是4个过程中的第2个。
你好,世界!我是4个过程中的第3个。
但如果我尝试通过这样的赛通来实现这一点:
helloworld_wrap.pyx
:
cimport mpi4py.MPI as MPI
cimport mpi4py.libmpi as libmpi
cdef extern from "helloworld.h":
void sayhello(libmpi.MPI_Comm comm)
def py_sayhello(MPI.Comm comm):
sayhello(comm)
还有:
setup.py
:
import os
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
mpi_compile_args = os.popen("mpicc --showme:compile").read().strip().split(' ')
mpi_link_args = os.popen("mpicc --showme:link").read().strip().split(' ')
ext_modules=[
Extension("helloworld_wrap",
sources = ["helloworld_wrap.pyx", "helloworld.c"],
language = 'c',
extra_compile_args = mpi_compile_args,
extra_link_args = mpi_link_args,
)
]
setup(
name = "helloworld_wrap",
cmdclass = {"build_ext": build_ext},
ext_modules = ext_modules
)
我收到以下错误消息:
helloworld_wrap.pyx:8:13:无法将python对象转换为“mpi_comm”
表明
MPI4PY.MPI.COMM
无法转换为
MPIICOM
. 那么我如何转换
MPI4PY.MPI.COMM
变成一个
MPIICOM
为了让我的包装工作?