gapi_streaming_utils_test.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2021 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include "../common/gapi_streaming_tests_common.hpp"
  8. #include <chrono>
  9. #include <future>
  10. #define private public
  11. #include "streaming/onevpl/accelerators/utils/shared_lock.hpp"
  12. #undef private
  13. #include "streaming/onevpl/accelerators/utils/elastic_barrier.hpp"
  14. namespace opencv_test
  15. {
  16. namespace
  17. {
  18. using cv::gapi::wip::onevpl::SharedLock;
  19. struct TestBarrier : public cv::gapi::wip::onevpl::elastic_barrier<TestBarrier> {
  20. void on_first_in_impl(size_t visitor_id) {
  21. static std::atomic<int> thread_counter{};
  22. thread_counter++;
  23. EXPECT_EQ(thread_counter.load(), 1);
  24. visitors_in.insert(visitor_id);
  25. last_visitor_id = visitor_id;
  26. thread_counter--;
  27. EXPECT_EQ(thread_counter.load(), 0);
  28. }
  29. void on_last_out_impl(size_t visitor_id) {
  30. static std::atomic<int> thread_counter{};
  31. thread_counter++;
  32. EXPECT_EQ(thread_counter.load(), 1);
  33. visitors_out.insert(visitor_id);
  34. last_visitor_id = visitor_id;
  35. thread_counter--;
  36. EXPECT_EQ(thread_counter.load(), 0);
  37. }
  38. size_t last_visitor_id = 0;
  39. std::set<size_t> visitors_in;
  40. std::set<size_t> visitors_out;
  41. };
  42. TEST(OneVPL_SharedLock, Create) {
  43. SharedLock lock;
  44. EXPECT_EQ(lock.shared_counter.load(), size_t{0});
  45. }
  46. TEST(OneVPL_SharedLock, Read_SingleThread)
  47. {
  48. SharedLock lock;
  49. const size_t single_thread_read_count = 100;
  50. for(size_t i = 0; i < single_thread_read_count; i++) {
  51. lock.shared_lock();
  52. EXPECT_FALSE(lock.owns());
  53. }
  54. EXPECT_EQ(lock.shared_counter.load(), single_thread_read_count);
  55. for(size_t i = 0; i < single_thread_read_count; i++) {
  56. lock.unlock_shared();
  57. EXPECT_FALSE(lock.owns());
  58. }
  59. EXPECT_EQ(lock.shared_counter.load(), size_t{0});
  60. }
  61. TEST(OneVPL_SharedLock, TryLock_SingleThread)
  62. {
  63. SharedLock lock;
  64. EXPECT_TRUE(lock.try_lock());
  65. EXPECT_TRUE(lock.owns());
  66. lock.unlock();
  67. EXPECT_FALSE(lock.owns());
  68. EXPECT_EQ(lock.shared_counter.load(), size_t{0});
  69. }
  70. TEST(OneVPL_SharedLock, Write_SingleThread)
  71. {
  72. SharedLock lock;
  73. lock.lock();
  74. EXPECT_TRUE(lock.owns());
  75. lock.unlock();
  76. EXPECT_FALSE(lock.owns());
  77. EXPECT_EQ(lock.shared_counter.load(), size_t{0});
  78. }
  79. TEST(OneVPL_SharedLock, TryLockTryLock_SingleThread)
  80. {
  81. SharedLock lock;
  82. lock.try_lock();
  83. EXPECT_FALSE(lock.try_lock());
  84. lock.unlock();
  85. EXPECT_FALSE(lock.owns());
  86. }
  87. TEST(OneVPL_SharedLock, ReadTryLock_SingleThread)
  88. {
  89. SharedLock lock;
  90. lock.shared_lock();
  91. EXPECT_FALSE(lock.owns());
  92. EXPECT_FALSE(lock.try_lock());
  93. lock.unlock_shared();
  94. EXPECT_TRUE(lock.try_lock());
  95. EXPECT_TRUE(lock.owns());
  96. lock.unlock();
  97. }
  98. TEST(OneVPL_SharedLock, WriteTryLock_SingleThread)
  99. {
  100. SharedLock lock;
  101. lock.lock();
  102. EXPECT_TRUE(lock.owns());
  103. EXPECT_FALSE(lock.try_lock());
  104. lock.unlock();
  105. EXPECT_TRUE(lock.try_lock());
  106. EXPECT_TRUE(lock.owns());
  107. lock.unlock();
  108. }
  109. TEST(OneVPL_SharedLock, Write_MultiThread)
  110. {
  111. SharedLock lock;
  112. std::promise<void> barrier;
  113. std::shared_future<void> sync = barrier.get_future();
  114. static const size_t inc_count = 10000000;
  115. size_t shared_value = 0;
  116. auto work = [&lock, &shared_value](size_t count) {
  117. for (size_t i = 0; i < count; i ++) {
  118. lock.lock();
  119. shared_value ++;
  120. lock.unlock();
  121. }
  122. };
  123. std::thread worker_thread([&barrier, sync, work] () {
  124. std::thread sub_worker([&barrier, work] () {
  125. barrier.set_value();
  126. work(inc_count);
  127. });
  128. sync.wait();
  129. work(inc_count);
  130. sub_worker.join();
  131. });
  132. sync.wait();
  133. work(inc_count);
  134. worker_thread.join();
  135. EXPECT_EQ(shared_value, inc_count * 3);
  136. }
  137. TEST(OneVPL_SharedLock, ReadWrite_MultiThread)
  138. {
  139. SharedLock lock;
  140. std::promise<void> barrier;
  141. std::future<void> sync = barrier.get_future();
  142. static const size_t inc_count = 10000000;
  143. size_t shared_value = 0;
  144. auto write_work = [&lock, &shared_value](size_t count) {
  145. for (size_t i = 0; i < count; i ++) {
  146. lock.lock();
  147. shared_value ++;
  148. lock.unlock();
  149. }
  150. };
  151. auto read_work = [&lock, &shared_value](size_t count) {
  152. auto old_shared_value = shared_value;
  153. for (size_t i = 0; i < count; i ++) {
  154. lock.shared_lock();
  155. EXPECT_TRUE(shared_value >= old_shared_value);
  156. old_shared_value = shared_value;
  157. lock.unlock_shared();
  158. }
  159. };
  160. std::thread writer_thread([&barrier, write_work] () {
  161. barrier.set_value();
  162. write_work(inc_count);
  163. });
  164. sync.wait();
  165. read_work(inc_count);
  166. writer_thread.join();
  167. EXPECT_EQ(shared_value, inc_count);
  168. }
  169. TEST(OneVPL_ElasticBarrier, single_thread_visit)
  170. {
  171. TestBarrier barrier;
  172. const size_t max_visit_count = 10000;
  173. size_t visit_id = 0;
  174. for (visit_id = 0; visit_id < max_visit_count; visit_id++) {
  175. barrier.visit_in(visit_id);
  176. EXPECT_EQ(barrier.visitors_in.size(), size_t{1});
  177. }
  178. EXPECT_EQ(barrier.last_visitor_id, size_t{0});
  179. EXPECT_EQ(barrier.visitors_out.size(), size_t{0});
  180. for (visit_id = 0; visit_id < max_visit_count; visit_id++) {
  181. barrier.visit_out(visit_id);
  182. EXPECT_EQ(barrier.visitors_in.size(), size_t{1});
  183. }
  184. EXPECT_EQ(barrier.last_visitor_id, visit_id - 1);
  185. EXPECT_EQ(barrier.visitors_out.size(), size_t{1});
  186. }
  187. TEST(OneVPL_ElasticBarrier, multi_thread_visit)
  188. {
  189. TestBarrier tested_barrier;
  190. static const size_t max_visit_count = 10000000;
  191. std::atomic<size_t> visit_in_wait_counter{};
  192. std::promise<void> start_sync_barrier;
  193. std::shared_future<void> start_sync = start_sync_barrier.get_future();
  194. std::promise<void> phase_sync_barrier;
  195. std::shared_future<void> phase_sync = phase_sync_barrier.get_future();
  196. auto visit_worker_job = [&tested_barrier,
  197. &visit_in_wait_counter,
  198. start_sync,
  199. phase_sync] (size_t worker_id) {
  200. start_sync.wait();
  201. // first phase
  202. const size_t begin_range = worker_id * max_visit_count;
  203. const size_t end_range = (worker_id + 1) * max_visit_count;
  204. for (size_t visit_id = begin_range; visit_id < end_range; visit_id++) {
  205. tested_barrier.visit_in(visit_id);
  206. }
  207. // notify all worker first phase ready
  208. visit_in_wait_counter.fetch_add(1);
  209. // wait main second phase
  210. phase_sync.wait();
  211. // second phase
  212. for (size_t visit_id = begin_range; visit_id < end_range; visit_id++) {
  213. tested_barrier.visit_out(visit_id);
  214. }
  215. };
  216. auto visit_main_job = [&tested_barrier,
  217. &visit_in_wait_counter,
  218. &phase_sync_barrier] (size_t total_workers_count,
  219. size_t worker_id) {
  220. const size_t begin_range = worker_id * max_visit_count;
  221. const size_t end_range = (worker_id + 1) * max_visit_count;
  222. for (size_t visit_id = begin_range; visit_id < end_range; visit_id++) {
  223. tested_barrier.visit_in(visit_id);
  224. }
  225. // wait all workers first phase done
  226. visit_in_wait_counter.fetch_add(1);
  227. while (visit_in_wait_counter.load() != total_workers_count) {
  228. std::this_thread::yield();
  229. };
  230. // TEST invariant: last_visitor_id MUST be one from any FIRST worker visitor_id
  231. bool one_of_available_ids_matched = false;
  232. for (size_t id = 0; id < total_workers_count; id ++) {
  233. size_t expected_last_visitor_for_id = id * max_visit_count;
  234. one_of_available_ids_matched |=
  235. (tested_barrier.last_visitor_id == expected_last_visitor_for_id) ;
  236. }
  237. EXPECT_TRUE(one_of_available_ids_matched);
  238. // unblock all workers to work out second phase
  239. phase_sync_barrier.set_value();
  240. // continue second phase
  241. for (size_t visit_id = begin_range; visit_id < end_range; visit_id++) {
  242. tested_barrier.visit_out(visit_id);
  243. }
  244. };
  245. size_t max_worker_count = std::thread::hardware_concurrency();
  246. if (max_worker_count < 2) {
  247. max_worker_count = 2; // logical 2 threads required at least
  248. }
  249. std::vector<std::thread> workers;
  250. workers.reserve(max_worker_count);
  251. for (size_t worker_id = 1; worker_id < max_worker_count; worker_id++) {
  252. workers.emplace_back(visit_worker_job, worker_id);
  253. }
  254. // let's go for first phase
  255. start_sync_barrier.set_value();
  256. // utilize main thread as well
  257. visit_main_job(max_worker_count, 0);
  258. // join all threads second phase
  259. for (auto& w : workers) {
  260. w.join();
  261. }
  262. // TEST invariant: last_visitor_id MUST be one from any LATTER worker visitor_id
  263. bool one_of_available_ids_matched = false;
  264. for (size_t id = 0; id < max_worker_count; id ++) {
  265. one_of_available_ids_matched |=
  266. (tested_barrier.last_visitor_id == ((id + 1) * max_visit_count - 1)) ;
  267. }
  268. EXPECT_TRUE(one_of_available_ids_matched);
  269. }
  270. }
  271. } // opencv_test