Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a matrix multiplication program where the work of calculating the product matrix is distributed among multiple threads created using the pthread library. To this

write a matrix multiplication program where the work of calculating the product matrix is distributed among multiple threads created using the pthread library. To this end, your program must do the following:

Define two global matrices A[X][Y] & B[Y][Z] to store the two matrices to be multiplied. X and Y must be set using a #define directive.

Define a global matrix C[X][Z] to store the product of matrices A and B. Once again, Z must be set using a #define directive.

Define a pthread function called pthread_multiply to perform the actual matrix multiplication operation (Note: Recall that a pthread function must have a very specific prototype). As a starting point, a regular (non-threaded) function for matrix multiplication, called multiply, has been provided below.

void multiply()

{

int row,col,inner;

for (row = 0; row < X; row++) {

for (col = 0; col < Z; col++) {

for (inner = 0; inner < Y; inner++) {

C[row][col] += A[row][inner] * B[inner][col];

}

}

}

}

The pthread_multiply function you write will essentially be a modification of the above multiply function. Instead of computing the entire product matrix C, the pthread_multiply function will only calculate a subset of rows of the product matrix C. Parameters must be passed to pthread_multiply to tell it which subset of rows to compute (Note: recall that parameter passing to a pthread function is somewhat different because of its specific prototype).

Define a main function and in this function, do the following:

Initialize the matrices A, B and C as follows: each element in A & B must be initialized to the sum of its row and column index and all elements in matrix C must be initialized to zero.

Using a loop, create multiple pthreads to execute the pthread_multiply function (use a #define directive to define the number of threads - this number must be set to more than 2). To each thread, pass appropriate parameters to tell it which subset of rows in the resultant matrix C it is responsible for computing. Each thread must be given an equal share of the resultant matrix as far as possible. For e.g., if you have A[10][10], B[10][10] and if the number of threads is 2 (Note: this is just an example - you must use more than 2 threads), then, thread 1 should be assigned the task of calculating results for rows 0 4 of matrix C and thread 2, those for rows 5 9. However, if you have A[9][9], B[9][9] and two threads, one of the threads will be assigned a lower number of rows than the other. I.e., in general, the last thread may have less number of rows to compute.

Using a second loop, wait until all the created pthreads have completed, using the pthread_join function.

Once all threads have completed, display the matrix C in an appropriate manner.

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions