| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <mpi.h>
|
|---|
| 4 | #ifdef _CIVL
|
|---|
| 5 | #include<civlc.cvh>
|
|---|
| 6 | #endif
|
|---|
| 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 */
|
|---|
| 13 | int numnodes,myid,mpi_err;
|
|---|
| 14 | #define mpi_root 0
|
|---|
| 15 | /* end of globals */
|
|---|
| 16 |
|
|---|
| 17 | void init_it(int *argc, char ***argv);
|
|---|
| 18 |
|
|---|
| 19 | void 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 |
|
|---|
| 25 | int main(int argc,char *argv[]){
|
|---|
| 26 | /* poe a.out -procs 3 -rmpool 1 */
|
|---|
| 27 | int *will_use;
|
|---|
| 28 | int *myray,*displacements=NULL,*counts=NULL,*allray=NULL;
|
|---|
| 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");
|
|---|
| 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
|
|---|
| 76 | }
|
|---|
| 77 | #ifdef _CIVL
|
|---|
| 78 | free(myray);
|
|---|
| 79 | free(displacements);
|
|---|
| 80 | free(counts);
|
|---|
| 81 | free(allray);
|
|---|
| 82 | #endif
|
|---|
| 83 | mpi_err = MPI_Finalize();
|
|---|
| 84 | }
|
|---|