#include <stdio.h>
#include "forkjoin.h"
#include "shared.h"
#include "barrier.h"int main()
{
int pid,nproc,*bar,shmid1,shmid2,shmid3,shmid4,shmid5,shmid6;
int shmid7,size,i,shmid8,shmid9,shmid10,shmid11,shmid12,shmid13;
float *sumy,*sumxy,*sumx2,*a,*b,*yc,ye;
float *x,*xy,dis,*mean,sum,*year,*x2,*y;
printf("Enter the size : ");
scanf("%d",&size);
x = (float *) create_memory(4*size,&shmid1);
y = (float *) create_memory(4*size,&shmid2);
xy = (float *) create_memory(4*size,&shmid3);
mean = (float *)create_memory(4,&shmid4);
bar = (int *) create_memory(2*4,&shmid5);
year = (float *) create_memory(4*size,&shmid6);
x2 = (float *) create_memory(4*size,&shmid7);
sumy = (float *) create_memory(4,&shmid8);
sumxy = (float *) create_memory(4,&shmid9);
sumx2 = (float *) create_memory(4,&shmid10);
a = (float *) create_memory(4,&shmid11);
b = (float *) create_memory(4,&shmid12);
yc = (float *) create_memory(4*size , &shmid13);
*mean = 0;
*sumy=0;
*sumx2=0;
*sumxy=0;
printf("Enter the year and corresponding production :\n");
for(i=0;i<size;i++)
{
printf("%d Year : ",i+1 );
scanf("%f",&year[i]);
printf("Production : ");
scanf("%f",&y[i]);
}
dis = year[2] - year[1];
printf("\nEnter the no of process : \n");
scanf("%d",&nproc);
barrier_init(bar,nproc);
*mean=0;
pid = create_process(&nproc);
sum =0 ;
for(i=pid;i<size;i+=nproc)
sum += year[i];
barrier(bar);
(*mean)+=sum/size;
barrier(bar);
for(i=pid;i<size;i+=nproc)
{
x[i] = x2[i] = xy[i] = 0;
x[i] = (year[i] - (*mean))/dis;
x2[i] = x[i]*x[i];
xy[i] = x[i] * y[i];
(*sumy) += y[i];
(*sumxy) += xy[i];
(*sumx2) += x2[i];
}
barrier(bar);
if(pid == 0)
{
*a = (*sumy)/size;
*b = (*sumxy)/(*sumx2);
}
barrier(bar);
for(i=pid;i<size;i+=nproc)
{
yc[i] = (*a) + ((*b)*(x[i]));
}
join_process(&nproc,&pid);
printf("\nYear\ty\tx\tx2\txy\tyc \n" );
printf("\n-----------------------------------------------
---------------------\n");
for(i=0;i<size;i++)
{
printf("%f\t %f\t %f \t %f\t %f \t %f \n",year[i],
y[i],x[i],x2[i],xy[i],yc[i]);
}
printf("\ny = %f\t xy = %f\t x2 = %f\t a = %f\t b = %f \t",
*sumy,*sumxy,*sumx2,*a,*b);
printf("Enter the Year for finding the treand value : ");
scanf("%f",&ye);
x[size] =0;
yc[size] =0;
x[size] = (ye - (*mean))/dis;
yc[size] = (*a) + ((*b)*(x[size]));
printf("\nThe Trend Values for Year %f is %f ",ye,yc[size]);
printf("\n");
cleanup_memory(&shmid1);
cleanup_memory(&shmid2);
cleanup_memory(&shmid3);
cleanup_memory(&shmid4);
cleanup_memory(&shmid5);
cleanup_memory(&shmid6);
cleanup_memory(&shmid7);
cleanup_memory(&shmid8);
cleanup_memory(&shmid9);
cleanup_memory(&shmid10);
cleanup_memory(&shmid11);
cleanup_memory(&shmid12);
cleanup_memory(&shmid13);
return 0;
}
/* :: OUTPUT ::
Enter the size : 9
Enter the year and corresponding production :
1 Year : 1995
Production : 10
2 Year : 1996
Production : 12
3 Year : 1997
Production : 11
4 Year : 1998
Production : 13
5 Year : 1999
Production : 14
6 Year : 2000
Production : 17
7 Year : 2001
Production : 18
8 Year : 2002
Production : 15
9 Year : 2003
Production : 17
Enter the no of process :
3
Year y x x2 xy yc
-----------------------------------------------------------------
1995.000000 10.000000 -4.000122 16.000977 -40.001221 10.44536619
1997.000000 11.000000 -2.000122 4.000488 -22.001343 12.278182
1998.000000 13.000000 -1.000122 1.000244 -13.001587 13.194591
1999.000000 14.000000 -0.000122 0.000000 -0.001709 14.110999
2000.000000 17.000000 0.999878 0.999756 16.997925 15.027407
2001.000000 18.000000 1.999878 3.999512 35.997803 15.943815
2002.000000 15.000000 2.999878 8.999268 44.998169 16.860224
2003.000000 17.000000 3.999878 15.999023 67.997925 17.776632
-----------------------------------------------------------------
y = 127.000000 xy = 54.984497 x2 = 60.000000
a = 14.111111 b = 0.916408
Enter the Year for finding the treand value : 2004
The Trend Values for Year 2004.000000 is 18.693041 */