Blender V5.0
BLI_task.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/* Use a define instead of `#pragma once` because of `bmesh_iterators_inline.hh` */
6#ifndef __BLI_TASK_H__
7#define __BLI_TASK_H__
8
9#include <string.h> /* for memset() */
10
14
15#include "BLI_threads.h"
16#include "BLI_utildefines.h"
17
18struct BLI_mempool;
19
20/* -------------------------------------------------------------------- */
29
33
35
36/* -------------------------------------------------------------------- */
50
55
56typedef struct TaskPool TaskPool;
57typedef void (*TaskRunFunction)(TaskPool *__restrict pool, void *taskdata);
58typedef void (*TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata);
59
67
73
81
88
93
95
98 void *taskdata,
99 bool free_taskdata,
100 TaskFreeFunction freedata);
101
110
117
122
124
125/* -------------------------------------------------------------------- */
128
139
140typedef void (*TaskParallelRangeFunc)(void *__restrict userdata,
141 int iter,
142 const TaskParallelTLS *__restrict tls);
143
144typedef void (*TaskParallelInitFunc)(const void *__restrict userdata, void *__restrict chunk);
145
146typedef void (*TaskParallelReduceFunc)(const void *__restrict userdata,
147 void *__restrict chunk_join,
148 void *__restrict chunk);
149
150typedef void (*TaskParallelFreeFunc)(const void *__restrict userdata, void *__restrict chunk);
151
152typedef struct TaskParallelSettings {
153 /* Whether caller allows to do threading of the particular range.
154 * Usually set by some equation, which forces threading off when threading
155 * overhead becomes higher than speed benefit.
156 * BLI_task_parallel_range() by itself will always use threading when range
157 * is higher than a chunk size. As in, threading will always be performed.
158 */
160 /* Each instance of looping chunks will get a copy of this data
161 * (similar to OpenMP's firstprivate).
162 */
163 void *userdata_chunk; /* Pointer to actual data. */
164 size_t userdata_chunk_size; /* Size of that data. */
165 /* Function called from calling thread once whole range have been
166 * processed.
167 */
168 /* Function called to initialize user data chunk,
169 * typically to allocate data, freed by `func_free`.
170 */
172 /* Function called to join user data chunk into another, to reduce
173 * the result to the original userdata_chunk memory.
174 * The reduce functions should have no side effects, so that they
175 * can be run on any thread. */
177 /* Function called to free data created by TaskParallelRangeFunc. */
179 /* Minimum allowed number of range iterators to be handled by a single
180 * thread. This allows to achieve following:
181 * - Reduce amount of threading overhead.
182 * - Partially occupy thread pool with ranges which are computationally
183 * expensive, but which are smaller than amount of available threads.
184 * For example, it's possible to multi-thread [0 .. 64] range into 4
185 * thread which will be doing 16 iterators each.
186 * This is a preferred way to tell scheduler when to start threading than
187 * having a global use_threading switch based on just range size.
188 */
191
193
194void BLI_task_parallel_range(int start,
195 int stop,
196 void *userdata,
198 const TaskParallelSettings *settings);
199
201
202typedef void (*TaskParallelMempoolFunc)(void *userdata,
203 MempoolIterData *iter,
204 const TaskParallelTLS *__restrict tls);
215void BLI_task_parallel_mempool(struct BLI_mempool *mempool,
216 void *userdata,
218 const TaskParallelSettings *settings);
219
222{
223 memset(settings, 0, sizeof(*settings));
224 settings->use_threading = true;
225 /* Use default heuristic to define actual chunk size. */
226 settings->min_iter_per_thread = 0;
227}
228
230{
231 memset(settings, 0, sizeof(*settings));
232 settings->use_threading = true;
233}
234
240
242
243/* -------------------------------------------------------------------- */
311
312struct TaskGraph;
313struct TaskNode;
314
315typedef void (*TaskGraphNodeRunFunction)(void *__restrict task_data);
317
318struct TaskGraph *BLI_task_graph_create(void);
319void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph);
320void BLI_task_graph_free(struct TaskGraph *task_graph);
321struct TaskNode *BLI_task_graph_node_create(struct TaskGraph *task_graph,
323 void *user_data,
325bool BLI_task_graph_node_push_work(struct TaskNode *task_node);
326void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node);
327
329
330/* -------------------------------------------------------------------- */
359
360void BLI_task_isolate(void (*func)(void *userdata), void *userdata);
361
363
364#endif
#define BLI_INLINE
void BLI_task_scheduler_init(void)
int BLI_task_scheduler_num_threads(void)
eTaskPriority
Definition BLI_task.h:51
@ TASK_PRIORITY_LOW
Definition BLI_task.h:52
@ TASK_PRIORITY_HIGH
Definition BLI_task.h:53
void(* TaskParallelReduceFunc)(const void *__restrict userdata, void *__restrict chunk_join, void *__restrict chunk)
Definition BLI_task.h:146
struct TaskNode * BLI_task_graph_node_create(struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
TaskPool * BLI_task_pool_create_suspended(void *userdata, eTaskPriority priority)
Definition task_pool.cc:503
void(* TaskGraphNodeFreeFunction)(void *task_data)
Definition BLI_task.h:316
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:550
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)
struct MempoolIterData MempoolIterData
Definition BLI_task.h:200
void BLI_task_isolate(void(*func)(void *userdata), void *userdata)
bool BLI_task_pool_current_canceled(TaskPool *pool)
Definition task_pool.cc:545
TaskPool * BLI_task_pool_create_no_threads(void *userdata)
Definition task_pool.cc:511
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:535
void BLI_task_pool_cancel(TaskPool *pool)
Definition task_pool.cc:540
void BLI_task_scheduler_exit(void)
bool BLI_task_graph_node_push_work(struct TaskNode *task_node)
void(* TaskRunFunction)(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task.h:57
void(* TaskParallelInitFunc)(const void *__restrict userdata, void *__restrict chunk)
Definition BLI_task.h:144
TaskPool * BLI_task_pool_create_background(void *userdata, eTaskPriority priority)
Definition task_pool.cc:489
TaskPool * BLI_task_pool_create_background_serial(void *userdata, eTaskPriority priority)
Definition task_pool.cc:516
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition task_range.cc:99
void(* TaskParallelRangeFunc)(void *__restrict userdata, int iter, const TaskParallelTLS *__restrict tls)
Definition BLI_task.h:140
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:484
void BLI_task_graph_free(struct TaskGraph *task_graph)
struct TaskGraph * BLI_task_graph_create(void)
Definition task_graph.cc:96
int BLI_task_parallel_thread_id(const TaskParallelTLS *tls)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition BLI_task.h:221
void BLI_task_parallel_mempool(struct BLI_mempool *mempool, void *userdata, TaskParallelMempoolFunc func, const TaskParallelSettings *settings)
BLI_INLINE void BLI_parallel_mempool_settings_defaults(TaskParallelSettings *settings)
Definition BLI_task.h:229
void(* TaskGraphNodeRunFunction)(void *__restrict task_data)
Definition BLI_task.h:315
void(* TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task.h:58
void(* TaskParallelMempoolFunc)(void *userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict tls)
Definition BLI_task.h:202
void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:521
void(* TaskParallelFreeFunc)(const void *__restrict userdata, void *__restrict chunk)
Definition BLI_task.h:150
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:526
static PyObject * free_func(PyObject *, PyObject *value)
Definition python.cpp:226
void * task_data
Definition task_graph.cc:43
TaskParallelReduceFunc func_reduce
Definition BLI_task.h:176
TaskParallelFreeFunc func_free
Definition BLI_task.h:178
TaskParallelInitFunc func_init
Definition BLI_task.h:171
size_t userdata_chunk_size
Definition BLI_task.h:164
void * userdata_chunk
Definition BLI_task.h:137
eTaskPriority priority
Definition task_pool.cc:175
void * userdata
Definition task_pool.cc:161