Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: complete workaround in the error msg seen on modern systems
[simgrid.git] / teshsuite / smpi / coll-scatter / coll-scatter.c
1 /* Copyright (c) 2012-2019. 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 #include <stdio.h>
8 #include <mpi.h>
9
10 int main(int argc, char **argv)
11 {
12   int size;
13   int rank;
14   int success = 1;
15   int retval;
16   int sendcount = 1;            // one double to each process
17   int recvcount = 1;
18   double *sndbuf = NULL;
19   double rcvd;
20   int root = 0;                 // arbitrary choice
21
22   MPI_Init(&argc, &argv);
23   MPI_Comm_size(MPI_COMM_WORLD, &size);
24   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25
26   // on root, initialize sendbuf
27   if (root == rank) {
28     sndbuf = malloc(size * sizeof(double));
29     for (int i = 0; i < size; i++) {
30       sndbuf[i] = (double) i;
31     }
32   }
33
34   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, NULL, recvcount, MPI_DOUBLE, root, MPI_COMM_WORLD);
35   if(retval!=MPI_ERR_BUFFER)
36     printf("MPI_Scatter did not return MPI_ERR_BUFFER for empty recvbuf\n");
37   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, -1, MPI_DOUBLE, root, MPI_COMM_WORLD);
38   if(retval!=MPI_ERR_COUNT)
39     printf("MPI_Scatter did not return MPI_ERR_COUNT for -1 recvcount\n");
40   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DATATYPE_NULL, root, MPI_COMM_WORLD);
41   if(retval!=MPI_ERR_TYPE)
42     printf("MPI_Scatter did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL sendtype\n");
43   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, -1, MPI_COMM_WORLD);
44   if(retval!=MPI_ERR_ROOT)
45     printf("MPI_Scatter did not return MPI_ERR_ROOT for root -1\n");
46   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, size+1, MPI_COMM_WORLD);
47   if(retval!=MPI_ERR_ROOT)
48     printf("MPI_Scatter did not return MPI_ERR_ROOT for root > size\n");
49   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, root, MPI_COMM_NULL);
50   if(retval!=MPI_ERR_COMM)
51     printf("MPI_Scatter did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
52
53   retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, root, MPI_COMM_WORLD);
54   if (root == rank) {
55     free(sndbuf);
56   }
57   if (retval != MPI_SUCCESS) {
58     fprintf(stderr, "(%s:%d) MPI_Scatter() returned retval=%d\n", __FILE__, __LINE__, retval);
59     return 0;
60   }
61   // verification
62   if ((double) rank != rcvd) {
63     fprintf(stderr, "[%d] has %f instead of %d\n", rank, rcvd, rank);
64     success = 0;
65   }
66   /* test 1 */
67   if (0 == rank)
68     printf("** Small Test Result: ...\n");
69   if (!success)
70     printf("\t[%d] failed.\n", rank);
71   else
72     printf("\t[%d] ok.\n", rank);
73
74   MPI_Finalize();
75   return 0;
76 }