c - How to use thread pool and message queues in Multithreaded Matrix Multiplication? -


i'm trying learn multithreading doing multithreaded matrix multiplication program.i'm computing 1 row @ time. facing problem when using fewer threads rows. read lot of similar posts not understand how can reuse them. there 2 possible methods.

  1. using thread pool , making task queue- did not understand after completion of task , how next task assigned particular thread among pool of threads

  2. message queues.

how use mutex lock on shared variable sum?

please suggest me possible changes should add in following program.

#include <pthread.h> #include <stdio.h> #include <stdlib.h>  #define m 6 #define k 7 #define n 8 #define num_threads 4  int a[m][k] = { { 1, 4, 8, 4, 5, 6, 2 }, { 7, 3, 2, 4, 1, 4, 5 }, { 2, 3, 9, 4,         7, 1, 5 }, { 4, 3, 9, 4, 7, 2, 5 }, { 1, 3, 9, 9, 7, 1, 3 }, { 2, 4, 9,         3, 7, 1, 5 } }; int b[k][n] = { { 8, 3, 8, 4, 5, 6, 2, 3 }, { 1, 3, 2, 2, 3, 4, 8, 1 }, { 8, 3,         9, 1, 7, 1, 5, 2 }, { 1, 3, 9, 2, 7, 2, 5, 2 },         { 1, 3, 9, 2, 7, 1, 3, 3 }, { 2, 4, 9, 3, 7, 1, 5, 2 }, { 2, 4, 9, 3, 7,                 1, 5, 2 } }; 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;     (i = 0; < num_threads; i++) {          //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[num_threads];       //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     (i = 0; < m; i++) {         (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, x, sum = 0; //the counter , sum      //calculating 1 row      (x = 0; x < n; x++) {         (n = 0; n < k; n++)          {             sum += a[data->i][n] * b[n][x];         }         //assign sum coordinate         c[data->i][data->j] = sum;     }     //exit thread     pthread_exit(0); // how can reuse thread row instead of exiting ? } 

any appreciated!

how use mutex lock on shared variable sum?

you need declare mutex , use lock sum before use sum ,and unlock after use sum.do every time use sum protect it.

here example :

pthread_mutex_t lock = pthread_mutex_initializer;  pthread_mutex_lock(&lock);  use sum ....  pthread_mutex_unlock(&lock); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -