Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6bec6714a468dd7010e93b8e77404fa14c30992a
[simgrid.git] / teshsuite / smpi / coll-reduce / coll-reduce.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
20   MPI_Init(&argc, &argv);
21   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22   MPI_Comm_size(MPI_COMM_WORLD, &size);
23
24   unsigned long long* sb = (unsigned long long *) xbt_malloc(size * sizeof(unsigned long long));
25   unsigned long long* rb = (unsigned long long *) xbt_malloc(size * sizeof(unsigned long long));
26
27   for (i = 0; i < size; ++i) {
28     sb[i] = rank*size + i;
29     rb[i] = 0;
30   }
31   printf("[%d] sndbuf=[", rank);
32   for (i = 0; i < size; i++)
33     printf("%llu ", sb[i]);
34   printf("]\n");
35   int root=0;
36
37   status = MPI_Reduce(NULL, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, root, MPI_COMM_WORLD);
38   if(status!=MPI_ERR_BUFFER)
39     printf("MPI_Reduce did not return MPI_ERR_BUFFER for empty sendbuf\n");
40   status = MPI_Reduce(sb, rb, -1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, root, MPI_COMM_WORLD);
41   if(status!=MPI_ERR_COUNT)
42     printf("MPI_Reduce did not return MPI_ERR_COUNT for -1 count\n");
43   status = MPI_Reduce(sb, rb, size, MPI_DATATYPE_NULL, MPI_SUM, root, MPI_COMM_WORLD);
44   if(status!=MPI_ERR_TYPE)
45     printf("MPI_Reduce did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL type\n");
46   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_OP_NULL, root, MPI_COMM_WORLD);
47   if(status!=MPI_ERR_OP)
48     printf("MPI_Reduce did not return MPI_ERR_COMM for MPI_OP_NULL op\n");
49   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, -1, MPI_COMM_WORLD);
50   if(status!=MPI_ERR_ROOT)
51     printf("MPI_Reduce did not return MPI_ERR_ROOT for root -1\n");
52   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, size+1, MPI_COMM_WORLD);
53   if(status!=MPI_ERR_ROOT)
54     printf("MPI_Reduce did not return MPI_ERR_ROOT for root > size\n");
55   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, root, MPI_COMM_NULL);
56   if(status!=MPI_ERR_COMM)
57     printf("MPI_Reduce did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
58
59   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, root, MPI_COMM_WORLD);
60   MPI_Barrier(MPI_COMM_WORLD);
61
62   if (rank == root) {
63     printf("[%d] rcvbuf=[", rank);
64     for (i = 0; i < size; i++)
65       printf("%llu ", rb[i]);
66     printf("]\n");
67     if (status != MPI_SUCCESS) {
68       printf("all_to_all returned %d\n", status);
69       fflush(stdout);
70     }
71   }
72
73   printf("[%d] second sndbuf=[", rank);
74   for (i = 0; i < 1; i++)
75     printf("%llu ", sb[i]);
76   printf("]\n");
77
78   root=size-1;
79   status = MPI_Reduce(sb, rb, 1, MPI_UNSIGNED_LONG_LONG, MPI_PROD, root, MPI_COMM_WORLD);
80   MPI_Barrier(MPI_COMM_WORLD);
81
82   if (rank == root) {
83     printf("[%d] rcvbuf=[", rank);
84     for (i = 0; i < 1; i++)
85       printf("%llu ", rb[i]);
86     printf("]\n");
87     if (status != MPI_SUCCESS) {
88       printf("all_to_all returned %d\n", status);
89       fflush(stdout);
90     }
91   }
92   free(sb);
93   free(rb);
94   MPI_Finalize();
95   return (EXIT_SUCCESS);
96 }