Aim: Using the concept of Barrier, write a Program to Compute the Absolute Deviation of an Array of Numbers.
MSc IT Parallel Processing Practical No. 5
Index of all Practicals ~ Click Here
Code:
Output:
MSc IT Parallel Processing Practical No. 5
Index of all Practicals ~ Click Here
Code:
#include<stdio.h>
#include"shmlib.h"
#include<math.h>
int main()
{
int *arr,i,j,k,id1,id2,sum,id,m,n,numProc;
int *procArr;
float *devArr;
int *finalsum;
float finalDev;
int lag,dev;
int id3,id4,id5,idd,id6,id7;
float *avg;
int *barr,*lock1,*total;
float finDev;
printf("Enter the no of elements in the array");
scanf("%d",&m);
finDev=0.0;
arr=(int *)shared(malloc(sizeof(int)*m),&id1);
avg=(float *)shared(malloc(sizeof(float)),&id3);
barr=(int *)shared(malloc(sizeof(int)),&id4);
lock1=(int *)shared(sizeof(int),&id5);
total=(int *)shared(malloc(sizeof(int)),&id6);
finalsum=(int *)shared(sizeof(int),&id7);
for(i=0;i<m;i++)
{
arr[i]=i+1;
printf("%d\n",arr[i]);
}
printf("How many process");
scanf("%d",&numProc);
barr=barrier_init(numProc+1);
procArr=(int *)shared(malloc(sizeof(int)*numProc),&id2);
devArr=(float *)shared(malloc(sizeof(float)*numProc),&id3);
id=create_process(numProc-1);
lag=(m/numProc);
for(i=id*lag;i<(id+1)*lag;i++)
{
procArr[id]=procArr[id]+arr[i];
printf("\nProcess no %d and the vlaue is %d",id,procArr[id]);
}
join_process(numProc);
goto abc;
barrier(barr);
abc:
*finalsum=0;
for(i=0;i<numProc;i++)
{
printf("\nSum for each process is %d == %d",i,procArr[i]);
*finalsum=*finalsum+procArr[i];
}
printf("\nFinal sum is %d ",*finalsum);
*avg=*finalsum/numProc;
printf("The value of average is %f",*avg);
printf("Hi");
idd=create_process(numProc-1);
for(i=idd*lag;i<(idd+1)*lag;i++)
{
devArr[idd]=(*avg-arr[i])*(*avg-arr[i]);
}
join_process(numProc,idd);
finalDev=0.0;
for(i=0;i<numProc;i++)
{
printf(" \n\ndev pointer %d == %f ",i,devArr[i]);
finalDev=finalDev+devArr[i];
}
printf("\n\nFinal Deviation is %f ",finalDev);
finalDev=finalDev/m;
printf("\n\nFinal Deviation is %f ",finalDev);
finalDev=sqrt(finalDev);
printf("Final deviation is given by %f",finalDev);
clean_barrier(barr);
}
#include"shmlib.h"
#include<math.h>
int main()
{
int *arr,i,j,k,id1,id2,sum,id,m,n,numProc;
int *procArr;
float *devArr;
int *finalsum;
float finalDev;
int lag,dev;
int id3,id4,id5,idd,id6,id7;
float *avg;
int *barr,*lock1,*total;
float finDev;
printf("Enter the no of elements in the array");
scanf("%d",&m);
finDev=0.0;
arr=(int *)shared(malloc(sizeof(int)*m),&id1);
avg=(float *)shared(malloc(sizeof(float)),&id3);
barr=(int *)shared(malloc(sizeof(int)),&id4);
lock1=(int *)shared(sizeof(int),&id5);
total=(int *)shared(malloc(sizeof(int)),&id6);
finalsum=(int *)shared(sizeof(int),&id7);
for(i=0;i<m;i++)
{
arr[i]=i+1;
printf("%d\n",arr[i]);
}
printf("How many process");
scanf("%d",&numProc);
barr=barrier_init(numProc+1);
procArr=(int *)shared(malloc(sizeof(int)*numProc),&id2);
devArr=(float *)shared(malloc(sizeof(float)*numProc),&id3);
id=create_process(numProc-1);
lag=(m/numProc);
for(i=id*lag;i<(id+1)*lag;i++)
{
procArr[id]=procArr[id]+arr[i];
printf("\nProcess no %d and the vlaue is %d",id,procArr[id]);
}
join_process(numProc);
goto abc;
barrier(barr);
abc:
*finalsum=0;
for(i=0;i<numProc;i++)
{
printf("\nSum for each process is %d == %d",i,procArr[i]);
*finalsum=*finalsum+procArr[i];
}
printf("\nFinal sum is %d ",*finalsum);
*avg=*finalsum/numProc;
printf("The value of average is %f",*avg);
printf("Hi");
idd=create_process(numProc-1);
for(i=idd*lag;i<(idd+1)*lag;i++)
{
devArr[idd]=(*avg-arr[i])*(*avg-arr[i]);
}
join_process(numProc,idd);
finalDev=0.0;
for(i=0;i<numProc;i++)
{
printf(" \n\ndev pointer %d == %f ",i,devArr[i]);
finalDev=finalDev+devArr[i];
}
printf("\n\nFinal Deviation is %f ",finalDev);
finalDev=finalDev/m;
printf("\n\nFinal Deviation is %f ",finalDev);
finalDev=sqrt(finalDev);
printf("Final deviation is given by %f",finalDev);
clean_barrier(barr);
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment