goclkernel.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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) 2018-2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_GOCLKERNEL_HPP
  7. #define OPENCV_GAPI_GOCLKERNEL_HPP
  8. #include <vector>
  9. #include <functional>
  10. #include <map>
  11. #include <unordered_map>
  12. #include <opencv2/core/mat.hpp>
  13. #include <opencv2/gapi/gcommon.hpp>
  14. #include <opencv2/gapi/gkernel.hpp>
  15. #include <opencv2/gapi/garg.hpp>
  16. // FIXME: namespace scheme for backends?
  17. namespace cv {
  18. namespace gimpl
  19. {
  20. // Forward-declare an internal class
  21. class GOCLExecutable;
  22. } // namespace gimpl
  23. namespace gapi
  24. {
  25. /**
  26. * @brief This namespace contains G-API OpenCL backend functions, structures, and symbols.
  27. */
  28. namespace ocl
  29. {
  30. /**
  31. * \addtogroup gapi_std_backends G-API Standard Backends
  32. * @{
  33. */
  34. /**
  35. * @brief Get a reference to OCL backend.
  36. *
  37. * At the moment, the OCL backend is built atop of OpenCV
  38. * "Transparent API" (T-API), see cv::UMat for details.
  39. *
  40. * @sa gapi_std_backends
  41. */
  42. GAPI_EXPORTS cv::gapi::GBackend backend();
  43. /** @} */
  44. } // namespace ocl
  45. } // namespace gapi
  46. // Represents arguments which are passed to a wrapped OCL function
  47. // FIXME: put into detail?
  48. class GAPI_EXPORTS GOCLContext
  49. {
  50. public:
  51. // Generic accessor API
  52. template<typename T>
  53. const T& inArg(int input) { return m_args.at(input).get<T>(); }
  54. // Syntax sugar
  55. const cv::UMat& inMat(int input);
  56. cv::UMat& outMatR(int output); // FIXME: Avoid cv::Mat m = ctx.outMatR()
  57. const cv::Scalar& inVal(int input);
  58. cv::Scalar& outValR(int output); // FIXME: Avoid cv::Scalar s = ctx.outValR()
  59. template<typename T> std::vector<T>& outVecR(int output) // FIXME: the same issue
  60. {
  61. return outVecRef(output).wref<T>();
  62. }
  63. template<typename T> T& outOpaqueR(int output) // FIXME: the same issue
  64. {
  65. return outOpaqueRef(output).wref<T>();
  66. }
  67. protected:
  68. detail::VectorRef& outVecRef(int output);
  69. detail::OpaqueRef& outOpaqueRef(int output);
  70. std::vector<GArg> m_args;
  71. std::unordered_map<std::size_t, GRunArgP> m_results;
  72. friend class gimpl::GOCLExecutable;
  73. };
  74. class GAPI_EXPORTS GOCLKernel
  75. {
  76. public:
  77. // This function is kernel's execution entry point (does the processing work)
  78. using F = std::function<void(GOCLContext &)>;
  79. GOCLKernel();
  80. explicit GOCLKernel(const F& f);
  81. void apply(GOCLContext &ctx);
  82. protected:
  83. F m_f;
  84. };
  85. // FIXME: This is an ugly ad-hoc implementation. TODO: refactor
  86. namespace detail
  87. {
  88. template<class T> struct ocl_get_in;
  89. template<> struct ocl_get_in<cv::GMat>
  90. {
  91. static cv::UMat get(GOCLContext &ctx, int idx) { return ctx.inMat(idx); }
  92. };
  93. template<> struct ocl_get_in<cv::GScalar>
  94. {
  95. static cv::Scalar get(GOCLContext &ctx, int idx) { return ctx.inVal(idx); }
  96. };
  97. template<typename U> struct ocl_get_in<cv::GArray<U> >
  98. {
  99. static const std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.inArg<VectorRef>(idx).rref<U>(); }
  100. };
  101. template<typename U> struct ocl_get_in<cv::GOpaque<U> >
  102. {
  103. static const U& get(GOCLContext &ctx, int idx) { return ctx.inArg<OpaqueRef>(idx).rref<U>(); }
  104. };
  105. template<class T> struct ocl_get_in
  106. {
  107. static T get(GOCLContext &ctx, int idx) { return ctx.inArg<T>(idx); }
  108. };
  109. struct tracked_cv_umat{
  110. //TODO Think if T - API could reallocate UMat to a proper size - how do we handle this ?
  111. //tracked_cv_umat(cv::UMat& m) : r{(m)}, original_data{m.getMat(ACCESS_RW).data} {}
  112. tracked_cv_umat(cv::UMat& m) : r(m), original_data{ nullptr } {}
  113. cv::UMat &r; // FIXME: It was a value (not a reference) before.
  114. // Actually OCL backend should allocate its internal data!
  115. uchar* original_data;
  116. operator cv::UMat& (){ return r;}
  117. void validate() const{
  118. //if (r.getMat(ACCESS_RW).data != original_data)
  119. //{
  120. // util::throw_error
  121. // (std::logic_error
  122. // ("OpenCV kernel output parameter was reallocated. \n"
  123. // "Incorrect meta data was provided ?"));
  124. //}
  125. }
  126. };
  127. template<typename... Outputs>
  128. void postprocess_ocl(Outputs&... outs)
  129. {
  130. struct
  131. {
  132. void operator()(tracked_cv_umat* bm) { bm->validate(); }
  133. void operator()(...) { }
  134. } validate;
  135. //dummy array to unfold parameter pack
  136. int dummy[] = { 0, (validate(&outs), 0)... };
  137. cv::util::suppress_unused_warning(dummy);
  138. }
  139. template<class T> struct ocl_get_out;
  140. template<> struct ocl_get_out<cv::GMat>
  141. {
  142. static tracked_cv_umat get(GOCLContext &ctx, int idx)
  143. {
  144. auto& r = ctx.outMatR(idx);
  145. return{ r };
  146. }
  147. };
  148. template<> struct ocl_get_out<cv::GScalar>
  149. {
  150. static cv::Scalar& get(GOCLContext &ctx, int idx)
  151. {
  152. return ctx.outValR(idx);
  153. }
  154. };
  155. template<typename U> struct ocl_get_out<cv::GArray<U> >
  156. {
  157. static std::vector<U>& get(GOCLContext &ctx, int idx) { return ctx.outVecR<U>(idx); }
  158. };
  159. template<typename U> struct ocl_get_out<cv::GOpaque<U> >
  160. {
  161. static U& get(GOCLContext &ctx, int idx) { return ctx.outOpaqueR<U>(idx); }
  162. };
  163. template<typename, typename, typename>
  164. struct OCLCallHelper;
  165. // FIXME: probably can be simplified with std::apply or analogue.
  166. template<typename Impl, typename... Ins, typename... Outs>
  167. struct OCLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
  168. {
  169. template<typename... Inputs>
  170. struct call_and_postprocess
  171. {
  172. template<typename... Outputs>
  173. static void call(Inputs&&... ins, Outputs&&... outs)
  174. {
  175. //not using a std::forward on outs is deliberate in order to
  176. //cause compilation error, by trying to bind rvalue references to lvalue references
  177. Impl::run(std::forward<Inputs>(ins)..., outs...);
  178. postprocess_ocl(outs...);
  179. }
  180. };
  181. template<int... IIs, int... OIs>
  182. static void call_impl(GOCLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
  183. {
  184. //TODO: Make sure that OpenCV kernels do not reallocate memory for output parameters
  185. //by comparing it's state (data ptr) before and after the call.
  186. //Convert own::Scalar to cv::Scalar before call kernel and run kernel
  187. //convert cv::Scalar to own::Scalar after call kernel and write back results
  188. call_and_postprocess<decltype(ocl_get_in<Ins>::get(ctx, IIs))...>::call(ocl_get_in<Ins>::get(ctx, IIs)..., ocl_get_out<Outs>::get(ctx, OIs)...);
  189. }
  190. static void call(GOCLContext &ctx)
  191. {
  192. call_impl(ctx,
  193. typename detail::MkSeq<sizeof...(Ins)>::type(),
  194. typename detail::MkSeq<sizeof...(Outs)>::type());
  195. }
  196. };
  197. } // namespace detail
  198. template<class Impl, class K>
  199. class GOCLKernelImpl: public cv::detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
  200. public cv::detail::KernelTag
  201. {
  202. using P = detail::OCLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
  203. public:
  204. using API = K;
  205. static cv::gapi::GBackend backend() { return cv::gapi::ocl::backend(); }
  206. static cv::GOCLKernel kernel() { return GOCLKernel(&P::call); }
  207. };
  208. #define GAPI_OCL_KERNEL(Name, API) struct Name: public cv::GOCLKernelImpl<Name, API>
  209. } // namespace cv
  210. #endif // OPENCV_GAPI_GOCLKERNEL_HPP