Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / cuda4dnn / primitives / const.hpp
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 #ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_CONST_HPP
6 #define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_CONST_HPP
7
8 #include "../../op_cuda.hpp"
9
10 #include "../csl/stream.hpp"
11 #include "../csl/tensor.hpp"
12 #include "../csl/tensor_ops.hpp"
13
14 #include <opencv2/core.hpp>
15
16 #include <utility>
17
18 namespace cv { namespace dnn { namespace cuda4dnn {
19
20     template <class T>
21     class ConstOp final : public CUDABackendNode {
22     public:
23         using wrapper_type = GetCUDABackendWrapperType<T>;
24
25         ConstOp(csl::Stream stream_, const cv::Mat& data)
26             : stream(std::move(stream_))
27         {
28             constTensor = csl::makeTensorHeader<T>(data);
29             csl::copyMatToTensor<T>(data, constTensor, stream);
30         }
31
32         void forward(
33             const std::vector<cv::Ptr<BackendWrapper>>& inputs,
34             const std::vector<cv::Ptr<BackendWrapper>>& outputs,
35             csl::Workspace& workspace) override
36         {
37             CV_Assert(outputs.size() == 1 && inputs.size() == 0);
38
39             auto output_wrapper = outputs[0].dynamicCast<wrapper_type>();
40             auto output = output_wrapper->getSpan();
41             csl::tensor_ops::copy<T>(stream, output, constTensor);
42         }
43
44     private:
45         csl::Stream stream;
46         csl::Tensor<T> constTensor;
47     };
48
49 }}} /* namespace cv::dnn::cuda4dnn */
50
51 #endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_CONST_HPP */