rambalachandran
rambalachandran

Reputation: 2201

How to pass MPI information to ctypes in python

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.

Upvotes: 4

Views: 2052

Answers (1)

Greg Inozemtsev
Greg Inozemtsev

Reputation: 4671

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.

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.

Upvotes: 5

Related Questions