|
@@ -1,27 +1,37 @@
|
|
| 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 |
int main(int argc, char* argv[]) {
|
| 7 |
int thread_count = omp_get_max_threads();
|
| 8 |
if (argc >= 2) {
|
| 9 |
thread_count = atoi(argv[1]);
|
| 10 |
}
|
| 11 |
|
| 12 |
std::vector<double> values;
|
| 13 |
|
| 14 |
double value = 0.0;
|
| 15 |
while (std::cin >> value) {
|
| 16 |
values.push_back(value);
|
| 17 |
}
|
| 18 |
|
| 19 |
-
double
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
for (size_t index = 0; index < values.size(); ++index) {
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
-
const double average = values.size() ?
|
| 26 |
std::cout << average << std::endl;
|
| 27 |
}
|
| 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 |
int main(int argc, char* argv[]) {
|
| 7 |
int thread_count = omp_get_max_threads();
|
| 8 |
if (argc >= 2) {
|
| 9 |
thread_count = atoi(argv[1]);
|
| 10 |
}
|
| 11 |
|
| 12 |
std::vector<double> values;
|
| 13 |
|
| 14 |
double value = 0.0;
|
| 15 |
while (std::cin >> value) {
|
| 16 |
values.push_back(value);
|
| 17 |
}
|
| 18 |
|
| 19 |
+
double total_sum = 0.0;
|
| 20 |
|
| 21 |
+
#pragma omp parallel num_threads(thread_count) \
|
| 22 |
+
default(none) shared(values, total_sum)
|
| 23 |
+
{
|
| 24 |
+
double my_partial_sum = 0.0;
|
| 25 |
+
|
| 26 |
+
#pragma omp for schedule(runtime)
|
| 27 |
for (size_t index = 0; index < values.size(); ++index) {
|
| 28 |
+
my_partial_sum += values[index];
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
#pragma omp critical(can_add)
|
| 32 |
+
total_sum += my_partial_sum;
|
| 33 |
}
|
| 34 |
|
| 35 |
+
const double average = values.size() ? total_sum / values.size() : 0.0;
|
| 36 |
std::cout << average << std::endl;
|
| 37 |
}
|