-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstack.cpp
More file actions
77 lines (63 loc) · 2.31 KB
/
stack.cpp
File metadata and controls
77 lines (63 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2025, IST Austria, developed by Erik Schultheis
// SPDX-License-Identifier: Apache-2.0
//
#include "stack.h"
#include "utilities/utils.h"
DeviceMemoryStack::DeviceMemoryStack(std::byte* memory, std::size_t amount, int device_id) :
mBackingMemory(memory), mTop(memory), mDeviceID(device_id), mCapacity(amount) {
}
std::byte* DeviceMemoryStack::allocate(std::size_t amount, const char* name) {
constexpr size_t alignment = 4096;
std::size_t aligned_amount = div_ceil(amount, alignment) * alignment;
std::byte* new_top = mTop + aligned_amount;
if(new_top > mBackingMemory + mCapacity) {
throw std::bad_alloc();
}
mAlloc.emplace_back(mTop, aligned_amount, name);
mTop = new_top;
_track_max();
return mAlloc.back().Pointer;
}
Tensor DeviceMemoryStack::allocate(ETensorDType dtype, const std::vector<long>& shape, const char* name) {
std::size_t total = std::accumulate(std::begin(shape), std::end(shape), (long)get_dtype_size(dtype), std::multiplies<>());
return Tensor::from_pointer(allocate(total, name), mDeviceID, dtype, shape);
}
void DeviceMemoryStack::free(std::byte* ptr) {
if(mAlloc.empty()) {
throw std::logic_error("DeviceMemoryStack::free_left called with empty allocation list");
}
if(mAlloc.back().Pointer != ptr) {
throw std::logic_error("DeviceMemoryStack::free_left called with wrong pointer");
}
mTop = mAlloc.back().Pointer;
mAlloc.pop_back();
}
std::vector<std::pair<std::string, long>> DeviceMemoryStack::get_allocation_stats() const {
std::vector<std::pair<std::string, long>> result;
for (auto& [ptr, amount, name]: get_high_mark()) {
result.emplace_back(name, amount);
}
return result;
}
void DeviceMemoryStack::_track_max() {
if(bytes_used() > mMaxUtilization) {
mMaxUtilization = bytes_used();
mHighMark = mAlloc;
}
}
std::size_t DeviceMemoryStack::unused_capacity() const {
return mCapacity - (mTop - mBackingMemory);
}
std::size_t DeviceMemoryStack::bytes_used() const {
return mCapacity - unused_capacity();
}
std::size_t DeviceMemoryStack::max_utilization() const {
return mMaxUtilization;
}
void DeviceMemoryStack::free(Tensor& tensor) {
free(tensor.Data);
tensor.Data = nullptr;
}
int DeviceMemoryStack::device_id() const {
return mDeviceID;
}