Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to know why the OpenMP code does not parallelise

I just started learning how to use OpenMP. I am trying to figure out why the following code does not run in parallel with Visual Studio 2008. It compiles and runs fine. However it uses only one core on my quad core machine. This is part of the code that I am trying to port to a MATLAB mex function. Any pointer is appreciated.

#pragma omp parallel for default(shared) private(dz, t, v, ts_count) reduction(+: sum_v)
for(t = 0; t<T; t++)
{
    dz = aRNG->randn();
    v += mrdt* (tv - v) +
         vv_v_sqrt_dt * dz +
         vv_vv_v_dt*(dz*dz - 1.);

    sum_v += v;
    if(t == ts_count-1)
    {
        int_v->at_w(k++) = sum_v/(double)(t+1);
        ts_count += ts;
    }
}
like image 845
leon Avatar asked Jan 21 '10 21:01

leon


People also ask

What does it mean to parallelize code with OpenMP?

Parallel code with OpenMP marks, through a special directive, sections to be executed in parallel. The part of the code that’s marked to run in parallel will cause threads to form. The main tread is the master thread. The slave threads all run in parallel and run the same code.

What is omp h header file?

This file is part of the GNU OpenMP Library (libgomp). Libgomp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

What is OpenMP pragma?

The pragma omp parallel is used to fork additional threads to carry out the work enclosed in the construct in parallel. The original thread will be denoted as master thread with thread ID 0.


1 Answers

The v variable is computed using the v value of the previous iteration

  for(t = 0; t<T; t++) {
     ...
     v += ... ( tv - v ) ....
     ...
  }

You cannot do that, it breaks the parallelism. The loop must be able to be run in any sequence, or with differents parallel chunks at once, with no side effects. From a first glance, it doesnt look like you can parallelize this kind of loop.

like image 95
Blklight Avatar answered Nov 14 '22 23:11

Blklight