Advertisement
Es7evam

progconc mat mult

Aug 27th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <error.h>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define N 8 // N <= 8
  8.  
  9. typedef struct {
  10.     int tam, row, c;
  11.     double (*MA)[N], (*MB)[N], (*MC)[N];
  12. }matrix_type_t;
  13.  
  14. typedef struct {
  15.     int l, r;
  16.     matrix_type_t mat;
  17.     int result;
  18. } param;
  19.  
  20. void* merge(void *w){
  21.     param* args;
  22.     args = (param*)w;
  23.     int l = args->l;
  24.     int r = args->r;
  25.     int row = args->mat.row;
  26.     int col = args->mat.c;
  27.     //cout << "row = " << row << endl;
  28.     if(l == r) {
  29.         args->result = args->mat.MA[row][l] * args->mat.MB[l][col]; //conferir
  30.         pthread_exit(NULL);
  31.     }else{
  32.         param *left, *right;
  33.         left = (param*)malloc(sizeof(param));
  34.         right = (param*)malloc(sizeof(param));
  35.  
  36.         // copiar dados
  37.  
  38.         int mid = (l + r)/2;
  39.  
  40.         left->l = l;
  41.         left->r = mid;
  42.         right->l = mid+1;
  43.         right->r = r;
  44.         pthread_t tleft, tright;
  45.         pthread_create(&tleft, NULL, merge, (void*)left);
  46.         pthread_create(&tright, NULL, merge, (void*)right);
  47.  
  48.         args->result = left->result + right->result;
  49.  
  50.         pthread_join(tleft, NULL);
  51.         pthread_join(tright, NULL);
  52.         free(left);
  53.         free(right);
  54.     }
  55.     return NULL;
  56. }
  57.  
  58. // chamar para cada linha e coluna com l=0 e r maximo
  59. void *mult(void *w){
  60.     matrix_type_t *work = (matrix_type_t *) w;
  61.     int row = work->row, c = work->c;
  62.     work-> MC[row][c] = 0;
  63.     for(int i=0;i<work->tam;i++){
  64.         work->MC[row][c] += work->MA[row][i] * work->MB[i][c];
  65.     }
  66.     return NULL;
  67. }
  68.  
  69.  
  70. int main(void){
  71.     int r, c;
  72.     double MA[N][N], MB[N][N], MC[N][N];
  73.  
  74.     for(int i=0;i<N;i++){
  75.         for(int j=0;j<N;j++){
  76.             MA[i][j] = MB[i][j] = 1;
  77.             MC[i][j] = 0;
  78.         }
  79.     }
  80.  
  81.     matrix_type_t *work;
  82.     pthread_t thread[N*N];
  83.     for(r=0;r<N;r++){
  84.         for(c=0;c<N;c++){
  85.             work = (matrix_type_t *) malloc(sizeof(matrix_type_t));
  86.             work->tam = N;
  87.             work->row = r;
  88.             work->c = c;
  89.             work->MA = MA;
  90.             work->MB = MB;
  91.             work->MC = MC;
  92.             pthread_create (&(thread[c+r*8]), NULL, mult, (void*) work);
  93.         }
  94.     }
  95.  
  96.     for(int i=0;i<N*N;i++)
  97.         pthread_join(thread[i], NULL);
  98.  
  99.     for(int i=0;i<N;i++){
  100.         for(int j=0;j<N;j++){
  101.             printf("%d ", MC[i][j]);
  102.         }
  103.         printf("\n");
  104.     }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement