Skip to content

Commit

Permalink
Merge pull request opencv#8204 from terfendail:ovx_tlcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Mar 1, 2017
2 parents 7878404 + 9a4b5a4 commit 47c4dcc
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 26 deletions.
13 changes: 11 additions & 2 deletions 3rdparty/openvx/hal/openvx_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ struct Tick

inline ivx::Context& getOpenVXHALContext()
{
// not thread safe
static ivx::Context instance = ivx::Context::create();
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
//CXX11
static thread_local ivx::Context instance = ivx::Context::create();
#else //__cplusplus >= 201103L || _MSC_VER >= 1800
//CXX98
#ifdef WIN32
static __declspec(thread) ivx::Context instance = ivx::Context::create();
#else
static __thread ivx::Context instance = ivx::Context::create();
#endif
#endif
return instance;
}

Expand Down
6 changes: 6 additions & 0 deletions modules/core/include/opencv2/core/openvx/ovx_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#define IVX_USE_OPENCV
#include "ivx.hpp"

namespace cv{
namespace ovx{
// Get common thread local OpenVX context
CV_EXPORTS_W ivx::Context& getOpenVXContext();
}}

#define CV_OVX_RUN(condition, func, ...) \
if (cv::useOpenVX() && (condition) && func) \
{ \
Expand Down
7 changes: 6 additions & 1 deletion modules/core/include/opencv2/core/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,9 @@ class CV_EXPORTS TLSDataContainer
virtual void deleteDataInstance(void* pData) const = 0;

int key_;

public:
void cleanup(); //! Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
};

// Main TLS data class
Expand All @@ -638,13 +641,15 @@ class TLSData : protected TLSDataContainer
inline ~TLSData() { release(); } // Release key and delete associated data
inline T* get() const { return (T*)getData(); } // Get data associated with key

// Get data from all threads
// Get data from all threads
inline void gather(std::vector<T*> &data) const
{
std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
gatherData(dataVoid);
}

inline void cleanup() { TLSDataContainer::cleanup(); }

private:
virtual void* createDataInstance() const {return new T;} // Wrapper to allocate data by template
virtual void deleteDataInstance(void* pData) const {delete (T*)pData;} // Wrapper to release data by template
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4673,7 +4673,7 @@ static bool _openvx_cvt(const T* src, size_t sstep,

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();

// Other conversions are marked as "experimental"
if(context.vendorID() == VX_ID_KHRONOS &&
Expand Down Expand Up @@ -5406,7 +5406,7 @@ static bool openvx_LUT(Mat src, Mat dst, Mat _lut)

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();

ivx::Image
ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
Expand Down
34 changes: 33 additions & 1 deletion modules/core/src/ovx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@
namespace cv
{

namespace ovx
{
#ifdef HAVE_OPENVX

// Simple TLSData<ivx::Context> doesn't work, because default constructor doesn't create any OpenVX context.
struct OpenVXTLSData
{
OpenVXTLSData() : ctx(ivx::Context::create()) {}
ivx::Context ctx;
};

static TLSData<OpenVXTLSData>& getOpenVXTLSData()
{
CV_SINGLETON_LAZY_INIT_REF(TLSData<OpenVXTLSData>, new TLSData<OpenVXTLSData>())
}

struct OpenVXCleanupFunctor
{
~OpenVXCleanupFunctor() { getOpenVXTLSData().cleanup(); }
};
static OpenVXCleanupFunctor g_openvx_cleanup_functor;

ivx::Context& getOpenVXContext()
{
return getOpenVXTLSData().get()->ctx;
}

#endif

} // namespace


bool haveOpenVX()
{
#ifdef HAVE_OPENVX
Expand All @@ -22,7 +54,7 @@ bool haveOpenVX()
{
try
{
ivx::Context context = ivx::Context::create();
ivx::Context context = ovx::getOpenVXContext();
vx_uint16 vComp = ivx::compiledWithVersion();
vx_uint16 vCurr = context.version();
g_haveOpenVX =
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ namespace cv

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
#ifndef VX_VERSION_1_1
if (ctx.vendorID() == VX_ID_KHRONOS)
return false; // Do not use OpenVX meanStdDev estimation for sample 1.0.1 implementation due to lack of accuracy
Expand Down Expand Up @@ -2312,7 +2312,7 @@ static bool openvx_minMaxIdx(Mat &src, double* minVal, double* maxVal, int* minI

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
ivx::Image
ia = ivx::Image::createFromHandle(ctx, stype == CV_8UC1 ? VX_DF_IMAGE_U8 : VX_DF_IMAGE_S16,
ivx::Image::createAddressing(cols, rows, stype == CV_8UC1 ? 1 : 2, (vx_int32)(src.step[0])), src.ptr());
Expand Down
18 changes: 14 additions & 4 deletions modules/core/src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ class TlsStorage
}

// Release TLS storage index and pass associated data to caller
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec)
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
{
AutoLock guard(mtxGlobalAccess);
CV_Assert(tlsSlots.size() > slotIdx);
Expand All @@ -1099,12 +1099,13 @@ class TlsStorage
if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
{
dataVec.push_back(thread_slots[slotIdx]);
threads[i]->slots[slotIdx] = 0;
thread_slots[slotIdx] = NULL;
}
}
}

tlsSlots[slotIdx] = 0;
if (!keepSlot)
tlsSlots[slotIdx] = 0;
}

// Get data by TLS storage index
Expand Down Expand Up @@ -1196,9 +1197,18 @@ void TLSDataContainer::release()
std::vector<void*> data;
data.reserve(32);
getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
key_ = -1;
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
deleteDataInstance(data[i]);
}

void TLSDataContainer::cleanup()
{
std::vector<void*> data;
data.reserve(32);
getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
deleteDataInstance(data[i]);
key_ = -1;
}

void* TLSDataContainer::getData() const
Expand Down
2 changes: 1 addition & 1 deletion modules/features2d/src/fast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ static bool openvx_FAST(InputArray _img, std::vector<KeyPoint>& keypoints,

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();
Image img = Image::createFromHandle(context, Image::matTypeToFormat(imgMat.type()),
Image::createAddressing(imgMat), (void*)imgMat.data);
ivx::Scalar threshold = ivx::Scalar::create<VX_TYPE_FLOAT32>(context, _threshold);
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/accum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ static bool openvx_accumulate(InputArray _src, InputOutputArray _dst, InputArray

try
{
ivx::Context context = ivx::Context::create();
ivx::Context context = ovx::getOpenVXContext();
ivx::Image srcImage = ivx::Image::createFromHandle(context, ivx::Image::matTypeToFormat(srcMat.type()),
ivx::Image::createAddressing(srcMat), srcMat.data);
ivx::Image dstImage = ivx::Image::createFromHandle(context, ivx::Image::matTypeToFormat(dstMat.type()),
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/canny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ static bool openvx_canny(const Mat& src, Mat& dst, int loVal, int hiVal, int kSi
{
using namespace ivx;

Context context = Context::create();
Context context = ovx::getOpenVXContext();
try
{
Image _src = Image::createFromHandle(
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/deriv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace cv

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
if ((vx_size)ksize > ctx.convolutionMaxDimension())
return false;

Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/featureselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static bool openvx_harris(Mat image, OutputArray _corners,

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();

Image ovxImage = Image::createFromHandle(context, Image::matTypeToFormat(image.type()),
Image::createAddressing(image), image.data);
Expand Down
4 changes: 2 additions & 2 deletions modules/imgproc/src/histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ namespace cv

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
#if VX_VERSION <= VX_VERSION_1_0
if (ctx.vendorID() == VX_ID_KHRONOS && (range % histSize))
return false;
Expand Down Expand Up @@ -3773,7 +3773,7 @@ static bool openvx_equalize_hist(Mat srcMat, Mat dstMat)

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();
Image srcImage = Image::createFromHandle(context, Image::matTypeToFormat(srcMat.type()),
Image::createAddressing(srcMat), srcMat.data);
Image dstImage = Image::createFromHandle(context, Image::matTypeToFormat(dstMat.type()),
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/imgwarp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4795,7 +4795,7 @@ static bool openvx_remap(Mat src, Mat dst, Mat map1, Mat map2, int interpolation

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();

Mat a;
if (dst.data != src.data)
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/pyramids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ static bool openvx_pyrDown( InputArray _src, OutputArray _dst, const Size& _dsz,

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();
if(context.vendorID() == VX_ID_KHRONOS)
{
// This implementation performs floor-like rounding
Expand Down
6 changes: 3 additions & 3 deletions modules/imgproc/src/smooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ namespace cv

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
if ((vx_size)(ksize.width) > ctx.convolutionMaxDimension() || (vx_size)(ksize.height) > ctx.convolutionMaxDimension())
return false;

Expand Down Expand Up @@ -2239,7 +2239,7 @@ static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
if ((vx_size)(ksize.width) > ctx.convolutionMaxDimension() || (vx_size)(ksize.height) > ctx.convolutionMaxDimension())
return false;

Expand Down Expand Up @@ -3361,7 +3361,7 @@ namespace cv

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();
#ifdef VX_VERSION_1_1
if ((vx_size)ksize > ctx.nonlinearMaxDimension())
return false;
Expand Down
2 changes: 1 addition & 1 deletion modules/imgproc/src/thresh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ static bool openvx_threshold(Mat src, Mat dst, int thresh, int maxval, int type)

try
{
ivx::Context ctx = ivx::Context::create();
ivx::Context ctx = ovx::getOpenVXContext();

ivx::Threshold thh = ivx::Threshold::createBinary(ctx, VX_TYPE_UINT8, thresh);
thh.setValueTrue(trueVal);
Expand Down
2 changes: 1 addition & 1 deletion modules/video/src/lkpyramid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ namespace

try
{
Context context = Context::create();
Context context = ovx::getOpenVXContext();

if(context.vendorID() == VX_ID_KHRONOS)
{
Expand Down

0 comments on commit 47c4dcc

Please sign in to comment.