Home c++ Maximum number of threads on C++ OpenMP is always equal to 1

Maximum number of threads on C++ OpenMP is always equal to 1

Author

Date

Category

set the task to write a pair of C++ algorithms using OpenMP.
Since I have MacBook, I had to install the IDE CLION and work with it.

Main.CPP code:

# include & lt; iostream & gt;
#Include "/usr/local/opt/libomp/include/omp.h"
#Define Thread_Num 4.
INT MAIN ()
{
  OMP_SET_NUM_THREADS (Thread_NUM);
  STD :: COUT & LT; & LT; OMP_GET_THREAD_LIMIT & LT; & LT; STD :: ENDL;
#Pragma Omp Parallel for Num_Threads (4)
  {
    STD :: COUT & LT; & LT; "Number of Available Threads:" & lt; & lt; OMP_GET_NUM_THREADS () & lt; & lt; STD :: ENDL;
    STD :: COUT & LT; & LT; "Current Thread Number:" & lt; & lt; OMP_GET_THREAD_NUM () & lt; & lt; STD :: ENDL;
    STD :: COUT & LT; & LT; "Hello, World!" & lt; & lt; STD :: ENDL;
  }
  Return 0;
}

Code CMakeLists.txt:

cmake_minimum_required (version 3.14)
Project (OpenMpTest)
set (cmake_cxx_standard 17)
Add_Executable (OpenMPTest main.cpp)
Find_Package (OpenMP Required)
target_link_librursees ($ {Project_name} $ {openmp_cxx_libraries})

When compiling, always the following:

1
Number of Available Threads: 1
Current Thread Number: 0
Hello, WORLD!

I can not understand the reason why the number of threads does not increase in accordance with the value that I try to ask him. Immediately I will say that before that I did not work with C++, nor with OpenMP.

OS: MacOS Catalina Beta version 10.15.6 (19G36E)


Answer 1, Authority 100%

You are using the OMP PARALLEL FOR directive, but you do not have a for cycle there. What do you expect to get it? Remove the word for and you will be happy.

# include & lt; iostream & gt;
#Include "/usr/local/opt/libomp/include/omp.h"
// #Include & lt; omp.h & gt;
#Define Thread_Num 4.
INT MAIN ()
{
  OMP_SET_NUM_THREADS (Thread_NUM);
  STD :: COUT & LT; & LT; OMP_GET_THREAD_LIMIT () & lt; & lt; STD :: ENDL;
#Pragma Omp Parallel Num_Threads (4)
  {
    STD :: COUT & LT; & LT; "Number of Available Threads:" & lt; & lt; OMP_GET_NUM_THREADS () & lt; & lt; STD :: ENDL;
    STD :: COUT & LT; & LT; "Current Thread Number:" & lt; & lt; OMP_GET_THREAD_NUM () & lt; & lt; STD :: ENDL;
    STD :: COUT & LT; & LT; "Hello, World!" & lt; & lt; STD :: ENDL;
  }
  Return 0;
}

An example was collected using Mingw-W64 under Win10; Command Line: G ++ test2.cpp -o test2 -o2 -fopenmp

PS: More, you did not write brackets from the OMP_GET_THREAD_LIMIT () function and because of this you did not receive the appropriate number.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions