You are on page 1of 2

EE 463 Operating Systems Date: 12/06/1439

Spring 2018 Homework #3 28/02/2018

Dr. A. M. Al-Qasimi Due: 12/03/2018

Objectives:
The purpose of this homework is to get hands-on experience with multithreaded programming, using the
POSIX thread API, (pthread library) under the Linux operating system.

Description:
Your assignment consists of designing and programming a multithreaded solution to perform matrix
multiplication fast. Write your multithreaded program in C, using the pthread library we discussed in
class.

Given two matrices A and B, where matrix A contains M rows and K columns and matrix B contains K
rows and N columns, the matrix product of A and B is matrix C, where C contains M rows and N
columns. The entry in matrix C for row i, column j (Ci,j) is the sum of the products of the elements for
row i in matrix A and column j in matrix B. That is,

k −1
=
Ci ,j ∑A
h =0
i ,h × B h , j=
; i 0 .. M −=
1, j 0 .. N − 1

For example, if A is a 3-by-2 matrix and B is a 2-by-5 matrix, then C is a 3-by-5 matrix and the element
C2,4 would be the sum of: A2,0 x B0,4 and A2,1 x B1,4.

In a system that have only one CPU it takes (N * K * M) multiply-add operations to get the resulting
matrix C, but we can do much better if more CPUs were available by assigning one multiply-add
operation to each processor. To reduce overhead one can have coarser granularity by assigning one vector
multiplication to each processor as follows:

Revise the matrix multiplication algorithm such that it calculates each element Ci,j in a separate worker
thread. This involves creating M x N worker threads, where each worker thread can be executed in a
separate CPU core. The main thread shall initialize matrices A and B and allocate sufficient memory for
matrix C, which will hold the product of matrices A and B. These matrices must be declared as global
data so that each worker thread has access to A, B, and C.

Matrices A and B shall be populated by reading-in values from a file. The main thread shall call the M x
N worker threads and pass to each (worker)i,j, the values of row i and column j that it is to use in
calculating its part of the matrix product. This requires passing two parameters to each thread. So, you
can put them in one struct type variable and pass its pointer. Since each worker thread can make its
calculation and get the corresponding element of C independent of the other worker threads, there is no
need to setup any shared memory area for synchronization between them.

Waiting for Threads to Complete


During the execution of the worker threads, the main thread will have to wait for all the worker threads to
finish before continuing. Once all worker threads have completed, the main thread shall output the
product contained in matrix C.

Read the multi-threaded example and the pthread documentation in the course web site to learn how to
create threads, pass the required parameters to them and how to make the main thread wait until the other
worker threads are finished.

Dr. A. M. Al-Qasimi Home Work #3, Spring 2018 Page 1 of 2


What to do:
Write a multithreaded program in C under Linux using the pthread user-thread API to calculate the matrix
multiplication as described above. Your program should read one command-line parameter which is the
name of a file that contains the following data:
1. In the first line: three integers M, K and N, indicating the dimensions of matrices AMxK and BKxN
2. The matrix values, one row per line starting by matrix A, then matrix B.
3. This means that the file must contain 3+(M x K)+(K x N) values in 1+M +K lines.

The program (main thread) must then create M x N worker threads, each responsible for calculating one
element of matrix C, then it must wait for all the worker threads to finish. Each worker thread must write
one output line identifying itself in terms of the parameters passed to it and printing the result of its
calculation. The main thread shall then print the resulting matrix and terminate. Your program must
check for all possible errors and if there are some errors, it shall attempt correcting them by interacting
with the user or terminate gracefully after printing a useful error message.

What to turn in:


A single .zip file that contains ONLY three files:
a) Your C source program, fully documented and commented,
b) Your test data file as described above, and
c) Your program output including: M, K, N, A, B and C.

Notes:
1) You must test your program thoroughly before submitting it. It should have no errors,
2) Any programs that do not work properly will get a zero mark. No excuses,
3) Your submitted file must be a .zip file not .rar, and named as your name (i.e. First_Last.zip).

Dr. A. M. Al-Qasimi Home Work #3, Spring 2018 Page 2 of 2

You might also like