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