Wednesday, 15 June 2011

multithreading - Errors in Multithreaded program in C -



multithreading - Errors in Multithreaded program in C -

could suggest why getting next errors , if programme right ? getting next errors in program.i not getting result input file 2 3 1 2 3 3 4 5 3 4 3 5 6 7 1 3 4 8 2 3 5 6 programme

#include <pthread.h> #include <stdio.h> #include <stdlib.h> int m,p,q,n; matrix has dimensions m, n , b has dimensions p,q int **a, ** b, **c; void *mulrow(void* row); void print(int** a,int _i, int _j) { int i,j; (i = 0; < _i; ++i) { (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; (i = 0; < _i; ++i) { (j = 0; j < _j; ++j) fprintf(f,"%d ",a[i][j]); fprintf(f,"\n"); } } void matrix_multiply() { // every row of matrix computed int i,r; pthread_t** threads = (pthread_t**) malloc(sizeof(pthread_t*)*m); int* rows = (int*) malloc(sizeof(int)*m); (i = 0; < 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 create thread @ mul2 @ = %d\n",i); } (i = 0; < m; ++i) pthread_join(*threads[i],null); free(threads); free(rows); } void *mulrow(void* row) { int j, = *((int*)row) ,k; (j = 0 ; j < n ; j ++) (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); (i = 0; < m; ++i) { a[i] = (int *) malloc(sizeof(int)*n); (j = 0; j < n; ++j) { fscanf(f,"%d",&a[i][j]); } homecoming a; } int main(void) { int i,j; file* in = fopen("input.txt","r"); fscanf(in,"%d %d",&m,&p); = 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"); homecoming 0; } c = (int**) malloc(sizeof(int *)*m); (i = 0; < m; ++i){ c[i] = (int *) malloc(sizeof(int)*n); (j = 0; j < n; ++j) c[i][j] = 0; } printf("result of multiplication \n"); matrix_multiply(); homecoming 0; }

in program, need comment out 1 statement.

int m,p,q,n; //matrix has dimensions m, n , b has dimensions p,q

there 1 closing brace missing @ lastly line i.e. after return 0;

with alter , linking pthread library should compile program.

gcc -o test_exe file.c -lpthread

i have been successful in compiling code.

c multithreading matrix-multiplication

No comments:

Post a Comment