/* nag_omp_set_num_threads (x06aac) 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 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 
   */

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

END:
  return exit_status;
}