| 1 | /**
|
|---|
| 2 | * This program illustrates MPI_Alltoallv.
|
|---|
| 3 | * online source: http://mpi.deino.net/mpi_functions/MPI_Alltoallv.html
|
|---|
| 4 | */
|
|---|
| 5 | #include "mpi.h"
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | /*
|
|---|
| 9 | This program tests MPI_Alltoallv by having processor i send different
|
|---|
| 10 | amounts of data to each processor.
|
|---|
| 11 | The first test sends i items to processor i from all processors.
|
|---|
| 12 | */
|
|---|
| 13 | int main( int argc, char **argv )
|
|---|
| 14 | {
|
|---|
| 15 | MPI_Comm comm;
|
|---|
| 16 | int *sbuf, *rbuf;
|
|---|
| 17 | int rank, size;
|
|---|
| 18 | int *sendcounts, *recvcounts, *rdispls, *sdispls;
|
|---|
| 19 | int i, j, *p;
|
|---|
| 20 |
|
|---|
| 21 | MPI_Init( &argc, &argv );
|
|---|
| 22 | comm = MPI_COMM_WORLD;
|
|---|
| 23 | /* Create the buffer */
|
|---|
| 24 | MPI_Comm_size(comm, &size );
|
|---|
| 25 | MPI_Comm_rank(comm, &rank );
|
|---|
| 26 | sbuf = (int *)malloc( size * size * sizeof(int) );
|
|---|
| 27 | rbuf = (int *)malloc( size * size * sizeof(int) );
|
|---|
| 28 | if (!sbuf || !rbuf) {
|
|---|
| 29 | printf("Could not allocated buffers!\n");
|
|---|
| 30 | MPI_Abort( comm, 1 );
|
|---|
| 31 | }
|
|---|
| 32 | /* Load up the buffers */
|
|---|
| 33 | for (i=0; i<size*size; i++) {
|
|---|
| 34 | sbuf[i] = i + 100*rank;
|
|---|
| 35 | rbuf[i] = -i;
|
|---|
| 36 | }
|
|---|
| 37 | /* Create and load the arguments to alltoallv */
|
|---|
| 38 | sendcounts = (int *)malloc( size * sizeof(int));
|
|---|
| 39 | recvcounts = (int *)malloc( size * sizeof(int));
|
|---|
| 40 | rdispls = (int *)malloc( size * sizeof(int));
|
|---|
| 41 | sdispls = (int *)malloc( size * sizeof(int));
|
|---|
| 42 | if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
|
|---|
| 43 | printf("Could not allocate arg items!\n" );
|
|---|
| 44 | MPI_Abort( comm, 1 );
|
|---|
| 45 | }
|
|---|
| 46 | for (i=0; i<size; i++) {
|
|---|
| 47 | sendcounts[i] = i;
|
|---|
| 48 | recvcounts[i] = rank;
|
|---|
| 49 | rdispls[i] = i * rank;
|
|---|
| 50 | sdispls[i] = (i * (i+1))/2;
|
|---|
| 51 | }
|
|---|
| 52 | MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
|
|---|
| 53 | rbuf, recvcounts, rdispls, MPI_INT, comm );
|
|---|
| 54 | /* Check rbuf */
|
|---|
| 55 | for (i=0; i<size; i++) {
|
|---|
| 56 | p = rbuf + rdispls[i];
|
|---|
| 57 | for (j=0; j<rank; j++) {
|
|---|
| 58 | if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
|
|---|
| 59 | printf("[%d] got %d expected %d for %dth\n",
|
|---|
| 60 | rank, p[j],(i*(i+1))/2 + j, j);
|
|---|
| 61 | }else{
|
|---|
| 62 | printf("[%d] got %d expected %d for %dth\n",
|
|---|
| 63 | rank, p[j],(i*(i+1))/2 + j, j);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | free( sdispls );
|
|---|
| 68 | free( rdispls );
|
|---|
| 69 | free( recvcounts );
|
|---|
| 70 | free( sendcounts );
|
|---|
| 71 | free( rbuf );
|
|---|
| 72 | free( sbuf );
|
|---|
| 73 | MPI_Finalize();
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|