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

(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...
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...
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?
C++ Question The aim of this assignment is to design and implement a computerized “Library Management...
C++ Question The aim of this assignment is to design and implement a computerized “Library Management System”. The system will be used in the back-office to manage the books in the library catalog and to keep track of the various users (borrowers) of the library. The system provides the following key functionalities: Books are characterized by a call number, a title, and a flag that indicates whether the book is currently ‘on-shelf’ (in the library) or ‘on-loan’. -Adding and removing...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT