File: helloworld.c

package info (click to toggle)
mpi4py 1.3%2Bhg20120611-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,020 kB
  • sloc: python: 9,503; ansic: 6,296; makefile: 571; f90: 158; sh: 146; cpp: 103
file content (42 lines) | stat: -rw-r--r-- 965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 *  You can use safely use mpi4py between multiple
 *  Py_Initialize()/Py_Finalize() calls ...
 *  but do not blame me for the memory leaks ;-)
 *
 */

#include <mpi.h>
#include <Python.h>

const char helloworld[] = \
  "from mpi4py import MPI                                \n"
  "hwmess = 'Hello, World! I am process %d of %d on %s.' \n"
  "myrank = MPI.COMM_WORLD.Get_rank()                    \n"
  "nprocs = MPI.COMM_WORLD.Get_size()                    \n"
  "procnm = MPI.Get_processor_name()                     \n"
  "print (hwmess % (myrank, nprocs, procnm))             \n"
  "";

int main(int argc, char *argv[])
{
  int i,n=5;

  MPI_Init(&argc, &argv);

  for (i=0; i<n; i++) {
    Py_Initialize();
    PyRun_SimpleString(helloworld);
    Py_Finalize();
  }

  Py_Initialize();
  PyRun_SimpleString(helloworld);
  MPI_Finalize();
  Py_Finalize();

  Py_Initialize();
  PyRun_SimpleString("from mpi4py import MPI\n");
  Py_Finalize();

  return 0;
}