source: CIVL/examples/mpi/routines/Gather_Scatter/mpi_gatherv.c@ dccd621

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since dccd621 was 0baeebd, checked in by Ziqing Luo <ziqing@…>, 12 years ago

cleaned up examples

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

  • Property mode set to 100644
File size: 1.0 KB
Line 
1#include <mpi.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <assert.h>
5#define SIZE 4
6
7#ifdef _CIVL
8$input int _NPROCS=4;
9#endif
10
11int main (int argc, char *argv[])
12{
13 int numtasks, rank, source;
14 int sendcounts[4] = {2, 4, 5, 5};
15 int recvcounts[4] = {2, 4, 5, 5};
16 int displs[4] = {0, 2, 6, 11};
17 float recvbuf[SIZE * SIZE];
18 float * sendbuf;
19
20 MPI_Init(&argc,&argv);
21 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22 MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
23
24 sendbuf = malloc(sizeof(float) * sendcounts[rank]);
25 if (numtasks == SIZE) {
26 source = 1;
27 //init sendbuf
28 for(int i=0; i<sendcounts[rank]; i++)
29 sendbuf[i] = (float)(displs[rank] + i);
30 MPI_Gatherv(sendbuf,sendcounts[rank],MPI_FLOAT,recvbuf, recvcounts, displs,
31 MPI_FLOAT,source,MPI_COMM_WORLD);
32 //assertions
33 if(rank == source){
34 for(int i=0; i<(SIZE*SIZE); i++){
35 printf("recvbuf[%d] : %f\n", i, recvbuf[i]);
36 assert(recvbuf[i] == i);
37 }
38 }
39 }
40 else
41 printf("Must specify %d processors. Terminating.\n",SIZE);
42
43 MPI_Finalize();
44 free(sendbuf);
45}
Note: See TracBrowser for help on using the repository browser.