|
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <mpi.h> |
4 | 4 |
|
5 | | -typedef struct { int myIntA; |
6 | | - int myIntB; |
7 | | - short myShort; |
8 | | - double myDouble; |
9 | | - char myChar; |
10 | | - float myFloat; } myStruct; |
| 5 | +typedef struct { int myIntA; // an integer (usually 4 bytes) |
| 6 | + int myIntB; // another integer (usually 4 bytes) |
| 7 | + short myShort; // a short integer (usually 2 bytes) |
| 8 | + double myDouble; // a double-precision floating point number (usually 8 bytes) |
| 9 | + char myChar; // a char = 1 single byte (usually 1 byte) |
| 10 | + float myFloat; // a single-precision floating point number (usually 4 bytes) |
| 11 | + } myStruct; // name of new data type = myStruct |
| 12 | + |
11 | 13 |
|
12 | 14 | int main(int argc, char *argv[]) { |
13 | 15 | int size, rank, cur, i, total, steps; |
14 | 16 | myStruct data, *send; |
15 | 17 | MPI_Datatype subtypes[6]; //the data types of the elements in myStruct |
16 | 18 | int subblocks[6]; //how often they occur in a row |
17 | 19 | MPI_Aint suboffsets[6]; //their address displacements |
18 | | - MPI_Datatype myStructType; |
| 20 | + MPI_Datatype myStructType; // the new data type |
19 | 21 |
|
20 | | - //initializing communication |
21 | | - MPI_Init(&argc, &argv); |
22 | | - MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 22 | + MPI_Init(&argc, &argv); // initialize MPI |
| 23 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank/ID |
23 | 24 |
|
24 | 25 | // create a new datatype description for the MPI job structure |
25 | | - i = 0; |
26 | | - subtypes[i] = MPI_INT; //myIntA + myIntB |
| 26 | + i = 0; // index of first record is 0 |
| 27 | + subtypes[i] = MPI_INT; //myIntA and myIntB |
27 | 28 | subblocks[i] = 2; //because there are 2 ints |
28 | | - suboffsets[i++] = (((MPI_Aint)(&data.myIntA)) - ((MPI_Aint)(&data))); |
| 29 | + suboffsets[i] = (((MPI_Aint)(&data.myIntA)) - ((MPI_Aint)(&data))); //offset from start |
29 | 30 |
|
30 | | - subtypes[i] = MPI_SHORT; //myShort |
31 | | - subblocks[i] = 1; |
32 | | - suboffsets[i++] = (((MPI_Aint)(&data.myShort)) - ((MPI_Aint)(&data))); |
| 31 | + subtypes[++i] = MPI_SHORT; //second record (++i): myShort |
| 32 | + subblocks[i] = 1; // there is one short |
| 33 | + suboffsets[i] = (((MPI_Aint)(&data.myShort)) - ((MPI_Aint)(&data))); //offset from start |
33 | 34 |
|
34 | | - subtypes[i] = MPI_DOUBLE; //myDouble |
35 | | - subblocks[i] = 1; |
36 | | - suboffsets[i++] = (((MPI_Aint)(&data.myDouble)) - ((MPI_Aint)(&data))); |
| 35 | + subtypes[++i] = MPI_DOUBLE; //third record (++i): myDouble |
| 36 | + subblocks[i] = 1; // there is one double |
| 37 | + suboffsets[i] = (((MPI_Aint)(&data.myDouble)) - ((MPI_Aint)(&data))); //offset from start |
37 | 38 |
|
38 | | - subtypes[i] = MPI_CHAR; //myChar |
39 | | - subblocks[i] = 1; |
40 | | - suboffsets[i++] = (((MPI_Aint)(&data.myChar)) - ((MPI_Aint)(&data))); |
| 39 | + subtypes[++i] = MPI_CHAR; //fourth record (++i): myChar |
| 40 | + subblocks[i] = 1; // one char |
| 41 | + suboffsets[i] = (((MPI_Aint)(&data.myChar)) - ((MPI_Aint)(&data))); //offset from start |
41 | 42 |
|
42 | | - subtypes[i] = MPI_FLOAT; //myFloat |
43 | | - subblocks[i] = 1; |
44 | | - suboffsets[i++] = (((MPI_Aint)(&data.myFloat)) - ((MPI_Aint)(&data))); |
| 43 | + subtypes[++i] = MPI_FLOAT; //fifth record (++i): myFloat |
| 44 | + subblocks[i] = 1; // there is one float |
| 45 | + suboffsets[i] = (((MPI_Aint)(&data.myFloat)) - ((MPI_Aint)(&data))); //offset from start |
45 | 46 |
|
| 47 | + // register data type structure |
46 | 48 | MPI_Type_create_struct(i, subblocks, suboffsets, subtypes, &myStructType); |
47 | | - MPI_Type_commit(&myStructType); |
| 49 | + MPI_Type_commit(&myStructType); // and commit it: it can now be used |
48 | 50 |
|
49 | | - if(rank == 0) { |
50 | | - MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 51 | + if(rank == 0) { // if we are root |
| 52 | + MPI_Comm_size(MPI_COMM_WORLD, &size); // get number of processes |
51 | 53 |
|
52 | | - send = (myStruct*)malloc(sizeof(myStruct) * size); |
| 54 | + send = (myStruct*)malloc(sizeof(myStruct) * size); // allocate memory |
53 | 55 | for(i = size; (--i) >= 0; ) { |
54 | 56 | send[i].myIntA = rank; |
55 | 57 | send[i].myIntB = i; |
|
0 commit comments