Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Restructure teshsuite simdag
[simgrid.git] / teshsuite / smpi / alltoall_basic.c
1 /* Copyright (c) 2009-2010, 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 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include "mpi.h"
12
13 #ifndef EXIT_SUCCESS
14 #define EXIT_SUCCESS 0
15 #define EXIT_FAILURE 1
16 #endif
17
18 int main(int argc, char *argv[])
19 {
20   int rank, size;
21   int i;
22   int *sb;
23   int *rb;
24   int status;
25
26   MPI_Init(&argc, &argv);
27   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
28   MPI_Comm_size(MPI_COMM_WORLD, &size);
29
30   sb = (int *) malloc(size * sizeof(int));
31   if (!sb) {
32     perror("can't allocate send buffer");
33     fflush(stderr);
34     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
35     exit(EXIT_FAILURE);
36   }
37   rb = (int *) malloc(size * sizeof(int));
38   if (!rb) {
39     perror("can't allocate recv buffer");
40     fflush(stderr);
41     free(sb);
42     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
43     exit(EXIT_FAILURE);
44   }
45   for (i = 0; i < size; ++i) {
46     sb[i] = rank + 1;
47     rb[i] = 0;
48   }
49
50   status = MPI_Alltoall(sb, 1, MPI_INT, rb, 1, MPI_INT, MPI_COMM_WORLD);
51
52   printf("[%d] rcvbuf=[", rank);
53   for (i = 0; i < size; i++)
54     printf("%d ", rb[i]);
55   printf("]\n");
56
57
58   if (rank == 0) {
59     if (status != MPI_SUCCESS) {
60       printf("all_to_all returned %d\n", status);
61       fflush(stdout);
62     }
63   }
64   free(sb);
65   free(rb);
66   MPI_Finalize();
67   return (EXIT_SUCCESS);
68 }