Skip to content

[ET-VK] 5/n Split dispatches between multiple command buffers. Track previously submitted command buffers in context and add function to execute all previous command buffers. #12527

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

Merged
2 changes: 1 addition & 1 deletion backends/vulkan/runtime/api/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void Context::submit_cmd_to_gpu(VkFence fence_handle, const bool final_use) {
}

void Context::flush() {
VK_CHECK(vkQueueWaitIdle(queue()));
VK_CHECK(vkQueueWaitIdle(queue().handle));

command_pool_.flush();
descriptor_pool_.flush();
Expand Down
8 changes: 6 additions & 2 deletions backends/vulkan/runtime/api/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class Context final {
return device_;
}

inline VkQueue queue() {
return queue_.handle;
inline vkapi::Adapter::Queue& queue() {
return queue_;
}

// Device Caches
Expand Down Expand Up @@ -230,6 +230,10 @@ class Context final {
VkFence fence_handle = VK_NULL_HANDLE,
const bool final_use = false);

vkapi::CommandBuffer& extract_cmd() {
return cmd_;
}

void flush();

#ifdef VULKAN_DEBUG
Expand Down
56 changes: 52 additions & 4 deletions backends/vulkan/runtime/graph/ComputeGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ ComputeGraph::~ComputeGraph() {

prepack_nodes_.clear();
execute_nodes_.clear();
clear_deferred_cmds();

context_->flush();
}
Expand Down Expand Up @@ -775,6 +776,53 @@ void ComputeGraph::submit_current_cmd_and_wait(const bool final_use) {
context_->fences().return_fence(fence);
}

void ComputeGraph::submit_cmd(
vkapi::CommandBuffer& cmd_buf,
VkSemaphore wait_semaphore,
VkSemaphore signal_semaphore,
VkFence fence) {
if (cmd_buf) {
cmd_buf.end();
context_->adapter_ptr()->submit_cmd(
context_->queue(),
cmd_buf.get_submit_handle(false),
fence,
wait_semaphore,
signal_semaphore);
}
}

void ComputeGraph::submit_deferred_cmds_and_wait() {
VkSemaphore prev_semaphore = VK_NULL_HANDLE;
vkapi::VulkanFence fence = context_->fences().get_fence();

for (uint32_t i = 0; i < deferred_cmd_list_.size(); i++) {
auto& cmd = deferred_cmd_list_[i];
VkSemaphore wait_semaphore = prev_semaphore;
VkSemaphore signal_semaphore = cmd.get_signal_semaphore();
prev_semaphore = signal_semaphore;

submit_cmd(
cmd,
wait_semaphore,
signal_semaphore,
i == (deferred_cmd_list_.size() - 1) ? fence.get_submit_handle()
: VK_NULL_HANDLE);
}
fence.wait();
context_->fences().return_fence(fence);
}

void ComputeGraph::clear_deferred_cmds() {
for (auto& cmd : deferred_cmd_list_) {
if (cmd) {
cmd.end();
cmd.invalidate();
}
}
deferred_cmd_list_.clear();
}

void ComputeGraph::prepack() {
int i = 0;
bool submitted = false;
Expand Down Expand Up @@ -813,6 +861,7 @@ void ComputeGraph::prepack() {
}

void ComputeGraph::encode_execute() {
clear_deferred_cmds();
context_->flush();
context_->set_cmd(/*reusable = */ true);

Expand All @@ -821,13 +870,12 @@ void ComputeGraph::encode_execute() {
for (std::unique_ptr<ExecuteNode>& node : execute_nodes_) {
node->encode(this);
}

deferred_cmd_list_.emplace_back(std::move(context_->extract_cmd()));
}

void ComputeGraph::execute() {
vkapi::VulkanFence fence = context_->fences().get_fence();
context_->submit_cmd_to_gpu(fence.get_submit_handle());
fence.wait();
context_->fences().return_fence(fence);
submit_deferred_cmds_and_wait();
execute_count_++;
}

Expand Down
22 changes: 22 additions & 0 deletions backends/vulkan/runtime/graph/ComputeGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class ComputeGraph final {
// Utility constexpr to express byte quantities
constexpr static size_t MB = 1024 * 1024;

// List of command buffers deferred for submission
std::vector<vkapi::CommandBuffer> deferred_cmd_list_;

protected:
size_t values_in_use_ = 0;
size_t execute_count_ = 0;
Expand Down Expand Up @@ -851,6 +854,25 @@ class ComputeGraph final {
*/
void submit_current_cmd_and_wait(const bool final_use = false);

/*
* Submit one command buffer to the GPU.
*/
void submit_cmd(
vkapi::CommandBuffer& cmd_buf,
VkSemaphore wait_semaphore,
VkSemaphore signal_semaphore,
VkFence fence);

/*
* Submits all the commands gathered in deferred_cmd_bufs_ to the GPU.
*/
void submit_deferred_cmds_and_wait();

/*
* Ends and invalidates all deferred commands.
*/
void clear_deferred_cmds();

public:
//
// Graph Prepacking
Expand Down
Loading