Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adding more categories to instrumented NAS DT benchmark
[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 static int ibm_test(int rank, int size)
28 {
29
30 #define MAXLEN  10000
31
32   int success = 1;
33   int root = 0;
34   int i, j, k;
35   int *out;
36   int *in;
37
38
39   out = malloc(MAXLEN * 64 * sizeof(int));
40   in = malloc(MAXLEN * sizeof(int));
41
42   for (j = 1; j <= MAXLEN; j *= 10) {
43     root = (root + 1) % size;
44     if (rank == root)
45       for (i = 0; i < j * size; i++)
46         out[i] = i;
47
48     MPI_Scatter(out, j, MPI_INT, in, j, MPI_INT, root, MPI_COMM_WORLD);
49
50     for (k = 0; k < j; k++) {
51       if (in[k] != k + rank * j) {
52         fprintf(stderr,
53                 "task %d bad answer (%d) at index %d k of %d (should be %d)",
54                 rank, in[k], k, j, (k + rank * j));
55         return (0);
56       }
57     }
58   }
59   free(out);
60   free(in);
61   MPI_Barrier(MPI_COMM_WORLD);
62   return (success);
63 }
64
65 /**
66  * small test: the root sends a single distinct double to other processes
67  **/
68 static int small_test(int rank, int size)
69 {
70   int success = 1;
71   int retval;
72   int sendcount = 1;            // one double to each process
73   int recvcount = 1;
74   int i;
75   double *sndbuf = NULL;
76   double rcvd;
77   int root = 0;                 // arbitrary choice 
78
79   // on root, initialize sendbuf
80   if (root == rank) {
81     sndbuf = malloc(size * sizeof(double));
82     for (i = 0; i < size; i++) {
83       sndbuf[i] = (double) i;
84     }
85   }
86
87   retval =
88       MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount,
89                   MPI_DOUBLE, root, MPI_COMM_WORLD);
90   if (retval != MPI_SUCCESS) {
91     fprintf(stderr, "(%s:%d) MPI_Scatter() returned retval=%d\n", __FILE__,
92             __LINE__, retval);
93     return 0;
94   }
95   // verification
96   if ((double) rank != rcvd) {
97     fprintf(stderr, "[%d] has %lf instead of %d\n", rank, rcvd, rank);
98     success = 0;
99   }
100   return (success);
101 }
102
103
104
105 int main(int argc, char **argv)
106 {
107   int size, rank;
108
109
110   MPI_Init(&argc, &argv);
111   MPI_Comm_size(MPI_COMM_WORLD, &size);
112   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
113
114   /* test 1 */
115   if (0 == rank)
116     printf("** Small Test Result: ... \n");
117   if (!small_test(rank, size))
118     printf("\t[%d] failed.\n", rank);
119   else
120     printf("\t[%d] ok.\n", rank);
121
122
123   MPI_Barrier(MPI_COMM_WORLD);
124
125   /* test 2 */
126   if (0 == rank)
127     printf("** IBM Test Result: ... \n");
128   if (!ibm_test(rank, size))
129     printf("\t[%d] failed.\n", rank);
130   else
131     printf("\t[%d] ok.\n", rank);
132
133   MPI_Finalize();
134   return 0;
135 }