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);

// set of thread attributes

// get the dedault attributes

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]

}``````
2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by noi80

>>void matrix_input(int mat[][dim], char* name)
What is variable name? It doesn't make much sense in the context of that function.

>>void matrix_input(int mat[][dim], char* name)
What is variable name? It doesn't make much sense in the context of that function.

Thank you for taking a look at the code. I can not figure out why the professor has included the char* argument. Do you think I should be able to get away without that argument? For example

``void matrix_input( int mat[][dim] ) {}``

Okay I was able to get most of the code done. Everything compiles and I get the prompt to input the two matrices. However, after I enter the second matrix and hit enter the program crashes with error "Segmentation Fault (core dumped)". Please help me find the error. I am going crazy trying to figure this out. Here is the almost finished code XP

``````#include <pthread.h>
#include <iostream>
#include <stdlib.h>
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] );

int main() {

int i, j;

// get input for matrix A and B
matrix_input( A, "A" );
matrix_input( B, "B" );

// set of thread attributes

// get the dedault attributes

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;

calc_mat, (void*) data);
}
}

// wait for the threads to end
for( i=0 ; i < dim ; i++ ) {
for ( j=0 ; i < dim ; j++ ) {
pthread_join( tid[i][j], NULL );
}
}

// print out the matrix
matrix_print( A );
matrix_print( B );

return 0;

}

//---------------------------------------------------------------

void matrix_input( int mat[][dim], char* name ) {

int i, j;

cout << "Input Matrix " << name << ": ";

for( i=0 ; i < dim ; i++ ) {
for( j=0 ; j < dim ; j++ ) {
cin >> mat[i][j];
}
}

}

//---------------------------------------------------------------

void matrix_print( int mat[][dim] ) {

int i, j;

for( i=0 ; i < dim ; i++ ) {
for ( j=0 ; i < dim ; j++ ) {
cout << mat[i][j] << " ";
}
cout << endl;
}

}

//---------------------------------------------------------------

void* calc_mat( void* param ) {

int i, j, k;

for( i=0 ; i < dim ; j++ ) {
for( j=0 ; j < dim ; j++ ) {
for( k=0 ; k < dim ; k++ ) {
C[i][k] += A[i][j] * B[j][k];
}
}
}

//	pthread_exit( 0 );

}``````

Nevermind. I figured it out. I'll post if I have anymore problems.
XD

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.