Skip to content

Instantly share code, notes, and snippets.

@shigh
Last active March 31, 2022 15:30
Show Gist options
  • Save shigh/6708484 to your computer and use it in GitHub Desktop.
Save shigh/6708484 to your computer and use it in GitHub Desktop.
Using MPI with Cython and C++. Specifically, using Cython to make Python wrappers for C++ code that uses MPI.
# Make sure to call this first to initialize MPI
from mpi4py import MPI
from pympi import *
stats = mpi_stats()
print "Hello from process %i of %i!" % (stats.rank, stats.size)
all:
python setup.py build_ext --inplace
clean:
rm -f pympi.cpp
rm -f pympi.so
rm -rf build
#include "mpi.h"
#include "mpi_stats.hpp"
// You could call these from mpi4py...
// but what would be the fun in that!
bool check_mpi()
{
int flag;
MPI_Initialized(&flag);
return flag == 1;
}
int get_rank()
{
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
return rank;
}
int get_comm_size()
{
int numtasks;
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
return numtasks;
}
int thread_level()
{
int provided = -1;
if(check_mpi())
MPI_Query_thread(&provided);
return provided;
}
bool has_thread_multiple()
{
return thread_level() == MPI_THREAD_MULTIPLE;
}
#ifndef __MPI_STATS_H
#define __MPI_STATS_H
#include "mpi.h"
bool check_mpi();
int get_rank();
int get_comm_size();
int thread_level();
bool has_thread_multiple();
#endif
from collections import namedtuple, Iterable
from libcpp cimport bool
cdef extern from "mpi_stats.hpp":
bool check_mpi()
int get_rank()
int get_comm_size()
int thread_level()
bool has_thread_multiple()
MPIStats = namedtuple("MPIStats", ["check_mpi", "rank", "size",
"thread_level", "has_thread_multiple"])
def mpi_stats():
return MPIStats(check_mpi(), get_rank(), get_comm_size(),
thread_level(), has_thread_multiple())
echo Checking MPI stats
mpirun -n 4 python stats.py
echo
echo Running hello_world
mpirun -n 4 python hello_world.py
import os
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
mpi_compile_args = os.popen("mpic++ --showme:compile").read().strip().split(' ')
mpi_link_args = os.popen("mpic++ --showme:link").read().strip().split(' ')
ext_modules=[
Extension("pympi",
sources = ["pympi.pyx", "mpi_stats.cpp"],
language = 'c++',
extra_compile_args = mpi_compile_args,
extra_link_args = mpi_link_args,
)
]
setup(
name = "pympi",
cmdclass = {"build_ext": build_ext},
ext_modules = ext_modules
)
# Make sure to call this first to initialize MPI
from mpi4py import MPI
from pympi import *
print mpi_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment