Question

Matrix Multiplication with Threads - C/C++ **Read entire question before answering** **Don't copy / paste code...

Matrix Multiplication with Threads - C/C++

**Read entire question before answering**

**Don't copy / paste code without testing it. I will downvote your answer and mark as spam.**
I have posted this question several times, do not copy / paste the same code that has been posted, IT DOESN'T WORK!!

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.

Homework Answers

Answer #1

#include < pthread.h >

#include < stdio.h >

#include < stdlib.h >

# define M 3

# define K 2

# define N 3

# define NUM_THREADS 10

int A [ M ] [ K ] = { { 1 , 4 } , { 2 ,5 } , { 3 , 6 } };

int B [ K ] [ N ] = { { 8,7,6 } , { 5 , 4 , 3 } } ;

int C [ M ] [ N ] ;

struct v {

         int i ;

         int j ;

};

void * runner ( void * param ) ;

int main ( int argc , char argv [ ] ) ; {

   int i , j , count = 0 ;

   for ( i = 0 ; i < M ; i++ ) {

      for ( j = 0 ; j < N ; j++ ) {

          struct v * data = ( struct v * ) malloc ( sizeof ( struct v ) ) ;

          data - > i = i ;

          data - > j = j ;

          pthread_t tid ;

         pthread_ attr_init( &attr ) ;

        pthread_create( & tid , &attr , runner , data ) ;

        pthread_ join ( tid , NULL ) ;

       count ++ ;

   }

}

for ( i = 0 ; i < M ; i++ ) {

     for ( j = 0 ; j < N ; j++ ) {

        printf ( " % d " , C [i ] [ j ] ) ;

        }

     printf ( " \n " ) ;

}

}

void * runner ( void * param ) {

    struct v * data = param ;

       int n , sum = 0 ;

       for ( n =0 ; n < K ; n++ )

          sum + = A [ data - > i ] [n] * B [n] [ data - > j ] ;

   }

   C [ data - > i ] [data - > j ] = sum ;

   pthread_exit ( 0 ) ;

}

this program is also for multiplication of matrices -

# include < stdio.h >

# include < conio.h >

void main ( )

{

       int a [ 10 ] [10] , b [ 10 ][ 10 ] , c [ 10 ] [ 10 ] , i , j , k , m ,n , p ,q ;

       clrscr ( ) ;

       print f ( " Enter the rows and columns of A , B ) ;

       scanf ( " %d , %d ,%d ,%d " , & m , & n , & p , & q );

       if ( n== p )

       {

           printf ( " Enter element of matrix A " ) ;

             for ( i = 0 ; i < m ; i++ )

              {

                   for ( j =0 ; j < n ; j++ )

                      {

                           scanf ( " %d " , & a[ i ] [ j ] ) ;

                      }

              }

            printf ( " Enter element of matrix B " );

   for ( i =0 ; i< p ; i++ )

   {

   for ( j =0 ; j < q ; j++ )

{

   scanf ( " %d " , & b [ i ] [ j ] ) ;

}

   }

for ( i =0 ; i < m ; i++ )

{

for ( j=0 ; j<q ; j++ )

{

c[ i ] [ j ] = 0 ;

   }

}

   for ( i =0 ; i < m ; i++ )

   {

for ( j=0 ; j <q ; j++ )

{

   c[i][j] + = a[i][k] * b[k][j];

}

   }

   printf ( " \n the resultant matrix \n " ) ;

}

for ( i =0 ; i<m ; i++ )

{

   for ( j =0 ; j<q ; j++ )

   {

printf ( " % 3d " , c [i ][j ] ) ;

}

   printf ( " \n " ) ;

   }

   else

     {

         printf ( " multiplication is not possible " ) ;

      }

     getch ( ) ;

}

  

Know the answer?
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
Similar Questions
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...