Extending Python with C: Cannot find MPI library

Spectrum spectrumdt at gmail.com
Thu Jun 19 07:47:39 EDT 2008


  I am writing some Python code using the Message Passing Interface
(MPI), an API used in parallel computing. There exist a number of
Python implementations of MPI, but apparently they all rely on the
Numeric Python (numpy) package. I need to run my code on a particular
machine made available by my university, which does not have numpy and
will not be getting it.

  So I am trying to write my own mini-wrapper for MPI in C to extend
my Python program.

  There exists a special C compiler to use when compiling C programs
that use MPI, called mpicc.

  Here is what I have tried:

  I have written a minimal C program that uses MPI. It works correctly
when compiled with mpicc.

  Then I've written Python boilerplate code and a Python script to
compile my C program into a Python-includable module. This does not
work a priori because the Python compilation script uses gcc rather
than mpicc. So I have taken the exact gcc command that the script uses
and replaced "gcc" with "mpicc".

  This produces an *.so file that compiles without errors.
Unfortunately, it fails when I try to import it in Python. It can't
find the MPI library.

  My MPI code looks like this (plus some boilerplate code):



  /* Return the MPI rank of the current process. */
  int rank(){
    int  argc;
    char **argv;
    int rank, size;
    MPI_Init( &argc, &argv );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Finalize();
    return rank;
  }

  /* Main. A 'Hello World' function. */
  int hello() {
    int rankNumber = rank();
    printf ("Hello, World. I am process %d.\n", rankNumber);
    return rankNumber;
  }



  My Python program that includes it looks like this:



  import ctest

  ctest.hello()




  My error message is this:



  [ore at localhost Opgave03]$ mpiexec -n 1 python ctest.py
  Traceback (most recent call last):
    File "ctest.py", line 1, in <module>
      import ctest
  ImportError: /big/School/Cluster/Opgave03/ctest.so: undefined
symbol: ompi_mpi_comm_world
  [ore at localhost Opgave03]$



  Can anyone suggest anything? Can I get MPI to work in Python?

  Last time I asked a similar question, someone recommended that I
check out Cython instead of C. Do MPI bindings for Cython exist?

Thanks in advance.

- Claus Appel



More information about the Python-list mailing list