Advertisement
Guest User

Untitled

a guest
Dec 12th, 2020
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <chrono>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <vector>
  5. #include <type_traits>
  6. #include <iostream>
  7. #include <mutex>
  8. #include <thread>
  9.  
  10. template<typename T>
  11. struct AccessCheckVector : public std::vector<T>
  12. {
  13. const T & operator[](size_t idx) const noexcept
  14. {
  15. std::lock_guard lock(mutex);
  16.  
  17. for (size_t i = access_marks.size(); i <= idx; i++) {
  18. access_marks.push_back(false);
  19. }
  20. access_marks[idx] = true;
  21.  
  22. return this->data()[idx];
  23. }
  24.  
  25. mutable std::vector<bool> access_marks{};
  26. mutable std::mutex mutex{};
  27. };
  28.  
  29. uint64_t stable_rand()
  30. {
  31. static uint64_t state = 42;
  32. state = state * 6364136223846793005 + 1442695040888963407;
  33. return state;
  34. }
  35.  
  36. template<typename T>
  37. T find_max(const std::vector<T> & arr)
  38. {
  39. T max = 0;
  40. for (size_t i = 0; i < arr.size(); i++) {
  41. max = std::max(arr[i], max);
  42. }
  43. return max;
  44. }
  45.  
  46. template<typename T>
  47. T find_max_ass_parallel(const std::vector<T> & arr, size_t threads_num)
  48. {
  49. std::vector<T> results(threads_num, 0);
  50. std::vector<std::thread> threads;
  51.  
  52. auto worker = [&arr, &results, &threads_num](size_t thread_num) {
  53. T max = 0;
  54. for (size_t i = thread_num; i < arr.size(); i += threads_num) {
  55. max = std::max(arr[i], max);
  56. }
  57. results[thread_num] = max;
  58. };
  59.  
  60. for (size_t i = 0; i < threads_num; i++) {
  61. threads.push_back(std::thread(worker, i));
  62. }
  63.  
  64. for (auto & thread : threads) {
  65. thread.join();
  66. }
  67.  
  68. /*for (size_t i = 0; i < arr.size(); i++) {
  69. if (!arr.access_marks[i]) {
  70. std::cout << "NO ACCESS: " << i << std::endl;
  71. }
  72. }*/
  73.  
  74. return find_max(results);
  75. }
  76.  
  77. template<typename T>
  78. float time_delta_ms(T a, T b)
  79. {
  80. return std::chrono::duration_cast<std::chrono::microseconds>(a - b).count() / 1000.0f;
  81. }
  82.  
  83. int main()
  84. {
  85. using Clock = std::chrono::high_resolution_clock;
  86. using T = uint64_t;
  87.  
  88. constexpr size_t N = 1'000'000'000;
  89. constexpr size_t THREADS_NUM = 4;
  90. constexpr size_t TESTS_NUM = 4;
  91.  
  92. std::vector<uint8_t> arr;
  93. arr.reserve(N);
  94.  
  95. for (size_t i = 0; i < N; i++) {
  96. arr.push_back(stable_rand() & std::numeric_limits<T>::max());
  97. }
  98.  
  99. for (size_t i = 0; i < TESTS_NUM; i++) {
  100. auto start = Clock::now();
  101. auto x = find_max(arr);
  102. auto end = Clock::now();
  103. std::cout << "[" << i << "] Serial max = " << static_cast<uint64_t>(x) << "; time = " << time_delta_ms(end, start) << std::endl;
  104.  
  105. start = Clock::now();
  106. x = find_max_ass_parallel(arr, THREADS_NUM);
  107. end = Clock::now();
  108. std::cout << "[" << i << "] Ass parallel max = " << static_cast<uint64_t>(x) << "; time = " << time_delta_ms(end, start) << std::endl;
  109. }
  110.  
  111. return EXIT_SUCCESS;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement