/* -----------------------------------------------------!
! The current code has two purposes:                    !
! (1) using Intel and PGI compilers and see how         !
!     OMP_NUM_THREADS was set by default.               !  
! (2) demonstrate how private variables behave outside  !
!     of parallell regions.                             !
!-------------------------------------------------------!
! Xiaoxu Guan at LSU HPC.                               !
! March 07, 2016                                        !
!----------------------------------------------------- */

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define nsize 410


/* global variables */
int id, istart,iend;
double array_a[nsize-1],array_b[nsize-1],array_c[nsize-1];
#pragma omp threadprivate (id,istart,iend) 
/* end of global variables */


int main () {

int i,nothread,nchunk;


   for(i = 0; i < nsize; i++ ) 
   {
      array_a[i] = (double) i;
      array_b[i] = (double) (i+i);
   }


#pragma omp parallel
    {
      nothread = omp_get_num_threads();
      id = omp_get_thread_num();
      nchunk = (double)(nsize) / (double)(nothread);
      istart = 1 + id*nchunk;
      iend = istart + nchunk - 1;
      if(id == nothread-1) {
         istart = 1 + id*nchunk;
         iend = nsize;
         nchunk = iend - istart + 1;
         printf("Hello from %5d %5d %5d\n", id, istart, iend);
        }
    }


       printf("Hello from master. id = %d\n", id);


#pragma omp parallel 
    {
       printf("Hello from %5d %5d %5d\n", id, istart, iend);
    }


}



