Unified Memory Group Allocator
include
GroupAllocator
GroupAllocator.h
Go to the documentation of this file.
1
4
#pragma once
5
6
#include <cmath>
7
#include <functional>
8
#include <mutex>
9
#include <vector>
10
#include <list>
11
#include <utility>
12
#include "
MultiPageAllocator.h
"
13
#include "
InPageAllocator.h
"
14
15
namespace
groupallocator {
16
20
class
GroupAllocator
{
21
public
:
22
23
GroupAllocator
() =
delete
;
24
29
GroupAllocator
(
int
group_num, std::size_t page_size)
30
: group_num_(group_num),
31
PAGE_SIZE(page_size) {
32
mpa =
new
MultiPageAllocator
(page_size);
33
}
34
38
~GroupAllocator
() {}
39
43
void
freeall
() {
44
for
(
auto
&e : ipas) {
45
delete
e;
46
}
47
delete
mpa;
48
ipas = std::vector<InPageAllocator *>{};
49
mpa =
nullptr
;
50
}
51
57
template
<
class
T>
58
void
free
(T *ptr) {
59
mpa->free(ptr);
60
m.lock();
61
for
(
auto
&e : ipas) {
62
if
(e->contains((
size_t
) ptr)) {
63
e->free(ptr);
64
break
;
65
}
66
}
67
m.unlock();
68
}
69
77
template
<
class
T>
78
void
allocate
(T **ptr,
size_t
s,
bool
forceAligned128) {
79
if
(ptr == NULL || s == 0) {
80
return
;
81
}
82
83
if
(s +
alignof
(T *) > PAGE_SIZE) {
84
mpa->
allocate
<T>(ptr, s, forceAligned128);
85
}
else
{
86
m.lock();
87
int
lastSize = ipas.size();
88
if
(lastSize == 0) {
89
InPageAllocator
*ipa_new =
90
new
InPageAllocator
(PAGE_SIZE);
91
ipas.push_back(ipa_new);
92
}
93
auto
ipa3 = ipas[ipas.size() - 1];
94
m.unlock();
95
ipa3->allocate<T>(ptr, s, forceAligned128);
96
while
(*ptr == NULL) {
97
InPageAllocator
*ipa2 =
98
new
InPageAllocator
(PAGE_SIZE);
99
m.lock();
100
if
(lastSize == ipas.size()) {
101
ipas.push_back(ipa2);
102
lastSize = ipas.size();
103
}
104
m.unlock();
105
m.lock();
106
auto
ipa = ipas[ipas.size() - 1];
107
m.unlock();
108
ipa->allocate<T>(ptr, s, forceAligned128);
109
}
110
}
111
}
112
118
void
moveToDevice
(
int
device, cudaStream_t stream) {
119
mpa->moveToDevice(device, stream);
120
m.lock();
121
for
(
auto
&e : ipas) {
122
e->moveToDevice(device, stream);
123
}
124
m.unlock();
125
}
126
127
size_t
pagesAllocated() {
128
auto
s = mpa->getPages();
129
m.lock();
130
s += ipas.size();
131
m.unlock();
132
return
s;
133
}
134
135
size_t
getPageSize() {
136
return
PAGE_SIZE;
137
}
138
139
private
:
140
std::vector<InPageAllocator *> ipas;
141
MultiPageAllocator *mpa;
142
std::mutex m;
143
int
group_num_;
144
const
size_t
PAGE_SIZE;
145
};
146
147
}
// namespace groupallocator
MultiPageAllocator.h
groupallocator::GroupAllocator::allocate
void allocate(T **ptr, size_t s, bool forceAligned128)
Definition:
GroupAllocator.h:78
groupallocator::MultiPageAllocator
Definition:
MultiPageAllocator.h:21
InPageAllocator.h
groupallocator::GroupAllocator::free
void free(T *ptr)
Definition:
GroupAllocator.h:58
groupallocator::GroupAllocator::~GroupAllocator
~GroupAllocator()
Definition:
GroupAllocator.h:38
groupallocator::GroupAllocator::GroupAllocator
GroupAllocator(int group_num, std::size_t page_size)
Definition:
GroupAllocator.h:29
groupallocator::GroupAllocator
Definition:
GroupAllocator.h:20
groupallocator::GroupAllocator::moveToDevice
void moveToDevice(int device, cudaStream_t stream)
Definition:
GroupAllocator.h:118
groupallocator::GroupAllocator::freeall
void freeall()
Definition:
GroupAllocator.h:43
groupallocator::MultiPageAllocator::allocate
void allocate(T **ptr, size_t s, bool forceAligned128)
Definition:
MultiPageAllocator.h:48
groupallocator::InPageAllocator
Definition:
InPageAllocator.h:21
Generated by
1.8.17