• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_LITE_INCLUDE_REGISTRY_OPENCL_RUNTIME_WRAPPER_H
18 #define MINDSPORE_LITE_INCLUDE_REGISTRY_OPENCL_RUNTIME_WRAPPER_H
19 #include <vector>
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <type_traits>
26 #include "CL/cl2.hpp"
27 #include "include/api/allocator.h"
28 #include "include/api/status.h"
29 
30 namespace mindspore::registry::opencl {
31 class OpenCLRuntimeWrapper {
32  public:
33   OpenCLRuntimeWrapper() = default;
34   ~OpenCLRuntimeWrapper() = default;
35 
36   /// \brief Load the OpenCl source code and bind the program name.
37   ///
38   /// \param[in] program_name Define OpenCl source program name.
39   /// \param[in] source Define OpenCl source.
40   ///
41   /// \return Status as a status identification of loading code.
42   Status LoadSource(const std::string &program_name, const std::string &source);
43 
44   /// \brief Building OpenCL code.
45   ///
46   /// \param[in] kernel Used to return the compiled kernel
47   /// \param[in] program_name Define OpenCl source program name.
48   /// \param[in] kernel_name Define OpenCl source kernel name.
49   /// \param[in] build_options_ext Define OpenCl kernel build options.
50   ///
51   /// \return Status as a status identification of build Kernel
52   Status BuildKernel(cl::Kernel *kernel, const std::string &program_name, const std::string &kernel_name,
53                      const std::vector<std::string> &build_options_ext = {});
54 
55   /// \brief Set kernel argument
56   ///
57   /// \param[in] kernel Define OpenCl kernel.
58   /// \param[in] index Define OpenCl kernel argument index.
59   /// \param[in] value Define OpenCl kernel argument value pointer.
60   /// \param[in] mem_type Define OpenCl kernel argument value memory type.
61   ///
62   /// \return Status as a status identification of set kernel argument
63   Status SetKernelArg(const cl::Kernel &kernel, uint32_t index, void *const value);
64 
65   /// \brief Set kernel argument
66   ///
67   /// \param[in] kernel Define OpenCl kernel.
68   /// \param[in] index Define OpenCl kernel argument index.
69   /// \param[in] value Define OpenCl kernel argument value.
70   /// \param[in] mem_type Define OpenCl kernel argument value memory type.
71   ///
72   /// \return Status as a status identification of set kernel argument
73   template <typename T>
SetKernelArg(const cl::Kernel & kernel,uint32_t index,const T value)74   typename std::enable_if<!std::is_pointer<T>::value, Status>::type SetKernelArg(const cl::Kernel &kernel,
75                                                                                  uint32_t index, const T value) {
76     if (const_cast<cl::Kernel &>(kernel).setArg(index, value) != CL_SUCCESS) {
77       return kLiteError;
78     } else {
79       return kSuccess;
80     }
81   }
82 
83   /// \brief Run OpenCl kernel
84   ///
85   /// \param[in] kernel Define OpenCl kernel.
86   /// \param[in] global Define the number of work items
87   /// \param[in] local Define the number of work_items in a work_group
88   /// \param[in] command_queue Define the command queue
89   /// \param[in] event Define event of kernel run
90   ///
91   /// \return Status as a status identification of run OpenCl kernel
92   Status RunKernel(const cl::Kernel &kernel, const cl::NDRange &global, const cl::NDRange &local,
93                    cl::CommandQueue *command_queue = nullptr, cl::Event *event = nullptr);
94 
95   /// \brief Synchronization command queue
96   ///
97   /// \return Status as a status identification of synchronization command queue
98   Status SyncCommandQueue();
99 
100   void *MapBuffer(void *host_ptr, int flags, bool sync = true);
101 
102   Status UnmapBuffer(void *host_ptr);
103 
104   Status ReadImage(void *buffer, void *dst_data);
105 
106   Status WriteImage(void *buffer, void *src_data);
107 
108   std::shared_ptr<Allocator> GetAllocator();
109 
110   uint64_t DeviceMaxWorkGroupSize();
111 
112   uint64_t GetMaxImage2DWidth();
113 
114   uint64_t GetMaxImage2DHeight();
115 
116   uint64_t GetImagePitchAlignment();
117 };
118 }  // namespace mindspore::registry::opencl
119 #endif  // MINDSPORE_LITE_INCLUDE_REGISTRY_OPENCL_RUNTIME_WRAPPER_H
120