Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve concurrency of stream_ordered_memory_resource by stealing less #851

Merged
merged 4 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions include/rmm/mr/device/detail/stream_ordered_memory_resource.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -116,11 +116,8 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
*/
// block_type expand_pool(size_t size, free_list& blocks, cuda_stream_view stream)

/// Struct representing a block that has been split for allocation
struct split_block {
void* allocated_pointer; ///< The pointer allocated from a block
block_type remainder; ///< The remainder of the block from which the pointer was allocated
};
/// Pair representing a block that has been split for allocation
using split_block = std::pair<block_type, block_type>;

/**
* @brief Split block `b` if necessary to return a pointer to memory of `size` bytes.
Expand Down Expand Up @@ -213,16 +210,13 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
rmm::bad_alloc,
"Maximum allocation size exceeded");
auto const b = this->underlying().get_block(bytes, stream_event);
auto split = this->underlying().allocate_from_block(b, bytes);
if (split.remainder.is_valid()) stream_free_blocks_[stream_event].insert(split.remainder);
RMM_LOG_TRACE("[A][stream {:p}][{}B][{:p}]",
fmt::ptr(stream_event.stream),
bytes,
fmt::ptr(split.allocated_pointer));

RMM_LOG_TRACE(
"[A][stream {:p}][{}B][{:p}]", fmt::ptr(stream_event.stream), bytes, fmt::ptr(b.pointer()));

log_summary_trace();

return split.allocated_pointer;
return b.pointer();
}

/**
Expand All @@ -234,9 +228,12 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
*/
virtual void do_deallocate(void* p, std::size_t bytes, cuda_stream_view stream) override
{
RMM_LOG_TRACE("[D][stream {:p}][{}B][{:p}]", fmt::ptr(stream.value()), bytes, p);

if (bytes <= 0 || p == nullptr) return;

lock_guard lock(mtx_);
auto stream_event = get_event(stream);
RMM_LOG_TRACE("[D][stream {:p}][{}B][{:p}]", fmt::ptr(stream_event.stream), bytes, p);

bytes = rmm::detail::align_up(bytes, rmm::detail::CUDA_ALLOCATION_ALIGNMENT);
auto const b = this->underlying().free_block(p, bytes);
Expand Down Expand Up @@ -300,8 +297,15 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
}();
}

block_type allocate_and_insert_remainder(block_type b, std::size_t size, free_list& blocks)
{
auto [allocated, remainder] = this->underlying().allocate_from_block(b, size);
if (remainder.is_valid()) blocks.insert(remainder);
return allocated;
}

/**
* @brief Get an avaible memory block of at least `size` bytes
* @brief Get an available memory block of at least `size` bytes
*
* @param size The number of bytes to allocate
* @param stream_event The stream and associated event on which the allocation will be used.
Expand All @@ -313,7 +317,7 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
auto iter = stream_free_blocks_.find(stream_event);
if (iter != stream_free_blocks_.end()) {
block_type b = iter->second.get_block(size);
if (b.is_valid()) { return b; }
if (b.is_valid()) { return allocate_and_insert_remainder(b, size, iter->second); }
}

free_list& blocks =
Expand All @@ -334,7 +338,10 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
log_summary_trace();

// no large enough blocks available after merging, so grow the pool
return this->underlying().expand_pool(size, blocks, cuda_stream_view{stream_event.stream});
block_type const b =
this->underlying().expand_pool(size, blocks, cuda_stream_view{stream_event.stream});

return allocate_and_insert_remainder(b, size, blocks);
}

/**
Expand Down Expand Up @@ -375,10 +382,19 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_

stream_free_blocks_.erase(it);

return blocks.get_block(size); // get the best fit block in merged lists
block_type const b = blocks.get_block(size); // get the best fit block in merged lists
if (b.is_valid()) { return allocate_and_insert_remainder(b, size, blocks); }
} else {
return other_blocks.get_block(size); // get the best fit block in other list
block_type const b =
other_blocks.get_block(size); // get the best fit block in other list
if (b.is_valid()) {
// Since we found a block associated with a different stream, we have to insert a wait
// on the stream's associated event into the allocating stream.
RMM_CUDA_TRY(cudaStreamWaitEvent(stream_event.stream, other_event, 0));
return allocate_and_insert_remainder(b, size, other_blocks);
}
}
return block_type{};
}();

if (b.is_valid()) {
Expand All @@ -387,12 +403,6 @@ class stream_ordered_memory_resource : public crtp<PoolResource>, public device_
fmt::ptr(stream_event.stream),
size,
fmt::ptr(it->first.stream));

if (not merge_first) {
merge_lists(stream_event, blocks, other_event, std::move(other_blocks));
stream_free_blocks_.erase(it);
}

return b;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/rmm/mr/device/fixed_size_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class fixed_size_memory_resource
*/
split_block allocate_from_block(block_type const& b, size_t size)
{
return split_block{b.pointer(), block_type{nullptr}};
return {b, block_type{nullptr}};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/rmm/mr/device/pool_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class pool_memory_resource final

auto rest =
(b.size() > size) ? block_type{b.pointer() + size, b.size() - size, false} : block_type{};
return {reinterpret_cast<void*>(alloc.pointer()), rest};
return {alloc, rest};
}

/**
Expand Down