I am trying to do matrix multiplication using pthreads and creating one thread for each computation of each row instead of each element. Suppose there are two matrices
A[M][K],B[K][N] . Where am I going wrong ?
int A[M][K];
int B[K][N];
int C[][];
void *runner (void *param);
struct v
{
int i;
int j;
};
pthread_t tid[M];
for (i = 0; i < M; i++) // It should create M threads
{
struct v *data = (struct v *) malloc (sizeof (struct v));
data->i = i;
data->j = j;
pthread_create (&tid[count], &attr, runner, data);
pthread_join (tid[count], NULL);
count++;
}
runner (void *param) //
{
struct v *test;
int t = 0;
test = (struct v *) param;
for (t = 0; t < K; t++) // I want to compute it for a row instead of an element
{
C[test->i][test->j] = C[test->i][test->j] + A[test->i][t] * B[t][test->j];
}
pthread_exit (0);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…