My motivation is to pass MPI information efficiently from python to C functions called by ctypes. I used mpi4py for MPI bindings in python. I would like to learn it with a simple example MPI code written in C and called via ctypes in python. I have described in detail the steps and error that I get while working 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 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 the comm function in C program The MPI functions help me remove the error. However, I would still like to know if this is the best way to transfer MPI information to program C.