Aim: Using the concept of Loop Splitting
A) Calculate average of elements
B) Histogram Computation
MSc IT Parallel Processing Practical No. 3
Index of all Practicals ~ Click Here
A) Calculate average of elements.
Code:
B) Histogram Computation
Code:
A) Calculate average of elements
B) Histogram Computation
MSc IT Parallel Processing Practical No. 3
Index of all Practicals ~ Click Here
A) Calculate average of elements.
Code:
#include<stdio.h>
#include"shmlib.h"
int main()
{
float *avg1,*avg2,avg;
int id,id1,id2,sum=0,sum1=0,i;
int num[10];
avg1=(float *)shared(sizeof(float),&id);
avg2=(float *)shared(sizeof(float),&id2);
printf("Enter 10 numbers: \n");
for(i=0;i<10;i++)
{
scanf("%d", &num[i]);
}
id1=create_process(1);
if(id1==0)
{
for(i=0;i<5;i++)
{
sum1=sum1+num[i];
}
*avg1=sum1/5;
}
if(id1==1)
{
for(i=5;i<10;i++)
{
sum=sum+num[i];
}
*avg2=sum/5;
}
join_process(2,id1);
printf("\nOutput of process A is: \t %f",*avg1);
printf("\nOutput of process B is: \t %f",*avg2);
avg=(*avg1+*avg2)/2;
printf("\nFinal Answer:%f\t",avg);
Output:#include"shmlib.h"
int main()
{
float *avg1,*avg2,avg;
int id,id1,id2,sum=0,sum1=0,i;
int num[10];
avg1=(float *)shared(sizeof(float),&id);
avg2=(float *)shared(sizeof(float),&id2);
printf("Enter 10 numbers: \n");
for(i=0;i<10;i++)
{
scanf("%d", &num[i]);
}
id1=create_process(1);
if(id1==0)
{
for(i=0;i<5;i++)
{
sum1=sum1+num[i];
}
*avg1=sum1/5;
}
if(id1==1)
{
for(i=5;i<10;i++)
{
sum=sum+num[i];
}
*avg2=sum/5;
}
join_process(2,id1);
printf("\nOutput of process A is: \t %f",*avg1);
printf("\nOutput of process B is: \t %f",*avg2);
avg=(*avg1+*avg2)/2;
printf("\nFinal Answer:%f\t",avg);
B) Histogram Computation
Code:
#include<stdio.h>
#include"shmlib.h"
int main()
{
int *arr;
int image[4][4];
int bit;
int largegl;
int i,j,c1;
int id,id1;
int c;
int x,y;
x=0;
y=0;
arr=(int *)shared(malloc(sizeof(int)*8),&id);
largegl=7;
printf("Enter the image(4*4)");
printf("\n");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
scanf("%d",&image[x][y]);
}
}
id1=create_process(1);
if(id1==1)
{
printf("Process A is executing\n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
c=image[i][j];
arr[c]=arr[c]+1;
printf("%d\t",c);
}
printf("\n");
}
}
else
{
printf("Process B is executing\n");
for(i=2;i<4;i++)
{
for(j=0;j<4;j++)
{
c=image[i][j];
arr[c]=arr[c]+1;
printf("%d\t",c);
}
printf("\n");
}
}
join_process(2,id1);
for(i=0;i<8;i++)
{
printf("%d \t %d",i,arr[i]);
printf("\n");
}
return 0;
}
#include"shmlib.h"
int main()
{
int *arr;
int image[4][4];
int bit;
int largegl;
int i,j,c1;
int id,id1;
int c;
int x,y;
x=0;
y=0;
arr=(int *)shared(malloc(sizeof(int)*8),&id);
largegl=7;
printf("Enter the image(4*4)");
printf("\n");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
scanf("%d",&image[x][y]);
}
}
id1=create_process(1);
if(id1==1)
{
printf("Process A is executing\n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
c=image[i][j];
arr[c]=arr[c]+1;
printf("%d\t",c);
}
printf("\n");
}
}
else
{
printf("Process B is executing\n");
for(i=2;i<4;i++)
{
for(j=0;j<4;j++)
{
c=image[i][j];
arr[c]=arr[c]+1;
printf("%d\t",c);
}
printf("\n");
}
}
join_process(2,id1);
for(i=0;i<8;i++)
{
printf("%d \t %d",i,arr[i]);
printf("\n");
}
return 0;
}
0 comments:
Confused? Feel free to ask
Post a Comment