Prevent unbounded growth of command allocator memory (#12114)

Prevent unbounded growth of command allocator memory
This commit is contained in:
Jeff Bloomfield 2022-07-07 19:55:06 -07:00 committed by GitHub
parent 3ce25db7eb
commit 2dd69cc3d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 18 deletions

View file

@ -28,26 +28,23 @@ namespace Dml
}
}
ID3D12CommandAllocator* GetCurrentAllocator()
ID3D12CommandAllocator* GetNextAllocator(GpuEvent nextCompletionEvent)
{
CommandAllocatorInfo& allocatorInfo = m_commandAllocators[m_currentCommandAllocator];
size_t earliestOtherAllocator = (m_currentCommandAllocator + 1) % AllocatorCount;
// Take the opportunity to reset the command allocator if possible.
if (allocatorInfo.completionEvent.IsSignaled())
assert(!m_commandAllocators[m_currentCommandAllocator].completionEvent.IsSignaled() ||
m_commandAllocators[earliestOtherAllocator].completionEvent.IsSignaled());
if (m_commandAllocators[earliestOtherAllocator].completionEvent.IsSignaled())
{
ORT_THROW_IF_FAILED(allocatorInfo.Get()->Reset());
ORT_THROW_IF_FAILED(m_commandAllocators[earliestOtherAllocator].Get()->Reset());
m_currentCommandAllocator = earliestOtherAllocator;
}
return m_commandAllocators[m_currentCommandAllocator].Get();
}
void AdvanceAllocator(GpuEvent completionEvent)
{
// Set the completion event for the current allocator so it can be reset eventually.
m_commandAllocators[m_currentCommandAllocator].completionEvent = completionEvent;
m_commandAllocators[m_currentCommandAllocator].completionEvent = nextCompletionEvent;
// Advance to the next allocator.
m_currentCommandAllocator = (m_currentCommandAllocator + 1) % AllocatorCount;
return m_commandAllocators[m_currentCommandAllocator].Get();
}
private:

View file

@ -314,14 +314,14 @@ void DmlCommandRecorder::Open()
{
assert(m_currentDescriptorHeap == nullptr);
ID3D12CommandAllocator* allocator = m_commandAllocatorRing.GetCurrentAllocator();
ID3D12CommandAllocator* allocator = m_commandAllocatorRing.GetNextAllocator(m_queue->GetNextCompletionEvent());
if (m_cachedCommandLists.empty())
{
ORT_THROW_IF_FAILED(m_d3dDevice->CreateCommandList(
0,
m_queue->GetType(),
m_commandAllocatorRing.GetCurrentAllocator(),
allocator,
nullptr,
IID_GRAPHICS_PPV_ARGS(m_currentCommandList.ReleaseAndGetAddressOf())));
}
@ -331,9 +331,6 @@ void DmlCommandRecorder::Open()
m_cachedCommandLists.pop_front();
ORT_THROW_IF_FAILED(m_currentCommandList->Reset(allocator, nullptr));
}
// The current command allocator will become eligible for reset once this command list completes execution
m_commandAllocatorRing.AdvanceAllocator(m_queue->GetNextCompletionEvent());
}
void DmlCommandRecorder::CloseAndExecute()