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