/* nag_omp_get_thread_num (x06adc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 25, 2014.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagx06.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 
   */

  #pragma omp parallel num_threads(num_req) private(in_para, me, num_got) \
          default(none)
  {
    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;
}