1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | #include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <omp.h>
#include <vector>
int main(int argc, char* argv[])
{
int thread_count = omp_get_max_threads();
if ( argc >= 2 )
thread_count = atoi(argv[1]);
std::vector<double> values;
std::copy(std::istream_iterator<double>(std::cin),
std::istream_iterator<double>(),
std::back_inserter(values));
double sum = 0;
#pragma omp parallel for num_threads(thread_count) schedule(static) \
default(none) shared(values) reduction(+:sum)
for ( size_t index = 0; index < values.size(); ++index )
sum += values[index];
std::cout << sum / values.size() << std::endl;
return 0;
}
|