Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. struct poz {
  7. int row, col;
  8. };
  9.  
  10. void *prod(void *param);
  11.  
  12. int m, n, p;
  13. int A[100][100];
  14. int B[100][100];
  15. int C[100][100];
  16.  
  17. int main(int argc, char *argv[]) {
  18. /// Citire
  19. FILE *fA = fopen(argv[1], "r");
  20. fscanf(fA, "%d %d", &m, &p);
  21. for(int i = 0; i < m; i++) {
  22. for(int j = 0; j < p; j++) {
  23. fscanf(fA, "%d", &(A[i][j]));
  24. }
  25. }
  26. FILE *fB = fopen(argv[2], "r");
  27. fscanf(fB, "%d %d", &p, &n);
  28. for(int i = 0; i < p; i++) {
  29. for(int j = 0; j < n; j++) {
  30. fscanf(fB, "%d", &(B[i][j]));
  31. }
  32. }
  33.  
  34. pthread_attr_t attr;
  35. pthread_t workers[100][100];
  36.  
  37. pthread_attr_init(&attr);
  38. for(int i = 0; i < m; i++) {
  39. for(int j = 0; j < n; j++) {
  40. poz param;
  41. param.row = i;
  42. param.col = j;
  43. pthread_create(&(workers[i][j]), &attr, prod, &param);
  44. }
  45. }
  46. for(int i = 0; i < m; i++) {
  47. for(int j = 0; j < n; j++) {
  48. pthread_join(&(workers[i][j]), NULL);
  49. }
  50. }
  51. }
  52.  
  53. void *prod(void *param) {
  54. int row = (*poz)(param) -> row;
  55. int col = (*poz)(param) -> col;
  56. C[row][col] = 0;
  57. for(int i = 0; i < p; i++) {
  58. C[row][col] += A[row][p] * B[p][col];
  59. }
  60. pthread_exit(0);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement