From 921296c0d588b933320646bf9513a5be13c749ee Mon Sep 17 00:00:00 2001 From: Howard Su Date: Sat, 8 Apr 2023 00:47:19 +0800 Subject: [PATCH] avoid malloc/free in critial path --- ggml.c | 11 ++++++----- thpool.c | 23 ++--------------------- thpool.h | 8 +++++++- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/ggml.c b/ggml.c index 788023d28..93f034d8f 100644 --- a/ggml.c +++ b/ggml.c @@ -2731,6 +2731,7 @@ enum ggml_task_type { }; struct ggml_compute_params { + job newjob; enum ggml_task_type type; int ith, nth; @@ -9529,11 +9530,11 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) // INIT struct ggml_compute_params params = { - /*.type =*/ GGML_TASK_INIT, - /*.ith =*/ 0, - /*.nth =*/ node->n_tasks, - /*.wsize =*/ cgraph->work ? ggml_nbytes(cgraph->work) : 0, - /*.wdata =*/ cgraph->work ? cgraph->work->data : NULL, + .type = GGML_TASK_INIT, + .ith = 0, + .nth = node->n_tasks, + .wsize = cgraph->work ? ggml_nbytes(cgraph->work) : 0, + .wdata = cgraph->work ? cgraph->work->data : NULL, }; ggml_compute_forward(¶ms, node); diff --git a/thpool.c b/thpool.c index 76cf5fc3f..6c9a40e32 100644 --- a/thpool.c +++ b/thpool.c @@ -156,14 +156,6 @@ typedef struct bsem { } bsem; -/* Job */ -typedef struct job{ - struct job* prev; /* pointer to previous job */ - void (*function)(void* arg); /* function pointer */ - void* arg; /* function's argument */ -} job; - - /* Job queue */ typedef struct jobqueue{ pthread_mutex_t rwmutex; /* used for queue r/w access */ @@ -279,18 +271,8 @@ struct thpool_* thpool_init(int num_threads){ /* Add work to the thread pool */ -int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){ - job* newjob; - - newjob=(struct job*)malloc(sizeof(struct job)); - if (newjob==NULL){ - err("thpool_add_work(): Could not allocate memory for new job\n"); - return -1; - } - - /* add function and argument */ +int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), job * newjob){ newjob->function=function_p; - newjob->arg=arg_p; /* add job to queue */ jobqueue_push(&thpool_p->jobqueue, newjob); @@ -460,9 +442,8 @@ static void* thread_do(struct thread* thread_p){ job* job_p = jobqueue_pull(&thpool_p->jobqueue); if (job_p) { func_buff = job_p->function; - arg_buff = job_p->arg; + arg_buff = job_p; func_buff(arg_buff); - free(job_p); } pthread_mutex_lock(&thpool_p->thcount_lock); diff --git a/thpool.h b/thpool.h index af3e68d16..fafbda5d1 100644 --- a/thpool.h +++ b/thpool.h @@ -16,6 +16,12 @@ extern "C" { typedef struct thpool_* threadpool; +/* Job */ +typedef struct job{ + struct job* prev; /* pointer to previous job */ + void (*function)(void* arg); /* function pointer */ +} job; + /** * @brief Initialize threadpool @@ -64,7 +70,7 @@ threadpool thpool_init(int num_threads); * @param arg_p pointer to an argument * @return 0 on success, -1 otherwise. */ -int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p); +int thpool_add_work(threadpool, void (*function_p)(void*), job *newjob); /**