Department of Computer Scienc2
Department of Computer Scienc2
Fall 2020
BS-IT-4AB
OPERATING SYSTEM
ASSIGNMENT #02
Assignment # 2: Weightage: 5
In this assignment you need to implement 2D square matrix multiplication in any language of your
interest
(recommend C language). You have to attempt two major tasks
Note:
(i) The implementation must be dynamic, i.e., your program should work for any size of
matrix. One such solution may be passing the size as an argument in argv [].
(ii) Use commands/APIs/methods to calculate time elapsed also include screenshots of the
outputs
1. Implement matrix multiplication without using multithreading and find the
time elapsed:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
clock_t t;
t = clock();
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("\n\n");
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\n");
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
3
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n\n");
printf("multiply of the matrix=");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
//time elapsed
printf("\n\n");
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("took %f seconds to execute \n", time_taken);
return 0;
}
4
2. Implement matrix multiplication using multithreading and find the time elapsed:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
#define MAX 4
void *mult(void* arg)
{
int *data = (int *)arg;
int k = 0, i = 0;
int x = data[0];
for (i = 1; i <= x; i++)
k += data[i]*data[i+x];
int *p = (int*)malloc(sizeof(int));
*p = k;
pthread_exit(p);
}
int main()
{
clock_t t;
t = clock();
int matA[MAX][MAX];
int matB[MAX][MAX];
int r1=MAX,c1=MAX,r2=MAX,c2=MAX,i,j,k;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
matA[i][j] = rand() % 10;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
matB[i][j] = rand() % 10;
for (i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
printf("%d ",matA[i][j]);
printf("\n");
}
for (i = 0; i < r2; i++){
for(j = 0; j < c2; j++)
printf("%d ",matB[i][j]);
printf("\n");
}
int max = r1*c2;
pthread_t *threads;
threads = (pthread_t*)malloc(max*sizeof(pthread_t));
int count = 0;
int* data = NULL;
for (i = 0; i < r1; i++)
for (j = 0; j < c2; j++)
{
data = (int *)malloc((20)*sizeof(int));
5
data[0] = c1;
for (k = 0; k < c1; k++)
data[k+1] = matA[i][k];
for (k = 0; k < r2; k++)
data[k+c1+1] = matB[k][j];
pthread_create(&threads[count++], NULL,
mult, (void*)(data));
}
printf("RESULTANT MATRIX IS :- \n");
for (i = 0; i < max; i++)
{
void *k;
pthread_join(threads[i], &k);
int *p = (int *)k;
printf("%d ",*p);
if ((i + 1) % c2 == 0)
printf("\n");
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("took %f seconds to execute \n", time_taken);
return 0;
}