source: CIVL/examples/mpi/collective/alltoallv.c@ 12ec1a6

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 12ec1a6 was 6e48678, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

added more examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2114 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.3 KB
Line 
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/*
9This program tests MPI_Alltoallv by having processor i send different
10amounts of data to each processor.
11The first test sends i items to processor i from all processors.
12*/
13int 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, err;
20
21 MPI_Init( &argc, &argv );
22 err = 0;
23 comm = MPI_COMM_WORLD;
24 /* Create the buffer */
25 MPI_Comm_size( comm, &size );
26 MPI_Comm_rank( comm, &rank );
27 sbuf = (int *)malloc( size * size * sizeof(int) );
28 rbuf = (int *)malloc( size * size * sizeof(int) );
29 if (!sbuf || !rbuf) {
30 fprintf( stderr, "Could not allocated buffers!\n" );
31 MPI_Abort( comm, 1 );
32 }
33 /* Load up the buffers */
34 for (i=0; i<size*size; i++) {
35 sbuf[i] = i + 100*rank;
36 rbuf[i] = -i;
37 }
38 /* Create and load the arguments to alltoallv */
39 sendcounts = (int *)malloc( size * sizeof(int) );
40 recvcounts = (int *)malloc( size * sizeof(int) );
41 rdispls = (int *)malloc( size * sizeof(int) );
42 sdispls = (int *)malloc( size * sizeof(int) );
43 if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
44 fprintf( stderr, "Could not allocate arg items!\n" );fflush(stderr);
45 MPI_Abort( comm, 1 );
46 }
47 for (i=0; i<size; i++) {
48 sendcounts[i] = i;
49 recvcounts[i] = rank;
50 rdispls[i] = i * rank;
51 sdispls[i] = (i * (i+1))/2;
52 }
53 MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
54 rbuf, recvcounts, rdispls, MPI_INT, comm );
55 /* Check rbuf */
56 for (i=0; i<size; i++) {
57 p = rbuf + rdispls[i];
58 for (j=0; j<rank; j++) {
59 if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
60 fprintf( stderr, "[%d] got %d expected %d for %dth\n",
61 rank, p[j],(i*(i+1))/2 + j, j );
62 fflush(stderr);
63 err++;
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}
Note: See TracBrowser for help on using the repository browser.