NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_omp_set_num_threads (x06aac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {

  /*  Scalars */
  Integer exit_status = 0;
  Integer num, num_max, num_set;

  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  /* Output preamble */
  printf("nag_omp_set_num_threads (x06aac)");
  printf(" Example Program Results\n\n");

  num_set = 5;

  /*
   * nag_omp_set_num_threads (x06aac).
   * Set the OpenMP Internal Control Variable (ICV) controlling the number
   * of threads
   */
  nag_omp_set_num_threads(num_set, &fail);

  if (fail.code != NE_NOERROR) {
    printf("Error from nag_omp_set_num_threads (x06aac).\n%s\n", fail.message);
    fflush(stdout);
    exit_status = 1;
    goto END;
  }

  /*
   * nag_omp_get_max_threads (x06acc).
   * Retrieve the value of the ICV
   */
  num_max = nag_omp_get_max_threads();
  printf("\n%s %11" NAG_IFMT " \n\n",
         "Value of ICV controlling the number of threads:", num_max);

  /*
   * nag_omp_get_num_threads (x06acc).
   * Get the number of threads in the current team
   */
  num = nag_omp_get_num_threads();

  printf("\n%s %11" NAG_IFMT " \n\n",
         "Number of threads outside the parallel region: ", num);

  /*
   * Spawn an OpenMP parallel region and have the master thread display
   * the number of threads in the current team
   */

#ifdef _OPENMP
#pragma omp parallel private(num) default(none)
#endif
  {
    num = x06abc();
#ifdef _OPENMP
#pragma omp master
#endif
    {
      printf("\n%s %11" NAG_IFMT " \n\n",
             "Number of threads inside the parallel region:  ", num);
    }
  }
  fflush(stdout);

END:
  return exit_status;
}