implement a multi-threaded C program that uses a shared bounded buffer to coordinate the production of NxN matrices for consumption in matrix multiplication.C ProgrammingC Programming

This assignment is to implement a multi-threaded C program that uses a shared bounded buffer to coordinate the production of NxN matrices for consumption in matrix multiplication.C Programming

Parallel Matrix Multiplier


 The purpose of this assignment is to implement a multi-threaded C program that uses a shared bounded buffer to coordinate the production of NxN matrices for consumption in matrix multiplication. For two matrices M1 and M2 to be multiplied, the number of columns of M1 must equal the number of rows of M2. The program will perform parallel work using multiple threads to: (1) produce NxN matrices and place them into a shared buffer, and (2) consume NxN matrices from the bounder buffer for pairing with another matrix for matrix multiplication having a valid number of rows and columns. Matrices consumed from the bounded buffer with an invalid number of elements for multiplication are discarded and the buffer is queried again to obtain a new candidate matrix for multiplication.

Starter code (pcmultiply.tar.gz in Canvas) is provided to help jumpstart implementing the parallel matrix multiplier with the synchronized bounded buffer. The goal of the project is to focus on synchronization and pthreads, not implementing matrix functions and operations as this code is already provided.


Producer algorithm:


One or more producer threads work together to produce “LOOPS” (defined in pcmatrix.h) # of matrices and place them in the shared bounded buffer. The producer should call Matrix * GenMatrixRandom() (refer to matrix.h and matrix.c) to generate a NxN matrix where the number of rows and columns is random between 1 and 4.

Consumer algorithm:

 One or more consumer threads work together to perform matrix multiplication. Each consumer thread gets a matrix from the bounded buffer (M1). Then the consumer thread gets a second matrix from the bounded buffer (M2). Calling the matrix.c routine Matrix * MatrixMultiply(Matrix * m1, Matrix * m2) will return a pointer with a result of the matrix multiplication (M3), or a NULL if matrix multiplication fails due to a mismatch of the number of elements. If a NULL is received, then the consumer thread

discards the matrix and memory is free’d by calling void FreeMatrix(Matrix * mat) (refer to matrix.h and

matrix.c). The consumer thread then grabs the next available matrix from the bounded buffer as M2. When a valid matrix M2 is found that pairs with M1, the matrix multiplication operation is performed and the result in M3 is printed using the void DisplayMatrix(Matrix * mat, FILE *stream) routine (refer to matrix.h and matrix.c).


Starter Code:


The following modules are provided:



Header file

Source file





Synchronized counter data structure




Matrix helper routines




Producer Consumer worker thread module




Program main module with int main()


A Makefile is provided to compile the modules into a pcMatrix binary.


An initial demonstration of the random matrix generation routine, matrix multiplication, and matrix display is provided in pcmatrix.c int main(). The matrix multiplication output format should be followed for the actual program implementation.


The following constant parameters and global values are defined in h: DEFAULTS


DEFAULT number of producer and consumer worker threads.


Integer true (1) / false (0) to enable or disable debug output. See matrix.c

for example use of #if OUTPUT / #endif.


DEFAULT size of the bounded buffer defined as an array of Matrix struct



DEFAULT number of matrices to produce/consume.


DEFAULT type of matrices to produce





Global variable defining the bounded buffer size.




Defines the type of matrices to produce. (0=random, 1-n=fixed row/col)


Note that the number of worker threads is handled using a local variable called numw in pcmatrix.c.

Code is included in pcmatrix.c to load command line arguments into the global variables for the user. Dynamically sizing the bounded buffer is already done. The matrix_mode is also already implemented in matrix.c. If no command line arguments are provided, the default values are used. A message is displayed indicating the parameterization:


C Programming Experts

Arapera Billing
C Programming

89 Answers

Simon M.
C Programming

44 Answers

View More Experts

The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.

Get Free Quote!

267 Experts Online