forked from preda/gpuowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllocTrac.h
67 lines (54 loc) · 1.62 KB
/
AllocTrac.h
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
// Copyright (C) Mihai Preda.
#pragma once
#include "common.h"
#include <atomic>
#include <new>
#include <string>
using namespace std::string_literals;
/*
class gpu_bad_alloc : public std::bad_alloc {
std::string w;
public:
gpu_bad_alloc(const std::string& w) : w(w) {}
gpu_bad_alloc(size_t size) : gpu_bad_alloc("GPU size "s + std::to_string(size)) {}
const char *what() const noexcept override { return w.c_str(); }
};
*/
class AllocTrac {
static std::atomic<size_t> totalAlloc;
static size_t maxAlloc;
size_t size{};
public:
AllocTrac() = default;
explicit AllocTrac(size_t size) : size(size) {
if (size) {
if (totalAlloc + size >= maxAlloc) {
log("Reached GPU maxAlloc limit %.1f GB\n", float(maxAlloc) / (1024 * 1024 * 1024));
throw bad_alloc();
}
totalAlloc += size;
// log("alloc %lu total %lu limit %lu\n", size, size_t(totalAlloc), maxAlloc);
}
}
~AllocTrac() {
if (size) {
totalAlloc -= size;
// log("release %lu total %lu limit %lu\n", size, size_t(totalAlloc), maxAlloc);
}
}
AllocTrac(const AllocTrac&) = delete;
void operator=(const AllocTrac&) = delete;
AllocTrac(AllocTrac&& rhs) : size(rhs.size) { rhs.size = 0; }
AllocTrac& operator=(AllocTrac&& rhs) {
AllocTrac tmp{std::move(rhs)};
swap(*this, tmp);
return *this;
}
friend void swap(AllocTrac& a, AllocTrac& b) noexcept {
using std::swap;
swap(a.size, b.size);
}
static void setMaxAlloc(size_t m) { maxAlloc = m; }
static size_t totalAllocBytes() { return totalAlloc; }
static size_t availableBytes() { return maxAlloc - totalAlloc; }
};