0% found this document useful (0 votes)
39 views5 pages

Department of Computer Scienc2

1. The document describes an assignment to implement 2D matrix multiplication in any programming language. It requires implementing matrix multiplication without and with multithreading and calculating the time elapsed for each. 2. For the first task, code is provided to multiply matrices without multithreading and calculate the time elapsed. 3. For the second task, code is provided to multiply matrices using multithreading by splitting the multiplication into threads and calculating the time elapsed.

Uploaded by

Javeria Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

Department of Computer Scienc2

1. The document describes an assignment to implement 2D matrix multiplication in any programming language. It requires implementing matrix multiplication without and with multithreading and calculating the time elapsed for each. 2. For the first task, code is provided to multiply matrices without multithreading and calculate the time elapsed. 3. For the second task, code is provided to multiply matrices using multithreading by splitting the multiplication into threads and calculating the time elapsed.

Uploaded by

Javeria Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Department of Computer Science


Bahria University, Islamabad

Fall 2020
BS-IT-4AB

OPERATING SYSTEM
ASSIGNMENT #02

Student Name: ____JAVERIA ABBASI_______________

Enrollment Number: _01-235191-014________


2

Assignment # 2: Weightage: 5

A note of warning: Start work on assignments as soon as


they are given. Do not underestimatethe demanding nature of
this course.Expect the system to crash the night before your
program is due. Aim to have it done the day
before.

Submit the assignment on LMS. Do not email me


assignments after due date. It will not be accepted in
any case. Students are required to submitactual content
written in MS word or Pdf. Hand written/ Scanned
assignments will not be accepted. Zero-level tolerance for
plagiarized content.

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;
}

You might also like