You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

opencl_runtime_wrapper.h 5.4 kB

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