CS 540 Operating Systems Spring 2019 Home Assignment 2 (Due: February 19, 2019 The goal of this programming assignment is to get some experience with parallel threads within a process. And of course, to get some more practice with C.:) Let P be ann k matrix of integers, and Q be a k n matrix of integers. matrix RPQ is a m X n matrix. An entry of R in row i and column j is given by: Then their product i,j x, 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 input: 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 Rj Matrices P, Q, and R should be global and visible to all threads The easiest way to pass more than one parameter to a thread is by declaring a structure, such as row/ int j /*column typedef struct int i /* INDICES The threads can now be created as follows: for -0; i
ii data jj *Now create a thread, and pass data as parameter */ After creating a threads, the mainline should wait for all of them to finish, by calling pthreadjoin for each of them. For this, the mainline wl 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 Riy The job ofa thread is to simply compute and store R 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, forQ 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 3 2 2 4 1 2 34 5 6 7 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. CS 540 Operating Systems Spring 2019 Home Assignment 2 (Due: February 19, 2019 The goal of this programming assignment is to get some experience with parallel threads within a process. And of course, to get some more practice with C.:) Let P be ann k matrix of integers, and Q be a k n matrix of integers. matrix RPQ is a m X n matrix. An entry of R in row i and column j is given by: Then their product i,j x, 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 input: 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 Rj Matrices P, Q, and R should be global and visible to all threads The easiest way to pass more than one parameter to a thread is by declaring a structure, such as row/ int j /*column typedef struct int i /* INDICES The threads can now be created as follows: for -0; i ii data jj *Now create a thread, and pass data as parameter */ After creating a threads, the mainline should wait for all of them to finish, by calling pthreadjoin for each of them. For this, the mainline wl 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 Riy The job ofa thread is to simply compute and store R 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, forQ 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 3 2 2 4 1 2 34 5 6 7 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