Skip to content

Commit b9b70dd

Browse files
author
Thomas Weise
committed
... and a bit more docu and comments
1 parent 1436d78 commit b9b70dd

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

mpi/structScatter.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,56 @@
22
#include <stdlib.h>
33
#include <mpi.h>
44

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+
1113

1214
int main(int argc, char *argv[]) {
1315
int size, rank, cur, i, total, steps;
1416
myStruct data, *send;
1517
MPI_Datatype subtypes[6]; //the data types of the elements in myStruct
1618
int subblocks[6]; //how often they occur in a row
1719
MPI_Aint suboffsets[6]; //their address displacements
18-
MPI_Datatype myStructType;
20+
MPI_Datatype myStructType; // the new data type
1921

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
2324

2425
// 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
2728
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
2930

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
3334

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
3738

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
4142

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
4546

47+
// register data type structure
4648
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
4850

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
5153

52-
send = (myStruct*)malloc(sizeof(myStruct) * size);
54+
send = (myStruct*)malloc(sizeof(myStruct) * size); // allocate memory
5355
for(i = size; (--i) >= 0; ) {
5456
send[i].myIntA = rank;
5557
send[i].myIntB = i;

mpi/structTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdio.h>
1+
#include <stdio.h> // for printf
22

33
typedef struct { int myIntA; // an integer (usually 4 bytes)
44
int myIntB; // another integer (usually 4 bytes)

0 commit comments

Comments
 (0)