Just parallelising a code, and the code needs to detect the number of threads that are available. There are a few things to consider at this point.
- To determine that the code is being compiled under OpenMP, check the _OPENMP #define. One of the benefits of using OpenMP is that the same code base can be used to generate serial and parallel versions of the code. The _OPENMP #define separates out the parallel specific code.
- The call to find the maximum number of threads that are available to do work is omp_get_max_threads() (from omp.h). This should not be confused with the similarly named omp_get_num_threads(). The 'max' call returns the maximum number of threads that can be put to use in a parallel region. The 'num' call returns the number of threads that are currently doing something. There's a big difference between the two. In a serial region omp_get_num_threads will return 1; in a parallel region it will return the number of threads that are being used.
- The call omp_set_num_threads will set the maximum number of threads that can be used (equivalent to setting the OMP_NUM_THREADS environment variable). The name of this call does not make it any easier to remember the call to get the (maximum) number of threads available.