129 lines
3.2 KiB
C++
129 lines
3.2 KiB
C++
#include "file_io.h"
|
|
#include "means.h"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <execution>
|
|
#include <omp.h>
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
cout << "File einlesen." << endl;
|
|
|
|
const string name("data_1.txt"); // name of input file
|
|
const string name2("out_1.txt"); // name of output file
|
|
vector<short> a; //-2^15 to 2^15-1 fits the values from the file
|
|
double min, max, ar, ge, ha, std;
|
|
|
|
|
|
read_vector_from_file(name, a);
|
|
const unsigned size = a.size();
|
|
double t_omp_start = omp_get_wtime();
|
|
min = a[0];
|
|
max = a[0];
|
|
|
|
#pragma omp parallel for reduction(min:min) reduction(max:max)
|
|
for (int i = 0; i < static_cast<int>(size); ++i) {
|
|
if (a[i] < min)
|
|
{
|
|
min = a[i];
|
|
}
|
|
if (a[i] > max) {
|
|
max = a[i];
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
means_vector(a, ar, ge, ha);
|
|
|
|
std = 0.0;
|
|
#pragma omp parallel for reduction(+:std)
|
|
for(unsigned int i = 0; i < size; i++)
|
|
{
|
|
std += pow(a.at(i)-ar,2);
|
|
}
|
|
std = sqrt(std/size);
|
|
|
|
double t_omp_end = omp_get_wtime();
|
|
double time_omp = t_omp_end - t_omp_start;
|
|
|
|
cout << "min: " << min << ", max: " << max << endl;
|
|
cout << "Arithmetic mean: " << ar
|
|
<< ", geometric mean: " << ge
|
|
<< ", harmonic mean: " << ha << endl;
|
|
cout << "std: " << std << endl;
|
|
|
|
vector<double> results = {min, max, ar, ge, ha, std};
|
|
write_vector_to_file(name2, results);
|
|
|
|
|
|
|
|
//C++17 execution policies
|
|
|
|
double min_ep = 0.0, max_ep = 0.0, ar_ep = 0.0, ge_ep = 0.0, ha_ep = 0.0, std_ep = 0.0;
|
|
|
|
double t_ep_start = omp_get_wtime();
|
|
|
|
|
|
auto p = minmax_element(execution::par, a.begin(), a.end());
|
|
min_ep = *p.first;
|
|
max_ep = *p.second;
|
|
|
|
double sum_ep = reduce(
|
|
execution::par,
|
|
a.begin(), a.end(),
|
|
0.0
|
|
);
|
|
ar_ep = sum_ep / size;
|
|
|
|
ge_ep = transform_reduce(
|
|
execution::par,
|
|
a.begin(), a.end(),
|
|
1.0,
|
|
multiplies<>(),
|
|
[size](short x){
|
|
return pow((double)x, 1.0 / size);
|
|
}
|
|
);
|
|
|
|
double sum_inv_ep = transform_reduce(
|
|
execution::par,
|
|
a.begin(), a.end(),
|
|
0.0,
|
|
plus<>(),
|
|
[](short x){ return 1.0 / (double)x; }
|
|
);
|
|
ha_ep = size / sum_inv_ep;
|
|
|
|
double sum_sq_ep = transform_reduce(
|
|
execution::par,
|
|
a.begin(), a.end(),
|
|
0.0,
|
|
plus<>(),
|
|
[ar_ep](short x){
|
|
double d = x - ar_ep;
|
|
return d * d;
|
|
}
|
|
);
|
|
std_ep = sqrt(sum_sq_ep / size);
|
|
|
|
|
|
double t_ep_end = omp_get_wtime();
|
|
double time_ep = t_ep_end - t_ep_start;
|
|
|
|
cout << "min: " << min_ep << ", max: " << max_ep << endl;
|
|
cout << "Arithmetic mean: " << ar_ep
|
|
<< ", geometric mean: " << ge_ep
|
|
<< ", harmonic mean: " << ha_ep << endl;
|
|
cout << "std: " << std_ep << endl;
|
|
|
|
|
|
cout << "OpenMP per run: " << (time_omp) << endl;
|
|
cout << "Execution policy per run: " << (time_ep) << endl;
|
|
return 0;
|
|
}
|