@@ -1,26 +1,64 @@
|
|
1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
2 |
|
3 |
#include <mpi.h>
|
|
|
4 |
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
int main(int argc, char* argv[]) {
|
|
|
7 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
8 |
int process_number = -1;
|
9 |
MPI_Comm_rank(MPI_COMM_WORLD, &process_number);
|
10 |
|
11 |
int process_count = -1;
|
12 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
13 |
|
14 |
char process_hostname[MPI_MAX_PROCESSOR_NAME] = { '\0' };
|
15 |
int hostname_length = -1;
|
16 |
MPI_Get_processor_name(process_hostname, &hostname_length);
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
MPI_Finalize();
|
22 |
} else {
|
23 |
std::cerr << "error: could not init MPI" << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
-
return 0;
|
26 |
}
|
1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
2 |
|
3 |
#include <mpi.h>
|
4 |
+
#include <cstdlib>
|
5 |
#include <iostream>
|
6 |
+
#include <stdexcept>
|
7 |
+
|
8 |
+
// DistributedExeption(process_number, exception_code, msg)
|
9 |
+
#define fail(msg) throw std::runtime_error(msg)
|
10 |
+
|
11 |
+
void greet(int process_number, int process_count, const char* process_hostname);
|
12 |
|
13 |
int main(int argc, char* argv[]) {
|
14 |
+
int error = EXIT_SUCCESS;
|
15 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
16 |
int process_number = -1;
|
17 |
MPI_Comm_rank(MPI_COMM_WORLD, &process_number);
|
18 |
|
19 |
int process_count = -1;
|
20 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
21 |
|
22 |
char process_hostname[MPI_MAX_PROCESSOR_NAME] = { '\0' };
|
23 |
int hostname_length = -1;
|
24 |
MPI_Get_processor_name(process_hostname, &hostname_length);
|
25 |
|
26 |
+
try {
|
27 |
+
greet(process_number, process_count, process_hostname);
|
28 |
+
} catch (const std::runtime_error& exception) {
|
29 |
+
std::cerr << "error: " << exception.what() << std::endl;
|
30 |
+
error = EXIT_FAILURE;
|
31 |
+
}
|
32 |
|
33 |
MPI_Finalize();
|
34 |
} else {
|
35 |
std::cerr << "error: could not init MPI" << std::endl;
|
36 |
+
error = EXIT_FAILURE;
|
37 |
+
}
|
38 |
+
return error;
|
39 |
+
}
|
40 |
+
|
41 |
+
void greet(int process_number, int process_count
|
42 |
+
, const char* process_hostname) {
|
43 |
+
const int previous_process = (process_count + process_number - 1)
|
44 |
+
% process_count;
|
45 |
+
const int next_process = (process_number + 1) % process_count;
|
46 |
+
bool can_print = true;
|
47 |
+
|
48 |
+
if (process_number > 0) {
|
49 |
+
// receive(&can_print, 1, previous_process)
|
50 |
+
if (MPI_Recv(&can_print, /*count*/ 1, MPI_C_BOOL, previous_process
|
51 |
+
, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE) != MPI_SUCCESS ) {
|
52 |
+
fail("could not receive message");
|
53 |
+
}
|
54 |
+
}
|
55 |
+
|
56 |
+
std::cout << "Hello from main thread of process " << process_number
|
57 |
+
<< " of " << process_count << " on " << process_hostname << std::endl;
|
58 |
+
|
59 |
+
// send(can_print, 1, next_process);
|
60 |
+
if (MPI_Send(&can_print, /*count*/ 1, MPI_C_BOOL, next_process, /*tag*/ 0
|
61 |
+
, MPI_COMM_WORLD) != MPI_SUCCESS) {
|
62 |
+
fail("could not send message");
|
63 |
}
|
|
|
64 |
}
|