@@ -1,61 +1,57 @@
|
|
1 |
/*
|
2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
3 |
* Creates a secondary thread that greets in the standard output
|
4 |
*/
|
5 |
|
6 |
#include <mpi.h>
|
7 |
#include <iomanip>
|
8 |
#include <iostream>
|
9 |
#include <cstdint>
|
10 |
#include <vector>
|
11 |
|
12 |
int main(int argc, char* argv[]) {
|
13 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
14 |
int my_rank = -1;
|
15 |
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
16 |
|
17 |
int process_count = -1;
|
18 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
19 |
|
20 |
char hostname[MPI_MAX_PROCESSOR_NAME];
|
21 |
int hostname_len = -1;
|
22 |
MPI_Get_processor_name(hostname, &hostname_len);
|
23 |
|
24 |
std::vector<int64_t> numbers;
|
25 |
double start_time = MPI_Wtime();
|
26 |
|
27 |
if (my_rank == 0) {
|
28 |
int64_t number = 0;
|
29 |
while (std::cin >> number) {
|
30 |
numbers.push_back(number);
|
31 |
}
|
|
|
32 |
|
33 |
size_t number_count = numbers.size();
|
34 |
-
|
35 |
-
|
36 |
-
, MPI_COMM_WORLD);
|
37 |
-
MPI_Send(&numbers[0], number_count, MPI_INT64_T, dest, /*tag*/ 0
|
38 |
, MPI_COMM_WORLD);
|
39 |
-
|
40 |
-
|
41 |
-
else {
|
42 |
-
size_t number_count = numbers.size();
|
43 |
-
MPI_Recv(&number_count, /*capacity*/ 1, MPI_UINT64_T
|
44 |
-
, /*source*/ 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
45 |
numbers.resize(number_count);
|
46 |
-
MPI_Recv(&numbers[0], /*capacity*/ number_count, MPI_INT64_T
|
47 |
-
, 0, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
48 |
}
|
49 |
|
|
|
|
|
|
|
50 |
double elapsed = MPI_Wtime() - start_time;
|
51 |
|
52 |
std::cout << "Process " << my_rank << " got " << numbers.size()
|
53 |
<< " numbers in " << std::fixed << std::setprecision(9) << elapsed
|
54 |
<< "s" << std::endl;
|
55 |
|
56 |
MPI_Finalize();
|
57 |
} else {
|
58 |
std::cout << "Error: could not init MPI environment" << std::endl;
|
59 |
}
|
60 |
return 0;
|
61 |
}
|
1 |
/*
|
2 |
* Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
|
3 |
* Creates a secondary thread that greets in the standard output
|
4 |
*/
|
5 |
|
6 |
#include <mpi.h>
|
7 |
#include <iomanip>
|
8 |
#include <iostream>
|
9 |
#include <cstdint>
|
10 |
#include <vector>
|
11 |
|
12 |
int main(int argc, char* argv[]) {
|
13 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
14 |
int my_rank = -1;
|
15 |
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
16 |
|
17 |
int process_count = -1;
|
18 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
19 |
|
20 |
char hostname[MPI_MAX_PROCESSOR_NAME];
|
21 |
int hostname_len = -1;
|
22 |
MPI_Get_processor_name(hostname, &hostname_len);
|
23 |
|
24 |
std::vector<int64_t> numbers;
|
25 |
double start_time = MPI_Wtime();
|
26 |
|
27 |
if (my_rank == 0) {
|
28 |
int64_t number = 0;
|
29 |
while (std::cin >> number) {
|
30 |
numbers.push_back(number);
|
31 |
}
|
32 |
+
}
|
33 |
|
34 |
size_t number_count = numbers.size();
|
35 |
+
|
36 |
+
MPI_Bcast(&number_count, /*count*/ 1, MPI_UINT64_T, /*root*/ 0
|
|
|
|
|
37 |
, MPI_COMM_WORLD);
|
38 |
+
|
39 |
+
if (my_rank != 0) {
|
|
|
|
|
|
|
|
|
40 |
numbers.resize(number_count);
|
|
|
|
|
41 |
}
|
42 |
|
43 |
+
MPI_Bcast(&numbers[0], number_count, MPI_INT64_T, /*root*/ 0
|
44 |
+
, MPI_COMM_WORLD);
|
45 |
+
|
46 |
double elapsed = MPI_Wtime() - start_time;
|
47 |
|
48 |
std::cout << "Process " << my_rank << " got " << numbers.size()
|
49 |
<< " numbers in " << std::fixed << std::setprecision(9) << elapsed
|
50 |
<< "s" << std::endl;
|
51 |
|
52 |
MPI_Finalize();
|
53 |
} else {
|
54 |
std::cout << "Error: could not init MPI environment" << std::endl;
|
55 |
}
|
56 |
return 0;
|
57 |
}
|