Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
leak --
[simgrid.git] / teshsuite / smpi / sendtest.c
1 /* A simple example pingpong pogram to test MPI_Send and MPI_Recv */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/time.h>
12 #include <mpi.h>
13
14 int main(int argc, char *argv[])
15 {
16     int rank;
17     int size;
18     MPI_Status status;
19
20     int n = 0, m = 0, bytes = 0, workusecs = 0;
21     int currusecs;
22   
23     char *buf = NULL;
24     int i, j;
25     struct timeval start_time, stop_time;
26     struct timeval start_pause, curr_pause;
27     unsigned long usecs;
28
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 */
32
33     if (size != 2) {
34         printf("run this program with exactly 2 processes (-np 2)\n");
35         MPI_Finalize();
36         exit(0);
37     }
38
39     if (0 == rank) {
40         if (argc > 1 && isdigit(argv[1][0])) {
41             n = atoi(argv[1]);
42         }
43         if (argc > 2 && isdigit(argv[2][0])) {
44             m = atoi(argv[2]);
45         }
46         if (argc > 3 && isdigit(argv[3][0])) {
47             bytes = atoi(argv[3]);
48         }
49         if (argc > 4 && isdigit(argv[4][0])) {
50             workusecs = atoi(argv[4]);
51         }
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);
64                 currusecs = 0;
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;
68                 }
69             }
70             MPI_Recv(buf, bytes, MPI_CHAR, 1, 0, MPI_COMM_WORLD, &status);
71         }
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);
75     } else {
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);
85             }
86             MPI_Send(buf, bytes, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
87         }
88     }
89     free(buf);
90     MPI_Finalize();
91     return 0;
92 }