1

I am trying to parallelize both loops of this fragment of code:

for ( j = 0 ; j < nt2 ; j++ ) {
   dm = MAX_LON + 1;

   for ( i = 0 ; i < nt1 ; i++ ) {
      d = distancia(tabla1[i],tabla2[j]);     
      if ( d < dm ) {
         dm = d;
         im = i;
      }
   }
   ps[j] = im;
}

Parallelizing of outer loop succeed:

#pragma omp parallel for private(i,d,dm,im)

But I still have a problem with the inner one. I tried with e.g. private(d,m) and reduction(max:im), but it doesn't work with any combination. The closest to sequencial results I've got with private(d). Any ideas? Thanks!

Brice
  • 1,560
  • 5
  • 10
Sim0n
  • 11
  • 1
  • You can't nest parallel blocks. – Shawn Nov 28 '18 at 17:54
  • 2
    There are a lot of questions and answers here on SO on the topic of *OpenMP nested loops*. Do a little more research, you may conclude that parallelisation of just the outer loop is the right way to go. – High Performance Mark Nov 28 '18 at 18:04
  • I know that I can't nest blocks, but I want to do two options where - one: paralalize only outer loop - two: paralalize only inner loop. And I have problem with the second one – Sim0n Nov 28 '18 at 18:09
  • You will need to implement a reduction scheme to parallelize the inner loop. See for instance [there](https://stackoverflow.com/questions/53410585/openmp-c-program-run-slower-than-sequential-code/53412396#53412396). As mentioned, there are *many* questions on the subject. – Brice Nov 29 '18 at 10:59

1 Answers1

0

Parallelizing the inner loop should be enough to consume all available threads. If you parallelize both it would only generate overhead.

Gabriel Garcia
  • 584
  • 8
  • 13