Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never

static thread_ret_t ggml_graph_compute_thread(void * data) {
    struct ggml_compute_state * state = (struct ggml_compute_state *) data;

    const int n_threads = state->shared->n_threads;

    while (true) {
        if (atomic_fetch_add(&state->shared->n_ready, 1) == n_threads - 1) {
            atomic_store(&state->shared->has_work, false);
            pthread_cond_broadcast(&state->shared->cv);
        } else {
            pthread_mutex_lock(&state->shared->mutex);
            while (atomic_load(&state->shared->has_work)) {
                if (atomic_load(&state->shared->stop)) {
                    pthread_mutex_unlock(&state->shared->mutex);
                    return 0;
                }
                pthread_cond_wait(&state->shared->cv, &state->shared->mutex);
//                ggml_lock_lock  (&state->shared->spin);
//                ggml_lock_unlock(&state->shared->spin);
            }
            pthread_mutex_unlock(&state->shared->mutex);
        }

        atomic_fetch_sub(&state->shared->n_ready, 1);

        // wait for work
        pthread_mutex_lock(&state->shared->mutex);
        while (!atomic_load(&state->shared->has_work)) {
            if (atomic_load(&state->shared->stop)) {
                pthread_mutex_unlock(&state->shared->mutex);
                return 0;
            }
            pthread_cond_wait(&state->shared->cv, &state->shared->mutex);
//            ggml_lock_lock  (&state->shared->spin);
//            ggml_lock_unlock(&state->shared->spin);
        }
        pthread_mutex_unlock(&state->shared->mutex);

        // check if we should stop
        if (atomic_load(&state->shared->stop)) {
            break;
        }

        if (state->node) {
            if (state->params.ith < state->params.nth) {
                ggml_compute_forward(&state->params, state->node);
            }

            state->node = NULL;
        } else {
            break;
        }
    }

    return 0;
}