Sunday, 27 January 2013

Complete multithreaded Matrix multiply program in C

I am not the author of this code but I have integrated it to while doing assignment I just forgot those links but anyway I said it, its not my code.
Also if you think doing this is good or bad please comment that will be a good feedback so may be I can improve things in future.
//starting of code
#include<stdio.h>   
#include<pthread.h>
#define ARRAY_SIZE 3
       
    typedef int matrix_t[ARRAY_SIZE][ARRAY_SIZE];   
    matrix_t MAm,MBm,MCm;
    /* main: allocates matrix, assigns values, computes the results */   
    typedef struct {   
    int       id;   
    int       size;   
    int       row;   
    int       column;   
    matrix_t  *MA;   
    matrix_t  *MB;   
    matrix_t  *MC;   
    } matrix_work_order_t;   

void mult(int size,             /* size of the matrix */   
              int row,              /* row of result to compute */   
              int column,           /* column of result to compute */   
              matrix_t MA,          /* input matrix */   
              matrix_t MB,          /* input matrix */   
              matrix_t MC) {       /* result matrix */   
       
          int position;   
       
          MC[row][column] = 0;   
          for(position = 0; position < size; position++) {   
                MC[row][column] = MC[row][column] +   
                  ( MA[row][position]  *  MB[position][column] ) ;   
          }   
    }
    /*   
    * Routine to start off a peer thread   
    */   
       
    void peer_mult(matrix_work_order_t *work_orderp)   
    {   
       
    mult(work_orderp->size,   
         work_orderp->row,   
         work_orderp->column,   
         *(work_orderp->MA),   
         *(work_orderp->MB),   
         *(work_orderp->MC));   
       
    free(work_orderp);   
    }   
   
    extern int   
    main(void) {   
        int size = ARRAY_SIZE, row, column;   
          /* Fill in matrix values, currently values are hardwired */
  for (row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      MAm[row][column] = 1;
    }
  }
  for (row = 0; row < size; row++) {
    for (column = 0; column < size; column++) {
      MBm[row][column] = row + column + 1;
    }
  }
  printf("MATRIX MAIN THREAD: The A array is is;\n");
  for(row = 0; row < size; row ++) {
    for (column = 0; column < size; column++) {
      printf("%5d ",MAm[row][column]);
    }
    printf("\n");
  }
  printf("MATRIX MAIN THREAD: The B array is is;\n");
  for(row = 0; row < size; row ++) {
    for (column = 0; column < size; column++) {
      printf("%5d ",MBm[row][column]);
    }
    printf("\n");
    }
       
        matrix_work_order_t *work_orderp;   
        pthread_t peer[size*size];       
        /* Process Matrix, by row, column */   
        int id=0 ;
        for(row = 0; row < size; row++)     {   
          for (column = 0; column < size; column++) {   
               id = column + row*3;   
               work_orderp =(matrix_work_order_t *)malloc(sizeof(matrix_work_order_t));   
               work_orderp->id = id;   
               work_orderp->size = size;   
               work_orderp->row = row;   
               work_orderp->column = column;   
               work_orderp->MA = &MAm;   
               work_orderp->MB = &MBm;   
               work_orderp->MC = &MCm;   
       
               pthread_create(&(peer[id]), NULL, (void *)peer_mult,   
                               (void *)work_orderp);   

          }   
        }   
        int i=0;   
               /* Wait for peers to exit */   
        for (i = 0; i < (size * size); i++) {   
           pthread_join(peer[i], NULL);   
        }       
printf("MATRIX: The resulting matrix C is:\n");   
          for(row = 0; row < size; row ++) {   
            for (column = 0; column < size; column++) {   
            printf("%5d ",MCm[row][column]);   
            }   
          printf("\n");   
          }
        return 0;   
    }

No comments:

Post a Comment