multithreading - array and vector multiplication using threads c language -
multithreading - array and vector multiplication using threads c language -
i'm writing programme uses threads compute production of array 1 dimensional array, dimensions equal "n". each thread must compute production of row of array 1 dimensional array. output i'm getting seems have got addresses values instead of values entered matrix elements. doing wrong? here's code wrote:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #define mat_dim 5 static struct param { int mat[mat_dim][mat_dim]; int vec[mat_dim]; int ind; int alter[mat_dim]; }; void *calculate_row(struct param tid) { int i; (i=0; i<5; i++) { tid.alter[tid.ind] = tid.alter[tid.ind]+tid.mat[tid.ind][i]*tid.vec[i]; } pthread_exit((void *)&tid); } int main (int argc, char *argv[]) { pthread_t thread[mat_dim]; pthread_attr_t attr; int rc; long t; void *status; int th_array[5][5]={{1,4,3,5,1},{4,6,2,8,5},{3,5,1,3,6},{1,5,6,2,8},{4,7,5,3,6}}; int th_vec[5]={1,2,1,2,1}; struct param thread_parameter; thread_parameter.mat[5][5]=th_array; thread_parameter.vec[5]=th_vec; int tmp[5]={0,0,0,0,0}; thread_parameter.alter[5]=tmp; /* initialize , set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, pthread_create_joinable); for(t=0; t<mat_dim; t++) { printf("main: creating thread %ld\n", t); thread_parameter.ind=t; rc = pthread_create(&thread[t], &attr, calculate_row,&thread_parameter); if (rc) { printf("error; homecoming code pthread_create() %d\n", rc); exit(-1); } } /* free attribute , wait other threads */ pthread_attr_destroy(&attr); printf("the result vector : "); for(t=0; t<mat_dim; t++) { rc = pthread_join(thread[t], null); if (rc) { printf("error; homecoming code pthread_join() %d\n", rc); exit(-1); } printf("%d, ",thread_parameter.alter[t]); } printf("main: programme completed. exiting.\n"); pthread_exit(null); }
you calling rc = pthread_create(&thread[t], &attr, calculate_row,&thread_parameter); struct param thread_parameter;
and function void *calculate_row(struct param tid) should void *calculate_row(struct param *tid) pointer passed , alter . ->.
c multithreading
Comments
Post a Comment