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

  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

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

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

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    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 * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • C++,please help. I have no idea how to do that begining with zero parameter constructor. and...

    C++,please help. I have no idea how to do that begining with zero parameter constructor. and help me to test the method, Write a class template Square Matrix that implements a square matrix of elements of a specified type. The class template should be fully in the Square Matrix.h file. The class uses "raw" pointers to handle the dynamically allocated 2D-array. "Raw" pointers are the classic pointers that you've used before. In addition to it, you will need a variable...

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
ADVERTISEMENT