openmp/average_mutex/{serial/average_serial.cpp → src/average_mutex.cpp} RENAMED
@@ -1,27 +1,30 @@
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 sum = 0.0;
20
 
 
 
21
  for (size_t index = 0; index < values.size(); ++index) {
 
22
  sum += values[index];
23
  }
24
 
25
  const double average = values.size() ? sum / values.size() : 0.0;
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 sum = 0.0;
20
 
21
+ #pragma omp parallel for num_threads(thread_count) schedule(runtime) \
22
+ default(none) shared(values, sum)
23
  for (size_t index = 0; index < values.size(); ++index) {
24
+ #pragma omp critical(can_add)
25
  sum += values[index];
26
  }
27
 
28
  const double average = values.size() ? sum / values.size() : 0.0;
29
  std::cout << average << std::endl;
30
  }