Advertisement
filip710

rrs_lv4

Sep 10th, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. 1. Sadrži ovisnosti, pristupa se prethodnim iteracijama petlji, više iteracija mijenja jednu varijablu.
  2.  
  3. 2. #include <omp.h>
  4. #include <iostream>
  5. #include <time.h>
  6. #include <cstdlib>
  7. using namespace std;
  8. int main(int argc, char* argv[]) {
  9. int n = 10500;
  10. //clock_t t1,t2;
  11. double t1, t2, t3, t4;
  12. int* v = new int[n];
  13. int** m = new int*[n];
  14. for(int i = 0; i < n; ++i)
  15. m[i] = new int[n];
  16.  
  17. int* rezultat = new int[n];
  18. int zbroj = 0;
  19. int zbroj_serial = 0;
  20. srand (time(NULL));
  21. for(int i=0; i<n; i++){
  22. v[i] = rand() % 100 + 1;
  23. rezultat[i] = 0;
  24. }
  25.  
  26. for(int i=0; i<n; i++){
  27. for(int j=0; j<n; j++) {
  28. m[i][j] = rand() % 100 + 1;
  29. }
  30. }
  31.  
  32. omp_set_num_threads(4);
  33. cout << "Množač: ";
  34. for(int i=0; i<n; i++){
  35. //cout<< v[i] << "\t";
  36. }
  37. cout << "\n";
  38. cout << "Matrica_before" << endl;
  39. for(int i=0; i<n; i++){
  40. for(int j=0; j<n; j++) {
  41. //cout << m[i][j] << "\t";
  42. }
  43. //cout << "\n";
  44. }
  45. cout << "Number_niti_active: " << omp_get_num_threads() ;
  46. cout << "\n";
  47. //t1 = clock();
  48. t1 = omp_get_wtime();
  49. #pragma omp parallel for firstprivate(zbroj)
  50. for(int i=0; i<n; i++){
  51. for(int j=0; j<n; j++) {
  52. zbroj += v[i]*m[j][i];
  53. }
  54. rezultat[i] = zbroj;
  55. zbroj = 0;
  56. //cout << "Hi, i am nit: " << omp_get_thread_num() << ". I work with redak " << i << ". Bye!" << "\n";
  57. }
  58. //t2 = clock();
  59. t2 = omp_get_wtime();
  60. cout << "Rezultati: ";
  61. for(int i=0; i<n; i++){
  62. //cout<< rezultat[i] << "\t";
  63. }
  64. cout << "\n";
  65. //cout << "Vrijeme_time: " << ((float)(t2-t1))/CLOCKS_PER_SEC << "\n";
  66. cout << "Vrijeme_time_parallel: " << t2-t1 << " s" << "\n";
  67.  
  68. //serijski dio
  69. t3 = omp_get_wtime();
  70.  
  71. for(int i=0; i<n; i++){
  72. for(int j=0; j<n; j++) {
  73. zbroj_serial += v[i]*m[j][i];
  74. }
  75. rezultat[i] = zbroj_serial;
  76. zbroj_serial = 0;
  77. }
  78. //t2 = clock();
  79. t4 = omp_get_wtime();
  80. cout << "Vrijeme_time_serial: " << t4-t3 << " s" << "\n";
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement