views:

165

answers:

3

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

I'm not too sure, it's been a long time since I used OpenMP, but you have the loop variable t set as private. Is that what you want? Isn't that the parallelization variable?

miked
A: 

One possibility is that you are using the "sum_v" variable. Since you are performing a reduction, the runtime probably expects to only accumulate values in there, and access it 'normally' only after the loop is done.

florin
+4  A: 

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.

Blklight
You should also give some consideration to your se of a random number generator within the loop. Depending on it's implementation this may lock on a shared resource, effectively serializing your code after if you address the issue above.
Ade Miller