| 1 | /*
|
|---|
| 2 | Shows how to use MPI_Type_vector to send noncontiguous blocks of data
|
|---|
| 3 | and MPI_Get_count and MPI_Get_elements to see the number of elements sent
|
|---|
| 4 | */
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <mpi.h>
|
|---|
| 8 | #include <math.h>
|
|---|
| 9 | int main(argc,argv)
|
|---|
| 10 | int argc;
|
|---|
| 11 | char *argv[];
|
|---|
| 12 | {
|
|---|
| 13 | int myid, numprocs,mpi_err;
|
|---|
| 14 | #define SIZE 25
|
|---|
| 15 | double svect[SIZE],rvect[SIZE];
|
|---|
| 16 | int i,bonk1,bonk2,numx,stride,extent;
|
|---|
| 17 | MPI_Datatype MPI_LEFT_RITE;
|
|---|
| 18 | MPI_Status status;
|
|---|
| 19 |
|
|---|
| 20 | MPI_Init(&argc,&argv);
|
|---|
| 21 | MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
|
|---|
| 22 | MPI_Comm_rank(MPI_COMM_WORLD,&myid);
|
|---|
| 23 |
|
|---|
| 24 | stride=5;
|
|---|
| 25 | numx=(SIZE+1)/stride;
|
|---|
| 26 | extent=1;
|
|---|
| 27 | if(myid == 1){
|
|---|
| 28 | printf("numx=%d extent=%d stride=%d\n",numx,extent,stride);
|
|---|
| 29 | }
|
|---|
| 30 | mpi_err=MPI_Type_vector(numx,extent,stride,MPI_DOUBLE,&MPI_LEFT_RITE);
|
|---|
| 31 | mpi_err=MPI_Type_commit(&MPI_LEFT_RITE);
|
|---|
| 32 | if(myid == 0){
|
|---|
| 33 | for (i=0;i<SIZE;i++)
|
|---|
| 34 | svect[i]=i;
|
|---|
| 35 | MPI_Send(svect,1,MPI_LEFT_RITE,1,100,MPI_COMM_WORLD);
|
|---|
| 36 | }
|
|---|
| 37 | if(myid == 1){
|
|---|
| 38 | for (i=0;i<SIZE;i++)
|
|---|
| 39 | rvect[i]=-1;
|
|---|
| 40 | MPI_Recv(rvect,1,MPI_LEFT_RITE,0,100,MPI_COMM_WORLD,&status);
|
|---|
| 41 | }
|
|---|
| 42 | if(myid == 1){
|
|---|
| 43 | MPI_Get_count(&status,MPI_LEFT_RITE,&bonk1);
|
|---|
| 44 | MPI_Get_elements(&status,MPI_DOUBLE,&bonk2);
|
|---|
| 45 | printf("got %d elements of type MY_TYPE\n",bonk1);
|
|---|
| 46 | printf("which contained %d elements of type MPI_DOUBLE\n",bonk2);
|
|---|
| 47 | for (i=0;i<SIZE;i++)
|
|---|
| 48 | if(rvect[i] != -1)printf("%d %g\n",i,rvect[i]);
|
|---|
| 49 | }
|
|---|
| 50 | MPI_Finalize();
|
|---|
| 51 | }
|
|---|
| 52 | /*
|
|---|
| 53 | output
|
|---|
| 54 | numx=5 extent=5 stride=1
|
|---|
| 55 | got 1 elements of type MY_TYPE
|
|---|
| 56 | which contained 5 elements of type MPI_DOUBLE
|
|---|
| 57 | 0 0
|
|---|
| 58 | 5 5
|
|---|
| 59 | 10 10
|
|---|
| 60 | 15 15
|
|---|
| 61 | 20 20
|
|---|
| 62 | */
|
|---|