Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6d4264890faa43d02687b3b33601b07972764964
[simgrid.git] / teshsuite / smpi / coll-allreduce / coll-allreduce.c
1 /* Copyright (c) 2009-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 <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include "mpi.h"
12
13 int main(int argc, char *argv[])
14 {
15   int rank;
16   int size;
17   int i;
18   int status;
19   int mult=1;
20
21   MPI_Init(&argc, &argv);
22   int maxlen = argc >= 2 ? atoi(argv[1]) : 1;
23
24   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25   MPI_Comm_size(MPI_COMM_WORLD, &size);
26   if (maxlen > 1)
27     mult = maxlen > size ? size : maxlen;
28   int* sb = xbt_new0(int, size * maxlen);
29   int* rb = xbt_new0(int, size * maxlen);
30
31   for (i = 0; i < size *maxlen; ++i) {
32     sb[i] = rank*size + i;
33     rb[i] = 0;
34   }
35
36   status = MPI_Allreduce(NULL, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
37   if(status!=MPI_ERR_BUFFER)
38     printf("MPI_Allreduce did not return MPI_ERR_BUFFER for empty sendbuf\n");
39   status = MPI_Allreduce(sb, NULL, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
40   if(status!=MPI_ERR_BUFFER)
41     printf("MPI_Allreduce did not return MPI_ERR_BUFFER for empty recvbuf\n");
42   status = MPI_Allreduce(sb, rb, -1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, MPI_COMM_WORLD);
43   if(status!=MPI_ERR_COUNT)
44     printf("MPI_Allreduce did not return MPI_ERR_COUNT for -1 count\n");
45   status = MPI_Allreduce(sb, rb, size, MPI_DATATYPE_NULL, MPI_SUM, MPI_COMM_WORLD);
46   if(status!=MPI_ERR_TYPE)
47     printf("MPI_Allreduce did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL type\n");
48   status = MPI_Allreduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_OP_NULL, MPI_COMM_WORLD);
49   if(status!=MPI_ERR_OP)
50     printf("MPI_Allreduce did not return MPI_ERR_COMM for MPI_OP_NULL op\n");
51   status = MPI_Allreduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, MPI_COMM_NULL);
52   if(status!=MPI_ERR_COMM)
53     printf("MPI_Allreduce did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
54
55   printf("[%d] sndbuf=[", rank);
56   for (i = 0; i < size *mult; i++)
57     printf("%d ", sb[i]);
58   printf("]\n");
59   status = MPI_Allreduce(sb, rb, size *maxlen, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
60
61   printf("[%d] rcvbuf=[", rank);
62   for (i =  0; i < size *mult; i++)//do not print everything
63     printf("%d ", rb[i]);
64   printf("]\n");
65
66   if (rank == 0 && status != MPI_SUCCESS) {
67     printf("all_to_all returned %d\n", status);
68     fflush(stdout);
69   }
70   xbt_free(sb);
71   xbt_free(rb);
72   MPI_Finalize();
73   return (EXIT_SUCCESS);
74 }