
/*      TESTING OF THE function fprintf and fscanf       
        FOR DISTRIBUTED ARRAY A[N].  
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define N 8
#define NL 1000
static void prsc1201();

static void ansyes(char tname[]);
static void ansno(char tname[]);

int main(int argc, char *argv[])
{
   printf("===START fpsc12========================\n");
      prsc1201();

      printf("=== END OF fpsc12 ========================= \n");
      return 0;
}
/* ---------------------------------------------prsc1201  */
      void prsc1201()
{


      char tname[]="FPRINT_FSCANF_1201";
      int i,nloopi,ni,nb,na;
      FILE *fp;
                
      #pragma dvm array distribute[*]          
      int A[N];
      #pragma dvm array align ([i] with A[i])
      int B[N];



      nloopi=NL;

      #pragma dvm region out(A,B) 
      {
    #pragma dvm parallel ([i] on A[i])
      for (i=0;i<=N-1;i++)
        {  A[i] = NL+i;
           B[i]=777;
        }
      } /*end region*/  

      #pragma dvm get_actual(A,B) 

     if((fp=fopen("ARFPSC12", "wb"))==NULL) {
     printf("ERROR OPENING FILE ARFPSC12\n");
     exit(1);
     }
        for (i=0;i<=N-1;i++) {
       #pragma dvm remote_access(A[i])
        {  
         na=A[i];
        }   
        ni=fprintf(fp, "%d ", na);  
        }
     fclose(fp);
     
     if ((fp=fopen("ARFPSC12", "rb"))==NULL) {
     printf("ERROR OPENING FILE ARFPSC12\n");
     exit(1);
     }
//     rewind(fp);
       
      for (i=0;i<=N-1;i++)
      {
      ni=fscanf(fp, "%d ",&nb);
      B[i]=nb;
      }      
     fclose(fp);
     remove("ARFPSC12");                
    #pragma dvm parallel ([i] on A[i])  reduction(min(nloopi))
      for (i=0;i<=N-1;i++)
          {
              if (A[i] !=B[i]) 
                if (nloopi > i) nloopi = i;
          }
//    printf ("nloopi=%d\n", nloopi);
    if (nloopi == NL )
        ansyes(tname);
    else
        ansno(tname);

      return ;

   
}
      

void ansyes(char name[])
{
      printf ("%s  -  complete\n",name);
    return ;
}
      void ansno(char name[])
{
     printf("%s  -  ***error\n",name);
      return ;
}
