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.
#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 ( ) ;
}
Get Answers For Free
Most questions answered within 1 hours.