Answered step by step
Verified Expert Solution
Link Copied!

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; i.e., 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 (i.e., 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:
in.txt
323
14
25
36
876
543
425
15
26
37
48
98765
43210
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 A1
14
25
36
MATRIX B1
876
543
MATRIX A1 x B1
282318
413427
544536
MATRIX A2
42
51
52
63
MATRIX B2
74898
76543
MATRIX A2 x B2
4228424438
4226454943
4932505346
6342636657
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 m*n 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 pthread_create().
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 A,**b to B, and **c to C. The function takes pointers to these pointers (i.e., pointers to two-dimensional arrays) as parameters (hence ***a,***b, 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 thread_join() 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 pthread_join().
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.
in.txt file:
323
14
25
36
876
543
425
15
26
37
48
98765
43210
CMakeLists.txt:
project(matrixMult C)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}-D_FILE_OFFSET_BITS=64")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}-Wall --pedantic -g -O0-D_DEBUG")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(SOURCE_FILES matrixMult.c)
add_executable(matrixMult ${SOURCE_FILES})
find_package(Threads)
target_link_libraries(matrixMult ${CMAKE_THREAD_LIBS_INIT})
matrixMult.c:
#include "matrixMult.h"
int
main(int argc, char** argv)
{
// Input file should be passed to the program: ./matrixMult in.txt
if (freopen(argv[1],"r", stdin)==0){
oops("Cannot open the input file.
",-1);
}
// two pairs of matrices
int** a1,** b1,** c1;
int** a2,** b2,** c2;
// dimensions of the matices m x k and k x n
int m1, k1, n1;
int m2, k2, n2;
allocateAndLoadMatrices(&a1, &b1, &c1, &m1, &k1, &n1);
allocateAndLoadMatrices(&a2, &b2, &c2, &m2, &k2, &n2);
//
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions