Matrix Multiplication with Threads CC Please read entire qu

Matrix Multiplication with Threads - C/C++

**Please read entire question before answering**

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.
Matrix Multiplication with Threads - C/C++

**Please read entire question before answering**

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.
Matrix Multiplication with Threads - C/C++

**Please read entire question before answering**

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.

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(\"\ \\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 \ \", 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\ \", p);

return 0;

}

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

{

pthread_t* thread;// creating threads

if (argc!=2)

{

printf(\"Usage: %p number_of_threads\ \",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(\"\ \ \");

printingMatrix(A);

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

printingMatrix(B);

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

printingMatrix(C);

printf(\"\ \ \");

free(thread);

return 0;

}

 Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a pro
 Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a pro
 Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a pro
 Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site