2
votes

The following code won't run more than one thread. I tried the following with no success (Visual Studio 2015 Update 3):

  • enabled support for OpenMP (Configuration Properties -> C/C++ -> Language -> Open MP Support -> Yes (/openmp))
  • defined the OMP_NUM_THREADS environment variable in Visual Studio (Configuration Properties -> Debugging -> Environment -> OMP_NUM_THREADS=4)
  • defined the OMP_NUM_THREADS environment variable in Windows
  • called omp_set_num_threads(4) within the code
  • tried using #pragma omp parallel num_threads(4)

I haven't been able to get more than one thread.

#include <iostream>
#include <omp.h>

int main(int argc, char* argv[])
{

    int nb_threads = omp_get_max_threads();
    printf(">> omp_get_max_thread()\n>> %i\n", nb_threads);

    omp_set_num_threads(4);
    printf(">> omp_set_num_threads(4)\n");

    nb_threads = omp_get_num_threads();
    printf(">> omp_get_num_threads()\n>> %i\n", nb_threads);

    int id;
    #pragma omp parallel
    {
        id = omp_get_thread_num();
        printf(">> omp_get_thread_num()\n>> %i\n", id);
    }
    return 0;
}

The output is:

>> omp_get_max_thread()
>> 8
>> omp_set_num_threads(4)
>> omp_get_num_threads()
>> 1
>> omp_get_thread_num()
>> 0
2
I don't see you doing any operations that would actually benefit from (or could) run on multiple threads, so what do you actually expect? A call to printf is not parellalizable.. Openmp is not magic that will automatically make your code faster (nor will using multiple threads) - there has to be some parallelism to exploit (and the overhead of creating threads has to be worth it compared to single thread execution)Jesper Juhl
@JesperJuhl, I am already aware that this code doesn't benefit from multi-threading. This exercise is for the sake of the demonstration which I found in many OpenMP tutorial such as this one (youtu.be/FQ1k_YpyG_A?t=3m30s). I expected, by explicitly asking for 4 threads with the call to "omp_set_num_threads(4)", to output four times the printf statement with the corresponding ID of the thread. I also tried with "#pragma omp parallel for" with a parallelizable for loop with no success before posting.SBD
Note that I would use #pragma omp parallel private(id) and do printf in a critical region.Z boson
Note that calling omp_get_num_threads() outside of a parallel region I think is pointless. Do this call inside a parallel region.Z boson

2 Answers

3
votes

The problem is that you only have a single id, and access to it is serialized. By giving each thread access to its own id, they can execute in parallel, so we want to change the pragma to:

#pragma omp parallel private(id)

This way, access to id isn't serialized, and each thread can execute on its own, so we get output like this:

>> omp_get_max_thread()
>> 4
>> omp_set_num_threads(4)
>> omp_get_num_threads()
>> 1
>> omp_get_thread_num()
>> 0
>> omp_get_thread_num()
>> 3
>> omp_get_thread_num()
>> 2
>> omp_get_thread_num()
>> 1
0
votes

You should use the compiler option /openmp to enable #pragma omp in source code.

If /openmp is not specified in a compilation, the compiler ignores OpenMP clauses and directives.