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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Ross Hopkins, president of Hopkins Hospitality, has developed the tasks, durations, and predecessor relationships in the...
    asked 1 minute ago
  • A trapezoidal channel is needed for a location where the bed slope is 0.008 ft/ft, discharge...
    asked 4 minutes ago
  • List and briefly explain each step in the ABCDE technique for examining irrational beliefs that contribute...
    asked 23 minutes ago
  • 1. Find the general solution of the first order linear differential equation: 2*x*dy/dx -y-3/sqrt(x)=0. sqrt() =...
    asked 45 minutes ago
  • Fairfield Homes is developing two parcels near Pigeon Fork, Tennessee. In order to test different advertising...
    asked 50 minutes ago
  • . For each of the following questions, say whether the random process is reasonably a binomial...
    asked 1 hour ago
  • Please discuss why empathy is so important in light of current events. Please give specific examples
    asked 1 hour ago
  • Describe ONE thing you learned from either Peter singer or Tibor Machan author that compelled you...
    asked 1 hour ago
  • Global logistics firms such as DHL Supply Chain and Global Forwarding or C. H. Robinson Worldwide...
    asked 1 hour ago
  • Please match each factor in adoption of a new product or service to the best match...
    asked 1 hour ago
  • Fatty Acid Synthesis Assignment Explain how the activation of acetyl-CoA carboxylase prevents excess citrate in the...
    asked 1 hour ago
  • 1. Discuss and give an example of how a person might “constructively” enter a building of...
    asked 1 hour ago