OpenMP and Multithreading: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
== Tutorials == | |||
* [https://computing.llnl.gov/tutorials/openMP From LLNL] | |||
== C++ == | == C++ == |
Latest revision as of 10:00, 5 September 2011
Tutorials
C++
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 (not efficient):
<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"> Type count = 0; Container<Type> a;
- pragma omp parallel for
for (int i = 0; i < a.size(); ++i)
{
- pragma omp atomic
count += a[i]; }
</source> or better <source lang="cpp"> Type count = 0; Container<Type> a;
- pragma omp parallel for reduction(+:count)
for (int i = 0; i < a.size(); ++i)
count += a[i];
</source> you can use the reduction sentence for a list of several variables (cannot be arrays or structured data type) <source lang="cpp"> double c = 0.0; double c2 = 1.0;
- pragma omp parallel for shared(a) reduction(+:c,c2)
for (int i = 0; i < a.size(); ++i)
{ c += a[i]; c2 += a[i]+1.0; }
</source>
Python
one may preferably use multiprocessing