spla
cl_accelerator.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of spla project */
3 /* https://github.com/SparseLinearAlgebra/spla */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2023 SparseLinearAlgebra */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef SPLA_CL_ACCELERATOR_HPP
29 #define SPLA_CL_ACCELERATOR_HPP
30 
31 #include <core/accelerator.hpp>
32 #include <core/common.hpp>
33 #include <core/logger.hpp>
34 #include <spla/library.hpp>
35 
36 #include <string>
37 #include <vector>
38 
39 #include <svector.hpp>
40 
41 #ifndef SPLA_RELEASE
42  #define CL_HPP_ENABLE_EXCEPTIONS
43 #endif
44 
45 #define CL_HPP_MINIMUM_OPENCL_VERSION 120
46 #define CL_HPP_TARGET_OPENCL_VERSION 120
47 #include <CL/opencl.hpp>
48 
49 #define VENDOR_CODE_NVIDIA "nvidia"
50 #define VENDOR_CODE_INTEL "intel"
51 #define VENDOR_CODE_AMD "amd"
52 #define VENDOR_CODE_IMG "img"
53 
54 namespace spla {
55 
65  class CLAccelerator final : public Accelerator {
66  public:
68  ~CLAccelerator() override;
69 
70  Status init() override;
71  Status set_platform(int index) override;
72  Status set_device(int index) override;
73  Status set_queues_count(int count) override;
74  const std::string& get_name() override;
75  const std::string& get_description() override;
76  const std::string& get_suffix() override;
77 
78  cl::Platform& get_platform() { return m_platform; }
79  cl::Device& get_device() { return m_device; }
80  cl::Context& get_context() { return m_context; }
81  cl::CommandQueue& get_queue_default() { return m_queues.front(); }
82  class CLProgramCache* get_cache() { return m_cache.get(); }
83  class CLCounterPool* get_counter_pool() { return m_counter_pool.get(); }
84  class CLAllocGeneral* get_alloc_general() { return m_alloc_general.get(); }
85  class CLAlloc* get_alloc_tmp() { return m_alloc_tmp; }
86 
87  [[nodiscard]] const std::string& get_vendor_name() const { return m_vendor_name; }
88  [[nodiscard]] const std::string& get_vendor_code() const { return m_vendor_code; }
89  [[nodiscard]] uint get_vendor_id() const { return m_vendor_id; }
90  [[nodiscard]] uint get_max_cu() const { return m_max_cu; }
91  [[nodiscard]] uint get_max_wgs() const { return m_max_wgs; }
92  [[nodiscard]] uint get_max_local_mem() const { return m_max_local_mem; }
93  [[nodiscard]] uint get_addr_align() const { return m_addr_align; }
94  [[nodiscard]] uint get_default_wgs() const { return m_default_wgs; }
95  [[nodiscard]] uint get_wave_size() const { return m_wave_size; }
96  [[nodiscard]] uint get_num_of_mem_banks() const { return m_num_of_mem_banks; }
97  [[nodiscard]] bool is_nvidia() const { return m_is_nvidia; }
98  [[nodiscard]] bool is_amd() const { return m_is_amd; }
99  [[nodiscard]] bool is_intel() const { return m_is_intel; }
100  [[nodiscard]] bool is_img() const { return m_is_img; }
101 
102  private:
103  cl::Platform m_platform;
104  cl::Device m_device;
105  cl::Context m_context;
106  std::unique_ptr<class CLProgramCache> m_cache;
107  std::unique_ptr<class CLCounterPool> m_counter_pool;
108  std::unique_ptr<class CLAllocLinear> m_alloc_linear;
109  std::unique_ptr<class CLAllocGeneral> m_alloc_general;
110  class CLAlloc* m_alloc_tmp = nullptr;
111 
112  std::string m_name = "OpenCL";
113  std::string m_description;
114  std::string m_suffix = "__cl";
115  std::string m_vendor_name;
116  std::string m_vendor_code;
117  uint m_vendor_id = 0;
118  uint m_max_cu = 0;
119  uint m_max_wgs = 0;
120  uint m_max_local_mem = 0;
121  uint m_addr_align = 128;
122  uint m_default_wgs = 64;
123  uint m_wave_size = 32;
124  uint m_num_of_mem_banks = 32;
125  bool m_is_nvidia = false;
126  bool m_is_amd = false;
127  bool m_is_intel = false;
128  bool m_is_img = false;
129 
130  ankerl::svector<cl::CommandQueue, 2> m_queues;
131  };
132 
138  static inline CLAccelerator* get_acc_cl() {
139  return dynamic_cast<CLAccelerator*>(Library::get()->get_accelerator());
140  }
141 
146 }// namespace spla
147 
148 #endif//SPLA_CL_ACCELERATOR_HPP
Status of library operation execution.
Interface for an computations acceleration backend.
Definition: accelerator.hpp:58
Single-device OpenCL acceleration implementation.
Definition: cl_accelerator.hpp:65
uint get_max_cu() const
Definition: cl_accelerator.hpp:90
const std::string & get_description() override
Definition: cl_accelerator.cpp:205
bool is_intel() const
Definition: cl_accelerator.hpp:99
uint get_max_local_mem() const
Definition: cl_accelerator.hpp:92
cl::Platform & get_platform()
Definition: cl_accelerator.hpp:78
bool is_amd() const
Definition: cl_accelerator.hpp:98
cl::Device & get_device()
Definition: cl_accelerator.hpp:79
uint get_default_wgs() const
Definition: cl_accelerator.hpp:94
uint get_num_of_mem_banks() const
Definition: cl_accelerator.hpp:96
class CLAlloc * get_alloc_tmp()
Definition: cl_accelerator.hpp:85
uint get_max_wgs() const
Definition: cl_accelerator.hpp:91
class CLProgramCache * get_cache()
Definition: cl_accelerator.hpp:82
bool is_img() const
Definition: cl_accelerator.hpp:100
Status set_platform(int index) override
Definition: cl_accelerator.cpp:61
uint get_vendor_id() const
Definition: cl_accelerator.hpp:89
const std::string & get_suffix() override
Definition: cl_accelerator.cpp:208
cl::CommandQueue & get_queue_default()
Definition: cl_accelerator.hpp:81
Status init() override
Definition: cl_accelerator.cpp:42
uint get_wave_size() const
Definition: cl_accelerator.hpp:95
const std::string & get_name() override
Definition: cl_accelerator.cpp:202
Status set_device(int index) override
Definition: cl_accelerator.cpp:84
class CLAllocGeneral * get_alloc_general()
Definition: cl_accelerator.hpp:84
cl::Context & get_context()
Definition: cl_accelerator.hpp:80
bool is_nvidia() const
Definition: cl_accelerator.hpp:97
class CLCounterPool * get_counter_pool()
Definition: cl_accelerator.hpp:83
const std::string & get_vendor_code() const
Definition: cl_accelerator.hpp:88
uint get_addr_align() const
Definition: cl_accelerator.hpp:93
~CLAccelerator() override
Status set_queues_count(int count) override
Definition: cl_accelerator.cpp:176
const std::string & get_vendor_name() const
Definition: cl_accelerator.hpp:87
Wrapper for default OpenCL buffer allcoation.
Definition: cl_alloc_general.hpp:40
Base class for any device-local opencl buffer allocator.
Definition: cl_alloc.hpp:39
Global pool with pre-allocated counters.
Definition: cl_counter.hpp:75
Runtime cache for compiled opencl programs.
Definition: cl_program_cache.hpp:49
static Library * get()
Access global library instance.
Definition: library.cpp:218
class Accelerator * get_accelerator()
Definition: library.cpp:198
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37