I created an empty C project using Visual Studio 2019 in Windows 11.
Then I ran this minimal piece of code to test multi-threading, using OpenMP:
#include <iostream>
#include <omp.h>
int main() {
omp_set_dynamic(0); // ensure that the number of threads doesn't change due to system demands
omp_set_num_threads(16);
std::cout << "Max threads: " << omp_get_max_threads() << "\n";
int id;
#pragma omp parallel private(id)
{
std::cout << "Number of threads running in parallel: " << omp_get_num_threads() << "\n";
std::cout << "Thread ID in parallel region: " << omp_get_thread_num() << "\n";
}
return 0;
}
This is the output I am getting:
Max threads: 16
Number of threads running in parallel: 1
Thread ID in parallel region: 0
I first tried multithreading in a CMake project (used CMake 3.22 and the modern way of including OpenMP in a CMake project) and attempted using both the Visual Studio 2019 and 2022 compilers. The result was the same, I couldn't get more than 1 thread to run in parallel. So, then I tried this simple example in Visual Studio 2019 and ran into the same issue.
I'm wondering if there is some Windows rule/setting that restricts the number of threads I can use. Btw, using Matlab 2021b and the Parallel Computing toolbox, I was able to use multithreading successfully (8/16 threads).
If you need any other information about the issue/my system, don't hesitate to ask it from me.
Thank you.
Edit:
I get the following output from CMake
"C:\Program Files\JetBrains\CLion 2022.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2022.1.1/bin/ninja/win/ninja.exe" -G Ninja -S C:\Users\parvanitis\Documents\MBI_code\cpp\ImagingFocus -B C:\Users\parvanitis\Documents\MBI_code\cpp\ImagingFocus\cmake-build-debug
- The CXX compiler identification is MSVC 19.32.31332.0
- The CUDA compiler identification is NVIDIA 11.6.124
- Detecting CXX compiler ABI info
- Detecting CXX compiler ABI info - done
- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe
- skipped
- Detecting CXX compile features
- Detecting CXX compile features - done
- Detecting CUDA compiler ABI info
- Detecting CUDA compiler ABI info - done
- Check for working CUDA compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6/bin/nvcc.exe - skipped
- Detecting CUDA compile features
- Detecting CUDA compile features - done
- Found Matlab: C:/Program Files/MATLAB/R2022a/extern/include (found version "9.12") found components: MAT_LIBRARY
- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.36.1.windows.1")
- Found OpenMP_CXX: -openmp (found version "2.0")
- Found OpenMP: TRUE (found version "2.0")
- Configuring done
- Generating done
- Build files have been written to: C:/Users/parvanitis/Documents/MBI_code/cpp/ImagingFocus/cmake-build-debug
[Finished]
CodePudding user response:
OK, real rookie mistake(s) here.
First of all, I didn't add the /openmp flag to the CMAKE_CXX_FLAGS which is necessary in Windows (I thought find_package(OpenMP) was enough, while adding -fopenmp, as in Linux, was not recognized, so I skipped it).
Even after I did, it didn't work. The reason was that the project I was building was also using CUDA and I was calling the OpenMP-multithreaded code from a .cu file, which is of course compiled using NVCC, which is ignorant of the CXX flags, and most likely does not have any reason to support them awyway. So, I needed to move the OpenMP-multithreaded code to a .cpp file in order for it to work. I didn't mention my project was also using CUDA in the first place because I didn't think that would cause a problem (silly me).
Thanks for your help anyway Jérôme.