github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/plaidml/gplaidmlkernel.hpp (about) 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) 2019 Intel Corporation 6 // 7 8 9 #ifndef OPENCV_GAPI_GPLAIDMLKERNEL_HPP 10 #define OPENCV_GAPI_GPLAIDMLKERNEL_HPP 11 12 #include <opencv2/gapi/gkernel.hpp> 13 #include <opencv2/gapi/garg.hpp> 14 15 namespace plaidml 16 { 17 namespace edsl 18 { 19 class Tensor; 20 } // namespace edsl 21 } // namespace plaidml 22 23 namespace cv 24 { 25 namespace gapi 26 { 27 namespace plaidml 28 { 29 30 GAPI_EXPORTS cv::gapi::GBackend backend(); 31 32 } // namespace plaidml 33 } // namespace gapi 34 35 struct GPlaidMLContext 36 { 37 // Generic accessor API 38 template<typename T> 39 const T& inArg(int input) { return m_args.at(input).get<T>(); } 40 41 // Syntax sugar 42 const plaidml::edsl::Tensor& inTensor(int input) 43 { 44 return inArg<plaidml::edsl::Tensor>(input); 45 } 46 47 plaidml::edsl::Tensor& outTensor(int output) 48 { 49 return *(m_results.at(output).get<plaidml::edsl::Tensor*>()); 50 } 51 52 std::vector<GArg> m_args; 53 std::unordered_map<std::size_t, GArg> m_results; 54 }; 55 56 class GAPI_EXPORTS GPlaidMLKernel 57 { 58 public: 59 using F = std::function<void(GPlaidMLContext &)>; 60 61 GPlaidMLKernel() = default; 62 explicit GPlaidMLKernel(const F& f) : m_f(f) {}; 63 64 void apply(GPlaidMLContext &ctx) const 65 { 66 GAPI_Assert(m_f); 67 m_f(ctx); 68 } 69 70 protected: 71 F m_f; 72 }; 73 74 75 namespace detail 76 { 77 78 template<class T> struct plaidml_get_in; 79 template<> struct plaidml_get_in<cv::GMat> 80 { 81 static const plaidml::edsl::Tensor& get(GPlaidMLContext& ctx, int idx) 82 { 83 return ctx.inTensor(idx); 84 } 85 }; 86 87 template<class T> struct plaidml_get_in 88 { 89 static T get(GPlaidMLContext &ctx, int idx) { return ctx.inArg<T>(idx); } 90 }; 91 92 template<class T> struct plaidml_get_out; 93 template<> struct plaidml_get_out<cv::GMat> 94 { 95 static plaidml::edsl::Tensor& get(GPlaidMLContext& ctx, int idx) 96 { 97 return ctx.outTensor(idx); 98 } 99 }; 100 101 template<typename, typename, typename> 102 struct PlaidMLCallHelper; 103 104 template<typename Impl, typename... Ins, typename... Outs> 105 struct PlaidMLCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> > 106 { 107 template<int... IIs, int... OIs> 108 static void call_impl(GPlaidMLContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>) 109 { 110 Impl::run(plaidml_get_in<Ins>::get(ctx, IIs)..., plaidml_get_out<Outs>::get(ctx, OIs)...); 111 } 112 113 static void call(GPlaidMLContext& ctx) 114 { 115 call_impl(ctx, 116 typename detail::MkSeq<sizeof...(Ins)>::type(), 117 typename detail::MkSeq<sizeof...(Outs)>::type()); 118 } 119 }; 120 121 } // namespace detail 122 123 template<class Impl, class K> 124 class GPlaidMLKernelImpl: public cv::detail::PlaidMLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>, 125 public cv::detail::KernelTag 126 { 127 using P = detail::PlaidMLCallHelper<Impl, typename K::InArgs, typename K::OutArgs>; 128 129 public: 130 using API = K; 131 132 static cv::gapi::GBackend backend() { return cv::gapi::plaidml::backend(); } 133 static cv::GPlaidMLKernel kernel() { return GPlaidMLKernel(&P::call); } 134 }; 135 136 #define GAPI_PLAIDML_KERNEL(Name, API) struct Name: public cv::GPlaidMLKernelImpl<Name, API> 137 138 } // namespace cv 139 140 #endif // OPENCV_GAPI_GPLAIDMLKERNEL_HPP