File: hellompi.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (46 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download | duplicates (4)
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
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char* argv[])
{
  int rank;
  int size;
  int i = 1;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  if (rank == 0)
    printf("*** Will print ten rows of dots.\n");  /* 1e7 / 1e6 == 10 */
  printf("Hello, world, I am %d of %d\n", rank, size);

  for (i = 1; i < (int)1e7; i++)
  { int buf;
    MPI_Status status;

    buf = i;
    if (rank == 0) {
      /* Send to neighbor on right */
      MPI_Send(&buf, 1, MPI_INT, (rank+1)%size, 0, MPI_COMM_WORLD);
    }
    /* Receive from neighbor on left */
    MPI_Recv(&buf, 1, MPI_INT, (rank-1+size)%size, 0, MPI_COMM_WORLD, &status);
    if (i != buf) {
      fprintf(stderr, "****** INCORRECT RESULT:  %d\n", i);
      exit(1);
    }
    if (rank != 0) {
      /* Send to neighbor on right */
      MPI_Send(&buf, 1, MPI_INT, (rank+1)%size, 0, MPI_COMM_WORLD);
    }

    if (rank == 0) {
      if (i % (int)1e5 == 0) {printf("."); fflush(stdout);}
      if (i % (int)5e6 == 0) printf("\n");
    }
  }
  MPI_Finalize();
  return 0;
}