Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Massive mv to use cmake as the default compilation infrastructure.
[simgrid.git] / examples / smpi / scatter.c
1 /**
2  * MESSAGE PASSING INTERFACE TEST CASE SUITE
3  *
4  * Copyright IBM Corp. 1995
5  * 
6  * IBM Corp. hereby grants a non-exclusive license to use, copy, modify, and
7  *distribute this software for any purpose and without fee provided that the
8  *above copyright notice and the following paragraphs appear in all copies.
9
10  * IBM Corp. makes no representation that the test cases comprising this
11  * suite are correct or are an accurate representation of any standard.
12
13  * In no event shall IBM be liable to any party for direct, indirect, special
14  * incidental, or consequential damage arising out of the use of this software
15  * even if IBM Corp. has been advised of the possibility of such damage.
16
17  * IBM CORP. SPECIFICALLY DISCLAIMS ANY WARRANTIES INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS AND IBM
20  * CORP. HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
21  * ENHANCEMENTS, OR MODIFICATIONS.
22  * ***************************************************************************
23  **/
24 #include <stdio.h>
25 #include <mpi.h>
26
27 int ibm_test(int rank, int size)  {
28
29 #define MAXLEN  10000
30
31           int success = 1;
32           int root=0;
33         int i,j,k;
34           int *out;
35           int *in;
36         
37         
38         out = malloc(MAXLEN*64*sizeof(int));
39         in  = malloc(MAXLEN*sizeof(int));
40
41           for(j=1;j<=MAXLEN;j*=10){
42                 root=(root+1)%size;
43                     if (rank == root)
44                                 for(i=0;i<j*size;i++)  
45                                 out[i] = i;
46
47                     MPI_Scatter(out,j,MPI_INT,in,j,MPI_INT,root,MPI_COMM_WORLD);
48
49                     for(k=0;k<j;k++) {
50                                 if(in[k] != k+rank*j) {
51                                           fprintf(stderr,"task %d bad answer (%d) at index %d k of %d (should be %d)", rank,in[k],k,j,(k+rank*j));
52                                           return( 0 ); 
53                                 }
54                     }
55           }
56         free(out);
57         free(in);
58           MPI_Barrier( MPI_COMM_WORLD );
59         return( success );
60 }
61
62 /**
63  * small test: the root sends a single distinct double to other processes
64  **/
65 int small_test( int rank, int size ) {
66         int success=1;
67         int retval;
68         int sendcount=1; // one double to each process
69         int recvcount=1;
70         int i;
71         double *sndbuf =NULL;
72         double rcvd;
73         int root=0; // arbitrary choice 
74
75         // on root, initialize sendbuf
76         if (root == rank )  {
77                 sndbuf = malloc( size * sizeof(double));
78                 for (i=0; i< size;i++) {
79                         sndbuf[i] = (double) i;
80                 }
81         }
82
83         retval=MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd,recvcount,MPI_DOUBLE,root,MPI_COMM_WORLD);
84         if (retval != MPI_SUCCESS) {
85                 fprintf(stderr,"(%s:%d) MPI_Scatter() returned retval=%d\n",__FILE__,__LINE__,retval);
86                 return 0;
87         }
88
89         // verification
90         if ((double)rank != rcvd) {
91                 fprintf(stderr,"[%d] has %lf instead of %d\n",rank,rcvd,rank);
92                 success=0;
93         }
94         return(success);
95 }
96
97
98
99 int main(int argc, char **argv)
100 {
101         int size, rank;
102
103
104         MPI_Init(&argc, &argv);
105         MPI_Comm_size(MPI_COMM_WORLD, &size);
106         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
107
108         /* test 1 */
109         if (0 == rank)
110                 printf("** Small Test Result: ... \n");
111         if (! small_test( rank, size ))
112                 printf("\t[%d] failed.\n", rank);
113         else
114                 printf("\t[%d] ok.\n", rank);
115
116
117         MPI_Barrier( MPI_COMM_WORLD );
118
119         /* test 2 */
120         if (0 == rank)
121                 printf("** IBM Test Result: ... \n");
122         if (!ibm_test(rank, size))
123                 printf("\t[%d] failed.\n", rank);
124         else
125                 printf("\t[%d] ok.\n", rank);
126
127         MPI_Finalize();
128         return 0;
129 }