timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
aligned_allocator.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25/** \file timemory/ert/aligned_allocator.hpp
26 * \headerfile timemory/ert/aligned_allocator.hpp "timemory/ert/aligned_allocator.hpp"
27 * Provides an aligned allocator
28 *
29 */
30
31#pragma once
32
33#include "timemory/backends/device.hpp"
34#include "timemory/backends/memory.hpp"
36
37namespace tim
38{
39namespace ert
40{
41//--------------------------------------------------------------------------------------//
42// allocator that can be used with STL containers
43//
44// default template parameter AlignV uses (8 * sizeof(Tp)) because alignment should
45// be specified in bits and sizeof(Tp) return bytes so for float (sizeof == 4), this
46// would be 32-bit alignment and for double (sizeof == 8), this would be 64-bit alignment
47//
48template <typename Tp, std::size_t AlignV = 8 * sizeof(Tp)>
50{
51public:
52 // The following will be the same for virtually all allocators.
53 using value_type = Tp;
54 using pointer = Tp*;
55 using reference = Tp&;
56 using const_pointer = const Tp*;
57 using const_reference = const Tp&;
58 using size_type = std::size_t;
59 using difference_type = ptrdiff_t;
60
61public:
62 // constructors and destructors
63 aligned_allocator() = default;
66 template <typename U>
68 {}
69 ~aligned_allocator() = default;
70
71public:
72 // operators
75 bool operator!=(const aligned_allocator& other) const { return !(*this == other); }
76 bool operator==(const aligned_allocator&) const { return true; }
77
78public:
79 TIMEMORY_NODISCARD Tp* address(Tp& r) const { return &r; }
80 TIMEMORY_NODISCARD const Tp* address(const Tp& s) const { return &s; }
81
82 TIMEMORY_NODISCARD std::size_t max_size() const
83 {
84 // avoid signed/unsigned warnings independent of size_t definition
85 return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(Tp);
86 }
87
88 // The following must be the same for all allocators.
89 template <typename U>
90 struct rebind
91 {
93 };
94
95 void construct(Tp* const p, const Tp& t) const
96 {
97 void* const pv = static_cast<void*>(p);
98 new(pv) Tp(t);
99 }
100
101 template <typename... ArgsT>
102 void construct(Tp* const p, ArgsT&&... args) const
103 {
104 ::new((void*) p) Tp(std::forward<ArgsT>(args)...);
105 }
106
107 void destroy(Tp* const p) const { p->~Tp(); }
108
109 TIMEMORY_NODISCARD Tp* allocate(const std::size_t n) const
110 {
111 if(n == 0)
112 return nullptr;
113
114 // integer overflow check that throws std::length_error in case of overflow
115 if(n > max_size())
116 {
117 throw std::length_error(
118 "aligned_allocator<Tp>::allocate() - Integer overflow.");
119 }
120
121 // Mallocator wraps malloc().
122 void* const ptr = memory::allocate_aligned<Tp, device::cpu>(n, AlignV);
123
124 // throw std::bad_alloc in the case of memory allocation failure.
125 if(ptr == nullptr)
126 {
127 std::cerr << "Allocation of type " << typeid(Tp).name() << " of size " << n
128 << " and alignment " << AlignV << " failed. ptr = " << ptr
129 << std::endl;
130 throw std::bad_alloc();
131 }
132
133 return static_cast<Tp*>(ptr);
134 }
135
136 void deallocate(Tp* const ptr, const std::size_t) const
137 {
138 memory::free_aligned<Tp, device::cpu>(ptr);
139 }
140
141 // same for all allocators that ignore hints.
142 template <typename U>
143 TIMEMORY_NODISCARD Tp* allocate(const std::size_t n, const U* /* const hint */) const
144 {
145 return allocate(n);
146 }
147
148 // returns the alignment in bytes
149 std::size_t get_alignment() { return AlignV / 8; }
150};
151
152} // namespace ert
153} // namespace tim
aligned_allocator(const aligned_allocator &)=default
aligned_allocator(const aligned_allocator< U, AlignV > &)
aligned_allocator< U, AlignV > other
bool operator!=(const aligned_allocator &other) const
Tp * allocate(const std::size_t n, const U *) const
void destroy(Tp *const p) const
aligned_allocator(aligned_allocator &&) noexcept
void deallocate(Tp *const ptr, const std::size_t) const
bool operator==(const aligned_allocator &) const
aligned_allocator & operator=(const aligned_allocator &)=delete
void construct(Tp *const p, ArgsT &&... args) const
void construct(Tp *const p, const Tp &t) const
Tp * allocate(const std::size_t n) const
const Tp * address(const Tp &s) const
aligned_allocator & operator==(aligned_allocator &&)=delete
Definition: kokkosp.cpp:39