Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. struct v
  6. {
  7. int i ; /* row */
  8. int j ; /* column */
  9. };
  10.  
  11. #define NUM_THREADS 10
  12.  
  13. #define M 3
  14. #define K 2
  15. #define N 3
  16. int A [M] [K] = {{1,4}, {2,5}, {3,6}};
  17. int B [K] [N] = {{8,7,6}, {5,4,3}};
  18. int C [M] [N] ;
  19.  
  20. void *runner(void *param);
  21.  
  22. int main(int argc, char* argv[]){
  23.  
  24. pthread_t workers[NUM_THREADS];
  25. for (int i = 0; i < M; i++){
  26. for (int j = 0; j < N; j++ ){
  27. struct v *data = (struct v *) malloc (sizeof (struct v)) ;
  28. data->i = i;
  29. data->j = j;
  30. pthread_create(&workers[j + M*i], NULL, runner, data);
  31. //pthread_join(workers[i], NULL);
  32. }
  33. }
  34.  
  35. for (int i = 0; i < NUM_THREADS; i++)
  36. pthread_join(workers[i], NULL);
  37.  
  38.  
  39. for(int i = 0; i < M; ++i){
  40. for(int j = 0; j < N; ++j)
  41. printf("%d ",C[i][j]);
  42. printf("\n");
  43. }
  44.  
  45. return 0;
  46.  
  47. }
  48.  
  49. void *runner(void *param){
  50. struct v *data = (struct v *)param;
  51.  
  52.  
  53. for(int x = 0; x < K; ++x)
  54. C[data->i][data->j] += A[data->i][x] * B[x][data->j];
  55.  
  56. pthread_exit(0);
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement