Tarta
Tarta

Reputation: 2063

OpenMP #pragma, only one thread working on my code

I am working on a code and I am trying to use more threads according to the guide I have been provided with. It says: "Parallelism is achieved using an OpenMP #pragma to dynamically allocate rows of the image to different threads, with a thread for each processor or core." And here it is in the code:

#pragma omp parallel for schedule(dynamic, 1) // OpenMP

for (int y = 0; y<height; y++){                       // Loop over image rows
    fprintf(stderr, "\rRendering (%d spp) %5.2f%%", samps, 100.*y / (height - 1));
    for (unsigned short x = 0, rng[3] = { 0, 0, y*y*y }; x<width; x++) {  // Loop cols
    ...

I tried to change the number of working threads according to this..

I have been looking in the manual, trying to find a solution. I found out that schedule is used to define how iterations of the loop are divided among the threads. So "dynamic" is used when the loop iterations are divided into chunks whose size is up to me. Since in this line of code the size was set to "1" I tried to change it to a way bigger number but the result doesn't change and still only 1 thread is used.

So I tried to set it to "static" instead but still no improvement.

I tried the "AUTO" option, in such a way that the scheduling session would have been up to the compiler.. still no solution.

I also found out that private(r) means that the variable r is private to each thread and not shared but that actually doesn't change the final outcome.

The compiler is cl.exe.. I am using Visual Studio 2013. Any way to increase the number of threads?

Upvotes: 1

Views: 2553

Answers (1)

Gilles
Gilles

Reputation: 9489

In OpenMP, there are several ways of defining the number of threads to use in a parallel region:

  • The main one is the environment variable OMP_NUM_THREADS. To use it, set it in the code's environment prior to running it.
  • The function omp_set_num_threads(). To use it, call it before reaching a parallel region.
  • The num_threads() clause of the parallel compiler directive.

The relative priority of these is defined by the standard and pretty much boils down to num_threads() taking precedence to omp_set_num_threads() which takes precedence to OMP_NUM_THREADS. If neither of the three is used, then the behaviour is implementation define. This can be for example, launching only one thread, or launching as many threads as there are of 'cores' on the machine, or something else...

That said, are you sure you enabled the OpenMP support at your compiler's level?

Upvotes: 1

Related Questions