OpenMP and Multithreading

From LPTMS Wiki
Jump to: navigation, search

Tutorials

C++

simple parallelization

  • brute force loop parallelization with direct access to elements using [] (for instance with int[], vectors<T>, valarray<T>...)

Container cont;
#pragma omp parallel for
for(int i=0; i < cont.size(); i++)
   foo(cont[i]);
  • with stl iterators on containers, provided foo() does independent processes (not efficient):

Container cont;
Container::iterator It;
#pragma omp parallel private(It)
{
    for(It = cont->begin(); It != cont->end(); It++)
    {
#pragma omp single nowait
     foo(It);
    }
}
  • calculating a sum

Type count = 0;
Container<Type> a;
#pragma omp parallel for
for (int i = 0; i < a.size(); ++i)
  {
#pragma omp atomic
    count += a[i];
  }

or better

Type count = 0;
Container<Type> a;
#pragma omp parallel for reduction(+:count)
for (int i = 0; i < a.size(); ++i)
    count += a[i];

you can use the reduction sentence for a list of several variables (cannot be arrays or structured data type)

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;
   }

Python

one may preferably use multiprocessing