multithreading - Posix thread Program Multiplication - Inquire about Code blog How to check that it issues different threads -
found in below link blog showing example on matrix multiplication using posix thread, , mentioned each threads holding row , column information, below link , copied program reference
https://macboypro.wordpress.com/2009/05/20/matrix-multiplication-in-c-using-pthreads-on-linux/
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #define m 3 #define k 2 #define n 3 #define num_threads 10 int [m][k] = { {1,4}, {2,5}, {3,6} }; int b [k][n] = { {8,7,6}, {5,4,3} }; int c [m][n]; struct v { int i; /* row */ int j; /* column */ }; void *runner(void *param); /* thread */ int main(int argc, char *argv[]) { int i,j, count = 0; for(i = 0; < m; i++) { for(j = 0; j < n; j++) { //assign row , column each thread struct v *data = (struct v *) malloc(sizeof(struct v)); data->i = i; data->j = j; /* create thread passing data parameter */ pthread_t tid; //thread id pthread_attr_t attr; //set of thread attributes //get default attributes pthread_attr_init(&attr); //create thread pthread_create(&tid,&attr,runner,data); //make sure parent waits thread complete pthread_join(tid, null); count++; } } //print out resulting matrix for(i = 0; < m; i++) { for(j = 0; j < n; j++) { printf("%d ", c[i][j]); } printf("\n"); } } //the thread begin control in function void *runner(void *param) { struct v *data = param; // structure holds our data int n, sum = 0; //the counter , sum //row multiplied column for(n = 0; n< k; n++){ sum += a[data->i][n] * b[n][data->j]; } //assign sum coordinate c[data->i][data->j] = sum; //exit thread pthread_exit(0); }
inquiry : issuing different threads each different row/column ? , how can define different threads issued ?
Comments
Post a Comment