source: CIVL/examples/mpi/collective/c_ex13.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.8 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 */
13void init_it(int *argc, char ***argv);
14
15void init_it(int *argc, char ***argv) {
16 mpi_err = MPI_Init(argc,argv);
17 mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
18 mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
19}
20
21int main(int argc,char *argv[]){
22/* poe a.out -procs 3 -rmpool 1 */
23 int *sray,*displacements,*counts,*allray;
24 int size,mysize,i;
25
26 init_it(&argc,&argv);
27 mysize=myid+1;
28/* counts and displacement arrays are only required on the root */
29 if(myid == mpi_root){
30 counts=(int*)malloc(numnodes*sizeof(int));
31 displacements=(int*)malloc(numnodes*sizeof(int));
32 }
33/* we gather the counts to the root */
34 mpi_err = MPI_Gather((void*)mysize,1,MPI_INT,
35 (void*)counts, 1,MPI_INT,
36 mpi_root,MPI_COMM_WORLD);
37/* calculate displacements and the size of the recv array */
38 if(myid == mpi_root){
39 displacements[0]=0;
40 for( i=1;i<numnodes;i++){
41 displacements[i]=counts[i-1]+displacements[i-1];
42 }
43 size=0;
44 for(i=0;i< numnodes;i++)
45 size=size+counts[i];
46 sray=(int*)malloc(size*sizeof(int));
47 for(i=0;i<size;i++)
48 sray[i]=i+1;
49 }
50/* different amounts of data for each processor */
51/* is scattered from the root */
52 allray=(int*)malloc(sizeof(int)*mysize);
53 mpi_err = MPI_Scatterv(sray,counts,displacements,MPI_INT,
54 allray, mysize, MPI_INT,
55 mpi_root,
56 MPI_COMM_WORLD);
57
58 for(i=0;i<mysize;i++)
59 printf("%d ",allray[i]);
60 printf("\n");
61
62 mpi_err = MPI_Finalize();
63}
Note: See TracBrowser for help on using the repository browser.