Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use smpiff to compile NAS written in fortran.
[simgrid.git] / examples / smpi / alltoall_basic.c
1 /* Copyright (c) 2009, 2010. 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 "mpi.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <errno.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   }
36   rb = (int *) malloc(size * sizeof(int));
37   if (!rb) {
38     perror("can't allocate recv buffer");
39     fflush(stderr);
40     free(sb);
41     MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
42   }
43   for (i = 0; i < size; ++i) {
44     sb[i] = rank + 1;
45     rb[i] = 0;
46   }
47   status = MPI_Alltoall(sb, 1, MPI_INT, rb, 1, MPI_INT, MPI_COMM_WORLD);
48
49   printf("[%d] rcvbuf=[", rank);
50   for (i = 0; i < size; i++)
51     printf("%d ", rb[i]);
52   printf("]\n");
53
54
55   if (rank == 0) {
56     if (status != 0) {
57       printf("all_to_all returned %d\n", status);
58       fflush(stdout);
59     }
60   }
61   free(sb);
62   free(rb);
63   MPI_Finalize();
64   return (EXIT_SUCCESS);
65 }