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

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

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

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


      char tname[]="FPRINT_FSCANF_2101";
      int i,j,nloopi,nloopj,ni,nb,na;
      FILE *fp;
                
      #pragma dvm array distribute[block][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("ARFPSC21", "wb"))==NULL) {
     printf("ERROR OPENING FILE ARFPSC21 \n");
     exit(1);
     }
        for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
       {
       #pragma dvm remote_access(A[i][j])
        {  
         na=A[i][j];
        }   
        ni=fprintf(fp, "%d ", na);  
        }
     fclose(fp);
     
     if ((fp=fopen("ARFPSC21", "rb"))==NULL) {
     printf("ERROR OPENING FILE ARFPSC21 \n");
     exit(1);
     }
//     rewind(fp);
       
      for (i=0;i<=N-1;i++)
          for (j=0;j<=M-1;j++)
      {
      ni=fscanf(fp, "%d ",&nb);
      B[i][j]=nb;
      }      
     fclose(fp);
     remove("ARFPSC21");                    
    #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 ;
}
