Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
composable_kernel_ROCM
Commits
810c3c1d
Commit
810c3c1d
authored
Dec 23, 2024
by
Ville Pietilä
Browse files
Bug fixes to pinned host memory pool.
parent
f0b61477
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
22 deletions
+39
-22
include/ck/utility/host_memory_allocator.hpp
include/ck/utility/host_memory_allocator.hpp
+39
-22
No files found.
include/ck/utility/host_memory_allocator.hpp
View file @
810c3c1d
...
@@ -193,7 +193,7 @@ namespace memory {
...
@@ -193,7 +193,7 @@ namespace memory {
}
}
// Memory became too fragmented, reserve a new block.
// Memory became too fragmented, reserve a new block.
// This should not happen very often.
// This should not happen very often
, practically never
.
allocateNewPinnedMemoryBlock
();
allocateNewPinnedMemoryBlock
();
return
allocateNewMemory
(
sizeInBytes
);
return
allocateNewMemory
(
sizeInBytes
);
}
}
...
@@ -204,24 +204,45 @@ namespace memory {
...
@@ -204,24 +204,45 @@ namespace memory {
if
(
memory_pool_
.
find
(
sizeInBytes
)
!=
memory_pool_
.
end
())
if
(
memory_pool_
.
find
(
sizeInBytes
)
!=
memory_pool_
.
end
())
{
{
auto
&
q
=
memory_pool_
[
sizeInBytes
];
memory_pool_
[
sizeInBytes
].
push
(
p
);
q
.
push
(
p
);
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
std
::
cout
<<
"[ StaticMemPool ] Deallocate: Added memory to back to pool for size "
<<
sizeInBytes
<<
std
::
cout
<<
"[ StaticMemPool ] Deallocate: Added memory to back to pool for size "
<<
sizeInBytes
<<
", pool has now "
<<
q
.
size
()
<<
" elements."
<<
std
::
endl
;
", pool has now "
<<
memory_pool_
[
sizeInBytes
]
.
size
()
<<
" elements."
<<
std
::
endl
;
}
}
}
}
else
{
else
{
std
::
queue
<
void
*>
q
;
std
::
queue
<
void
*>
q
;
q
.
push
(
p
);
q
.
push
(
p
);
memory_pool_
.
insert
(
std
::
make_pair
(
sizeInBytes
,
std
::
move
(
q
)
))
;
memory_pool_
[
sizeInBytes
]
=
std
::
move
(
q
);
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
std
::
cout
<<
"[ StaticMemPool ] Deallocate: Created new pool for size "
<<
sizeInBytes
<<
std
::
endl
;
std
::
cout
<<
"[ StaticMemPool ] Deallocate: Created new pool for size "
<<
sizeInBytes
<<
", pool has now "
<<
memory_pool_
[
sizeInBytes
].
size
()
<<
" elements."
<<
std
::
endl
;
}
}
}
size_t
currentOffsetInBytes
()
const
{
return
offsetInBytes_
;
}
size_t
numberOfPinnedMemoryBlocks
()
const
{
return
pinnedMemoryBaseAddress_
.
size
();
}
}
size_t
memoryPoolSizeInBytes
()
const
{
return
memoryPoolSizeInBytes_
;
}
}
const
std
::
map
<
size_t
,
std
::
queue
<
void
*>>&
memoryPool
()
const
{
return
memory_pool_
;
}
}
private:
private:
constexpr
static
size_t
defaultMaxMemoryPoolSizeInBytes_
=
10
*
1024
*
1024
;
// 10MB
constexpr
static
size_t
defaultMaxMemoryPoolSizeInBytes_
=
10
*
1024
*
1024
;
// 10MB
std
::
mutex
mutex_
;
// Mutex to protect access to the memory pool.
std
::
mutex
mutex_
;
// Mutex to protect access to the memory pool.
...
@@ -241,7 +262,7 @@ namespace memory {
...
@@ -241,7 +262,7 @@ namespace memory {
offsetInBytes_
=
0
;
offsetInBytes_
=
0
;
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
std
::
cout
<<
"[ StaticMemPool ] Allocation:
c
reated new pinned memory block of "
<<
memoryPoolSizeInBytes_
<<
" bytes."
<<
std
::
endl
;
std
::
cout
<<
"[ StaticMemPool ] Allocation:
C
reated new pinned memory block of "
<<
memoryPoolSizeInBytes_
<<
" bytes."
<<
std
::
endl
;
}
}
}
}
...
@@ -253,7 +274,7 @@ namespace memory {
...
@@ -253,7 +274,7 @@ namespace memory {
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
const
auto
pct
=
100.0
f
*
static_cast
<
float
>
(
offsetInBytes_
)
/
memoryPoolSizeInBytes_
;
const
auto
pct
=
100.0
f
*
static_cast
<
float
>
(
offsetInBytes_
)
/
memoryPoolSizeInBytes_
;
std
::
cout
<<
"[ StaticMemPool ] Allocation:
r
eturn new memory of "
<<
sizeInBytes
<<
std
::
cout
<<
"[ StaticMemPool ] Allocation:
R
eturn new memory of "
<<
sizeInBytes
<<
" bytes, pinned host memory usage: "
<<
pct
<<
"%."
<<
std
::
endl
;
" bytes, pinned host memory usage: "
<<
pct
<<
"%."
<<
std
::
endl
;
}
}
return
p
;
return
p
;
...
@@ -264,12 +285,13 @@ namespace memory {
...
@@ -264,12 +285,13 @@ namespace memory {
if
(
memory_pool_
.
find
(
sizeInBytes
)
!=
memory_pool_
.
end
()
&&
!
memory_pool_
[
sizeInBytes
].
empty
())
if
(
memory_pool_
.
find
(
sizeInBytes
)
!=
memory_pool_
.
end
()
&&
!
memory_pool_
[
sizeInBytes
].
empty
())
{
{
// If there is a memory pool for the requested size, return memory from the pool.
// If there is a memory pool for the requested size, return memory from the pool.
void
*
p
=
memory_pool_
[
sizeInBytes
].
front
();
memory_pool_
[
sizeInBytes
].
pop
();
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
std
::
cout
<<
"[ StaticMemPool ] Allocation: reusing memory from pool for size "
<<
sizeInBytes
<<
std
::
endl
;
std
::
cout
<<
"[ StaticMemPool ] Allocation: Reusing memory from pool for size "
<<
sizeInBytes
<<
", pool has now "
<<
memory_pool_
[
sizeInBytes
].
size
()
<<
" elements."
<<
std
::
endl
;
}
}
void
*
p
=
memory_pool_
[
sizeInBytes
].
front
();
memory_pool_
[
sizeInBytes
].
pop
();
return
p
;
return
p
;
}
}
...
@@ -285,13 +307,12 @@ namespace memory {
...
@@ -285,13 +307,12 @@ namespace memory {
if
(
nearest_queue_size
!=
std
::
numeric_limits
<
size_t
>::
max
())
if
(
nearest_queue_size
!=
std
::
numeric_limits
<
size_t
>::
max
())
{
{
auto
&
nearest_queue
=
memory_pool_
[
nearest_queue_size
];
void
*
p
=
memory_pool_
[
nearest_queue_size
].
front
();
void
*
p
=
nearest_queue
.
front
();
memory_pool_
[
nearest_queue_size
].
pop
();
nearest_queue
.
pop
();
if
(
enableLogging_
)
if
(
enableLogging_
)
{
{
std
::
cout
<<
"[ StaticMemPool ] Allocation:
r
eusing memory from pool for size "
<<
nearest_queue_size
<<
std
::
cout
<<
"[ StaticMemPool ] Allocation:
R
eusing memory from pool for size "
<<
nearest_queue_size
<<
" to allocate "
<<
sizeInBytes
<<
" bytes, pool has "
<<
nearest_queue
.
size
()
<<
" elements."
<<
" to allocate "
<<
sizeInBytes
<<
" bytes, pool has "
<<
memory_pool_
[
nearest_queue
_size
]
.
size
()
<<
" elements."
<<
std
::
endl
;
std
::
endl
;
}
}
return
p
;
return
p
;
...
@@ -305,8 +326,8 @@ namespace memory {
...
@@ -305,8 +326,8 @@ namespace memory {
class
PinnedHostMemoryAllocatorBase
class
PinnedHostMemoryAllocatorBase
{
{
p
rotected
:
p
ublic
:
virtual
IMemPool
*
get_memory_pool
()
{
IMemPool
*
get_memory_pool
()
{
static
DynamicMemPool
dynamic_memory_pool
;
static
DynamicMemPool
dynamic_memory_pool
;
static
StaticMemPool
static_memory_pool
;
static
StaticMemPool
static_memory_pool
;
static
bool
use_dynamic_mem_pool
=
ck
::
EnvIsEnabled
(
CK_ENV
(
CK_USE_DYNAMIC_MEM_POOL
));
static
bool
use_dynamic_mem_pool
=
ck
::
EnvIsEnabled
(
CK_ENV
(
CK_USE_DYNAMIC_MEM_POOL
));
...
@@ -366,10 +387,6 @@ namespace memory {
...
@@ -366,10 +387,6 @@ namespace memory {
void
destroy
(
U
*
p
)
noexcept
{
void
destroy
(
U
*
p
)
noexcept
{
p
->~
U
();
p
->~
U
();
}
}
protected:
IMemPool
*
get_memory_pool
()
override
{
return
PinnedHostMemoryAllocatorBase
::
get_memory_pool
();
}
};
};
template
<
typename
T
,
typename
U
>
template
<
typename
T
,
typename
U
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment