Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compile the sendrecv example
[simgrid.git] / examples / smpi / reduce.c
1 #include <stdio.h>
2 #include <mpi.h>
3
4 /**
5  * MESSAGE PASSING INTERFACE TEST CASE SUITE
6  *
7  * Copyright IBM Corp. 1995
8  * 
9  * IBM Corp. hereby grants a non-exclusive license to use, copy, modify, and
10  *distribute this software for any purpose and without fee provided that the
11  *above copyright notice and the following paragraphs appear in all copies.
12
13  * IBM Corp. makes no representation that the test cases comprising this
14  * suite are correct or are an accurate representation of any standard.
15
16  * In no event shall IBM be liable to any party for direct, indirect, special
17  * incidental, or consequential damage arising out of the use of this software
18  * even if IBM Corp. has been advised of the possibility of such damage.
19
20  * IBM CORP. SPECIFICALLY DISCLAIMS ANY WARRANTIES INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS AND IBM
23  * CORP. HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
24  * ENHANCEMENTS, OR MODIFICATIONS.
25  * ***************************************************************************
26  **/
27 int ibm_test(int rank, int size)
28 {
29   int success = 1;
30 #define MAXLEN  10000
31
32   int root, i, j, k;
33   int out[MAXLEN];
34   int in[MAXLEN];
35   root = size / 2;
36
37   for (j = 1; j <= MAXLEN; j *= 10) {
38     for (i = 0; i < j; i++)
39       out[i] = i;
40
41     MPI_Reduce(out, in, j, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
42
43     if (rank == root) {
44       for (k = 0; k < j; k++) {
45         if (in[k] != k * size) {
46           printf("bad answer (%d) at index %d of %d (should be %d)", in[k], k,
47                  j, k * size);
48           success = 0;
49           break;
50         }
51       }
52     }
53   }
54   return (success);
55 }
56
57
58
59
60 int main(int argc, char **argv)
61 {
62   int size, rank;
63   int root = 0;
64   int value;
65   int sum = -99, sum_mirror = -99, min = 999, max = -999;
66
67   double start_timer;
68
69
70   MPI_Init(&argc, &argv);
71   MPI_Comm_size(MPI_COMM_WORLD, &size);
72   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
73
74   start_timer = MPI_Wtime();
75
76   value = rank + 1;             /* easy to verify that sum= (size*(size+1))/2; */
77
78   printf("[%d] has value %d\n", rank, value);
79   MPI_Reduce(&value, &sum, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
80   MPI_Reduce(&value, &sum_mirror, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
81
82   MPI_Reduce(&value, &min, 1, MPI_INT, MPI_MIN, root, MPI_COMM_WORLD);
83   MPI_Reduce(&value, &max, 1, MPI_INT, MPI_MAX, root, MPI_COMM_WORLD);
84   if (rank == root) {
85     printf("** Scalar Int Test Result:\n");
86     printf("\t[%d] sum=%d ... validation ", rank, sum);
87     if (((size * (size + 1)) / 2 == sum) && (sum_mirror == sum))
88       printf("ok.\n");
89     else
90       printf("failed (sum=%d,sum_mirror=%d while both sould be %d.\n",
91              sum, sum_mirror, (size * (size + 1)) / 2);
92     printf("\t[%d] min=%d ... validation ", rank, min);
93     if (1 == min)
94       printf("ok.\n");
95     else
96       printf("failed.\n");
97     printf("\t[%d] max=%d ... validation ", rank, max);
98     if (size == max)
99       printf("ok.\n");
100     else
101       printf("failed.\n");
102     printf("Elapsed time=%lf s\n", MPI_Wtime() - start_timer);
103   }
104
105   MPI_Barrier(MPI_COMM_WORLD);
106
107   if (0 == rank)
108     printf("** IBM Test Result: ... \n");
109   if (!ibm_test(rank, size))
110     printf("\t[%d] failed.\n", rank);
111   else
112     printf("\t[%d] ok.\n", rank);
113
114   MPI_Finalize();
115   return 0;
116 }