Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. //
  2. // main.c
  3. // matrix_multiplication_with_threads
  4. //
  5. // Created by Ahmed on 10/18/17.
  6. // Copyright © 2017 Abdellah. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <pthread.h>
  12.  
  13. typedef long long ll;
  14.  
  15. struct Matrix {
  16. ll **mat;
  17. int n,m;
  18.  
  19. } a, b, res1, res2;
  20.  
  21.  
  22. // this function to allocate matrix
  23. void allocate(struct Matrix *a,int n,int m) {
  24. a->n = n , a->m = n;
  25. a->mat = (ll**) malloc(n*sizeof(ll*));
  26. for(int i=0; i < n ; ++i)
  27. a->mat[i] = (ll*) malloc(m*sizeof(ll));
  28.  
  29. for(int i=0 ; i < n ; ++i)
  30. for (int j=0; j < m; ++j)
  31. a->mat[i][j] = 0;
  32. }
  33.  
  34. // this struct for identifying the cell in method_1 thread
  35. struct cell {
  36. int i,j;
  37. };
  38.  
  39. // this method for computing the cell in (method_1)
  40. void * compute_cell(void *args) {
  41.  
  42. int i = ((struct cell*) args)->i , j = ((struct cell*) args)->j;
  43. printf("cell:%d %d\n",i,j);
  44.  
  45. for(int k = 0; k < b.n ; ++k)
  46. res1.mat[i][j] += a.mat[i][k]*b.mat[k][j];
  47.  
  48. pthread_exit(0);
  49. }
  50.  
  51.  
  52.  
  53.  
  54. // this funciton for calculating result matrix each elemnt in it copmuted in separate thread
  55. void method_1 () {
  56.  
  57. pthread_t threads[a.n][b.m];
  58. for(int i=0 ; i < a.n ; ++i) {
  59. for (int j=0; j < b.m ; ++j) {
  60. struct cell* args = (struct cell*) malloc(sizeof(struct cell));
  61. args->i = i , args->j = j;
  62. pthread_attr_t attr;
  63. pthread_attr_init(&attr);
  64. int rc = pthread_create(&threads[i][j], &attr, compute_cell, (void*) args);
  65. if(rc)
  66. printf("Error, happened in creating thread int Method_1 cell:%d %d and returns %d.",i,j,rc);
  67. free(args);
  68. }
  69. }
  70.  
  71. // finish each thread and each cell in matrix
  72. for(int i=0 ; i < a.n ; ++i)
  73. for (int j=0; j < b.m; ++j)
  74. pthread_join(threads[i][j], NULL);
  75.  
  76. }
  77.  
  78.  
  79. // this function for computing the roww in (method_2)
  80. void *compute_row (void *args) {
  81.  
  82. int *row = (int*) args;
  83.  
  84. for(int j=0 ; j < b.m ; ++j)
  85. for(int k=0 ; k < b.n ; ++k)
  86. res2.mat[*row][j] += a.mat[*row][k]*b.mat[k][j];
  87.  
  88. pthread_exit(0);
  89. }
  90.  
  91. // this function for calculating result matrix each row in it computed in separate thread
  92. void method_2 () {
  93. pthread_t threads[a.n];
  94.  
  95. for(int i=0; i < a.n ;++i) {
  96. int args = i;
  97. pthread_attr_t attr;
  98. pthread_attr_init(&attr);
  99. int rc = pthread_create(&threads[i], &attr, compute_row, &args);
  100. if(rc)
  101. printf("Error, happened in creating thread int Method_2 row:%d and returns %d.",i,rc);
  102.  
  103. }
  104. // finish each threa and each row in matrix
  105. for (int i=0; i < a.n ; ++i)
  106. pthread_join(threads[i], NULL);
  107.  
  108. }
  109.  
  110. // this function for reading and allocating each matrix
  111. void read_matrix(struct Matrix *a) {
  112. int n,m;
  113. scanf("%d%d",&n,&m);
  114.  
  115. allocate(a,n,m);
  116.  
  117. for(int i=0; i < a->n ; ++i)
  118. for(int j=0 ; j < a->m ; ++j)
  119. scanf("%lld",&a->mat[i][j]);
  120.  
  121. }
  122.  
  123. void print(struct Matrix *a) {
  124. for(int i=0; i < a->n ; ++i) {
  125. for(int j=0 ; j < a->m ; ++j)
  126. printf("%lld\t",a->mat[i][j]);
  127. printf("\n");
  128. }
  129. }
  130.  
  131. void true_ans() {
  132.  
  133. printf("---------- true answer ----------\n");
  134. for(int i=0;i<a.n;++i){
  135. for(int j=0;j<b.m;++j){
  136. ll cell = 0;
  137. for(int k=0;k<b.n;++k) cell += a.mat[i][k]*b.mat[k][j];
  138. printf("%lld\t",cell);
  139. }
  140. printf("\n");
  141. }
  142. }
  143. int main(int argc, const char * argv[]) {
  144.  
  145.  
  146. read_matrix(&a) , read_matrix(&b);
  147. allocate(&res1, a.n, b.m) , allocate(&res2, a.n, b.m);
  148. printf("Method_ 1:\n");
  149. method_1() , print(&res1);
  150. printf("Method_ 2:\n");
  151. method_2() , print(&res2);
  152. true_ans();
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement