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

callback to abort ggml_graph_compute() #328

Merged
merged 13 commits into from
Jul 11, 2023
Prev Previous commit
Next Next commit
use pthread_cancel
  • Loading branch information
CCLDArjun committed Jul 2, 2023
commit 8bff0bdb50de6612eca6006665b38e77de931984
14 changes: 7 additions & 7 deletions src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -16655,7 +16655,7 @@ typedef pthread_t ggml_thread_t;

#define ggml_thread_create pthread_create
#define ggml_thread_join pthread_join
#define ggml_thread_kill pthread_kill
#define ggml_thread_cancel pthread_cancel

#else

Expand Down Expand Up @@ -16781,7 +16781,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {

while (true) {
if (state->ith == 0 && state->shared->abort_callback()) {
return 0;
return GGML_EXIT_ABORTED;
}
if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {
// all other threads are finished and spinning
Expand Down Expand Up @@ -16865,7 +16865,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
}
}

return 0;
return GGML_EXIT_SUCCESS;
}

bool always_false() { return false; }
CCLDArjun marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -17253,18 +17253,18 @@ void ggml_graph_compute_with_abort(struct ggml_context * ctx, struct ggml_cgraph
const int64_t perf_start_time_us = ggml_perf_time_us();

// this is a work thread too
ggml_graph_compute_thread(&workers[0]);
int compute_status = ggml_graph_compute_thread(&workers[0]);

// don't leave affinity set on the main thread
clear_numa_thread_affinity();

// join or kill thread pool
if (n_threads > 1) {
for (int j = 1; j < n_threads; j++) {
if (abort_callback()) {
const int rc = ggml_thread_kill(workers[j].thrd, SIGKILL);
if (compute_status == GGML_EXIT_ABORTED) {
const int rc = ggml_thread_cancel(workers[j].thrd);
GGML_ASSERT(rc == 0);
} else {
} else if (compute_status == GGML_EXIT_SUCCESS) {
const int rc = ggml_thread_join(workers[j].thrd, NULL);
GGML_ASSERT(rc == 0);
}
Expand Down