Aim: Matrix Multiplication using Block by Block Method.
MSc IT Parallel Processing Practical No. 7
Index of all Practicals ~ Click Here
Code:
Output:
MSc IT Parallel Processing Practical No. 7
Index of all Practicals ~ Click Here
Code:
#include<stdio.h>
#include"shmlib.h"
#include<stdlib.h>
int main()
{
int i,j,k,x,y,z;
static int mat1[4][4],mat2[4][4],mat3[4][4],mat4[4][4];
int m,n,temp,temp1;
int tmp[4][4];
int *arr1,*arr2,*arr3,*arr4;
int id1,id2,id3,id4,idd;
printf("Enter number of rows in matrix:");
scanf("%d",&m);
printf("Enter number of column in matrix:");
scanf("%d",&n);
arr1=(int *)shared(malloc(sizeof(int)),&id1);
arr2=(int *)shared(malloc(sizeof(int)),&id2);
arr3=(int *)shared(malloc(sizeof(int)),&id3);
arr4=(int *)shared(malloc(sizeof(int)),&id4);
printf("Enter the matrix 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat1[i][j]=0;
mat1[i][j]=i+1;
}
}
printf("\n\nEnter the matrix 2:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mat2[i][j]=0;
mat2[i][j]=i+1;
}
}
printf("Matrix1 printed are:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat1[i][j]);
}
printf("\n");
}
printf("Matrix2 printed are:\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat2[i][j]);
}
printf("\n");
}
// First Block Calculation
idd=create_process(3);
if(idd==0)
{
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k]*mat2[k][j];
mat3[i][j]=mat3[i][j]+temp;
}}}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k+2]*mat2[k+2][j];
tmp[i][j]=tmp[i][j]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j]=mat3[i][j]+tmp[i][j];
arr1[x]=mat3[i][j];
x++;
}
}
printf("\n\n\n");
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("\t%d",mat3[i][j]);
}
printf("\n");
}
}
// Second Block Calculation
if(idd==1)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k]*mat2[k][j+2];
mat3[i][j+2]=mat3[i][j+2]+temp;
}
printf(" out put is %d",mat3[i][j+2]);
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k+2]*mat2[k+2][j+2];
tmp[i][j+2]=tmp[i][j+2]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j+2]=mat3[i][j+2]+tmp[i][j+2];
arr2[x]=mat3[i][j+2];
x++;
}
}
}
// Third Block
if(idd==2)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k]*mat2[k][j];
mat3[i+2][j]=mat3[i+2][j]+temp;
}
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i+2][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k+2]*mat2[k+2][j];
tmp[i+2][j]=tmp[i+2][j]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j]=mat3[i+2][j]+tmp[i+2][j];
arr3[x]=mat3[i+2][j];
x++;
}
}
}
// Fourth Block
if(idd==3)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k]*mat2[k][j+2];
mat3[i+2][j+2]=mat3[i+2][j+2]+temp;
}
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i+2][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k+2]*mat2[k+2][j+2];
tmp[i+2][j+2]=tmp[i+2][j+2]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j+2]=mat3[i+2][j+2]+tmp[i+2][j+2];
arr4[x]=mat3[i+2][j+2];
x++;
}
}
}
join_process(4,idd);
printf("\n\n\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr1[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr2[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr3[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr4[i]);
}
for(i=0;i<2;i++)
{
mat4[0][i]=arr1[i];
}
for(i=0;i<2;i++)
{
mat4[1][i]=arr1[i+2];
}
for(i=0;i<2;i++)
{
mat4[0][i+2]=arr2[i];
}
for(i=0;i<2;i++)
{
mat4[1][i+2]=arr2[i+2];
}
for(i=0;i<2;i++)
{
mat4[2][i]=arr3[i];
}
for(i=0;i<2;i++)
{
mat4[3][i]=arr3[i+2];
}
for(i=0;i<2;i++)
{
mat4[2][i+2]=arr4[i];
}
for(i=0;i<2;i++)
{
mat4[3][i+2]=arr4[i+2];
}
printf("\n\nFinal matrix after multiplication is:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat4[i][j]);
}
printf("\n");
}
return 0;
}
#include"shmlib.h"
#include<stdlib.h>
int main()
{
int i,j,k,x,y,z;
static int mat1[4][4],mat2[4][4],mat3[4][4],mat4[4][4];
int m,n,temp,temp1;
int tmp[4][4];
int *arr1,*arr2,*arr3,*arr4;
int id1,id2,id3,id4,idd;
printf("Enter number of rows in matrix:");
scanf("%d",&m);
printf("Enter number of column in matrix:");
scanf("%d",&n);
arr1=(int *)shared(malloc(sizeof(int)),&id1);
arr2=(int *)shared(malloc(sizeof(int)),&id2);
arr3=(int *)shared(malloc(sizeof(int)),&id3);
arr4=(int *)shared(malloc(sizeof(int)),&id4);
printf("Enter the matrix 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat1[i][j]=0;
mat1[i][j]=i+1;
}
}
printf("\n\nEnter the matrix 2:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mat2[i][j]=0;
mat2[i][j]=i+1;
}
}
printf("Matrix1 printed are:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat1[i][j]);
}
printf("\n");
}
printf("Matrix2 printed are:\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat2[i][j]);
}
printf("\n");
}
// First Block Calculation
idd=create_process(3);
if(idd==0)
{
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k]*mat2[k][j];
mat3[i][j]=mat3[i][j]+temp;
}}}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k+2]*mat2[k+2][j];
tmp[i][j]=tmp[i][j]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j]=mat3[i][j]+tmp[i][j];
arr1[x]=mat3[i][j];
x++;
}
}
printf("\n\n\n");
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("\t%d",mat3[i][j]);
}
printf("\n");
}
}
// Second Block Calculation
if(idd==1)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k]*mat2[k][j+2];
mat3[i][j+2]=mat3[i][j+2]+temp;
}
printf(" out put is %d",mat3[i][j+2]);
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i][k+2]*mat2[k+2][j+2];
tmp[i][j+2]=tmp[i][j+2]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i][j+2]=mat3[i][j+2]+tmp[i][j+2];
arr2[x]=mat3[i][j+2];
x++;
}
}
}
// Third Block
if(idd==2)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k]*mat2[k][j];
mat3[i+2][j]=mat3[i+2][j]+temp;
}
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i+2][j]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k+2]*mat2[k+2][j];
tmp[i+2][j]=tmp[i+2][j]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j]=mat3[i+2][j]+tmp[i+2][j];
arr3[x]=mat3[i+2][j];
x++;
}
}
}
// Fourth Block
if(idd==3)
{
temp=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k]*mat2[k][j+2];
mat3[i+2][j+2]=mat3[i+2][j+2]+temp;
}
}
}
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
tmp[i+2][j+2]=0;
for(k=0;k<m/2;k++)
{
temp=mat1[i+2][k+2]*mat2[k+2][j+2];
tmp[i+2][j+2]=tmp[i+2][j+2]+temp;
}
}
}
x=0;
for(i=0;i<m/2;i++)
{
for(j=0;j<n/2;j++)
{
mat3[i+2][j+2]=mat3[i+2][j+2]+tmp[i+2][j+2];
arr4[x]=mat3[i+2][j+2];
x++;
}
}
}
join_process(4,idd);
printf("\n\n\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr1[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr2[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr3[i]);
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\t%d",arr4[i]);
}
for(i=0;i<2;i++)
{
mat4[0][i]=arr1[i];
}
for(i=0;i<2;i++)
{
mat4[1][i]=arr1[i+2];
}
for(i=0;i<2;i++)
{
mat4[0][i+2]=arr2[i];
}
for(i=0;i<2;i++)
{
mat4[1][i+2]=arr2[i+2];
}
for(i=0;i<2;i++)
{
mat4[2][i]=arr3[i];
}
for(i=0;i<2;i++)
{
mat4[3][i]=arr3[i+2];
}
for(i=0;i<2;i++)
{
mat4[2][i+2]=arr4[i];
}
for(i=0;i<2;i++)
{
mat4[3][i+2]=arr4[i+2];
}
printf("\n\nFinal matrix after multiplication is:\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",mat4[i][j]);
}
printf("\n");
}
return 0;
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment