Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your assignment consists of designing and programming a multithreaded solution to perform a fast matrix transposition (if you have enough CPU/GPU cores). Write your multithreaded

image text in transcribedimage text in transcribed

Your assignment consists of designing and programming a multithreaded solution to perform a fast matrix transposition (if you have enough CPU/GPU cores). Write your multithreaded program in C, using the pthread library we discussed in class. Calculating the transpose of a matrix is essential in many important applications. Given matrix A, of M rows and N columns, its transpose is the matrix A' of N rows and M columns, where A can be calculated by copying the element in row , columnj of matrix A (ie. Aiv) to row, column i of matrix A1 (ie. A,i) That is In a system that have only one CPU core it takes (M * V) copying operations to get the resulting matrix A', but we can do much better if more CPU cores were available by assigning one-element copying operation to each core. To reduce overhead one can have coarser granularity by assigning one-vector copying operation to each core, instead. Design your matrix transposition algorithm such that it processes one-element per core (i.e. each element is processed in a separate worker thread) or design it such that it processes one-vector per core (i.e. one row or one column is processed in a separate worker thread). The first option involves creating Mx N worker threads, while the second option involves creating M or N worker threads. The main thread shall initialize matrix A and allocate sufficient memory for matrix A'. These matrices must be declared as global data so that each worker thread has access to both A and A Matrix A shall be populated by reading-in values from a file. Then, if you chose the first option, the main thread shall call the worker threads and pass to each (worker ij, the values of row i and columnj that it should use in its operation. On the other hand, if you chose the second option, the main thread shall call the worker threads and pass to each (worker)i or (worker)/, the value of row i and its size or the value of column j and its size, that should be used in its operation, respectively. This requires passing two parameters to each thread. So, you can put them in one struct type variable and pass its pointer. Since each worker thread can make its operation and get the corresponding element(s) of A independent of the other worker threads, there is no need to setup any shared memory protection or synchronization between the threads Waiting for Threads to Complete During the execution of the worker threads, the main thread will have to wait for all the workers to finish before continuing. Once all worker threads have completed, the main thread shall output A. Read the multi-threaded example and the pthread documentation in the course web site to learn how to create threads, pass the required parameters to them and how to make the main thread wait until the other worker threads are finished What to do: Write a multithreaded program in C under Linux using the pthread user-thread API to calculate the matrix transpose as described above. Your program should read one command-line parameter which is the name of a file that contains the following data: 1. In the first line: two integers M and N, indicating the dimensions of matrices AMxN. 2. The matrix A4 values, one row per line. 3. This means that the file must contain 2+(M x N) values in 1+M lines. The program (main thread) must then create the worker threads and wait for all of them to finish. Each worker thread must write one output line identifying itself in terms of the parameters passed to it and printing what it is doing. The main thread shall then print the resulting matrix and terminate. Your program must check for all possible errors and if there are some errors, it shall attempt correcting them by interacting with the user or terminate gracefully after printing a useful error message. What to turn in: A single zip file that contains ONLY three files: a) Your C source program, fully documented and commented, b) Your test data file as described above, and c Your program output including: M, N, A, andA Your assignment consists of designing and programming a multithreaded solution to perform a fast matrix transposition (if you have enough CPU/GPU cores). Write your multithreaded program in C, using the pthread library we discussed in class. Calculating the transpose of a matrix is essential in many important applications. Given matrix A, of M rows and N columns, its transpose is the matrix A' of N rows and M columns, where A can be calculated by copying the element in row , columnj of matrix A (ie. Aiv) to row, column i of matrix A1 (ie. A,i) That is In a system that have only one CPU core it takes (M * V) copying operations to get the resulting matrix A', but we can do much better if more CPU cores were available by assigning one-element copying operation to each core. To reduce overhead one can have coarser granularity by assigning one-vector copying operation to each core, instead. Design your matrix transposition algorithm such that it processes one-element per core (i.e. each element is processed in a separate worker thread) or design it such that it processes one-vector per core (i.e. one row or one column is processed in a separate worker thread). The first option involves creating Mx N worker threads, while the second option involves creating M or N worker threads. The main thread shall initialize matrix A and allocate sufficient memory for matrix A'. These matrices must be declared as global data so that each worker thread has access to both A and A Matrix A shall be populated by reading-in values from a file. Then, if you chose the first option, the main thread shall call the worker threads and pass to each (worker ij, the values of row i and columnj that it should use in its operation. On the other hand, if you chose the second option, the main thread shall call the worker threads and pass to each (worker)i or (worker)/, the value of row i and its size or the value of column j and its size, that should be used in its operation, respectively. This requires passing two parameters to each thread. So, you can put them in one struct type variable and pass its pointer. Since each worker thread can make its operation and get the corresponding element(s) of A independent of the other worker threads, there is no need to setup any shared memory protection or synchronization between the threads Waiting for Threads to Complete During the execution of the worker threads, the main thread will have to wait for all the workers to finish before continuing. Once all worker threads have completed, the main thread shall output A. Read the multi-threaded example and the pthread documentation in the course web site to learn how to create threads, pass the required parameters to them and how to make the main thread wait until the other worker threads are finished What to do: Write a multithreaded program in C under Linux using the pthread user-thread API to calculate the matrix transpose as described above. Your program should read one command-line parameter which is the name of a file that contains the following data: 1. In the first line: two integers M and N, indicating the dimensions of matrices AMxN. 2. The matrix A4 values, one row per line. 3. This means that the file must contain 2+(M x N) values in 1+M lines. The program (main thread) must then create the worker threads and wait for all of them to finish. Each worker thread must write one output line identifying itself in terms of the parameters passed to it and printing what it is doing. The main thread shall then print the resulting matrix and terminate. Your program must check for all possible errors and if there are some errors, it shall attempt correcting them by interacting with the user or terminate gracefully after printing a useful error message. What to turn in: A single zip file that contains ONLY three files: a) Your C source program, fully documented and commented, b) Your test data file as described above, and c Your program output including: M, N, A, andA

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

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

Describe voluntary benefits.

Answered: 1 week ago

Question

Describe the major job evaluation systems.

Answered: 1 week ago