
/*      TESTING OF THE function fwrite and fread       
        FOR LOCAL ARRAY A[N].  
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define N 8
#define NL 1000
static void wrre1201();
static void wrre1202();
static void wrre1203();

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

int main(int argc, char *argv[])
{
   printf("===START fwrre12========================\n");
      wrre1201();
      wrre1202();
      wrre1203();

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


      char tname[]="FWRITE_FREAD_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("ARWRRE01", "wb"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE01\n");
     exit(1);
     }

     if (fwrite(A, sizeof(int), N, fp)!=N)
     printf("ERROR WRITING FILE ARWRRE01\n");
     fclose(fp);
     
     if ((fp=fopen("ARWRRE01", "rb"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE01 \n");
     exit(1);
     }
//     rewind(fp);
     if (fread(B, sizeof(int), N, fp)!=N)
        {
        if(feof(fp)) printf("PREMATURE END OF FILE ARWRRE01");
        else printf("ERROR READING FILE ARWRRE01\n");
  }
     fclose(fp);
     remove("ARWRRE01");
                    
    #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 ;

   
}
      
/* ---------------------------------------------wrre1202  */
      void wrre1202()
{


      char tname[]="FWRITE_FREAD_1202";
      int i,nloopi,ni,nb,na,K;
      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("ARWRRE02_%04d", "wbl"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE02_%%04d\n");
     exit(1);
     }

     K=fwrite(A, sizeof(int), N, fp);
//     printf("NUMBER=%d\n",K);
     fclose(fp);
     
     if ((fp=fopen("ARWRRE02_%04d", "rbl"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE02_%%04d\n");
     exit(1);
     }
//     rewind(fp);
     K=fread(B, sizeof(int), N, fp);
//     printf("NUMBER1=%d\n",K);
     fclose(fp);
     dvmh_remove_local("ARWRRE02_%04d");
                    
    #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 ;

   
}
/* ---------------------------------------------wrre1203  */
      void wrre1203()
{


      char tname[]="FWRITE_FREAD_1203";
      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) 
      {
    #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("ARWRRE03", "wbp"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE01\n");
     exit(1);
     }

     if (fwrite(A, sizeof(int), N, fp)!=N)
     printf("ERROR WRITING FILE ARWRRE01\n");
     fclose(fp);
     
     if ((fp=fopen("ARWRRE03", "rbp"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE01 \n");
     exit(1);
     }
//     rewind(fp);
     if (fread(B, sizeof(int), N, fp)!=N)
        {
        if(feof(fp)) printf("PREMATURE END OF FILE ARWRRE01");
        else printf("ERROR READING FILE ARWRRE01\n");
  }
     fclose(fp);
     remove("ARWRRE03");
                    
    #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 ;
}
