Question

In: Computer Science

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 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

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...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
What is a responsibility assignment matrix (RAM)? Describe how you might use a responsibility assignment matrix...
What is a responsibility assignment matrix (RAM)? Describe how you might use a responsibility assignment matrix to manage the resources assigned to your project. The assignment is to answer the question provided above in essay form. This is to be in narrative form. Bullet points should not to be used. The paper should be at least 1.5 - 2 pages in length, Times New Roman 12-pt font, double-spaced, 1 inch margins and utilizing at least one outside scholarly or professional...
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
The following program uses Pthreads to create two threads. They do some work for the process...
The following program uses Pthreads to create two threads. They do some work for the process and then exit. The process then outputs a result. Assume all supporting libraries and other functions have been included. => Use the answer text field to describe what work (operations) the threads are doing, and what kind of result (what is it?) is output by the process. #include <pthread.h> #include <stdio.h> #include <stdlib.h> int res1, res2, a[100], b[100]; void *runner1(void *param); void *runner2(void *param);...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at()...
In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at() to access its elements. Write a program that multiplies a 2D matrix with a vector. If you need to see the math, follow this link: https://mathinsight.org/matrix_vector_multiplication (Links to an external site.) For simplicity, our matrix will be of size 3 x 3. Initialize the matrix as shown in to become [1.0, 2.0, 3.0] [4.0 ,5.0 ,6.0] [7.0, 8.0, 9.0] Read the three values of...
Generating a sequence using Pthreads Having multiple threads call a function, like will have a non-deterministic...
Generating a sequence using Pthreads Having multiple threads call a function, like will have a non-deterministic execution. Write a program with 3 threads that call a function called do_work. Each thread will be responsible for generating a number and appending it to a buffer. Thread 1 generates number 1, thread 2 generates number 2, and thread 3 generates number 3. These numbers assigned to the threads are passed in as arguments. Each thread will store its value in a shared...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !! * DO not use [ ] 'brackets', focus on using malloc, calloc, etc... Program will ask user for the name of the text file to be read Read the datafile with format: 1 2 1 1 2 3 4 So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT