Created
May 24, 2012 15:04
-
-
Save msanroman/2782104 to your computer and use it in GitHub Desktop.
Second sum_vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void sum_vector (int *X, int n) | |
{ | |
int sum = 0; | |
int i; | |
int slice = n/OMP_get_num_threads(); | |
vector<int> threads(n); | |
for (int i = 0; i < OMP_get_num_threads(); ++i) | |
{ | |
#pragma omp task | |
threads[i] = sum_slice(X, i*slice, (i+1)*slice); | |
} | |
#pragma omp taskwait | |
for(int i = 0; i < threads.size(); ++i) | |
sum += threads[i]; | |
return sum; | |
} | |
int sum_slice(int *X, int start, int end) { | |
int sum = 0; | |
for(int i = start; i < end; ++i) | |
sum += X[i]; | |
return sum; | |
} | |
void main() | |
{ | |
... | |
#pragma omp parallel | |
{ | |
#pragma omp single | |
sum_vector(v,N); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment