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

Prevent unbounded growth of command allocator memory #12114

Merged
merged 2 commits into from
Jul 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
ORT_THROW_IF_FAILED(allocatorInfo.Get()->Reset());
}

return m_commandAllocators[m_currentCommandAllocator].Get();
}
assert(!m_commandAllocators[m_currentCommandAllocator].completionEvent.IsSignaled() ||
m_commandAllocators[earliestOtherAllocator].completionEvent.IsSignaled());

void AdvanceAllocator(GpuEvent completionEvent)
{
if (m_commandAllocators[earliestOtherAllocator].completionEvent.IsSignaled())
{
ORT_THROW_IF_FAILED(m_commandAllocators[earliestOtherAllocator].Get()->Reset());
m_currentCommandAllocator = earliestOtherAllocator;
}

// 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
Expand All @@ -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()
Expand Down