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

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...
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...
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...
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: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
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: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
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: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT