source: CIVL/examples/mpi/collective/c_ex08.c@ e34d3e8

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

added more mpi examples

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

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <mpi.h>
4/*
5! This program shows how to use MPI_Gatherv. Each processor sends a
6! different amount of data to the root processor. We use MPI_Gather
7! first to tell the root how much data is going to be sent.
8*/
9/* globals */
10int numnodes,myid,mpi_err;
11#define mpi_root 0
12/* end of globals */
13
14void init_it(int *argc, char ***argv);
15
16void init_it(int *argc, char ***argv) {
17 mpi_err = MPI_Init(argc,argv);
18 mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
19 mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
20}
21
22int main(int argc,char *argv[]){
23/* poe a.out -procs 3 -rmpool 1 */
24 int *will_use;
25 int *myray,*displacements,*counts,*allray;
26 int size,mysize,i;
27
28 init_it(&argc,&argv);
29 mysize=myid+1;
30 myray=(int*)malloc(mysize*sizeof(int));
31 for(i=0;i<mysize;i++)
32 myray[i]=myid+1;
33/* counts and displacement arrays are only required on the root */
34 if(myid == mpi_root){
35 counts=(int*)malloc(numnodes*sizeof(int));
36 displacements=(int*)malloc(numnodes*sizeof(int));
37 }
38/* we gather the counts to the root */
39 mpi_err = MPI_Gather((void*)myray,1,MPI_INT,
40 (void*)counts, 1,MPI_INT,
41 mpi_root,MPI_COMM_WORLD);
42/* calculate displacements and the size of the recv array */
43 if(myid == mpi_root){
44 displacements[0]=0;
45 for( i=1;i<numnodes;i++){
46 displacements[i]=counts[i-1]+displacements[i-1];
47 }
48 size=0;
49 for(i=0;i< numnodes;i++)
50 size=size+counts[i];
51 allray=(int*)malloc(size*sizeof(int));
52 }
53/* different amounts of data from each processor */
54/* is gathered to the root */
55 mpi_err = MPI_Gatherv(myray, mysize, MPI_INT,
56 allray,counts,displacements,MPI_INT,
57 mpi_root,
58 MPI_COMM_WORLD);
59
60 if(myid == mpi_root){
61 for(i=0;i<size;i++)
62 printf("%d ",allray[i]);
63 printf("\n");
64 }
65 mpi_err = MPI_Finalize();
66}
Note: See TracBrowser for help on using the repository browser.