| [6e48678] | 1 | /**
|
|---|
| 2 | * This program illustrates MPI_Alltoall.
|
|---|
| 3 | * online source: http://mpi.deino.net/mpi_functions/MPI_Alltoall.html
|
|---|
| 4 | */
|
|---|
| 5 | #include "mpi.h"
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #include <string.h>
|
|---|
| 9 | #include <errno.h>
|
|---|
| 10 |
|
|---|
| 11 | #ifndef EXIT_SUCCESS
|
|---|
| 12 | #define EXIT_SUCCESS 0
|
|---|
| 13 | #define EXIT_FAILURE 1
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | int main( int argc, char *argv[] )
|
|---|
| 17 | {
|
|---|
| 18 | int rank, size;
|
|---|
| 19 | int chunk = 128;
|
|---|
| 20 | int i;
|
|---|
| 21 | int *sb;
|
|---|
| 22 | int *rb;
|
|---|
| 23 | int status, gstatus;
|
|---|
| 24 |
|
|---|
| 25 | MPI_Init(&argc,&argv);
|
|---|
| 26 | MPI_Comm_rank(MPI_COMM_WORLD,&rank);
|
|---|
| 27 | MPI_Comm_size(MPI_COMM_WORLD,&size);
|
|---|
| 28 | for ( i=1 ; i < argc ; ++i ) {
|
|---|
| 29 | if ( argv[i][0] != '-' )
|
|---|
| 30 | continue;
|
|---|
| 31 | switch(argv[i][1]) {
|
|---|
| 32 | case 'm':
|
|---|
| 33 | chunk = atoi(argv[++i]);
|
|---|
| 34 | break;
|
|---|
| 35 | default:
|
|---|
| 36 | fprintf(stderr, "Unrecognized argument %s\n", argv[i]);fflush(stderr);
|
|---|
| 37 | MPI_Abort(MPI_COMM_WORLD,EXIT_FAILURE);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | sb = (int *)malloc(size*chunk*sizeof(int));
|
|---|
| 41 | if ( !sb ) {
|
|---|
| 42 | perror( "can't allocate send buffer" );fflush(stderr);
|
|---|
| 43 | MPI_Abort(MPI_COMM_WORLD,EXIT_FAILURE);
|
|---|
| 44 | }
|
|---|
| 45 | rb = (int *)malloc(size*chunk*sizeof(int));
|
|---|
| 46 | if ( !rb ) {
|
|---|
| 47 | perror( "can't allocate recv buffer");fflush(stderr);
|
|---|
| 48 | free(sb);
|
|---|
| 49 | MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
|
|---|
| 50 | }
|
|---|
| 51 | for ( i=0 ; i < size*chunk ; ++i ) {
|
|---|
| 52 | sb[i] = rank + 1;
|
|---|
| 53 | rb[i] = 0;
|
|---|
| 54 | }
|
|---|
| 55 | status = MPI_Alltoall(sb, chunk, MPI_INT, rb, chunk, MPI_INT, MPI_COMM_WORLD);
|
|---|
| 56 | MPI_Allreduce( &status, &gstatus, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
|
|---|
| 57 | if (rank == 0) {
|
|---|
| 58 | if (gstatus != 0) {
|
|---|
| 59 | printf("all_to_all returned %d\n",gstatus);fflush(stdout);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | free(sb);
|
|---|
| 63 | free(rb);
|
|---|
| 64 | MPI_Finalize();
|
|---|
| 65 | return(EXIT_SUCCESS);
|
|---|
| 66 | }
|
|---|