Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9647aee838250022f95cc88e227afca009ba1a2f
[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,i,j,k;
33           int out[MAXLEN*64];
34           int in[MAXLEN];
35
36
37           for(j=1,root=0;j<=MAXLEN;j*=10,root=(root+1)%size)  {
38                     if(rank == root)
39                                 for(i=0;i<j*size;i++)  out[i] = i;
40
41                     MPI_Scatter(out,j,MPI_INT,in,j,MPI_INT,root,MPI_COMM_WORLD);
42
43                     for(k=0;k<j;k++) {
44                                 if(in[k] != k+rank*j) {
45                                           fprintf(stderr,"task %d bad answer (%d) at index %d k of %d (should be %d)", rank,in[k],k,j,(k+rank*j));
46                                           return( 0 ); 
47                                 }
48                     }
49           }
50         return( success );
51           MPI_Barrier( MPI_COMM_WORLD );
52 }
53
54 /**
55  * small test: the root sends a single distinct double to other processes
56  **/
57 int small_test( int rank, int size ) {
58   int success=1;
59   int sendcount=size;
60   int recvcount=1;
61   int i;
62   double *sndbuf =NULL;
63   double rcvd;
64   int root=0; // arbitrary choice 
65
66   // on root, initialize sendbuf
67   if (root == rank )  {
68             sndbuf = malloc( sendcount*sizeof(double));
69             for (i=0; i<sendcount;i++) {
70                         sndbuf[i] = (double) rank;
71             }
72   }
73
74    MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd,recvcount,MPI_DOUBLE,root,MPI_COMM_WORLD);
75
76   return(success);
77 }
78
79
80
81 int main(int argc, char **argv)
82 {
83   int size, rank;
84
85
86   MPI_Init(&argc, &argv);
87   MPI_Comm_size(MPI_COMM_WORLD, &size);
88   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
89
90
91
92   if (0 == rank)
93     printf("** Small Test Result: ... \n");
94   small_test( rank, size );
95
96
97   if (0 == rank)
98     printf("** IBM Test Result: ... \n");
99 /*  if (!ibm_test(rank, size))
100     printf("\t[%d] failed.\n", rank);
101   else
102     printf("\t[%d] ok.\n", rank);
103 */
104   MPI_Finalize();
105   return 0;
106 }