Question

In: Computer Science

Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy /...

Matrix Multiplication with Threads - C/C++

**Please read entire question before answering**
**Please don't copy / paste code**

In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm.

Use the matrix mulltiplication algorithm.

Write a program that contains three functions:

(1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from the heap and randomly fill that dynamically allocated array.

(2) A function that uses the algorithm described above to compute the matrix square of an array. It should be passed a pointer to an array of integers and an integer defining the dimensions of the array. It should return a pointer to the array containing the product.

(3) A function that uses pthreads to compute the matrix square of an array. This function should have the same parameters and return values of the previous function

The main() function in your program needs to use these functions to compute the square of matrices with 100, 500, 1000, 5000, and 10000 integers
Assume that the values used for the size of these square arrays will always be even.

My suggestion is to think about dividing each array into smaller and smaller pieces until you reach some reasonably small size. At that point multiply the matrices using the iterative algorithm.

Solutions

Expert Solution

#include <pthread.h>

#include <stdlib.h>

#include <stdio.h>

#define MATRIXSIZE 5000 // matrix size

int total_threads; // number of threads

int A[MATRIXSIZE][MATRIXSIZE], B[MATRIXSIZE][MATRIXSIZE], C[MATRIXSIZE][MATRIXSIZE],D[MATRIXSIZE][MATRIXSIZE],E[MATRIXSIZE][MATRIXSIZE];

// matrix creating function

void createMatrix(int z[MATRIXSIZE][MATRIXSIZE])

{

int value = 0;

for (int i = 0; i < MATRIXSIZE; i++)

for (int j = 0; j < MATRIXSIZE; j++)

z[i][j] = value++;

}

void printingMatrix(int z[MATRIXSIZE][MATRIXSIZE])

{

for (int i = 0; i < MATRIXSIZE; i++) {

printf("\n \t| ");

for (int j = 0; j < MATRIXSIZE; j++)

printf("%3d ", z[i][j]);

printf("|");

}

}

// multiplySlice thread function

void* multiplySlice(void* sliceArray)

{

int p = (int)sliceArray;

int from = (p * MATRIXSIZE)/total_threads;

int to = ((p+1) * MATRIXSIZE)/total_threads;

printf("calculating slicepiece %d - from row %d to %d \n", p, from, to-1);

for (int i = from; i < to; i++)

{

for (int j = 0; j < MATRIXSIZE; j++)

{

C[i][j] = 0;

for ( int k = 0; k < MATRIXSIZE; k++)

       C[i][j] += A[i][k]*B[k][j];

}

}

printf("completed sliceArray %d\n", p);

return 0;

}

int main(int argc, char* argv[])

{

pthread_t* thread;// creating threads

if (argc!=2)

{

printf("Usage: %p number_of_threads\n",argv[0]);

exit(-1);

}

total_threads = atoi(argv[1]);

createMatrix(A);

createMatrix(B);

thread = (pthread_t*) malloc(total_threads*sizeof(pthread_t));

// if thread is 1, then it will not enter loop

for (int i = 1; i < total_threads; i++)

{

// each thread creating here

if (pthread_create (&thread[i], NULL, multiplySlice, (void*)i) != 0 )

{

perror("Unable to create thread");

free(thread);

exit(-1);

}

}

multiplySlice(0);

// here main thread waiting....

for (i = 1; i < total_threads; i++)

   pthread_join (thread[i], NULL);

printf("\n\n");

printingMatrix(A);

printf("\n\n\t * \n");

printingMatrix(B);

printf("\n\n\t = \n");

printingMatrix(C);

printf("\n\n");

free(thread);

return 0;

}


Related Solutions

Please read through the entire question before answering. I'm asking for assistance on how to calculate...
Please read through the entire question before answering. I'm asking for assistance on how to calculate LOSS, not gain. Thank you! The major stock market indexes had strong results in 2014. The mean one-year return for stocks in the S&P 500, a group of 500 very large companies was +11.4%. The mean one year return for the NASDAQ, a group of 3200 small and medium-sized companies was +13.4%. Historically, the one-year returns are approximately normal, the standard deviation in the...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
Simulation (Please read the scenario before answering the question. Also type your answer thanks) This is...
Simulation (Please read the scenario before answering the question. Also type your answer thanks) This is Becky, the nurse in the emergency Department. I am caring for Robert jones a 60-year-old African- American male. We admitted him at at 4:30   this morning. He reported increased shortness of breath and weakness.    His vitals were: blood pressure 80 over 62, R 30 pulse 164 and in a-fib. We gave him a 1000 milliliter bolus of sodium chloride to bring his blood pressure....
Please read the following restrictions before answering the question below: Restrictions: – Do not import any...
Please read the following restrictions before answering the question below: Restrictions: – Do not import any modules other than math and check. – You are always allowed to define your own helper/wrapper functions. Do not use Python constructs from later modules (e.g. dictionaries, zip, anything with sets or enumerators, list comprehension, commands continue or break). – Abstract list functions and recursion will not be allowed. Use only the functions and methods as follows: ∗ abs, len, max, min, sum, range...
Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator,...
Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator, @, does a proper matrix multiplcation. To do this, first write a function, mat_mult(a1, a2) that creates a new 2d array of zeros, and using nested for loops, fill in the elements of the matrix multiplication, and return this new array. Then, write a second function, matrix_diff(b, c) that takes two arrays as arguments then generates and returns the following ratio: sqrt((∑(??,?−??,?)^2)/(∑(??,?+??,?)^2)) This function...
ATTENTION: Someone was answering this question but to the fact of time it can't, please don't...
ATTENTION: Someone was answering this question but to the fact of time it can't, please don't answer this at least you're the one. Identify social institutions: Families, Community, Religion, Government and Economy. Present your importance in society. (For this part, answer "Why is it important in society?" Your answer can be presented in bullets.) A. Example Social Institutions • Family - This Institution is ... In addition, it is important for the society due ...
Stimulation (Please read the simulation before answering the questions . Please do not scan your answer,...
Stimulation (Please read the simulation before answering the questions . Please do not scan your answer, type please) Hey Ben. You’re getting an admission in the next 15 minutes coming from the outpatient clinic. BEN: Okay, what can you tell me about this client? JAN: Well, Susan Choi is 33 years old. (PATIENT) She’s been followed for depression over the past six months and is being admitted for acute mania. She is on administrative leave from her employment as a...
My topic is the Automobile Industry Please read it carefully before answering Thankyou Continuing with the...
My topic is the Automobile Industry Please read it carefully before answering Thankyou Continuing with the business you have chosen, write an analysis of your business in three parts: i) Management & Leadership, ii) Organizational Structure, and iii) Operations Management, based on the material you have collected for the three topics. Each analysis should also refer back to the material you have learned in the course, and be between 250 and 500 words per topic, although you can go a...
PLEASE READ THIS CAREFULLY FIRST BEFORE ANSWERING!!!!!! A DOWN VOTE WILL BE GIVEN IF YOU JUST...
PLEASE READ THIS CAREFULLY FIRST BEFORE ANSWERING!!!!!! A DOWN VOTE WILL BE GIVEN IF YOU JUST PASTE AN ANSWER WITHOUT JUSTIFYING IT LIKE I'M ASKING FOR. I know this question is already on here but i have seen too many differing answers for a) and ultimately b) & c) so i am after justification for your answer that you provide. The main answers i have seen for a) are 35955.4 Kg/Hr and 44398.3 Kg/Hr dependant on which side they put...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT