次のエラーが発生する理由と、プログラムが正しいかどうかを誰かが提案できますか? プログラムで次のエラーが発生します。入力ファイルの結果が得られません
2 3
1 2 3
3 4 5
3 4
3 5 6 7
1 3 4 8
2 3 5 6
私のプログラムは
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int m,p,q,n; Matrix A has dimensions m, n and B has dimensions p,q
int **a, ** b, **c;
void *mulrow(void* row);
void print(int** a,int _i, int _j)
{
int i,j;
for (i = 0; i < _i; ++i) {
for (j = 0; j < _j; ++j)
printf("%d ",a[i][j]);
printf("\n");
}
}
void writeMat(int** a,int _i, int _j, FILE* f)
{
int i,j;
for (i = 0; i < _i; ++i) {
for (j = 0; j < _j; ++j)
fprintf(f,"%d ",a[i][j]);
fprintf(f,"\n");
}
}
void matrix_multiply()
{
// Every row of matrix is computed
int i,r;
pthread_t** threads = (pthread_t**) malloc(sizeof(pthread_t*)*m);
int* rows = (int*) malloc(sizeof(int)*m);
for (i = 0; i < m; ++i) {
threads[i] =(pthread_t*) malloc(sizeof(pthread_t));
rows[i] = i;
r = pthread_create(threads[i], NULL, mulrow, (void*) &rows[i]);
if (r<0)
printf("failed to create thread at mul2 at i = %d\n",i);
}
for (i = 0; i < m; ++i)
pthread_join(*threads[i],NULL);
free(threads);
free(rows);
}
void *mulrow(void* row)
{
int j, i = *((int*)row) ,k;
for (j = 0 ; j < n ; j ++)
for (k = 0 ; k < p ; k++)
c[i][j] += a[i][k] * b[k][j];
pthread_exit(NULL);
}
int **read_matrix(FILE *f,int m, int n)
{
int i,j;
int **a = (int**) malloc(sizeof(int *)*m);
for (i = 0; i < m; ++i) {
a[i] = (int *) malloc(sizeof(int)*n);
for (j = 0; j < n; ++j) {
fscanf(f,"%d",&a[i][j]);
}
return a;
}
int main(void)
{
int i,j;
FILE* in = fopen("input.txt","r");
fscanf(in,"%d %d",&m,&p);
a = read_matrix(in,m,p);
fscanf(in,"%d %d",&q,&n);
b = read_matrix(in,q,n);
if (p!=q){
printf("incompatible matrices --not supported\n");
return 0;
}
c = (int**) malloc(sizeof(int *)*m);
for (i = 0; i < m; ++i){
c[i] = (int *) malloc(sizeof(int)*n);
for (j = 0; j < n; ++j)
c[i][j] = 0;
}
printf("Result of multiplication \n");
matrix_multiply();
return 0;
}