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 code [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);
}
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 Wrapper [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
I get the following error
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
Update:
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.
MPI_COMM_WORLDinsayhello? - mgilsonsayhelloa function which accepts 0 arguments. e.g.void sayhello(){ ...; MPI_Comm_size(MPI_COMM_WORLD,&size);... }- mgilsonMPI_COMM_WORLDis of typeMPI_Commwhich could be just about anything. It could betypedefed toint,long,long long... or it could even be some sort ofstruct(the standard doesn't say)`. Likely, it is an 4 byte integer on most systems to be compatible with the fortran bindings, but that's definitely not guaranteed. - mgilson