Skip to content

Instantly share code, notes, and snippets.

@msanroman
Created May 24, 2012 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msanroman/2782104 to your computer and use it in GitHub Desktop.
Save msanroman/2782104 to your computer and use it in GitHub Desktop.
Second sum_vector
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