/***************************************************************************/
/* AUTHOR : Hasan Timucin OZDEMIR                                          */
/* DATE   : October, 1995                                                  */
/* PROGRAM : ASSIGN1.M                                                     */
/* ALGORITHM :                                                             */
/*             1. Get number of processor array dimension                  */
/*             2. Get number of iteration per processor n                  */
/*             3. set value of each processor to 0(zero)                   */
/*             4. each processor finds the value of function where         */ 
/*                x is set to the value calculated according to its        */ 
/*                coordinate in the processor array.                       */
/*             5. each processor updates its value by using new calculated */
/*                value.                                                   */ 
/*             6. Mesh Collapse algorithm to sum the results coming from   */
/*                each processor.                                          */
/***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<reduce.h>
#include<mpl.h>
#include<ctype.h>

#define square(x) ((x)*(x))

float TotalValue;
int k,l,dimen,iteration_num;
plural double value;
plural double x;
plural int i;
double n;

void main(void)
{  
   double when();
   double start,finish;
   double dimen2;
   char intvariable[8];
 
   TotalValue = 0.0;
   iteration_num=6;
   dimen=4;

   /* Read Number Of Processor */
   printf("\n Enter Number of Processor =");
   scanf("%s",&intvariable);
   dimen = atoi(intvariable);
   dimen=(dimen>100?100:dimen);

   /* Read Number of Iterations Per Process */
   printf("\n Enter Number of iteration =");
   scanf("%s",&intvariable);
   iteration_num = atoi(intvariable);

   start = when();

   value = (double) 0;
   dimen2= (double) dimen*dimen;
   n = dimen2 * iteration_num; /* find the number of total iteration */

   if((ixproc < dimen ) && ( iyproc < dimen ) ) /* Select the area */
   { 
     for(i=0;i<iteration_num;i++)
     { x = dimen2 *i + (double) dimen * iyproc + 1.0 + ixproc ; 

       /* for(l=0;l<dimen;l++)
         for(k=0;k<dimen;k++)
           printf("\n %d=x, %d=y, x_value=%12.4f",k,l,proc[l][k].x); 
       */
       /* add every processor's value onto it */ 
       value = value + (n*4.0) / ( square(n) + square(x+0.5) );

       /* for(l=0;l<dimen;l++)
         for(k=0;k<dimen;k++)
            printf("\n %d=x, %d=y, value=%f",k,l,proc[l][k].value); 
       */
       /* Total Value for this iteration 
       TotalValue=0.0; 
       for(l=0;l<dimen;l++)
         for(k=0;k<dimen;k++)
            { printf("\n %f + %f =", TotalValue,proc[l][k].value);
              TotalValue = TotalValue + proc[l][k].value;
              printf(" %f ", TotalValue);
            }
      */
     }/* end of for */
   }/* end of if */


  TotalValue=0.0; 
  for(l=0;l<dimen;l++)
    for(k=0;k<dimen;k++)
          TotalValue = TotalValue + proc[l][k].value;

          printf("\n %f + %f =", TotalValue,proc[l-1][k-1].value);

   /* Mesh Collapse  */
   for(i=1;i<dimen;i++)
     if((iyproc<dimen) && (ixproc==i)) value += xnetE[1].value;

   for(i=1;i<dimen;i++)
     if((ixproc==dimen-1) && (iyproc==i)) value += xnetN[1].value;

   printf("\n After %d iteration, the approximated value is %f ",iteration_num*dimen*dimen,proc[dimen-1][dimen-1].value);
   finish = when();

   printf("\n Total Elapsed time is %f seconds. \n",finish-start);

}/* end of main */

double when() /* time function */
{ struct timeval tp;
  struct timezone tzp;
  gettimeofday(&tp, &tzp);
  return( (double) tp.tv_sec + (double) tp.tv_usec * 1e-6 ) ;
}/* end of when() */


