• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-2023 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 
20 #include <vector>
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <utility>
26 #include <type_traits>
27 #include "CL/cl2.hpp"
28 #include "include/api/allocator.h"
29 #include "include/api/status.h"
30 #include "include/api/dual_abi_helper.h"
31 
32 namespace mindspore::registry::opencl {
33 class MS_API OpenCLRuntimeWrapper {
34  public:
35   OpenCLRuntimeWrapper() = default;
36   ~OpenCLRuntimeWrapper() = default;
37 
38   /// \brief Load the OpenCl source code and bind the program name.
39   ///
40   /// \param[in] program_name Define OpenCl source program name.
41   /// \param[in] source Define OpenCl source.
42   ///
43   /// \return Status as a status identification of loading code.
44   inline Status LoadSource(const std::string &program_name, const std::string &source);
45 
46   /// \brief Building OpenCL code.
47   ///
48   /// \param[in] kernel Used to return the compiled kernel
49   /// \param[in] program_name Define OpenCl source program name.
50   /// \param[in] kernel_name Define OpenCl source kernel name.
51   /// \param[in] build_options_ext Define OpenCl kernel build options.
52   ///
53   /// \return Status as a status identification of build Kernel
54   inline Status BuildKernel(cl::Kernel *kernel, const std::string &program_name, const std::string &kernel_name,
55                             const std::vector<std::string> &build_options_ext = {});
56 
57   /// \brief Set kernel argument
58   ///
59   /// \param[in] kernel Define OpenCl kernel.
60   /// \param[in] index Define OpenCl kernel argument index.
61   /// \param[in] value Define OpenCl kernel argument value pointer.
62   /// \param[in] mem_type Define OpenCl kernel argument value memory type.
63   ///
64   /// \return Status as a status identification of set kernel argument
65   Status SetKernelArg(const cl::Kernel &kernel, uint32_t index, void *const value);
66 
67   /// \brief Set kernel argument
68   ///
69   /// \param[in] kernel Define OpenCl kernel.
70   /// \param[in] index Define OpenCl kernel argument index.
71   /// \param[in] value Define OpenCl kernel argument value.
72   /// \param[in] mem_type Define OpenCl kernel argument value memory type.
73   ///
74   /// \return Status as a status identification of set kernel argument
75   template <typename T>
SetKernelArg(const cl::Kernel & kernel,uint32_t index,const T value)76   typename std::enable_if<!std::is_pointer<T>::value, Status>::type SetKernelArg(const cl::Kernel &kernel,
77                                                                                  uint32_t index, const T value) {
78     if (const_cast<cl::Kernel &>(kernel).setArg(index, value) != CL_SUCCESS) {
79       return kLiteError;
80     } else {
81       return kSuccess;
82     }
83   }
84 
85   /// \brief Run OpenCl kernel
86   ///
87   /// \param[in] kernel Define OpenCl kernel.
88   /// \param[in] global Define the number of work items
89   /// \param[in] local Define the number of work_items in a work_group
90   /// \param[in] command_queue Define the command queue
91   /// \param[in] event Define event of kernel run
92   ///
93   /// \return Status as a status identification of run OpenCl kernel
94   Status RunKernel(const cl::Kernel &kernel, const cl::NDRange &global, const cl::NDRange &local,
95                    cl::CommandQueue *command_queue = nullptr, cl::Event *event = nullptr);
96 
97   /// \brief Synchronization command queue
98   ///
99   /// \return Status as a status identification of synchronization command queue
100   Status SyncCommandQueue();
101 
102   void *MapBuffer(void *host_ptr, int flags, bool sync = true);
103 
104   Status UnmapBuffer(void *host_ptr);
105 
106   Status ReadImage(void *buffer, void *dst_data);
107 
108   Status WriteImage(void *buffer, void *src_data);
109 
110   std::shared_ptr<Allocator> GetAllocator();
111 
112   uint64_t DeviceMaxWorkGroupSize();
113 
114   uint64_t GetMaxImage2DWidth();
115 
116   uint64_t GetMaxImage2DHeight();
117 
118   uint64_t GetImagePitchAlignment();
119 
120  private:
121   Status LoadSource(const std::vector<char> &program_name, const std::vector<char> &source);
122 
123   Status BuildKernel(cl::Kernel *kernel, const std::vector<char> &program_name, const std::vector<char> &kernel_name,
124                      const std::vector<std::vector<char>> &build_options_ext);
125 };
126 
LoadSource(const std::string & program_name,const std::string & source)127 Status OpenCLRuntimeWrapper::LoadSource(const std::string &program_name, const std::string &source) {
128   return LoadSource(StringToChar(program_name), StringToChar(source));
129 }
130 
BuildKernel(cl::Kernel * kernel,const std::string & program_name,const std::string & kernel_name,const std::vector<std::string> & build_options_ext)131 Status OpenCLRuntimeWrapper::BuildKernel(cl::Kernel *kernel, const std::string &program_name,
132                                          const std::string &kernel_name,
133                                          const std::vector<std::string> &build_options_ext) {
134   return BuildKernel(kernel, StringToChar(program_name), StringToChar(kernel_name),
135                      VectorStringToChar(build_options_ext));
136 }
137 }  // namespace mindspore::registry::opencl
138 #endif  // MINDSPORE_LITE_INCLUDE_REGISTRY_OPENCL_RUNTIME_WRAPPER_H_
139