Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Operating systems The goal of this programming assignment is to get some experience with parallel threads withina process. And of course, to get some more

Operating systems

image text in transcribedimage text in transcribed

The goal of this programming assignment is to get some experience with parallel threads withina process. And of course, to get some more practice with C. :) Let P be a mx k matrix of integers, and Q be a k n matrix of integers. Then their product matrix RPQ is a m x n matrix. An entry of R in row i and column j is given by: x 0 We will develop a C program that, in addition to the main thread, has mn parallel "worker" threads, one for computing each entry in the product matrix R. We will use the pthreads library, as in Figure 2-15 of the textbook. Mainline: The mainline should start by reading the sizes and values of matrices P and Q from the file stdin as follows Line 1 of input: The values m and k, separated by whitespace (i.e. size of P) Lines 2 thru (m + 1) of input: k integer values, separated by whitespace, in that row of P Line (m + 2) of input: The values k and n, separated by whitespace (i.e. size of Q) . Lines (m+ 3) thru (m+ 2+k) of nput:n integer values, separated by whitespace, in that row of Q According to the sizes read above of P and Q, dynamically allocate enough memory for them, and store their values in row-major order, which is the default matrix storage order in C. Also allocate memory for R Now create mn worker threads, passing each worker the values of row i and column j that it is to use to compute and store the value Rij. Matrices P, Q, and R should be global and visible to all threads The easiest way to pass more than one parameter to a t hread is by declaring a structure, such as typedef struct int irow int j /column/ INDICES; The threads can now be created as follows: for (i -0; ii i; data -> jj; /Now create a thread, and pass data as parameter/ After creating all threads, the mainline should wait for all of them to finish, by calling pthread_join) for each of them. For this, the mainline will need to remember the ID of each thread created, in a dynamically created m x n matrix of base type pthread t. After all threads have terminated, the mainline should output the product matrix R to the file stdout, one row per line, with values preferably tab-separated. BTW, it is possible to interleave the waiting for threads with outputting values of R, i.e. have just one loop in which first wait for the thread for Rij and then output that value (while some other threads might still be unfinished) Worker thread for Rij The job of a thread is to simply compute and store Rj from the given indices i and j, according to the formula given above An Example Run: Here's an example run (user input for P in red, for Q in blue, and process output in orange) In this example, ha2 is the executable file, and input redirection is not used for stdin (although input redirection will probably be used often to avoid re-typing matrices for each run) > ./ha2 1 2 1 2 3 4 5 67 8 14 30 46 17 37 57 20 23 35 68 Assignment Submission: Write the entire C code in one source file, ha2.c, and submit this source file on Blackboard. The goal of this programming assignment is to get some experience with parallel threads withina process. And of course, to get some more practice with C. :) Let P be a mx k matrix of integers, and Q be a k n matrix of integers. Then their product matrix RPQ is a m x n matrix. An entry of R in row i and column j is given by: x 0 We will develop a C program that, in addition to the main thread, has mn parallel "worker" threads, one for computing each entry in the product matrix R. We will use the pthreads library, as in Figure 2-15 of the textbook. Mainline: The mainline should start by reading the sizes and values of matrices P and Q from the file stdin as follows Line 1 of input: The values m and k, separated by whitespace (i.e. size of P) Lines 2 thru (m + 1) of input: k integer values, separated by whitespace, in that row of P Line (m + 2) of input: The values k and n, separated by whitespace (i.e. size of Q) . Lines (m+ 3) thru (m+ 2+k) of nput:n integer values, separated by whitespace, in that row of Q According to the sizes read above of P and Q, dynamically allocate enough memory for them, and store their values in row-major order, which is the default matrix storage order in C. Also allocate memory for R Now create mn worker threads, passing each worker the values of row i and column j that it is to use to compute and store the value Rij. Matrices P, Q, and R should be global and visible to all threads The easiest way to pass more than one parameter to a t hread is by declaring a structure, such as typedef struct int irow int j /column/ INDICES; The threads can now be created as follows: for (i -0; ii i; data -> jj; /Now create a thread, and pass data as parameter/ After creating all threads, the mainline should wait for all of them to finish, by calling pthread_join) for each of them. For this, the mainline will need to remember the ID of each thread created, in a dynamically created m x n matrix of base type pthread t. After all threads have terminated, the mainline should output the product matrix R to the file stdout, one row per line, with values preferably tab-separated. BTW, it is possible to interleave the waiting for threads with outputting values of R, i.e. have just one loop in which first wait for the thread for Rij and then output that value (while some other threads might still be unfinished) Worker thread for Rij The job of a thread is to simply compute and store Rj from the given indices i and j, according to the formula given above An Example Run: Here's an example run (user input for P in red, for Q in blue, and process output in orange) In this example, ha2 is the executable file, and input redirection is not used for stdin (although input redirection will probably be used often to avoid re-typing matrices for each run) > ./ha2 1 2 1 2 3 4 5 67 8 14 30 46 17 37 57 20 23 35 68 Assignment Submission: Write the entire C code in one source file, ha2.c, and submit this source file on Blackboard

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

Step: 3

blur-text-image

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

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions