Hello. I am currently working on a school project. I need to implement 4x4 matrix multiplication with multi-threads (pthread) doing each multiplication. By studying the assignment description and other codes found online, I was able to get a general idea of what I need to do. However, I am having trouble figuring out how to obtain input for the matrices. I am used to using a for loop with cin function to get each row, column input.
for (row=0; row<height; row++) {
for (column=0;column<width;column++) {
cin>>matrix[row][column];
}
}
However, the professor requires me to use
void matrix_input(int mat[][dim], char* name)
implementation to get input. I am stumped. Also because I cannot figure out how to get input, I am having trouble implementing the calculation and print functions too. Please provide help as to how I can implement the input function. Here is what I have come up with so far. Thank you!
#include <pthread.h>
#include <iostream>
#include <iomanip>
using namespace std;
struct v {
int i; // row
int j; // column
};
const int dim = 4;
int A[dim][dim];
int B[dim][dim];
int C[dim][dim];
//void *calc_mat( void *param ); // the thread
//void matrix_input( int mat[][dim], char* name);
//void matrix_print( int mat[][dim] );
v matrix;
int main() {
// get input for matrix A and B
// matrix_input( mat[][dim], A);
// matrix_input( mat[][dim], B);
// thread indentifier
pthread_t tid[dim][dim];
// set of thread attributes
pthread_attr_t attr;
// get the dedault attributes
pthread_attr_init( &attr );
// create threads
for( i=0 ; i < dim ; i++ ) {
for( j=0 ; j < dim ; j++ ) {
struct v *data = (struct v *)
malloc(sizeof(struct v));
data->i = i;
data->j = j;
pthread_create( &tid[i][j], NULL, calc, &data);
}
}
// wait for the threads to end
for( int c=0 ; c < dim ; c++ )
pthread_join( tid[c], NULL );
// print out the matrix
return 0;
}
//---------------------------------------------------------------
void matrix_input( int mat[][dim], char* name ) {
}
//---------------------------------------------------------------
void matrix_print( int mat[][dim] ) {
}
//---------------------------------------------------------------
void* calc_mat( void* param ) {
//multiply A[i][j] * B[i][j] and store in C[i][j]
pthread_exit( 0 );
}