Answered step by step
Verified Expert Solution
Question
1 Approved Answer
complete a C program to employ multi-threading to multiply two matrices using the Pthreads library. You will create as many threads as there are
complete a C program to employ multi-threading to multiply two matrices using the Pthreads library. You will create as many threads as there are cells in the resulting matrix. A review of matrix multiplication follows. Your solution will start from an initial source file: Mod2/matrix/matrix- mult.c. A makefile and several sample test files are in the same directory. 2. Matrix Multiplication Matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The resulting matrix, known as the matrix product, has the number of rows of the first and the number of columns of the second matrix. If A is an m x n matrix and B is an n xp matrix, -619-69 B A- 012 such that: 421 022 by by b Cmp buy The matrix product C=AB is defined to be the xp matrix. C21 C22 +6868 Cml Cm2 bap c = anby, +anby,+ + aimbus = aubus, You should familiarize yourself with the types pthread_t and pthread_attr_t as well as the system calls pthread_create() and pthread_join(). When you compile your program, you will need to include the pthread and math libraries: gcc matrix-mult.c -pthread -fno-stack-protector -Im -o matrix-mult 4. Other Requirements The following list includes other requirements that your program must meet: The program must print a heading which includes the class name (ACO350) and your name (not "Prof. Eckert"). Each cell of the resulting matrix must be calculated by a different thread. Turn in only your source file: matrix-mult.c. . 5. Approach The main thread will create multiple threads. Each additional thread will solve one cell of the matrix multiplication problem in the following way: Given two matrices A and B: Where: . C = A x B is: 1 A = The result multiplying A x B is: 4 2 5 58 139 3 6 64 154 58 (1 x 7) + (2 x 9) + (3 x 11) 64 (1 x 8) + (2 x 10) + (3 x 12) 139 = (4 x 7) + (5 x 9) + (6 x 11) 154 = (4 x 8) + (5 x 10) + (6 x 12) B = 7 9 11 8 10 12 First row of A, first column of B First row of A, second column of B Second row of A, first column of B Second row of A, second column of B The work required to perform this multiplication can be divided in n parts (one part per cell in the final matrix). Each part will be assigned to a different thread with an assigned row from matrix A and column from matrix B. For example, the first thread (assigned to A row 0 and B column 0) will compute the result of C[0,0] = (1 x 7) + (2 x 9) + (3 x 11) = 58. Each thread will update its cell of the final matrix C. Start with the source code: matrix_multo.c. This file has a skeletal main function as well as complete functions to read a matrix from the source file and print a matrix. Be sure to read and understand the starting code before beginning your project. The source code has several comments which contain the phrase "TO DO" for which you should be looking. There are two sample data files that you can use to test your program: data1.txt and data2.txt. The expected results can be seen in the Sample Output section below. 6. Required Source File matrix-mult.c 7. Sample Output data1.txt pi@pi4:-/AC0350/sample_code/module_2 $ ./mat datal.txt Multi-threaded Matrix Multiplication AC0350 Prof. Eckert A [2x3]: 1 2 3 456 B [3x2]: 7 8 9 10 11 12 A x B= C [2x2]: 58 64 139 154 data2.txt pi@pi4:-/AC0350/sample_code/module 2 $ ./mat data2.txt Multi-threaded Matrix Multiplication - AC0350 Prof. Eckert A [10x10]: 1 3 7 43 56 32 33 44 12 1 2 3 6 7 9 54 34 2 34 5 74 89 2 2 3 6 45 89 51 3 42 75 10 5 87 42 85 5 22 12 5 90 10 8 53 90 53 78 54 78 21 50 20 2 7 67 34 63 92 43 76 10 11 77 42 76 9 88 21 45 68 77 30 5 23 8 65 1 78 9 54 9 43 2 3 45 2 6 54 3 67 2 10 2 34 80 5 76 1 15 43 18 93 29 3 92 8 77 7 56 83 46 32 B [10x10]: 26 9 23 26 39 3 29 55 9 49 22 30 57 5 50 81 12 43 36 27 19 64 44 33 87 24 92 24 95 64 79 92 85 66 51 4 5 8 47 81 53 5 44 77 12 50 37 92 35 55 72 62 55 40 60 98 83 70 63 58 44 38 21 10 0 12 27 37 44 53 62 73 86 90 1 67 42 26 9 88 61 45 28 7 80 A x B= C [10x10]: 5863 6900 9595 7595 9424 10393 10548 10722 9049 6345 7127 13421 13213 13327 19182 15460 22217 21748 14993 14966 8884 8658 8549 8567 15190 8400 10043 10203 7777 6980 9411 7348 9196 5594 13611 12206 10874 9797 7263 10967 4322 11138 8329 15963 19878 17169 17774 20295 19243 10445 11912 15289 11195 18721 19109 18102 19564 20215 18820 10701 12070 8809 14743 10503 19969 12073 20561 23678 17756 14868 15784 18937 22688 19643 29550 23386 27434 30690 21937 22499 15455 17432 17618 19054 24581 19789 17799 20486 16484 14503 16121 22715 21707 21363 30985 24424 32915 30216 19885 22928
Step by Step Solution
★★★★★
3.46 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
To create a C program that employs multithreading to multiply two matrices using the Pthreads library you can use the following code as a starting poi...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