Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some MSG examples mainly useful to test the model-checker
[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" );fflush(stderr);
33         MPI_Abort(MPI_COMM_WORLD,EXIT_FAILURE);
34     }
35     rb = (int *)malloc(size*sizeof(int));
36     if ( !rb ) {
37         perror( "can't allocate recv buffer");fflush(stderr);
38         free(sb);
39         MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
40     }
41     for ( i=0 ; i < size ; ++i ) {
42         sb[i] = rank + 1;
43         rb[i] = 0;
44     }
45     status = MPI_Alltoall(sb, 1, MPI_INT, rb, 1, MPI_INT, MPI_COMM_WORLD);
46
47     printf("[%d] rcvbuf=[",rank);
48     for (i=0;i<size;i++) 
49             printf("%d ",rb[i]);
50     printf("]\n");
51
52
53     if (rank == 0) {
54         if (status != 0) {
55             printf("all_to_all returned %d\n",status);fflush(stdout);
56         }
57     }
58     free(sb);
59     free(rb);
60     MPI_Finalize();
61     return(EXIT_SUCCESS);
62 }