Skip to content

Commit 1436d78

Browse files
author
Thomas Weise
committed
Added More Comments, Fixed Some Issues
structtest should now compile under more scenarios
1 parent 6b4f776 commit 1436d78

File tree

8 files changed

+112
-104
lines changed

8 files changed

+112
-104
lines changed

mpi/broadcast.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
#include <mpi.h>
2-
#include <stdio.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // import for printf
33

44
int main(int argc, char *argv[]) {
5-
char message[60];
6-
int rank, size;
7-
MPI_Status status;
5+
char message[60]; // space allocated for the message
6+
int rank; // variable for process id
87

9-
MPI_Init(&argc, &argv);
10-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
11-
MPI_Comm_size(MPI_COMM_WORLD, &size);
8+
MPI_Init(&argc, &argv); // initialize MPI
9+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank
1210

13-
if (rank == 0) {
11+
if (rank == 0) { // create message if process is "root" (rank = 0)
1412
sprintf(message, "Message from root (rank %d).", rank);
1513
}
1614

15+
// broadcast: send message to all if rank==0, otherwise receive
1716
MPI_Bcast(message, 60, MPI_CHAR, 0, MPI_COMM_WORLD);
1817
printf("The message sent/received at node %d is \"%s\"\n", rank, message);
1918

20-
MPI_Finalize();
19+
MPI_Finalize(); // shutdown MPI
2120
return 0;
2221
}

mpi/deadlock.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
int main(int argc, char **argv) {
66
int rank, size, prev, next;
77
MPI_Status status;
8-
char message[20];
8+
char messageIn[20], messageOut[20];
99

10-
MPI_Init(&argc, &argv);
11-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
12-
MPI_Comm_size(MPI_COMM_WORLD, &size);
10+
MPI_Init(&argc, &argv); // initialize MPI
11+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank/ID
12+
MPI_Comm_size(MPI_COMM_WORLD, &size); // get total number of processes
1313

14-
prev = ((size + rank - 1) % size);
15-
next = ((rank + 1) % size);
16-
strcpy(message, "Important message!");
14+
prev = ((size + rank - 1) % size); // get rank of process to receive from, wrap at 0
15+
MPI_Recv(messageIn, 20, MPI_CHAR, prev, 0, MPI_COMM_WORLD, &status); // receive msg
16+
printf("Process %d received message %s from process %d.\n", rank, messageIn, prev);
1717

18-
MPI_Recv(message, 20, MPI_CHAR, prev, 0, MPI_COMM_WORLD, &status);
19-
printf("Process %d received message %s from process %d.\n", rank, message, prev);
20-
printf("Process %d is sending message %s to process %d.\n", rank, message, next);
21-
MPI_Send(message, 20, MPI_CHAR, next, 0, MPI_COMM_WORLD);
18+
next = ((rank + 1) % size); // get rank of process to send message to
19+
strcpy(messageOut, "Important message!"); // construct message
20+
printf("Process %d is sending message %s to process %d.\n", rank, messageOut, next);
21+
MPI_Send(messageOut, 20, MPI_CHAR, next, 0, MPI_COMM_WORLD); // send message
2222

23-
MPI_Finalize();
23+
MPI_Finalize(); // shut down MPI
2424
return 0;
2525
}

mpi/gatherScatterBareBones.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
#include <mpi.h>
2-
#include <stdio.h>
3-
#include <math.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // needed for printf
43

5-
#define DATA_SIZE 1024
4+
#define DATA_SIZE 1024 // the data size
65

76
int main(int argc, char *argv[]) {
87
int send[DATA_SIZE], recv[DATA_SIZE];
98
int rank, size, count, root, res;
109
MPI_Status status;
1110

12-
MPI_Init(&argc, &argv);
13-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
14-
MPI_Comm_size(MPI_COMM_WORLD, &size);
11+
MPI_Init(&argc, &argv); // initialize MPI
12+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank/ID
13+
MPI_Comm_size(MPI_COMM_WORLD, &size); // get total number of processes
1514

1615
if(rank == 0) { //If root: Generate data to be distributed.
1716
}
1817

1918
//Send data to all nodes. here: an integer array of length "count".
20-
count = (DATA_SIZE / size);
19+
count = (DATA_SIZE / size); // each receive gets chunk of same size
20+
// scatter: if rank=0, send data (and get own share); otherwise: receive data
2121
MPI_Scatter(send, count, MPI_INT, recv, count, MPI_INT, 0, MPI_COMM_WORLD);
2222

23-
// Each node now processes its share of data and sends the results (here: int "res") to root.
23+
// Each node processes its share of data and sends the result (here: int "res") to root.
2424
MPI_Gather(&res, 1, MPI_INT, recv, 1, MPI_INT, 0, MPI_COMM_WORLD);
2525

2626
if(rank == 0) { //If root: process the received data.
2727
}
2828

29-
MPI_Finalize();
29+
MPI_Finalize(); // shut down MPI
3030
return 0;
3131
}

mpi/gatherScatterPrimes.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
#include <mpi.h>
2-
#include <stdio.h>
3-
#include <math.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // needed for printf
3+
#include <math.h> // needed for sqrt
44

5-
#define DATA_SIZE 1024
5+
#define DATA_SIZE 1024 // let's count the primes among the first 1024 numbers
66

77
int main(int argc, char *argv[]) {
88
int send[DATA_SIZE], recv[DATA_SIZE];
99
int rank, size, count, root, res, i, j;
1010
MPI_Status status;
1111

12-
MPI_Init(&argc, &argv);
13-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
14-
MPI_Comm_size(MPI_COMM_WORLD, &size);
12+
MPI_Init(&argc, &argv); // initialize MPI
13+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank/ID
14+
MPI_Comm_size(MPI_COMM_WORLD, &size); // get total number of processes
1515

16-
if(rank == 0) { //generate data if root
16+
if(rank == 0) { //generate data (i.e., the first DATA_SIZE natural numbers) if root
1717
for(i = DATA_SIZE; (--i)>=0; ) { send[i] = (i + 1); }
1818
}
1919

20-
count = (DATA_SIZE / size);
20+
count = (DATA_SIZE / size); // divide the data among _all_ processes
21+
// scatter: if rank=0, send data (and get own share); otherwise: receive data
2122
MPI_Scatter(send, count, MPI_INT, recv, count, MPI_INT, 0, MPI_COMM_WORLD);
2223

23-
// each node now processes its share of data
24+
// each node now processes its share of the numbers
2425
res = count; //here: count how many prime numbers are contained in the array
25-
outer: for(i = count; (--i) >= 0; ) {
26-
for(j = ((int)(sqrt(recv[i]))|1); j>1; j--) {
27-
if((recv[i] % j) == 0) {
28-
res--;
29-
break; }
26+
for(i = count; (--i) >= 0; ) { //j: test all odd numbers 1<j<sqrt(j)|1
27+
for(j = ((int)(sqrt(recv[i]))|1); j>1; j -= 2) {
28+
if((recv[i] % j) == 0) { // if a number can be divided by j
29+
res--; // it cannot be a prime number, reduce number of primes
30+
break; } // break inner loop to test next number
3031
}
3132
}
3233
printf("Process %d discovered %d primes in the numbers from %d to %d.\n", rank, res, recv[0], recv[count-1]);
3334

35+
// gather: all processes send data to root, only root receives data
3436
MPI_Gather(&res, 1, MPI_INT, recv, 1, MPI_INT, 0, MPI_COMM_WORLD);
3537

3638
if(rank == 0) { //if root, process the received data

mpi/nonBlockingPointToPoint.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
#include <mpi.h>
2-
#include <stdio.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // needed for printf
33

44
int main(int argc, char *argv[]) {
55
int rank, size, prev, next;
6-
char buffer[30], buffer2[30];
7-
MPI_Request request, request2;
6+
char receiveBuffer[30], sendBuffer[30];
7+
MPI_Request receiveRequest, sendRequest;
88
MPI_Status status;
99

10-
MPI_Init(&argc,&argv);
11-
MPI_Comm_size(MPI_COMM_WORLD, &size);
12-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
10+
MPI_Init(&argc,&argv); // initialize MPI
11+
MPI_Comm_size(MPI_COMM_WORLD, &size); // get own rank / ID
12+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get total number of processes
1313

14-
next = ((rank + 1) % size);
15-
prev = ((rank + size - 1) % size);
14+
next = ((rank + 1) % size); // get rank of process to receive from
15+
// _initiate_ receive operation, but do not wait for its completion
16+
MPI_Irecv(receiveBuffer, 30, MPI_CHAR, prev, 42, MPI_COMM_WORLD, &receiveRequest);
1617

17-
MPI_Irecv(buffer, 30, MPI_CHAR, prev, 42, MPI_COMM_WORLD, &request);
18+
prev = ((rank + size - 1) % size); // get rank of process to send to
19+
sprintf(sendBuffer, "Non-blocking from %d!", rank);
20+
// _initiate_ send operation, but do not wait for its completion
21+
MPI_Isend(sendBuffer, 30, MPI_CHAR, next, 42, MPI_COMM_WORLD, &sendRequest);
1822

19-
sprintf(buffer2, "Non-blocking from %d!", rank);
20-
MPI_Isend(buffer2, 30, MPI_CHAR, next, 42, MPI_COMM_WORLD, &request2);
23+
MPI_Wait(&receiveRequest, &status); // wait for receive to complete
24+
printf("%d received \"%s\"\n", rank, receiveBuffer); // print received msg
2125

22-
MPI_Wait(&request, &status);
23-
printf("%d received \"%s\"\n", rank, buffer);
26+
MPI_Wait(&sendRequest, &status); // wait for send to complete
2427

25-
MPI_Wait(&request2, &status);
26-
27-
MPI_Finalize();
28+
MPI_Finalize(); // shut down MPI
2829
return 0;
2930
}

mpi/piGatherScatter.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include <mpi.h>
2-
#include <stdlib.h>
3-
#include <string.h>
4-
#include <time.h>
5-
#include <stdio.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // needed for printf
3+
#include <stdlib.h> // for rand and RAND_MAX
4+
#include <string.h> // for memset
5+
#include <time.h> // for srand(time(NULL));
66

77
int main(int argc, char **argv) {
88
int i, size, rank; double x, y;

mpi/reducePrimes.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
#include <mpi.h>
2-
#include <stdio.h>
3-
#include <math.h>
1+
#include <mpi.h> // import MPI header
2+
#include <stdio.h> // needed for printf
3+
#include <math.h> // needed for sqrt
44

5-
#define DATA_SIZE 1024
5+
#define DATA_SIZE 1024 // let's count the primes among the first 1024 numbers
66

77
int main(int argc, char *argv[]) {
8-
int sent[DATA_SIZE], recv[DATA_SIZE];
8+
int send[DATA_SIZE], recv[DATA_SIZE];
99
int rank, size, count, root, res, i, j;
1010
MPI_Status status;
1111

12-
MPI_Init(&argc, &argv);
13-
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
14-
MPI_Comm_size(MPI_COMM_WORLD, &size);
12+
MPI_Init(&argc, &argv); // initialize MPI
13+
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get own rank/ID
14+
MPI_Comm_size(MPI_COMM_WORLD, &size); // get total number of processes
1515

16-
if(rank == 0) { //generate data if root
17-
for(i = DATA_SIZE; (--i)>=0; ) { sent[i] = (i + 1); }
16+
if(rank == 0) { //generate data (i.e., the first DATA_SIZE natural numbers) if root
17+
for(i = DATA_SIZE; (--i)>=0; ) { send[i] = (i + 1); }
1818
}
1919

20-
count = (DATA_SIZE / size);
21-
MPI_Scatter(sent, count, MPI_INT, recv, count, MPI_INT, 0, MPI_COMM_WORLD);
20+
count = (DATA_SIZE / size); // divide the data among _all_ processes
21+
// scatter: if rank=0, send data (and get own share); otherwise: receive data
22+
MPI_Scatter(send, count, MPI_INT, recv, count, MPI_INT, 0, MPI_COMM_WORLD);
2223

23-
// each node now processes its share of data
24+
// each node now processes its share of the numbers
2425
res = count; //here: count how many prime numbers are contained in the array
25-
outer: for(i = count; (--i) >= 0; ) {
26-
for(j = ((int)(sqrt(recv[i]))|1); j>1; j--) {
27-
if((recv[i] % j) == 0) {
28-
res--;
29-
break; }
26+
for(i = count; (--i) >= 0; ) { //j: test all odd numbers 1<j<sqrt(j)|1
27+
for(j = ((int)(sqrt(recv[i]))|1); j>1; j -= 2) {
28+
if((recv[i] % j) == 0) { // if a number can be divided by j
29+
res--; // it cannot be a prime number, reduce number of primes
30+
break; } // break inner loop to test next number
3031
}
3132
}
3233
printf("Process %d discovered %d primes in the numbers from %d to %d.\n", rank, res, recv[0], recv[count-1]);
3334

35+
// reduce: each node takes results, applies operator MPI_SUM locally, sends result to root, where MPI_SUM is
36+
// applied again. (here: locally summing up does not matter, as only 1 number). The final result is returned.
3437
MPI_Reduce(&res, recv, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
3538
if(rank == 0) { //if root, print
3639
printf("The total number of primes in the first %d natural numbers is %d.\n", (count*size), recv[0]);
3740
}
3841

39-
MPI_Finalize();
42+
MPI_Finalize(); // shut down MPI
4043
return 0;
4144
}

mpi/structTest.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#include <stdio.h>
22

3-
typedef struct { int myIntA;
4-
int myIntB;
5-
short myShort;
6-
double myDouble;
7-
char myChar;
8-
float myFloat; } myStruct;
3+
typedef struct { int myIntA; // an integer (usually 4 bytes)
4+
int myIntB; // another integer (usually 4 bytes)
5+
short myShort; // a short integer (usually 2 bytes)
6+
double myDouble; // a double-precision floating point number (usually 8 bytes)
7+
char myChar; // a char = 1 single byte (usually 1 byte)
8+
float myFloat; // a single-precision floating point number (usually 4 bytes)
9+
} myStruct; // name of new data type = myStruct
10+
11+
#define LU(expression) ((long unsigned int)(expression)) // macro for type cast
912

1013
int main(int argc, char *argv[]) {
11-
myStruct ds;
14+
myStruct ds; // the data structure
1215

13-
printf("Struct is %lu bytes large.\n", sizeof(ds));
14-
printf("Field \"myIntA\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myIntA) - ((long)&ds)), sizeof(ds.myIntA));
15-
printf("Field \"myIntB\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myIntB) - ((long)&ds)), sizeof(ds.myIntB));
16-
printf("Field \"myShort\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myShort) - ((long)&ds)), sizeof(ds.myShort));
17-
printf("Field \"myDouble\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myDouble) - ((long)&ds)), sizeof(ds.myDouble));
18-
printf("Field \"myChar\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myChar) - ((long)&ds)), sizeof(ds.myChar));
19-
printf("Field \"myFloat\" begins at offset %lu and is %lu bytes large.\n", (((long)&ds.myFloat) - ((long)&ds)), sizeof(ds.myFloat));
20-
printf("Effectively used data size is %lu.\n", (sizeof(ds.myIntA) + sizeof(ds.myIntB) + sizeof(ds.myShort) + sizeof(ds.myDouble) +
21-
sizeof(ds.myChar) + sizeof(ds.myFloat)));
16+
printf("Struct is %lu bytes large.\n", LU(sizeof(ds)));
17+
printf("Field \"myIntA\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myIntA) - LU(&ds)), LU(sizeof(ds.myIntA)));
18+
printf("Field \"myIntB\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myIntB) - LU(&ds)), LU(sizeof(ds.myIntB)));
19+
printf("Field \"myShort\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myShort) - LU(&ds)), LU(sizeof(ds.myShort)));
20+
printf("Field \"myDouble\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myDouble) - LU(&ds)), LU(sizeof(ds.myDouble)));
21+
printf("Field \"myChar\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myChar) - LU(&ds)), LU(sizeof(ds.myChar)));
22+
printf("Field \"myFloat\" begins at offset %lu and is %lu bytes large.\n", LU(LU(&ds.myFloat) - LU(&ds)), LU(sizeof(ds.myFloat)));
23+
printf("Effectively used data size is %lu.\n", LU(sizeof(ds.myIntA) + sizeof(ds.myIntB) + sizeof(ds.myShort) + sizeof(ds.myDouble) +
24+
sizeof(ds.myChar) + sizeof(ds.myFloat)));
2225
return 0;
2326
}

0 commit comments

Comments
 (0)