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

NAG CL Interface Introduction
Example description
/* nag_omp_get_thread_num (x06adc) 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 in_para, me, num_got;
#ifdef _OPENMP
  Integer num_req = 5;
#endif

  /* Output preamble */
  printf("nag_omp_get_thread_num (x06adc)");
  printf(" Example Program Results\n\n");

  /*
   * nag_omp_get_thread_num (x06adc).
   * Get the OpenMP thread number
   */
  me = nag_omp_get_thread_num();
  printf("\n%s %11" NAG_IFMT " \n\n", "Thread number:            ", me);

  /*
   * nag_omp_in_parallel (x06afc).
   * Check whether we are in an active parallel region
   */
  in_para = nag_omp_in_parallel();
  printf("\n%s %11" NAG_IFMT " \n\n", "In active parallel region:", in_para);
  fflush(stdout);

  /*
   * Spawn an OpenMP parallel region, have the master thread display the
   * number of threads and check whether we are in an active parallel region
   */

#ifdef _OPENMP
#pragma omp parallel num_threads(num_req) private(in_para, me,                 \
                                                  num_got) default(none)
#endif
  {
    me = nag_omp_get_thread_num();
    /*
     * nag_omp_get_num_threads (x06abc).
     * Get the number of OpenMP threads in the current team
     */
    num_got = nag_omp_get_num_threads();
    in_para = nag_omp_in_parallel();

    if (me == 0) {
      printf("\n%s %11" NAG_IFMT " \n\n",
             "Number of threads:        ", num_got);
      printf("\n%s %11" NAG_IFMT " \n\n",
             "In active parallel region:", in_para);
    }
  }
  fflush(stdout);

  return exit_status;
}