avoid malloc/free in critial path

This commit is contained in:
Howard Su 2023-04-08 00:47:19 +08:00
parent 455f6f79bc
commit 921296c0d5
3 changed files with 15 additions and 27 deletions

11
ggml.c
View File

@ -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(&params, node);

View File

@ -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);

View File

@ -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);
/**