opencv/modules/gapi/test/own/conc_queue_tests.cpp
Dmitry Matveev 2477103707 Merge pull request #15216 from dmatveev:dm/ng-0010-g-api-streaming-api
* G-API-NG/Streaming: Introduced a Streaming API

Now a GComputation can be compiled in a special "streaming" way
and then "played" on a video stream.

Currently only VideoCapture is supported as an input source.

* G-API-NG/Streaming: added threading & real streaming

* G-API-NG/Streaming: Added tests & docs on Copy kernel

- Added very simple pipeline tests, not all data types are covered yet
  (in fact, only GMat is tested now);
- Started testing non-OCV backends in the streaming mode;
- Added required fixes to Fluid backend, likely it works OK now;
- Added required fixes to OCL backend, and now it is likely broken
- Also added a UMat-based (OCL) version of Copy kernel

* G-API-NG/Streaming: Added own concurrent queue class

- Used only if TBB is not available

* G-API-NG/Streaming: Fixing various issues

- Added missing header to CMakeLists.txt
- Fixed various CI issues and warnings

* G-API-NG/Streaming: Fixed a compile-time GScalar queue deadlock

- GStreamingExecutor blindly created island's input queues for
  compile-time (value-initialized) GScalars which didn't have any
  producers, making island actor threads wait there forever

* G-API-NG/Streaming: Dropped own version of Copy kernel

One was added into master already

* G-API-NG/Streaming: Addressed GArray<T> review comments

- Added tests on mov()
- Removed unnecessary changes in garray.hpp

* G-API-NG/Streaming: Added Doxygen comments to new public APIs

Also fixed some other comments in the code

* G-API-NG/Streaming: Removed debug info, added some comments & renamed vars

* G-API-NG/Streaming: Fixed own-vs-cv abstraction leak

- Now every island is triggered with own:: (instead of cv::)
  data objects as inputs;
- Changes in Fluid backend required to support cv::Mat/Scalar were
  reverted;

* G-API-NG/Streaming: use holds_alternative<> instead of index/index_of test

- Also fixed regression test comments
- Also added metadata check comments for GStreamingCompiled

* G-API-NG/Streaming: Made start()/stop() more robust

- Fixed various possible deadlocks
- Unified the shutdown code
- Added more tests covering different corner cases on start/stop

* G-API-NG/Streaming: Finally fixed Windows crashes

In fact the problem hasn't been Windows-only.
Island thread popped data from queues without preserving the Cmd
objects and without taking the ownership over data acquired so when
islands started to process the data, this data may be already freed.

Linux version worked only by occasion.

* G-API-NG/Streaming: Fixed (I hope so) Windows warnings

* G-API-NG/Streaming: fixed typos in internal comments

- Also added some more explanation on Streaming/OpenCL status

* G-API-NG/Streaming: Added more unit tests on streaming

- Various start()/stop()/setSource() call flow combinations

* G-API-NG/Streaming: Added tests on own concurrent bounded queue

* G-API-NG/Streaming: Added more tests on various data types, + more

- Vector/Scalar passed as input;
- Vector/Scalar passed in-between islands;
- Some more assertions;
- Also fixed a deadlock problem when inputs are mixed (1 constant, 1 stream)

* G-API-NG/Streaming: Added tests on output data types handling

- Vector
- Scalar

* G-API-NG/Streaming: Fixed test issues with IE + Windows warnings

* G-API-NG/Streaming: Decoupled G-API from videoio

- Now the core G-API doesn't use a cv::VideoCapture directly,
  it comes in via an abstract interface;
- Polished a little bit the setSource()/start()/stop() semantics,
  now setSource() is mandatory before ANY call to start().

* G-API-NG/Streaming: Fix STANDALONE build (errors brought by render)
2019-10-18 19:29:58 +03:00

198 lines
5.2 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019 Intel Corporation
#include "../test_precomp.hpp"
#include <unordered_set>
#include <thread>
#include "executor/conc_queue.hpp"
namespace opencv_test
{
using namespace cv::gapi;
TEST(ConcQueue, PushPop)
{
own::concurrent_bounded_queue<int> q;
for (int i = 0; i < 100; i++)
{
q.push(i);
}
for (int i = 0; i < 100; i++)
{
int x;
q.pop(x);
EXPECT_EQ(x, i);
}
}
TEST(ConcQueue, TryPop)
{
own::concurrent_bounded_queue<int> q;
int x = 0;
EXPECT_FALSE(q.try_pop(x));
q.push(1);
EXPECT_TRUE(q.try_pop(x));
EXPECT_EQ(1, x);
}
TEST(ConcQueue, Clear)
{
own::concurrent_bounded_queue<int> q;
for (int i = 0; i < 10; i++)
{
q.push(i);
}
q.clear();
int x = 0;
EXPECT_FALSE(q.try_pop(x));
}
// In this test, every writer thread produce its own range of integer
// numbers, writing those to a shared queue.
//
// Every reader thread pops elements from the queue (until -1 is
// reached) and stores those in its own associated set.
//
// Finally, the master thread waits for completion of all other
// threads and verifies that all the necessary data is
// produced/obtained.
using StressParam = std::tuple<int // Num writer threads
,int // Num elements per writer
,int // Num reader threads
,std::size_t>; // Queue capacity
namespace
{
constexpr int STOP_SIGN = -1;
constexpr int BASE = 1000;
}
struct ConcQueue_: public ::testing::TestWithParam<StressParam>
{
using Q = own::concurrent_bounded_queue<int>;
using S = std::unordered_set<int>;
static void writer(int base, int writes, Q& q)
{
for (int i = 0; i < writes; i++)
{
q.push(base + i);
}
q.push(STOP_SIGN);
}
static void reader(Q& q, S& s)
{
int x = 0;
while (true)
{
q.pop(x);
if (x == STOP_SIGN) return;
s.insert(x);
}
}
};
TEST_P(ConcQueue_, Test)
{
int num_writers = 0;
int num_writes = 0;
int num_readers = 0;
std::size_t capacity = 0u;
std::tie(num_writers, num_writes, num_readers, capacity) = GetParam();
CV_Assert(num_writers < 20);
CV_Assert(num_writes < BASE);
Q q;
if (capacity)
{
// see below (2)
CV_Assert(static_cast<int>(capacity) > (num_writers - num_readers));
q.set_capacity(capacity);
}
// Start reader threads
std::vector<S> storage(num_readers);
std::vector<std::thread> readers;
for (S& s : storage)
{
readers.emplace_back(reader, std::ref(q), std::ref(s));
}
// Start writer threads, also pre-generate reference numbers
S reference;
std::vector<std::thread> writers;
for (int w = 0; w < num_writers; w++)
{
writers.emplace_back(writer, w*BASE, num_writes, std::ref(q));
for (int r = 0; r < num_writes; r++)
{
reference.insert(w*BASE + r);
}
}
// Every writer puts a STOP_SIGN at the end,
// There are three cases:
// 1) num_writers == num_readers
// every reader should get its own STOP_SIGN from any
// of the writers
//
// 2) num_writers > num_readers
// every reader will get a STOP_SIGN but there're more
// STOP_SIGNs may be pushed to the queue - and if this
// number exceeds capacity, writers block (to a deadlock).
// The latter situation must be avoided at parameters level.
// [a] Also not every data produced by writers will be consumed
// by a reader in this case. Master thread will read the rest
//
// 3) num_readers > num_writers
// in this case, some readers will stuck and will never get
// a STOP_SIGN. Master thread will push extra STOP_SIGNs to the
// queue.
// Solution to (2a)
S remnants;
if (num_writers > num_readers)
{
int extra = num_writers - num_readers;
while (extra)
{
int x = 0;
q.pop(x);
if (x == STOP_SIGN) extra--;
else remnants.insert(x);
}
}
// Solution to (3)
if (num_readers > num_writers)
{
int extra = num_readers - num_writers;
while (extra--) q.push(STOP_SIGN);
}
// Wait for completions
for (auto &t : readers) t.join();
for (auto &t : writers) t.join();
// Accumulate and validate the result
S result(remnants.begin(), remnants.end());
for (const auto &s : storage) result.insert(s.begin(), s.end());
EXPECT_EQ(reference, result);
}
INSTANTIATE_TEST_CASE_P(ConcQueueStress, ConcQueue_,
Combine( Values(1, 2, 4, 8, 16) // writers
, Values(1, 32, 96, 256) // writes
, Values(1, 2, 10) // readers
, Values(0u, 16u, 32u))); // capacity
} // namespace opencv_test