Description
Learning Objectives:
The primary learning objectives of this assignment include learning to use concurrent programming
abstractions including POSIX threading. Additional learning objectives include being able to reason about the
concurrent execution of threads and use of synchronization abstractions.
Homework:
Write a multi-threaded console application to perform [M×N] × [N×P] matrix multiplication to produce an
[M×P] matrix using POSIX threads (pthreads). The program’s name should be “matrix-multiply”.
Specifications
1. You must create threads using the POSIX pthreads APIs.
2. You must perform matrix multiplication on two input matrices [A] x [B]. For example:
Let A be a [3×2] matrix:
Let B be [2×3] matrix:
Then, [A] × [B] = [C], which is the [3×3] matrix:
3. Each element (C11, C12 ,…., C33) of the resultant matrix C must be calculated by a separate thread. For
example the value of C11 needs to be calculated by thread_1, the value of C12 needs to be calculated by
thread_2 and so on. You must use M×P threads to complete this multiplication.
4. The input matrices A and B will be described in two input files. Your program must accept the two input
files and the output file as command-line arguments in this format:
./matrix-multiply input-file1 input-file2 outfile
5. Each line of the input file will describe each row of a matrix. Each element on a line is separated by 1 or
more whitespace characters. A whitespace character is a space or tab for this application. Extra whitespace
characters may be present. For example, to specify the matrices A and B:
3
ECE-3574: Applied Software Engineering, Fall 2016
Homework 6
the 2 input files may have the format:
Input-file1 Input-file2
1 4
2 5
3 6
8 7 6
5 4 3
6. The input files will contain the matrices A and B, respectively, for any values of M, N and P. M, N and P
are not given explicitly. They should be inferred from the input files.
7. The outer dimensions of the matrices are not necessarily the same. The output matrix must be sized
according to these dimensions.
8. You should also check for the correctness of the input files. The input files should contain all integer
values. An integer value is an optional minus sign followed by 1 or more digits.
10. Reading the two input files must be done in parallel, using two threads created by the main thread.
11. The MxP matrix multiplication threads should also run in parallel. Each thread computing its output value
(for example C12) should write it in a single temporary file, named “temp.txt”. Each thread writes the position
that it is computing along with the computed value. For example, the thread that computes C12 writes to the
file “C1,2=V” on a new line (V is the value computed for C1,2). At the end, the “temp.txt” should contain all
the values of the output matrix, one per line. After all the values have been written into the “temp.txt” file, the
thread executing the main function joins the execution of all other threads and prepares the output file. The
output file will contain the resultant matrix C (computed by your program) in the same format as the input
files.
13. Make sure you implement appropriate synchronization mechanisms where needed. Your main cpp file
should have a comment at the top of the file, where you explain where you used such mechanisms and why,
or why you didn’t have to use any. This should be around 50-200 words.
14. You are NOT allowed to use QThreads for this homework. You need to use the POSIX “pthread” APIs.
You would have to #include in your code and during compilation use -pthread for
linking to the pthread libraries. For example, if you write a multi-threaded application using pthreads in
a file named main.cpp, the compilation step may be the following command:
g++ -pthread main.cpp -o matrix-multiply
15. Make sure to properly implement error handling in your program. An error message must be printed for
the following errors:
I. Incorrectly formatted command line arguments
II. Unopenable, unreadable or unwritable files where doing so is necessary
III. Multiple-length rows for a single matrix
IV. Data format disagreement of matrices
V. Matrix inner dimension mismatch
16. Your program will not be expected to handle the case in which a matrix would have a 0 sized dimension.
Submission
1. Please ensure that you submit before the deadline.
2. Please include the “.pro” files for your projects if used.