| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <mpi.h>
|
|---|
| 4 |
|
|---|
| 5 | /*
|
|---|
| 6 | ! This program shows how to use MPI_Alltoall. Each processor
|
|---|
| 7 | ! send/rec a different random number to/from other processors.
|
|---|
| 8 | */
|
|---|
| 9 | /* globals */
|
|---|
| 10 | int numnodes,myid,mpi_err;
|
|---|
| 11 | #define mpi_root 0
|
|---|
| 12 | /* end module */
|
|---|
| 13 |
|
|---|
| 14 | void init_it(int *argc, char ***argv);
|
|---|
| 15 | void seed_random(int id);
|
|---|
| 16 | void random_number(float *z);
|
|---|
| 17 |
|
|---|
| 18 | void 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 |
|
|---|
| 24 | int main(int argc,char *argv[]){
|
|---|
| 25 | int *sray,*rray;
|
|---|
| 26 | int *sdisp,*scounts,*rdisp,*rcounts;
|
|---|
| 27 | int ssize,rsize,i,k,j;
|
|---|
| 28 | float z;
|
|---|
| 29 |
|
|---|
| 30 | init_it(&argc,&argv);
|
|---|
| 31 | scounts=(int*)malloc(sizeof(int)*numnodes);
|
|---|
| 32 | rcounts=(int*)malloc(sizeof(int)*numnodes);
|
|---|
| 33 | sdisp=(int*)malloc(sizeof(int)*numnodes);
|
|---|
| 34 | rdisp=(int*)malloc(sizeof(int)*numnodes);
|
|---|
| 35 | /*
|
|---|
| 36 | ! seed the random number generator with a
|
|---|
| 37 | ! different number on each processor
|
|---|
| 38 | */
|
|---|
| 39 | seed_random(myid);
|
|---|
| 40 | /* find data to send */
|
|---|
| 41 | for(i=0;i<numnodes;i++){
|
|---|
| 42 | random_number(&z);
|
|---|
| 43 | scounts[i]=(int)(10.0*z)+1;
|
|---|
| 44 | }
|
|---|
| 45 | printf("myid= %d scounts=",myid);
|
|---|
| 46 | for(i=0;i<numnodes;i++)
|
|---|
| 47 | printf("%d ",scounts[i]);
|
|---|
| 48 | printf("\n");
|
|---|
| 49 | /* send the data */
|
|---|
| 50 | mpi_err = MPI_Alltoall( scounts,1,MPI_INT,
|
|---|
| 51 | rcounts,1,MPI_INT,
|
|---|
| 52 | MPI_COMM_WORLD);
|
|---|
| 53 | printf("myid= %d rcounts=",myid);
|
|---|
| 54 | for(i=0;i<numnodes;i++)
|
|---|
| 55 | printf("%d ",rcounts[i]);
|
|---|
| 56 | printf("\n");
|
|---|
| 57 | #ifdef _CIVL
|
|---|
| 58 | free(scounts);
|
|---|
| 59 | free(rcounts);
|
|---|
| 60 | free(sdisp);
|
|---|
| 61 | free(rdisp);
|
|---|
| 62 | #endif
|
|---|
| 63 | mpi_err = MPI_Finalize();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void seed_random(int id){
|
|---|
| 67 | srand((unsigned int)id);
|
|---|
| 68 | }
|
|---|
| 69 | void random_number(float *z){
|
|---|
| 70 | int i;
|
|---|
| 71 | i=rand();
|
|---|
| 72 | *z=(float)i/RAND_MAX;
|
|---|
| 73 | }
|
|---|