Skip to content
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ class BSCachingAllocator : public CacheAllocator {

static size_t getAllocateSize(size_t nbytes) {
nbytes = getMemoryAlignmentStrategy()->roundBytes(nbytes);
static bool less_fragmentation =
std::getenv("DIPU_BS_MORE_ADAPTABLE") == nullptr;
return less_fragmentation ? getAllocateSizeLessFragmentation(nbytes)
: getAllocateSizeMoreAdaptable(nbytes);
constexpr size_t kMinBlockSize = 1 << 20; // 1M
if (nbytes <= kMinBlockSize) {
return kMinBlockSize;
}
int clz = __builtin_clzll(nbytes - 1);
return (1 << (sizeof(int64_t) - clz));
}

c10::DataPtr allocate(size_t size) const override {
DIPU_DEBUG_ALLOCATOR(8, "BSCachingAllocator::allocate "
<< size << ",allocator:" << this
<< ", memory-usage" << memory_allocated() << "/"
<< memory_reserved());
std::lock_guard<mutex_t> lk(mutex_);
flush_mem_pool();
std::lock_guard<mutex_t> lk(mutex_);
size_t nbytes = getAllocateSize(size);
void* ptr = nullptr;
auto& idel_blocks = impl->idel_blocks_[nbytes];
Expand Down Expand Up @@ -138,6 +140,7 @@ class BSCachingAllocator : public CacheAllocator {
}

void empty_resource_pool() const {
std::lock_guard<mutex_t> lk(mutex_);
DIPU_DEBUG_ALLOCATOR(
8, "BSCachingAllocator::empty_resource_pool ,allocator:" << this);
while (!async_mem_pool()->empty()) {
Expand Down Expand Up @@ -180,6 +183,7 @@ class BSCachingAllocator : public CacheAllocator {
void release_all_memory() const override { release_all_memory_impl(); }

void flush_mem_pool() const {
std::lock_guard<mutex_t> lk(mutex_);
DIPU_DEBUG_ALLOCATOR(
8, "BSCachingAllocator::flush_mem_pool allocator:" << this);
while (async_mem_pool()->ready()) {
Expand Down