Advertisement
Guest User

CPd

a guest
Oct 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // Lab03.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include "omp.h"
  7.  
  8.  
  9.  
  10. void calcul_paralel(int **a , int **b ,int **c, int n) {
  11. #pragma omp parallel num_threads(16)
  12. {
  13. #pragma omp for
  14. for (int i = 0; i < n; i++) {
  15. for (int j = 0; j < n; j++) {
  16. c[i][j] = 0;
  17. for (int k = 0; k < n; k++)
  18. c[i][j] = c[i][j] + a[i][k] * b[k][j];
  19. }
  20. }
  21. }
  22. }
  23.  
  24.  
  25. void calcul_secvential(int **a, int **b, int **c, int n) {
  26. for (int i = 0; i < n; i++) {
  27. for (int j = 0; j < n; j++) {
  28. c[i][j] = 0;
  29. for (int k = 0; k < n; k++)
  30. c[i][j] = c[i][j] + a[i][k] * b[k][j];
  31. }
  32. }
  33. }
  34.  
  35.  
  36.  
  37.  
  38. int main()
  39. {
  40. int n;
  41. std::cout << "Intoduceti dimesiunea matricilor:";
  42. std::cout<<std::endl;;
  43. std::cin >> n;
  44.  
  45. int **a = (int **)malloc(n * sizeof(int*));
  46. int **b = (int **)malloc(n * sizeof(int*));
  47. int **c = (int **)malloc(n * sizeof(int*));
  48. int **d = (int **)malloc(n * sizeof(int*));
  49.  
  50. for (int i = 0; i < n; i++) {
  51. a[i] = (int *)malloc(n * sizeof(int));
  52. b[i] = (int *)malloc(n * sizeof(int));
  53. c[i] = (int *)malloc(n * sizeof(int));
  54. d[i] = (int *)malloc(n * sizeof(int));
  55. }
  56.  
  57. for (int i = 0; i < n; i++) {
  58. for (int j = 0; j < n; j++)
  59. {
  60. a[i][j] = 1;
  61. b[i][j] = 1;
  62. }
  63. }
  64.  
  65.  
  66.  
  67. double t1, t2, elapsed_seconds, t3, t4, elapsed_seconds2;
  68.  
  69. t1 = omp_get_wtime();
  70. calcul_paralel(a, b, c, n);
  71. t2= omp_get_wtime();
  72. elapsed_seconds = t2 - t1;
  73.  
  74.  
  75. t3 = omp_get_wtime();
  76. calcul_secvential(a, b, d, n);
  77. t4 = omp_get_wtime();
  78. elapsed_seconds2 = t4 - t3;
  79.  
  80. //for (int i = 0; i < n; i++) {
  81. // for (int j = 0; j < n; j++) {
  82. // std::cout << d[i][j] << " ";
  83. // }
  84. // std::cout << std::endl;
  85. //}
  86.  
  87. std::cout << std::endl;
  88.  
  89. std::cout << " Timp paralel :" << elapsed_seconds << std::endl;
  90. std::cout << " Timp secvential :" << elapsed_seconds2 << std::endl;
  91. std::cout << " Raport timp :" << elapsed_seconds2 / elapsed_seconds << std::endl;
  92.  
  93. system("pause");
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement