Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task Description Lets say that we want to multiply matrix A with the dimension m by k by a matrix B with the dimension k
Task Description
Lets say that we want to multiply matrix A with the dimension m by k by a matrix B with the dimension k by n To obtain the product, say matrix C we compute dot products for each row of A and each column of B; ie to compute a specific element cij of the matrix C we compute a dot product of row ai from the matrix A and the column bj from the matrix B
LaTeX
The application should accept input in the following format:
the first line contains three numbers that specify the sizes of the matrices; for example, m k and n followed by
m lines of k numbers that represent matrix A and in turn followed by
k lines of n numbers that represent matrix B
The program should assume the validity of m k and n ie that the matrices A and B can be legally multiplied
The tests should verify that any number of pairs of matrices can be multiplied concurrently.
Sample input for concurrent multiplication of two pairs of matrices:
intxt
The program should output the content of both input matrices followed by their product as shown here:
Sample output for concurrent multiplication of two pairs of matrices:
MATRIX A
MATRIX B
MATRIX A x B
MATRIX A
MATRIX B
MATRIX A x B
Implementation
Recall that we have several choices to passing information to the threads:
global variables
or a struct.
Global variables will not work since this will prevent the program from multiplying multiple pairs of matrices concurrently. In general, we stay away from global variables when using threads, though there are cases where it makes sense. For this lab, you must not use global variables to pass information.
In your implementation, you must use mn single unique threads to compute the values of every element cij of the product matrix. Therefore, you must pass a pointer to the following structure:
struct matrixCell
int i;
int j;
int k;
int a;
int b;
int c;
;
as a parameter to pthreadcreate
The integer i is the index of the row in the matrix A
j is the index of the column in the matrix B
and k indicates the number of elements in the row and
the number of elements in the column.
The pointers will point to matrices A B and C that will be placed in dynamically allocated space in the function allocatAndLoadMatrices The pointer a points to the matrix Ab to B and c to C The function takes pointers to these pointers ie pointers to twodimensional arrays as parameters hence ab and c You must allocate space for all rows and all columns in every of the arrays, so they can hold the corresponding matrices. The function loadMatrix is an auxiliary function that focuses on loading one matrix at a time.
You MUST NOT use threadjoin after the creation of each thread, since that would cause sequential execution of the threads, and there would be no concurrency whatsoever. However, you must wait for the threads to complete in another place before printing the result, since if you do not, then the product matrix C will be only partially calculated as the program may exit before all threads manage to complete their execution.
Note, this means that you must create both sets of multiplication threads one for each pair of input before calling pthreadjoin
The archive matrixMult.zip contains starting code for your implementation. Implement all missing parts as indicated in the comments
You must submit the following:
the signed source code,
the CMakeLists.txt file with which you built your application,
and multiple scripts of robust tests that confirm that your program is capable of multiplying any a number of matrix pairs concurrently.
intxt file:
CMakeLists.txt:
projectmatrixMult C
setCMAKECSTANDARD
setCMAKECFLAGS $CMAKECFLAGSDFILEOFFSETBITS
setCMAKECFLAGSDEBUG $CMAKECFLAGSDEBUGWall pedantic g ODDEBUG"
setCMAKEMODULEPATH $CMAKECURRENTSOURCEDIRCMake $CMAKEMODULEPATH
setCMAKEEXPORTCOMPILECOMMANDS
setSOURCEFILES matrixMult.c
addexecutablematrixMult $SOURCEFILES
findpackageThreads
targetlinklibrariesmatrixMult $CMAKETHREADLIBSINIT
matrixMult.c:
#include "matrixMult.h
int
mainint argc, char argv
Input file should be passed to the program: matrixMult intxt
if freopenargvr stdin
oopsCannot open the input file.
;
two pairs of matrices
int a b c;
int a b c;
dimensions of the matices m x k and k x n
int m k n;
int m k n;
allocateAndLoadMatrices&a &b &c &m &k &n;
allocateAndLoadMatrices&a &b &c &m &k &n;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started