The following program, if my understanding is correct, should create at most 10 threads and run the omp parallel block :
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
omp_set_num_threads(10);
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("%d %d %d\n", id, omp_get_num_threads(), omp_get_max_threads());
}
return 0;
}
The output shows that it was run 12 times. But omp_get_num_threads() = 1
in all of the threads, and somehow omp_get_max_threads = 12
except for the first thread.
output :
0 1 10
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
0 1 12
I have compiled the exact same code with cmd line : g++ -fopenmp .\openMp.c -o prog
in VS code terminal, the output was expected and is correct :
1 10 10
2 10 10
3 10 10
4 10 10
0 10 10
5 10 10
6 10 10
7 10 10
8 10 10
9 10 10
-fopenmp -Xclang
flags added to C/C++ > Additional OptionsNote : I am using a 6 cores CPU with 12 logical processors.
the only thing you need to do in MSVC to enable openmp is to enable it with
this step is equivalent to adding a command line argument /openmp
the issue you see is caused by manually linking libomp.lib
, you should remove that.
you also shouln't pass -fopenmp -Xclang
, as those are clang arguments, not MSVC arguments.
If you are working with multiple compilers and platforms, I recommend you use a build system to automatically set-up your toolchain, CMake has those flags built-in How to set linker flags for OpenMP in CMake's try_compile function, and most IDEs including VSCode and Visual Studio have good support for CMake, and you don't need to learn the quirks of each compiler.