OpenMP and Multithreading: Difference between revisions

From LPTMS Wiki
Jump to navigation Jump to search
mNo edit summary
 
(One intermediate revision 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 54: Line 59:
   }
   }
</source>
</source>
== Python ==
one may preferably use [http://docs.python.org/library/multiprocessing.html multiprocessing]

Latest revision as of 09: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;

  1. 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;

  1. pragma omp parallel private(It)

{

   for(It = cont->begin(); It != cont->end(); It++)
   {
  1. pragma omp single nowait
    foo(It);
   }

} </source>

  • calculating a sum

<source lang="cpp"> Type count = 0; Container<Type> a;

  1. pragma omp parallel for

for (int i = 0; i < a.size(); ++i)

 {
  1. pragma omp atomic
   count += a[i];
 }

</source> or better <source lang="cpp"> Type count = 0; Container<Type> a;

  1. 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;

  1. 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