Skip to content

Commit 921296c

Browse files
committed
avoid malloc/free in critial path
1 parent 455f6f7 commit 921296c

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

ggml.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,7 @@ enum ggml_task_type {
27312731
};
27322732

27332733
struct ggml_compute_params {
2734+
job newjob;
27342735
enum ggml_task_type type;
27352736

27362737
int ith, nth;
@@ -9529,11 +9530,11 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph)
95299530

95309531
// INIT
95319532
struct ggml_compute_params params = {
9532-
/*.type =*/ GGML_TASK_INIT,
9533-
/*.ith =*/ 0,
9534-
/*.nth =*/ node->n_tasks,
9535-
/*.wsize =*/ cgraph->work ? ggml_nbytes(cgraph->work) : 0,
9536-
/*.wdata =*/ cgraph->work ? cgraph->work->data : NULL,
9533+
.type = GGML_TASK_INIT,
9534+
.ith = 0,
9535+
.nth = node->n_tasks,
9536+
.wsize = cgraph->work ? ggml_nbytes(cgraph->work) : 0,
9537+
.wdata = cgraph->work ? cgraph->work->data : NULL,
95379538
};
95389539

95399540
ggml_compute_forward(&params, node);

thpool.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ typedef struct bsem {
156156
} bsem;
157157

158158

159-
/* Job */
160-
typedef struct job{
161-
struct job* prev; /* pointer to previous job */
162-
void (*function)(void* arg); /* function pointer */
163-
void* arg; /* function's argument */
164-
} job;
165-
166-
167159
/* Job queue */
168160
typedef struct jobqueue{
169161
pthread_mutex_t rwmutex; /* used for queue r/w access */
@@ -279,18 +271,8 @@ struct thpool_* thpool_init(int num_threads){
279271

280272

281273
/* Add work to the thread pool */
282-
int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){
283-
job* newjob;
284-
285-
newjob=(struct job*)malloc(sizeof(struct job));
286-
if (newjob==NULL){
287-
err("thpool_add_work(): Could not allocate memory for new job\n");
288-
return -1;
289-
}
290-
291-
/* add function and argument */
274+
int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), job * newjob){
292275
newjob->function=function_p;
293-
newjob->arg=arg_p;
294276

295277
/* add job to queue */
296278
jobqueue_push(&thpool_p->jobqueue, newjob);
@@ -460,9 +442,8 @@ static void* thread_do(struct thread* thread_p){
460442
job* job_p = jobqueue_pull(&thpool_p->jobqueue);
461443
if (job_p) {
462444
func_buff = job_p->function;
463-
arg_buff = job_p->arg;
445+
arg_buff = job_p;
464446
func_buff(arg_buff);
465-
free(job_p);
466447
}
467448

468449
pthread_mutex_lock(&thpool_p->thcount_lock);

thpool.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ extern "C" {
1616

1717
typedef struct thpool_* threadpool;
1818

19+
/* Job */
20+
typedef struct job{
21+
struct job* prev; /* pointer to previous job */
22+
void (*function)(void* arg); /* function pointer */
23+
} job;
24+
1925

2026
/**
2127
* @brief Initialize threadpool
@@ -64,7 +70,7 @@ threadpool thpool_init(int num_threads);
6470
* @param arg_p pointer to an argument
6571
* @return 0 on success, -1 otherwise.
6672
*/
67-
int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
73+
int thpool_add_work(threadpool, void (*function_p)(void*), job *newjob);
6874

6975

7076
/**

0 commit comments

Comments
 (0)