How to pass MPI information to ctypes in python

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.

+4
source share
1 answer

You cannot map Python MPI.COMM_WORLD (which is an instance of the mpi4py Comm class) to MPI_COMM_WORLD (which is an int descriptor). This can be done by creating a wrapper using SWIG. The mpi4py tutorial has basically the same example as yours, but with the added SWIG interface file.

If you do not want to use SWIG, you can do the conversion in C code. If you look at the mpi4py.i file that imports the SWIG example, you can see that the conversion is done using PyMPIComm_Get . mpi4py source comes with an example that does not use SWIG .

+4
source

Source: https://habr.com/ru/post/1434604/


All Articles