Logo AND Algorithmique Numérique Distribuée

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