Logo AND Algorithmique Numérique Distribuée

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