From 3f787a4d5a8def13337f010227d0e21f854ea80d Mon Sep 17 00:00:00 2001 From: Paul Tsochantaris Date: Sun, 14 Jan 2024 18:36:55 +0000 Subject: [PATCH] Collecting command buffer completions on single thread --- ggml-metal.m | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ggml-metal.m b/ggml-metal.m index 5ef12239b..59e681a15 100644 --- a/ggml-metal.m +++ b/ggml-metal.m @@ -720,10 +720,11 @@ static bool ggml_metal_graph_compute( const int n_nodes = gf->n_nodes; const int n_cb = ctx->n_cb; const int n_nodes_per_cb = (n_nodes + n_cb - 1) / n_cb; - __block BOOL all_buffers_succeeded = true; + id command_buffers[n_cb]; for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) { id command_buffer = [ctx->queue commandBufferWithUnretainedReferences]; + command_buffers[cb_idx] = command_buffer; // enqueue the command buffers in order to specify their execution order [command_buffer enqueue]; @@ -2229,21 +2230,25 @@ static bool ggml_metal_graph_compute( [encoder endEncoding]; [command_buffer commit]; - - [command_buffer waitUntilCompleted]; - - MTLCommandBufferStatus status = [command_buffer status]; - if (status != MTLCommandBufferStatusCompleted) { - all_buffers_succeeded = false; - GGML_METAL_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, cb_idx, status); - } }); } - // wait for all threads to finish + // Wait for all command buffers to be committed dispatch_barrier_sync(ctx->d_queue, ^{}); - return all_buffers_succeeded; + // Wait for completion and check status of each command buffer + for (int cb_idx = 0; cb_idx < n_cb; ++cb_idx) { + id command_buffer = command_buffers[cb_idx]; + [command_buffer waitUntilCompleted]; + + MTLCommandBufferStatus status = [command_buffer status]; + if (status != MTLCommandBufferStatusCompleted) { + GGML_METAL_LOG_INFO("%s: command buffer %d failed with status %lu\n", __func__, cb_idx, status); + return false; + } + } + + return true; } }