spla
Loading...
Searching...
No Matches
cl_alloc_linear.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/JetBrains-Research/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_ALLOC_LINEAR_HPP
29#define SPLA_CL_ALLOC_LINEAR_HPP
30
32#include <opencl/cl_alloc.hpp>
33
34#include <svector.hpp>
35
36namespace spla {
37
47 class CLAllocLinear : public CLAlloc {
48 public:
49 static constexpr const uint DEFAULT_SIZE = 1024 * 1024;// 1 MiB
50 static constexpr const uint DEFAULT_ALIGNMENT = 128;
51
52 explicit CLAllocLinear(std::size_t arena_size = DEFAULT_SIZE, std::size_t alignment = DEFAULT_ALIGNMENT);
53 ~CLAllocLinear() override = default;
54
55 cl::Buffer alloc(std::size_t size) override;
56 void free(cl::Buffer buffer) override;
57 void free_all() override;
58
59 protected:
60 void expand();
61 void shrink();
62
63 private:
64 ankerl::svector<cl::Buffer, 4> m_arena;
65 std::size_t m_alignment = 128;
66 std::size_t m_curr_offset = 0;
67 std::size_t m_total_allocated = 0;
68 std::size_t m_arena_size = 0;
69 std::size_t m_fallback_size = 0;
70 };
71
72}// namespace spla
73
74#endif//SPLA_CL_ALLOC_LINEAR_HPP
Linear allocator for temporary device local buffer allocations.
Definition cl_alloc_linear.hpp:47
static constexpr const uint DEFAULT_SIZE
Definition cl_alloc_linear.hpp:49
void free(cl::Buffer buffer) override
Definition cl_alloc_linear.cpp:68
CLAllocLinear(std::size_t arena_size=DEFAULT_SIZE, std::size_t alignment=DEFAULT_ALIGNMENT)
Definition cl_alloc_linear.cpp:36
void expand()
Definition cl_alloc_linear.cpp:79
void free_all() override
Definition cl_alloc_linear.cpp:72
static constexpr const uint DEFAULT_ALIGNMENT
Definition cl_alloc_linear.hpp:50
void shrink()
Definition cl_alloc_linear.cpp:85
~CLAllocLinear() override=default
cl::Buffer alloc(std::size_t size) override
Definition cl_alloc_linear.cpp:46
Base class for any device-local opencl buffer allocator.
Definition cl_alloc.hpp:39
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
Definition algorithm.hpp:37