|
@@ -1,28 +1,44 @@
|
|
| 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 <iostream>
|
| 8 |
|
| 9 |
int main(int argc, char* argv[]) {
|
| 10 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
| 11 |
int my_rank = -1;
|
| 12 |
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
| 13 |
|
| 14 |
int process_count = -1;
|
| 15 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
| 16 |
|
| 17 |
char hostname[MPI_MAX_PROCESSOR_NAME];
|
| 18 |
int hostname_len = -1;
|
| 19 |
MPI_Get_processor_name(hostname, &hostname_len);
|
| 20 |
|
| 21 |
std::cout << "Hello from main thread of process " << my_rank
|
| 22 |
<< " of " << process_count << " on " << hostname << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
MPI_Finalize();
|
| 24 |
} else {
|
| 25 |
std::cout << "Error: could not init MPI environment" << std::endl;
|
| 26 |
}
|
| 27 |
return 0;
|
| 28 |
}
|
| 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 <omp.h>
|
| 8 |
#include <iostream>
|
| 9 |
|
| 10 |
int main(int argc, char* argv[]) {
|
| 11 |
if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
|
| 12 |
int my_rank = -1;
|
| 13 |
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
| 14 |
|
| 15 |
int process_count = -1;
|
| 16 |
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
|
| 17 |
|
| 18 |
char hostname[MPI_MAX_PROCESSOR_NAME];
|
| 19 |
int hostname_len = -1;
|
| 20 |
MPI_Get_processor_name(hostname, &hostname_len);
|
| 21 |
|
| 22 |
std::cout << "Hello from main thread of process " << my_rank
|
| 23 |
<< " of " << process_count << " on " << hostname << std::endl;
|
| 24 |
+
|
| 25 |
+
int thread_count = omp_get_max_threads();
|
| 26 |
+
if (argc >= 2) {
|
| 27 |
+
thread_count = atoi(argv[1]);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
#pragma omp parallel num_threads(thread_count)\
|
| 31 |
+
default(none) shared(std::cout, my_rank, process_count, hostname)
|
| 32 |
+
{
|
| 33 |
+
#pragma omp critical(cout)
|
| 34 |
+
std::cout << " Hello from thread " << omp_get_thread_num()
|
| 35 |
+
<< " of " << omp_get_num_threads() << " of process " << my_rank
|
| 36 |
+
<< " of " << process_count << " on " << hostname << std::endl;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
MPI_Finalize();
|
| 40 |
} else {
|
| 41 |
std::cout << "Error: could not init MPI environment" << std::endl;
|
| 42 |
}
|
| 43 |
return 0;
|
| 44 |
}
|