OpenMP and Multithreading: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Tutorials == | |||
* [https://computing.llnl.gov/tutorials/openMP From LLNL] | |||
== C++ == | |||
=== simple parallelization === | === simple parallelization === | ||
Line 10: | Line 15: | ||
</source> | </source> | ||
* with stl iterators on containers, provided foo() does independent processes ''not efficient'': | * with stl iterators on containers, provided foo() does independent processes (''not efficient''): | ||
<source lang="cpp"> | <source lang="cpp"> | ||
Container cont; | Container cont; | ||
Line 54: | Line 59: | ||
} | } | ||
</source> | </source> | ||
== Python == | |||
one may preferably use [http://docs.python.org/library/multiprocessing.html multiprocessing] |
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