OpenMP and Multithreading: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=== simple parallelization === * brute force loop parallelization with direct access to elements using [] (for instance with int[], vectors<T>, valarray<T>...) <source lang="cp...") |
No edit summary |
||
Line 20: | Line 20: | ||
{ | { | ||
#pragma omp single nowait | #pragma omp single nowait | ||
foo(It); | foo(It); | ||
} | } | ||
} | } | ||
</source> | |||
* calculating a sum | |||
<source lang="cpp"> | |||
int count = 0; | |||
Container cont; | |||
#pragma omp parallel for | |||
for (int i = 0; i < cont.size(); ++i) | |||
{ | |||
#pragma omp atomic | |||
count += cont[i]; | |||
} | |||
</source> | </source> |
Revision as of 12:06, 7 April 2011
simple parallelization
- brute force loop parallelization with direct access to elements using [] (for instance with int[], vectors<T>, valarray<T>...)
<source lang="cpp"> Container cont;
- pragma omp parallel for
for(int i=0; i < cont.size(); i++)
foo(cont[i]);
</source>
- with stl iterators on containers, provided foo() does independent processes:
<source lang="cpp"> Container cont; Container::iterator It;
- pragma omp parallel private(It)
{
for(It = cont->begin(); It != cont->end(); It++) {
- pragma omp single nowait
foo(It); }
} </source>
- calculating a sum
<source lang="cpp"> int count = 0; Container cont;
- pragma omp parallel for
for (int i = 0; i < cont.size(); ++i)
{
- pragma omp atomic
count += cont[i]; }
</source>