# Homework Help: Multiply matrices using threads

1. Feb 14, 2012

### cs23

1. The problem statement, all variables and given/known data
In my main function i am filling 2 matrices. Matrix A is 18x16 and MatrixB is 16x18. Then i am multiplying them in my thread function using an array of threads. However, i am getting segmentations faults when trying to run the program.

Code (Text):
#include<stdio.h>

int mata[18][16];
int matb[16][18];
int matc[18][18];

void* compute_c_ij(void* arg)
{
int k;
int n= *((int*)arg);

for(k=0;k<16;++k)
{
matc[n][n] += mata[n][k] + matb[k][n];
}

}

int main()
{

int n,m;

int i,j;

for(i=0;i<18;++i)
for(j=0;j<16;++j)
{
mata[i][j] = (i+1) +(j+1);
}

for (i=0; i<16; ++i)
for(j=0;j<18;++j)
{
matb[i][j]= (i+1)+(2*(j+1));
}

for (i=0;i<18 ; ++i)
{
}

for (i=0 ; i<18 ; ++i)
{
printf("%d\n",matc[i][i]);
}

}

1. The problem statement, all variables and given/known data

2. Relevant equations

3. The attempt at a solution

Last edited: Feb 14, 2012
2. Feb 14, 2012

### Staff: Mentor

Just an educated guess, but I don't think you are passing the right parameters into pthread_create. Your start function, compute_c_ij takes a parameter, and this is what should go in the 4th parameter of pthread_create (where you have NULL).

I found this example, which might be helpful, although much of it is in Spanish - http://www.dreamincode.net/forums/topic/53953-pthreadh/.