spla
Loading...
Searching...
No Matches
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#define VENDOR_CODE_POCL_CPU "pocl_cpu"
54
55#define SPLA_OPENCL_PLATFORM "SPLA_OPENCL_PLATFORM"
56#define SPLA_OPENCL_DEVICE "SPLA_OPENCL_DEVICE"
57
58namespace spla {
59
69 class CLAccelerator final : public Accelerator {
70 public:
72 ~CLAccelerator() override;
73
74 Status init() override;
75 Status set_platform(int index) override;
76 Status set_device(int index) override;
77 Status set_queues_count(int count) override;
78 const std::string& get_name() override;
79 const std::string& get_description() override;
80 const std::string& get_suffix() override;
81
82 cl::Platform& get_platform() { return m_platform; }
83 cl::Device& get_device() { return m_device; }
84 cl::Context& get_context() { return m_context; }
85 cl::CommandQueue& get_queue_default() { return m_queues.front(); }
86 class CLProgramCache* get_cache() { return m_cache.get(); }
87 class CLCounterPool* get_counter_pool() { return m_counter_pool.get(); }
88 class CLAllocGeneral* get_alloc_general() { return m_alloc_general.get(); }
89 class CLAlloc* get_alloc_tmp() { return m_alloc_tmp; }
90
91 [[nodiscard]] const std::string& get_vendor_name() const { return m_vendor_name; }
92 [[nodiscard]] const std::string& get_vendor_code() const { return m_vendor_code; }
93 [[nodiscard]] uint get_vendor_id() const { return m_vendor_id; }
94 [[nodiscard]] uint get_max_cu() const { return m_max_cu; }
95 [[nodiscard]] uint get_max_wgs() const { return m_max_wgs; }
96 [[nodiscard]] uint get_max_local_mem() const { return m_max_local_mem; }
97 [[nodiscard]] uint get_addr_align() const { return m_addr_align; }
98 [[nodiscard]] uint get_default_wgs() const { return m_default_wgs; }
99 [[nodiscard]] uint get_wave_size() const { return m_wave_size; }
100 [[nodiscard]] uint get_num_of_mem_banks() const { return m_num_of_mem_banks; }
101 [[nodiscard]] bool is_nvidia() const { return m_is_nvidia; }
102 [[nodiscard]] bool is_amd() const { return m_is_amd; }
103 [[nodiscard]] bool is_intel() const { return m_is_intel; }
104 [[nodiscard]] bool is_img() const { return m_is_img; }
105
106 private:
107 cl::Platform m_platform;
108 cl::Device m_device;
109 cl::Context m_context;
110 std::unique_ptr<class CLProgramCache> m_cache;
111 std::unique_ptr<class CLCounterPool> m_counter_pool;
112 std::unique_ptr<class CLAllocLinear> m_alloc_linear;
113 std::unique_ptr<class CLAllocGeneral> m_alloc_general;
114 class CLAlloc* m_alloc_tmp = nullptr;
115
116 std::string m_name = "OpenCL";
117 std::string m_description;
118 std::string m_suffix = "__cl";
119 std::string m_vendor_name;
120 std::string m_vendor_code;
121 uint m_vendor_id = 0;
122 uint m_max_cu = 0;
123 uint m_max_wgs = 0;
124 uint m_max_local_mem = 0;
125 uint m_addr_align = 128;
126 uint m_default_wgs = 64;
127 uint m_wave_size = 32;
128 uint m_num_of_mem_banks = 32;
129 bool m_is_pocl = false;
130 bool m_is_nvidia = false;
131 bool m_is_amd = false;
132 bool m_is_intel = false;
133 bool m_is_img = false;
134
135 ankerl::svector<cl::CommandQueue, 2> m_queues;
136 };
137
143 static inline CLAccelerator* get_acc_cl() {
144 return dynamic_cast<CLAccelerator*>(Library::get()->get_accelerator());
145 }
146
151}// namespace spla
152
153#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:69
uint get_max_cu() const
Definition cl_accelerator.hpp:94
cl::CommandQueue & get_queue_default()
Definition cl_accelerator.hpp:85
const std::string & get_vendor_name() const
Definition cl_accelerator.hpp:91
const std::string & get_description() override
Definition cl_accelerator.cpp:225
class CLAllocGeneral * get_alloc_general()
Definition cl_accelerator.hpp:88
bool is_intel() const
Definition cl_accelerator.hpp:103
uint get_max_local_mem() const
Definition cl_accelerator.hpp:96
bool is_amd() const
Definition cl_accelerator.hpp:102
uint get_default_wgs() const
Definition cl_accelerator.hpp:98
uint get_num_of_mem_banks() const
Definition cl_accelerator.hpp:100
uint get_max_wgs() const
Definition cl_accelerator.hpp:95
cl::Platform & get_platform()
Definition cl_accelerator.hpp:82
const std::string & get_vendor_code() const
Definition cl_accelerator.hpp:92
class CLCounterPool * get_counter_pool()
Definition cl_accelerator.hpp:87
class CLAlloc * get_alloc_tmp()
Definition cl_accelerator.hpp:89
bool is_img() const
Definition cl_accelerator.hpp:104
cl::Device & get_device()
Definition cl_accelerator.hpp:83
Status set_platform(int index) override
Definition cl_accelerator.cpp:66
uint get_vendor_id() const
Definition cl_accelerator.hpp:93
const std::string & get_suffix() override
Definition cl_accelerator.cpp:228
cl::Context & get_context()
Definition cl_accelerator.hpp:84
Status init() override
Definition cl_accelerator.cpp:42
uint get_wave_size() const
Definition cl_accelerator.hpp:99
const std::string & get_name() override
Definition cl_accelerator.cpp:222
Status set_device(int index) override
Definition cl_accelerator.cpp:89
class CLProgramCache * get_cache()
Definition cl_accelerator.hpp:86
bool is_nvidia() const
Definition cl_accelerator.hpp:101
uint get_addr_align() const
Definition cl_accelerator.hpp:97
~CLAccelerator() override
Status set_queues_count(int count) override
Definition cl_accelerator.cpp:196
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