1

I'm trying to parallelise some code with OpenMP in C, where I have 2 nested loops. I'm wondering if it's feasible to parallelise my inner loop.

#pragma omp parallel for
for (int i = A; i < B; i++)
{
        int number_of_multiples= 0;
        for (int j = 1; j < i; j++)
            if (is_multiple(i, j))
                number_of_multiples++;
        ... 
}

I've looked into collapse() but I don't think that's feasible here, given I need to initialise number_of_multiples outside the inner loop so that I can calculate the number_of_multiples for each iteration of the outer loop.

Additionally, since the inner loop's condition depends on i, I'm unsure if it's even appropriate to parallelise?

bleuj
  • 151
  • 6
  • 2
    As long as `B - A` is bigger (ideally much bigger) than your number of processors or hyperthreads, trying to parallelize the inner loop wont help. What you should do is make sure that the differing workload between loop iterations doesn't become a load balancing problem (some threads taking much longer than others). To do that you should specify e.g. dynamic scheduling of threads: `schedule(dynamic)`. – paleonix Feb 04 '22 at 10:14
  • 2
    In the case that `B - A` is rather small relative to the number of processors, but `A` an `B` themselves can be quite big, you might want to only parallelize the inner loop. Depending on the OpenMP implementation you might have to still start the `parallel` section outside the outer loop to cut down on thread creation overhead (I think many implementations nowadays will keep a threadpool alive either way, so the benefit might not be big). – paleonix Feb 04 '22 at 10:20
  • 1
    Yes, it can be compiled with OpenMP 5.0 (e.g. using clang, but not with gcc 11.2 which supports OpenMP 4.5 only). For more details please check [this](https://stackoverflow.com/questions/68382400/is-collapse-clause-with-non-rectangular-loops-allowed-by-the-openmp-5-1-spec). Regarding the performance I have doubts about it will be faster than suggestions mentioned by @paleonix above. Also note that reversing the first loop can be advantageous, because the load balance could be much better especially if you use `guided` schedule. – Laci Feb 04 '22 at 10:51

0 Answers0