|
@@ -1,51 +1,86 @@
|
|
| 1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
| 2 |
#include <omp.h>
|
| 3 |
#include <iostream>
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
int main(int argc, char* argv[]) {
|
| 6 |
int thread_count = omp_get_max_threads();
|
| 7 |
if (argc >= 2) {
|
| 8 |
thread_count = atoi(argv[1]);
|
| 9 |
}
|
| 10 |
|
| 11 |
int iteration_count = thread_count;
|
| 12 |
if (argc >= 3) {
|
| 13 |
iteration_count = atoi(argv[2]);
|
| 14 |
}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
#pragma omp parallel num_threads(thread_count) \
|
| 17 |
-
default(none) shared(iteration_count,
|
| 18 |
{
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 21 |
-
|
| 22 |
-
std::cout << "stage 1: " << omp_get_thread_num() << '/'
|
| 23 |
-
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 24 |
-
<< iteration_count << std::endl;
|
| 25 |
}
|
| 26 |
|
| 27 |
#pragma omp single
|
| 28 |
-
|
| 29 |
-
// #pragma omp barrier
|
| 30 |
|
| 31 |
-
|
| 32 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 33 |
-
|
| 34 |
-
std::cout << "stage 2: " << omp_get_thread_num() << '/'
|
| 35 |
-
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 36 |
-
<< iteration_count << std::endl;
|
| 37 |
}
|
| 38 |
|
| 39 |
#pragma omp single
|
| 40 |
-
|
| 41 |
-
// #pragma omp barrier
|
| 42 |
|
| 43 |
-
|
| 44 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 45 |
-
|
| 46 |
-
std::cout << "stage 3: " << omp_get_thread_num() << '/'
|
| 47 |
-
<< omp_get_num_threads() << ": iteration " << iteration << '/'
|
| 48 |
-
<< iteration_count << std::endl;
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
}
|
| 1 |
// Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
|
| 2 |
#include <omp.h>
|
| 3 |
#include <iostream>
|
| 4 |
+
#include <vector>
|
| 5 |
+
|
| 6 |
+
void print_mapping(const char* type, const std::vector<int>& mapping);
|
| 7 |
|
| 8 |
int main(int argc, char* argv[]) {
|
| 9 |
int thread_count = omp_get_max_threads();
|
| 10 |
if (argc >= 2) {
|
| 11 |
thread_count = atoi(argv[1]);
|
| 12 |
}
|
| 13 |
|
| 14 |
int iteration_count = thread_count;
|
| 15 |
if (argc >= 3) {
|
| 16 |
iteration_count = atoi(argv[2]);
|
| 17 |
}
|
| 18 |
|
| 19 |
+
int block_size = 0;
|
| 20 |
+
if (argc >= 4) {
|
| 21 |
+
block_size = atoi(argv[3]);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
std::vector<int> mapping(iteration_count);
|
| 25 |
+
|
| 26 |
#pragma omp parallel num_threads(thread_count) \
|
| 27 |
+
default(none) shared(iteration_count, mapping, block_size)
|
| 28 |
{
|
| 29 |
+
if (block_size == 0) {
|
| 30 |
+
#pragma omp for schedule(static)
|
| 31 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 32 |
+
mapping[iteration] = omp_get_thread_num();
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
#pragma omp single
|
| 36 |
+
print_mapping("static ", mapping);
|
| 37 |
+
|
| 38 |
+
#pragma omp for schedule(dynamic)
|
| 39 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 40 |
+
mapping[iteration] = omp_get_thread_num();
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
#pragma omp single
|
| 44 |
+
print_mapping("dynamic ", mapping);
|
| 45 |
+
|
| 46 |
+
#pragma omp for schedule(guided)
|
| 47 |
+
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 48 |
+
mapping[iteration] = omp_get_thread_num();
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
#pragma omp single
|
| 52 |
+
print_mapping("guided ", mapping);
|
| 53 |
+
} else {
|
| 54 |
+
#pragma omp for schedule(static, block_size)
|
| 55 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 56 |
+
mapping[iteration] = omp_get_thread_num();
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
#pragma omp single
|
| 60 |
+
print_mapping("static,N ", mapping);
|
|
|
|
| 61 |
|
| 62 |
+
#pragma omp for schedule(dynamic, block_size)
|
| 63 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 64 |
+
mapping[iteration] = omp_get_thread_num();
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
#pragma omp single
|
| 68 |
+
print_mapping("dynamic,N ", mapping);
|
|
|
|
| 69 |
|
| 70 |
+
#pragma omp for schedule(guided, block_size)
|
| 71 |
for (int iteration = 0; iteration < iteration_count; ++iteration) {
|
| 72 |
+
mapping[iteration] = omp_get_thread_num();
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
+
|
| 75 |
+
#pragma omp single
|
| 76 |
+
print_mapping("guided,N ", mapping);
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
void print_mapping(const char* type, const std::vector<int>& mapping) {
|
| 82 |
+
std::cout << type;
|
| 83 |
+
for (size_t index = 0; index < mapping.size(); ++index) {
|
| 84 |
+
std::cout << mapping[index] << (index == mapping.size() - 1 ? '\n' : ' ');
|
| 85 |
}
|
| 86 |
}
|