source: CIVL/examples/mpi/collective/alltoallv.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[6e48678]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;
[06af073]19 int i, j, *p;
[6e48678]20
21 MPI_Init( &argc, &argv );
22 comm = MPI_COMM_WORLD;
23 /* Create the buffer */
[06af073]24 MPI_Comm_size(comm, &size );
25 MPI_Comm_rank(comm, &rank );
[6e48678]26 sbuf = (int *)malloc( size * size * sizeof(int) );
27 rbuf = (int *)malloc( size * size * sizeof(int) );
28 if (!sbuf || !rbuf) {
[06af073]29 printf("Could not allocated buffers!\n");
[6e48678]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 */
[06af073]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));
[6e48678]42 if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
[06af073]43 printf("Could not allocate arg items!\n" );
[6e48678]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) {
[06af073]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 }
[6e48678]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.