Matrix Multiply with Threads (each thread performs a single multiplication)

I am looking to make a matrix multiply by threads, where each thread performs one multiplication, and then the main thread will add all the results and put them in the appropriate place in the final matrix (after the other threads came out).

The way I'm trying to do this is to create an array of strings containing the results of each thread. Then I will go through the array and add + put the results in the resulting matrix.

Example: if you have matrices:

A = [{1,4}, {2,5}, {3,6}] B = [{8,7,6}, {5,4,3}]

Then I need an array containing [8, 20, 7, 16, 6, 12, 16, etc.]. Then I go through the array, adding up every 2 numbers and putting them in my last array.

This is the purpose of HW, so I am not looking for the exact code, but some logic on how to properly store the results in an array. I'm struggling to keep track of where I am in each matrix so as not to miss any numbers.

Thank.

EDIT2: Remember that there must be one thread for each individual multiplication. The value for the above example will consist of 18 threads, each of which performs its own calculation.

EDIT: I use this code as the basis for work.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define M 3
#define K 2
#define N 3
#define NUM_THREADS 10

int A [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); /* the thread */

int main(int argc, char *argv[]) {

   int i,j, count = 0;
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         //Assign a row and column for each thread
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         /* Now create the thread passing it data as a parameter */
         pthread_t tid;       //Thread ID
         pthread_attr_t attr; //Set of thread attributes
         //Get the default attributes
         pthread_attr_init(&attr);
         //Create the thread
         pthread_create(&tid,&attr,runner,data);
         //Make sure the parent waits for all thread to complete
         pthread_join(tid, NULL);
         count++;
      }
   }

   //Print out the resulting matrix
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         printf("%d ", C[i][j]);
      }
      printf("\n");
   }
}

//The thread will begin control in this function
void *runner(void *param) {
   struct v *data = param; // the structure that holds our data
   int n, sum = 0; //the counter and sum

   //Row multiplied by column
   for(n = 0; n< K; n++){
      sum += A[data->i][n] * B[n][data->j];
   }
   //assign the sum to its coordinate
   C[data->i][data->j] = sum;

   //Exit the thread
   pthread_exit(0);
}

Source: http://macboypro.wordpress.com/2009/05/20/matrix-multiplication-in-c-using-pthreads-on-linux/

+5
source share
1 answer

, , , , . , C , , .. - :

#define NUM_THREADS 64
/*
 * struct to pass parameters to a dispatched thread 
 */
typedef struct {
  int   value;     /* thread number */
  char  somechar[128];   /* char data passed to thread */
  unsigned long ret;
  struct foo *row;
} thread_parm_t;

, *, foo. . , .

/*
 * the thread to actually crunch the row data
 */
void *thr_rowcrunch( void *parm );

pthread_t tid[NUM_THREADS]; /* POSIX array of thread IDs */

- :

thread_parm_t *parm=NULL;

- :

for ( i = 0; i < NUM_THREADS; i++) {
    parm = malloc(sizeof(thread_parm_t));
    parm->value = i;
    strcpy(parm->somechar, char_data_to-pass );
    fill_in_row ( parm->row, my_row_data );
    pthread_create(&tid[i], NULL, thr_insert, (void *)parm);
}

:

for ( i = 0; i < NUM_THREADS; i++)
    pthread_join(tid[i], NULL);

thr_rowcrunch (void * parm), , . , , .

, , .

0

All Articles