问题描述
我的动机是将MPI信息有效地从python传递到通过ctypes调用的C函数.我在python中将mpi4py用于MPI绑定.我想通过一个用C编写并通过python中的ctypes调用的简单MPI示例代码来学习它.我已经详细说明了在下面运行时遇到的步骤和错误.
My motivation is to pass MPI information effectively from python to C functions invoked through ctypes. I used mpi4py for MPI bindings in python. I would like to learn it through a simple example MPI code written in C and invoked through ctypes in python. I have detailed the steps and the error that I get while running below.
C代码 [ passMpi4Py.c ]
#include <stdio.h>
#include <mpi.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);
}
我用gcc/openmpi-1.6编译了上面的c代码,如下所示:
I compiled the above c code with gcc/openmpi-1.6 as follows:
mpicc -shared -Wl,-soname,passMpi4Py -o passMpi4Py.so -fPIC passMpi4Py.c
Python包装器 [ passMpi4PyWrapper.py ]
import ctypes
from mpi4py import MPI
testlib = ctypes.CDLL('path-to-file/passMpi4Py/passMpi4Py.so')
testlib.sayhello(MPI.COMM_WORLD)
当我尝试通过运行以上代码时
When i try to run the above code by using
mpirun -np 4 python passMpi4PyWrapper.py
我收到以下错误
Traceback (most recent call last):
Traceback (most recent call last):
File "passMpi4PyWrapper.py", line 5, in <module>
Traceback (most recent call last):
File "passMpi4PyWrapper.py", line 5, in <module>
File "passMpi4PyWrapper.py", line 5, in <module>
Traceback (most recent call last):
testlib.sayhello(MPI.COMM_WORLD)
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
testlib.sayhello(MPI.COMM_WORLD)
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
testlib.sayhello(MPI.COMM_WORLD)
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
File "passMpi4PyWrapper.py", line 5, in <module>
testlib.sayhello(MPI.COMM_WORLD)
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1
更新:
在C程序MPI函数中使用* MPI_COMM_WORLD *代替 comm ,可以帮助我消除错误.但是,我仍然想知道这是否是将MPI信息传递给C程序的最佳方法.
Using *MPI_COMM_WORLD* instead of comm in the C program MPI functions helps me to remove the error. However I still would like to know if this is the best possible way to pass MPI information to a C program.
推荐答案
您缺少一种将Python的MPI.COMM_WORLD(这是mpi4py的Comm类的实例)映射到MPI_COMM_WORLD(这是一个int
句柄)的方法. .这可以通过使用SWIG生成包装器来完成. mpi4py教程具有与您基本相同的示例,但添加了SWIG接口文件.
You are missing a way to map Python's MPI.COMM_WORLD (which is an instance of the mpi4py's Comm class) to MPI_COMM_WORLD (which is an int
handle). This can be done by generating a wrapper with SWIG. The mpi4py tutorial has basically the same example as you have, but with the SWIG interface file added.
如果不想使用SWIG,则可以用C代码执行转换.如果查看SWIG示例正在导入的文件mpi4py.i,可以看到使用PyMPIComm_Get
完成了转换. mpi4py
源带有未使用的示例SWIG .
If you'd rather not use SWIG, you can perform the conversion in the C code. If you look at the file mpi4py.i that the SWIG example is importing, you can see that conversion is done with PyMPIComm_Get
. mpi4py
source comes with an example that does not use SWIG.
这篇关于如何在python中将MPI信息传递给ctypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!