Logo AND Algorithmique Numérique Distribuée

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