-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenmp.cpp
More file actions
34 lines (27 loc) · 724 Bytes
/
openmp.cpp
File metadata and controls
34 lines (27 loc) · 724 Bytes
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
29
30
31
32
33
34
#include <iostream>
#include <vector>
using namespace std;
/**
* Calculates the power given by pow of all values in the given vector.
* Compiled with -fopenmp the operation will execute in parallel.
**/
void vectorPow(vector<double> &vector, const int &pow) {
#pragma omp parallel for
for (size_t i = 0; i < vector.size(); ++i) {
double value = vector[i];
//#pragma omp parallel for // The code runs faster with this commented out
for (int j = 0; j < pow-1; ++j) {
vector[i] *= value;
}
}
}
int main() {
const int N = 1e6;
vector<double> vector;
int pow = 2000;
for (int i = 0; i < N; ++i) {
vector.push_back(i);
}
vectorPow(vector, pow);
return 0;
}