#include <stdio.h>
#include "forkjoin.h"
#include "shared.h"
#include "barrier.h"
main()
{
float *x,*xsav,*y,*ysav;
int *imin,*imax,iexcess,size;
int *bar,nproc,i,pid,npts;
int shmid1,shmid2,shmid3,shmid4,shmid5,shmid6,shmid7;
printf("Enter the size of the Array : ");
scanf("%d",&size);
printf("Enter the no of process : ");
scanf("%d",&nproc);
x = (float *)create_memory(4*size,&shmid1);
y = (float *)create_memory(4*size,&shmid6);
xsav = (float *)create_memory(4*size,&shmid2);
ysav = (float *)create_memory(4*size,&shmid7);
imin = (int *)create_memory(2*size,&shmid3);
imax = (int *)create_memory(2*size,&shmid4);
bar = (int *)create_memory(2*4,&shmid5);
printf("Enter the Elements of the Array x :\n");
for(i=1;i<=size;i++)
{
printf("%d Element : ",i);
scanf("%f",&x[i]);
}
printf("\nEnter the Elements of the Array y :\n");
for(i=1;i<=size;i++)
{
printf("%d Element : ",i);
scanf("%f",&y[i]);
}
barrier_init(bar,nproc);
pid = create_process(&nproc);
npts = size/nproc;
iexcess = 0;
iexcess = size-(nproc*npts);
if(iexcess == 0)
{
imax[pid] = imax[pid]=0;
imin[pid] = (pid*npts)+1;
imax[pid] = (pid * npts) + npts;
}
else
{
if(pid<iexcess)
{
imax[pid] = imax[pid]=0;
imin[pid] = pid * (npts + 1) + 1;
imax[pid] = imin[pid] + npts;
}
else
{
imax[pid] = imax[pid]=0;
imin[pid] = (pid * npts) + 1 + iexcess;
imax[pid] = imin[pid] + npts - 1;
}
}
xsav[pid] = x[imax[pid] + 1];
ysav[pid] = y[imax[pid] + 1];
barrier(bar);
for(i=imin[pid];i<imax[pid];i++)
{
x[i] = x[i+1]+y[i];
}
if(imax[pid]!=size)
x[imax[pid]] = xsav[pid]+ysav[pid];
join_process(&nproc,&pid);
printf("\nThe New Array is : \n");
for(i=1;i<=size;i++)
printf("\n%d Element : %f",i,x[i]);
printf("\n");
cleanup_memory(&shmid1);
cleanup_memory(&shmid2);
cleanup_memory(&shmid3);
cleanup_memory(&shmid4);
cleanup_memory(&shmid5);
cleanup_memory(&shmid6);
cleanup_memory(&shmid7);
}
/**************************************************************************
:: OUTPUT ::
Enter the size of the Array : 5
Enter the no of process : 4
Enter the Elements of the Array x :
1 Element : 1
2 Element : 2
3 Element : 3
4 Element : 4
5 Element : 5
Enter the Elements of the Array y :
1 Element : 6
2 Element : 7
3 Element : 8
4 Element : 9
5 Element : 10
The New Array is :
1 Element : 8.000000
2 Element : 11.000000
3 Element : 13.000000
4 Element : 15.000000
5 Element : 5.000000