Search code examples
c++openmp

How to determine the maximum number of threads?


I am learning parallel programming. I use OpenMP with C++ and I want to understand how to determine the maximum number of parallel threads I can set on my laptop. In the documentation I found the function omp_get_max_threads(), but how can I make sure the return value is correct?

When I'm compiling this code:

#include <omp.h>
#include <iostream>
using namespace std;
int main()
{
   cout << omp_get_max_threads() << "\n";
    
#pragma omp parallel 
        {

           
        }
        return 0;

}

Output is: 8

When this:

#include <omp.h>
#include <iostream>
using namespace std;
int main()
{

    
#pragma omp parallel 
        {
            cout << omp_get_max_threads();
           
        }
        return 0;

}

Output is: 88888888


Solution

  • When you want to find out how many threads you are able to use. You can do this by calling std::thread::hardware_concurrency().