1 /* A simple example pingpong pogram to test MPI_Send and MPI_Recv */
3 /* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
14 int main(int argc, char *argv[])
20 int n = 0, m = 0, bytes = 0, workusecs = 0;
25 struct timeval start_time, stop_time;
26 struct timeval start_pause, curr_pause;
29 MPI_Init(&argc, &argv); /* Initialize MPI */
30 MPI_Comm_size(MPI_COMM_WORLD, &size); /* Get nr of tasks */
31 MPI_Comm_rank(MPI_COMM_WORLD, &rank); /* Get id of this process */
34 printf("run this program with exactly 2 processes (-np 2)\n");
40 if (argc > 1 && isdigit(argv[1][0])) {
43 if (argc > 2 && isdigit(argv[2][0])) {
46 if (argc > 3 && isdigit(argv[3][0])) {
47 bytes = atoi(argv[3]);
49 if (argc > 4 && isdigit(argv[4][0])) {
50 workusecs = atoi(argv[4]);
52 buf = malloc(sizeof(char) * bytes);
53 for (i = 0; i < bytes; i++) buf[i] = i % 256;
54 MPI_Send(&n, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
55 MPI_Send(&m, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
56 MPI_Send(&bytes, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
57 MPI_Send(&workusecs, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
58 MPI_Barrier(MPI_COMM_WORLD);
59 gettimeofday(&start_time, NULL);
60 for (i = 0; i < n; i++) {
61 for (j = 0; j < m; j++) {
62 MPI_Send(buf, bytes, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
63 gettimeofday(&start_pause, NULL);
65 while (currusecs < workusecs) {
66 gettimeofday(&curr_pause, NULL);
67 currusecs = (curr_pause.tv_sec - start_pause.tv_sec) * 1e6 + curr_pause.tv_usec - start_pause.tv_usec;
70 MPI_Recv(buf, bytes, MPI_CHAR, 1, 0, MPI_COMM_WORLD, &status);
72 gettimeofday(&stop_time, NULL);
73 usecs = (stop_time.tv_sec - start_time.tv_sec) * 1e6 + stop_time.tv_usec - start_time.tv_usec;
74 printf("n: %d m: %d bytes: %d sleep: %d usecs: %u\n", n, m, bytes, workusecs, usecs);
76 MPI_Recv(&n, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
77 MPI_Recv(&m, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
78 MPI_Recv(&bytes, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
79 MPI_Recv(&workusecs, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
80 buf = malloc(sizeof(char) * bytes);
81 MPI_Barrier(MPI_COMM_WORLD);
82 for (i = 0; i < n; i++) {
83 for (j = 0; j < m; j++) {
84 MPI_Recv(buf, bytes, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
86 MPI_Send(buf, bytes, MPI_CHAR, 0, 0, MPI_COMM_WORLD);