Logo AND Algorithmique Numérique Distribuée

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