
/*      TESTING OF THE function fwrite and fread       
        FOR DISTRIBUTED ARRAY A[N][M].  
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define N 8
#define M 16
#define NL 1000
static void wrre2201();
static void wrre2202();
static void wrre2203();

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

int main(int argc, char *argv[])
{
   printf("===START fwrre22========================\n");
      wrre2201();
      wrre2202();
      wrre2203();

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


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



      nloopi=NL;
      nloopj=NL;

      #pragma dvm region out(A,B) 
      {
    #pragma dvm parallel ([i][j] on A[i][j])
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
        {  A[i][j] = NL+i+j;
           B[i][j]=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*M, fp)!=N*M)
     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*M, fp)!=N*M)
        {
        if(feof(fp)) printf("PREMATURE END OF FILE ARWRRE01");
        else printf("ERROR READING FILE ARWRRE01\n");
  }
     fclose(fp);
     remove("ARWRRE01");
                    
     #pragma dvm parallel ([i][j] on A[i][j])  reduction(min(nloopi),min(nloopj))
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
              if (A[i][j] !=B[i][j]) 
            {
                if (nloopi > i) nloopi = i;
                if (nloopj > j) nloopj = j;
            }
    if (nloopi == NL && nloopj == NL)
        ansyes(tname);
    else
        ansno(tname);

      return ;

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


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



      nloopi=NL;
      nloopj=NL;
      #pragma dvm region out(A,B) 
      {
    #pragma dvm parallel ([i][j] on A[i][j])
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
        {  A[i][j] = NL+i+j;
           B[i][j]=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*M, 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*M, fp);
//     printf("NUMBER1=%d\n",K);
     fclose(fp);
     dvmh_remove_local("ARWRRE02_%04d");
                    
    #pragma dvm parallel ([i][j] on A[i][j])  reduction(min(nloopi))
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
              if (A[i][j] !=B[i][j]) 
            {
                if (nloopi > i) nloopi = i;
                if (nloopj > j) nloopj = j;
            }
    if (nloopi == NL && nloopj == NL)
        ansyes(tname);
    else
        ansno(tname);

      return ;

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


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



      nloopi=NL;
      nloopj=NL;
      #pragma dvm region out(A,B) 
      {
    #pragma dvm parallel ([i][j] on A[i][j])
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
        {  A[i][j] = NL+i+j;
           B[i][j]=777;

        }
      } /*end region*/  

      #pragma dvm get_actual(A,B) 

     if((fp=fopen("ARWRRE03_%04d", "wbp"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE03_%%04d\n");
     exit(1);
     }

     K=fwrite(A, sizeof(int), N*M, fp);
//     printf("NUMBER=%d\n",K);
     fclose(fp);
     
     if ((fp=fopen("ARWRRE03_%04d", "rbp"))==NULL) {
     printf("ERROR OPENING FILE ARWRRE02_%%04d\n");
     exit(1);
     }
//     rewind(fp);
     K=fread(B, sizeof(int), N*M, fp);
//     printf("NUMBER1=%d\n",K);
     fclose(fp);
     remove("ARWRRE03_%04d");
                    
    #pragma dvm parallel ([i][j] on A[i][j])  reduction(min(nloopi))
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
              if (A[i][j] !=B[i][j]) 
            {
                if (nloopi > i) nloopi = i;
                if (nloopj > j) nloopj = j;
            }
    if (nloopi == NL && nloopj == 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 ;
}
