source: CIVL/examples/mpi/collective/c_ex08.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.2 KB
RevLine 
[b28f397]1#include <stdio.h>
2#include <stdlib.h>
3#include <mpi.h>
[6e48678]4#ifdef _CIVL
5#include<civlc.cvh>
6#endif
[b28f397]7/*
8! This program shows how to use MPI_Gatherv. Each processor sends a
9! different amount of data to the root processor. We use MPI_Gather
10! first to tell the root how much data is going to be sent.
11*/
12/* globals */
13int numnodes,myid,mpi_err;
14#define mpi_root 0
15/* end of globals */
16
17void init_it(int *argc, char ***argv);
18
19void init_it(int *argc, char ***argv) {
20 mpi_err = MPI_Init(argc,argv);
21 mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &numnodes );
22 mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &myid);
23}
24
25int main(int argc,char *argv[]){
26/* poe a.out -procs 3 -rmpool 1 */
27 int *will_use;
[6e48678]28 int *myray,*displacements=NULL,*counts=NULL,*allray=NULL;
[b28f397]29 int size,mysize,i;
30
31 init_it(&argc,&argv);
32 mysize=myid+1;
33 myray=(int*)malloc(mysize*sizeof(int));
34 for(i=0;i<mysize;i++)
35 myray[i]=myid+1;
36/* counts and displacement arrays are only required on the root */
37 if(myid == mpi_root){
38 counts=(int*)malloc(numnodes*sizeof(int));
39 displacements=(int*)malloc(numnodes*sizeof(int));
40 }
41/* we gather the counts to the root */
42 mpi_err = MPI_Gather((void*)myray,1,MPI_INT,
43 (void*)counts, 1,MPI_INT,
44 mpi_root,MPI_COMM_WORLD);
45/* calculate displacements and the size of the recv array */
46 if(myid == mpi_root){
47 displacements[0]=0;
48 for( i=1;i<numnodes;i++){
49 displacements[i]=counts[i-1]+displacements[i-1];
50 }
51 size=0;
52 for(i=0;i< numnodes;i++)
53 size=size+counts[i];
54 allray=(int*)malloc(size*sizeof(int));
55 }
56/* different amounts of data from each processor */
57/* is gathered to the root */
58 mpi_err = MPI_Gatherv(myray, mysize, MPI_INT,
59 allray,counts,displacements,MPI_INT,
60 mpi_root,
61 MPI_COMM_WORLD);
62
63 if(myid == mpi_root){
64 for(i=0;i<size;i++)
65 printf("%d ",allray[i]);
66 printf("\n");
[6e48678]67#ifdef _CIVL
68 for(int j=0, k=1, t=0; j<size && t<=k; j++,t++){
69 if(t==k){
70 t=0;
71 k++;
72 }
73 $assert(allray[j] == k);
74 }
75#endif
[b28f397]76 }
[4358486]77#ifdef _CIVL
78 free(myray);
79 free(displacements);
80 free(counts);
81 free(allray);
82#endif
[b28f397]83 mpi_err = MPI_Finalize();
84}
Note: See TracBrowser for help on using the repository browser.