File: cpi-master.cxx

package info (click to toggle)
mpi4py 4.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,540 kB
  • sloc: python: 34,465; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (31 lines) | stat: -rw-r--r-- 673 bytes parent folder | download | duplicates (5)
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
#include <mpi.h>
#include <cstdio>
#include <cstring>
#include <cmath>

int main(int argc, char *argv[])
{
  MPI::Init();

  char cmd[32] = "./cpi-worker-cxx.exe";
  if (argc > 1) std::strcpy(cmd, argv[1]);
  std::printf("%s -> %s\n", argv[0], cmd);

  MPI::Intercomm worker;
  worker = MPI::COMM_SELF.Spawn(cmd, MPI::ARGV_NULL, 5,
                                MPI::INFO_NULL, 0);

  int n = 100;
  worker.Bcast(&n, 1, MPI::INT, MPI::ROOT);

  double pi;
  worker.Reduce(MPI::BOTTOM, &pi, 1, MPI::DOUBLE,
                MPI::SUM, MPI::ROOT);

  worker.Disconnect();

  std::printf("pi: %.16f, error: %.16f\n", pi, std::fabs(M_PI-pi));

  MPI::Finalize();
  return 0;
}