diff options
Diffstat (limited to 'tensorflow/core/common_runtime/bfc_allocator.cc')
-rw-r--r-- | tensorflow/core/common_runtime/bfc_allocator.cc | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/tensorflow/core/common_runtime/bfc_allocator.cc b/tensorflow/core/common_runtime/bfc_allocator.cc index 9cda17867b..3bf0532491 100644 --- a/tensorflow/core/common_runtime/bfc_allocator.cc +++ b/tensorflow/core/common_runtime/bfc_allocator.cc @@ -155,10 +155,6 @@ bool BFCAllocator::Extend(size_t alignment, size_t rounded_bytes) { region_manager_.set_handle(c->ptr, h); - // TODO(vrv): Try to merge this new region with an existing region, - // if the address space is contiguous, to avoid fragmentation - // across regions. - // Insert the chunk into the right bin. InsertFreeChunkIntoBin(h); @@ -465,49 +461,33 @@ void BFCAllocator::FreeAndMaybeCoalesce(BFCAllocator::ChunkHandle h) { Chunk* c = ChunkFromHandle(h); CHECK(c->in_use() && (c->bin_num == kInvalidBinNum)); - // Mark the chunk as no longer in use + // Mark the chunk as no longer in use. c->allocation_id = -1; // Updates the stats. stats_.bytes_in_use -= c->size; - // This chunk is no longer in-use, consider coalescing the chunk - // with adjacent chunks. - ChunkHandle chunk_to_reassign = h; - - // If the next chunk is free, coalesce the two - if (c->next != kInvalidChunkHandle) { - Chunk* cnext = ChunkFromHandle(c->next); - if (!cnext->in_use()) { - // VLOG(8) << "Chunk at " << cnext->ptr << " merging with c " << - // c->ptr; - - chunk_to_reassign = h; + ChunkHandle coalesced_chunk = h; - // Deletes c->next - RemoveFreeChunkFromBin(c->next); - Merge(h, ChunkFromHandle(h)->next); - } + // If the next chunk is free, merge it into c and delete it. + if (c->next != kInvalidChunkHandle && !ChunkFromHandle(c->next)->in_use()) { + // VLOG(8) << "Merging c->next " << ChunkFromHandle(c->next)->ptr + // << " with c " << c->ptr; + RemoveFreeChunkFromBin(c->next); + Merge(h, c->next); } - // If the previous chunk is free, coalesce the two - c = ChunkFromHandle(h); - if (c->prev != kInvalidChunkHandle) { - Chunk* cprev = ChunkFromHandle(c->prev); - if (!cprev->in_use()) { - // VLOG(8) << "Chunk at " << c->ptr << " merging into c->prev " - // << cprev->ptr; - - chunk_to_reassign = c->prev; + // If the previous chunk is free, merge c into it and delete c. + if (c->prev != kInvalidChunkHandle && !ChunkFromHandle(c->prev)->in_use()) { + // VLOG(8) << "Merging c " << c->ptr << " into c->prev " + // << ChunkFromHandle(c->prev)->ptr; - // Deletes c - RemoveFreeChunkFromBin(c->prev); - Merge(ChunkFromHandle(h)->prev, h); - c = ChunkFromHandle(h); - } + coalesced_chunk = c->prev; + RemoveFreeChunkFromBin(c->prev); + Merge(c->prev, h); } - InsertFreeChunkIntoBin(chunk_to_reassign); + InsertFreeChunkIntoBin(coalesced_chunk); } void BFCAllocator::AddAllocVisitor(Visitor visitor) { |