| 1 | /************************************************************************
|
|---|
| 2 |
|
|---|
| 3 | A simple MPI example program that uses MPI_Type_struct to build a
|
|---|
| 4 | derived datatype.
|
|---|
| 5 |
|
|---|
| 6 | The program should be run with two processes. It first builds a
|
|---|
| 7 | derived datatype consisting of a struct with three members:
|
|---|
| 8 | two floating-point values and one integer. Process zero reads in
|
|---|
| 9 | three input values (two floats and one int) from standard input,
|
|---|
| 10 | places these in a struct and sends all three values in a message
|
|---|
| 11 | to process one. This receives the message and sends it back to
|
|---|
| 12 | process zero, which prints out the values.
|
|---|
| 13 |
|
|---|
| 14 | Compile the program with 'mpicc -O2 datatype.c -o datatype'
|
|---|
| 15 | Run the program with 2 proceses.
|
|---|
| 16 |
|
|---|
| 17 | ************************************************************************/
|
|---|
| 18 |
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include <mpi.h>
|
|---|
| 22 | int main(int argc, char *argv[]) {
|
|---|
| 23 | const int tag = 42; /* Message tag */
|
|---|
| 24 | int id, ntasks, err;
|
|---|
| 25 | MPI_Status status;
|
|---|
| 26 |
|
|---|
| 27 | MPI_Datatype my_type; /* This is the new MPI datatype that we will build */
|
|---|
| 28 |
|
|---|
| 29 | typedef struct { /* Data structure for input data */
|
|---|
| 30 | float a;
|
|---|
| 31 | float b;
|
|---|
| 32 | int n;
|
|---|
| 33 | } Indata_type;
|
|---|
| 34 |
|
|---|
| 35 | Indata_type indata, recdata; /* Variables for input data */
|
|---|
| 36 |
|
|---|
| 37 | int lengtharray[3]; /* Array of lengths */
|
|---|
| 38 | MPI_Aint disparray[3]; /* Array of displacements */
|
|---|
| 39 | MPI_Datatype typearray[3]; /* Array of MPI datatypes */
|
|---|
| 40 |
|
|---|
| 41 | MPI_Aint startaddress, address; /* Variables used to calculate displacements */
|
|---|
| 42 |
|
|---|
| 43 | err = MPI_Init(&argc, &argv); /* Initialize MPI */
|
|---|
| 44 | if (err != MPI_SUCCESS) {
|
|---|
| 45 | printf("MPI initialization failed!\n");
|
|---|
| 46 | exit(1);
|
|---|
| 47 | }
|
|---|
| 48 | MPI_Comm_size(MPI_COMM_WORLD, &ntasks); /* Get nr of tasks */
|
|---|
| 49 | MPI_Comm_rank(MPI_COMM_WORLD, &id); /* Get id of this process */
|
|---|
| 50 |
|
|---|
| 51 | if (ntasks != 2) {
|
|---|
| 52 | if (id == 0) printf("You have to run this program with 2 processes\n");
|
|---|
| 53 | MPI_Finalize();
|
|---|
| 54 | exit(0);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (id == 0) { /* Process 0 does this */
|
|---|
| 58 | printf("Enter a, b and n: \n");
|
|---|
| 59 | fflush(stdout);
|
|---|
| 60 | scanf("%f %f %d", &indata.a, &indata.b, &indata.n); /* Read input */
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | lengtharray[0] = lengtharray[1] =lengtharray[2] = 1; /* Set array of lengths */
|
|---|
| 64 | typearray[0] = typearray[1] = MPI_FLOAT; /* and data types */
|
|---|
| 65 | typearray[2] = MPI_INT;
|
|---|
| 66 |
|
|---|
| 67 | /* First element, a, is at displacement 0 */
|
|---|
| 68 | disparray[0] = 0;
|
|---|
| 69 |
|
|---|
| 70 | /* Calculate displacement of b */
|
|---|
| 71 | MPI_Get_address(&indata.a, &startaddress);
|
|---|
| 72 | MPI_Get_address(&indata.b, &address);
|
|---|
| 73 | disparray[1] = address-startaddress; /* Displacement of second element, b */
|
|---|
| 74 |
|
|---|
| 75 | MPI_Get_address(&indata.n, &address);
|
|---|
| 76 | disparray[2] = address-startaddress; /* Displacement of third element, n */
|
|---|
| 77 |
|
|---|
| 78 | /* Build the data structure my_type */
|
|---|
| 79 | MPI_Type_struct(3, lengtharray, disparray, typearray, &my_type);
|
|---|
| 80 | MPI_Type_commit(&my_type);
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | if (id==0) { /* Process 0 does this */
|
|---|
| 84 | printf("Sending input data to process 1\n");
|
|---|
| 85 | MPI_Send(&indata, 1, my_type, 1, tag, MPI_COMM_WORLD);
|
|---|
| 86 | MPI_Recv(&recdata, 1, my_type, 1, tag, MPI_COMM_WORLD, &status);
|
|---|
| 87 | printf("Received message with values %3.1f %3.1f and %d from process %d\n", \
|
|---|
| 88 | recdata.a, recdata.b, recdata.n, status.MPI_SOURCE);
|
|---|
| 89 | }
|
|---|
| 90 | else { /* Process 1 does this */
|
|---|
| 91 | MPI_Recv(&indata, 1, my_type, 0, tag, MPI_COMM_WORLD, &status);
|
|---|
| 92 | MPI_Send(&indata, 1, my_type, 0, tag, MPI_COMM_WORLD);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | err = MPI_Finalize(); /* Terminate MPI */
|
|---|
| 96 | exit(0);
|
|---|
| 97 | }
|
|---|