Question

Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you...

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

}

Add a comment
Know the answer?
Add Answer to:
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge...

    3460:209 Assignment 9-B Assignment9-B: The Element Shifter The purpose of this assignment is to help gauge your skills in writing small programs that involve pointers. The program also contains functions and may perform input, output, files and file processing, use arrays and vectors and/or c-string/string arrays, flow of control, and/or calculations. PROGRAM SPECIFICATION For this program, we are going to expand a standard array by dynamically allocating a new one with a larger footprint. The program will use a function...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a tes...

    In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a test grade (integer value) for each student in a class. Validation Tests are graded on a 100 point scale with a 5 point bonus question. So a valid grade should be 0 through 105, inclusive. Processing Your program should work for any...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as...

    Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as parameters to functions. Write the pseudocode (algorithm) for the following program definition: A text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...

    Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array). The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT