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
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 Juhlomp_get_num_threads()
outside of a parallel region I think is pointless. Do this call inside a parallel region. – Z boson