Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
roots of a generic surf_resource_t type
[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           int success=1;
29 #define MAXLEN  10000
30
31           int root,i,j,k;
32           int out[MAXLEN];
33           int in[MAXLEN];
34           root = size/2;
35
36           for(j=1;j<=MAXLEN;j*=10)  {
37                     for(i=0;i<j;i++)  out[i] = i;
38
39                     MPI_Reduce(out,in,j,MPI_INT,MPI_SUM,root,MPI_COMM_WORLD);
40
41                     if(rank == root)  {
42                                 for(k=0;k<j;k++) {
43                                           if(in[k] != k*size) {  
44                                                     printf("bad answer (%d) at index %d of %d (should be %d)", in[k],k,j,k*size);
45                                                     success=0;
46                                                     break; 
47                                           }
48                                 }
49                     }
50           }
51           return( success );
52 }
53
54
55
56
57 int main (int argc, char **argv) {
58           int size, rank;
59           int root=0;
60           int value;
61           int sum=-99,sum_mirror=-99,min=999,max=-999;
62
63   double start_timer;
64
65
66   MPI_Init(&argc, &argv);
67   MPI_Comm_size(MPI_COMM_WORLD, &size);
68   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
69
70   start_timer = MPI_Wtime();
71
72   value=rank+1;  /* easy to verify that sum= (size*(size+1))/2; */
73
74   printf("[%d] has value %d\n", rank, value);
75   MPI_Reduce(&value, &sum, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
76   MPI_Reduce(&value, &sum_mirror, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
77
78   MPI_Reduce(&value, &min, 1, MPI_INT, MPI_MIN, root, MPI_COMM_WORLD);
79   MPI_Reduce(&value, &max, 1, MPI_INT, MPI_MAX, root, MPI_COMM_WORLD);
80   if ( rank == root) {
81           printf("** Scalar Int Test Result:\n");
82             printf("\t[%d] sum=%d ... validation ",rank,sum);
83           if (((size*(size+1))/2 == sum) && (sum_mirror==sum) )
84                         printf("ok.\n");
85             else 
86                         printf("failed (sum=%d,sum_mirror=%d while both sould be %d.\n",
87                                             sum,sum_mirror,(size*(size+1))/2);
88           printf("\t[%d] min=%d ... validation ",rank,min);
89           if (1 == min )
90                         printf("ok.\n");
91             else
92                         printf("failed.\n");
93           printf("\t[%d] max=%d ... validation ",rank,max);
94           if ( size == max )
95                         printf("ok.\n");
96             else
97                         printf("failed.\n");
98             printf("Elapsed time=%lf s\n", MPI_Wtime()-start_timer);
99   }
100
101   MPI_Barrier( MPI_COMM_WORLD );
102
103   if ( 0 == rank ) 
104             printf("** IBM Test Result: ... \n");
105   if (!ibm_test( rank, size))
106             printf("\t[%d] failed.\n",rank);
107   else
108             printf("\t[%d] ok.\n",rank);
109
110   MPI_Finalize();
111   return 0;
112 }
113