Saturday, 26 January 2013


Aim: Write a Program to show Matrix Multiplication using Self-Scheduling.
MSc IT Parallel Processing Practical No. 6
Index of all Practicals ~ Click Here

Code:
#include<stdio.h>
#include"shmlib.h"

int main()
{

int *A,*B,id,id1,*C,i,j,P1flag=0,P2flag=0,k;
int id2,id3;
A=(int *) shared(sizeof(int),&id1);
B=(int *) shared(sizeof(int),&id2);
C=(int *) shared(sizeof(int),&id3);
int x;
x=2;
printf("Enter value of matrix A:");
printf("\n");
for(j=0;j<4;j++)
{
scanf("%d",&A[j]);
}
printf("Enter value of matrix B:\n");

for(i=0;i<4;i++)
{
scanf("%d",&B[i]);
}

printf("\nmatrix A\n");
for(j=0;j<2;j++)
{
printf("\t%d",A[j]);
}
printf("\n");
for(j=2;j<4;j++)
{
printf("\t%d",A[j]);
}

printf("\nmatrix B\n");
for(j=0;j<4;j++)
{
printf("\t%d",B[j]);
if(j==1)


printf("\n");
}

printf("\n");

id=create_process(1);

if(id==0)
{
C[0]=A[0]*B[0]+A[1]*B[2];
P1flag=1;

}

if(id==1)
{
C[1]=A[0]*B[1]+A[1]*B[3];
P2flag=1;

}

if(P1flag==1)
{
id=0;
C[2]=A[2]*B[0]+A[3]*B[2];
P1flag=2;

}
else
{
id=1;
C[2]=A[2]*B[0]+A[3]*B[2];

P2flag=2;
}


if(P1flag==2)
{
id=0;
C[3]=A[2]*B[1]+A[3]*B[3];

}
else
{
id=1;
C[3]=A[2]*B[1]+A[3]*B[3];

}



join_process(2,id);

printf("\n\nFinal Matrix after Multiplication:\n");

for(k=0;k<2;k++)
{
printf("\t%d",C[k]);
}
printf("\n");
for(k=2;k<4;k++)
{
printf("\t%d",C[k]);
}

printf("\n");

return 0;
}

Output:

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment