Blender V4.3
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
15#include "BLI_threads.h"
16#include "BLI_utildefines.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22struct BLI_mempool;
23
24/* -------------------------------------------------------------------- */
37
40/* -------------------------------------------------------------------- */
59
60typedef struct TaskPool TaskPool;
61typedef void (*TaskRunFunction)(TaskPool *__restrict pool, void *taskdata);
62typedef void (*TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata);
63
71
77
85
92
97
99
101 TaskRunFunction run,
102 void *taskdata,
103 bool free_taskdata,
104 TaskFreeFunction freedata);
105
114
121
126
131
134/* -------------------------------------------------------------------- */
148
149typedef void (*TaskParallelRangeFunc)(void *__restrict userdata,
150 int iter,
151 const TaskParallelTLS *__restrict tls);
152
153typedef void (*TaskParallelInitFunc)(const void *__restrict userdata, void *__restrict chunk);
154
155typedef void (*TaskParallelReduceFunc)(const void *__restrict userdata,
156 void *__restrict chunk_join,
157 void *__restrict chunk);
158
159typedef void (*TaskParallelFreeFunc)(const void *__restrict userdata, void *__restrict chunk);
160
161typedef struct TaskParallelSettings {
162 /* Whether caller allows to do threading of the particular range.
163 * Usually set by some equation, which forces threading off when threading
164 * overhead becomes higher than speed benefit.
165 * BLI_task_parallel_range() by itself will always use threading when range
166 * is higher than a chunk size. As in, threading will always be performed.
167 */
169 /* Each instance of looping chunks will get a copy of this data
170 * (similar to OpenMP's firstprivate).
171 */
172 void *userdata_chunk; /* Pointer to actual data. */
173 size_t userdata_chunk_size; /* Size of that data. */
174 /* Function called from calling thread once whole range have been
175 * processed.
176 */
177 /* Function called to initialize user data chunk,
178 * typically to allocate data, freed by `func_free`.
179 */
181 /* Function called to join user data chunk into another, to reduce
182 * the result to the original userdata_chunk memory.
183 * The reduce functions should have no side effects, so that they
184 * can be run on any thread. */
186 /* Function called to free data created by TaskParallelRangeFunc. */
188 /* Minimum allowed number of range iterators to be handled by a single
189 * thread. This allows to achieve following:
190 * - Reduce amount of threading overhead.
191 * - Partially occupy thread pool with ranges which are computationally
192 * expensive, but which are smaller than amount of available threads.
193 * For example, it's possible to multi-thread [0 .. 64] range into 4
194 * thread which will be doing 16 iterators each.
195 * This is a preferred way to tell scheduler when to start threading than
196 * having a global use_threading switch based on just range size.
197 */
200
202
203void BLI_task_parallel_range(int start,
204 int stop,
205 void *userdata,
207 const TaskParallelSettings *settings);
208
210
211typedef void (*TaskParallelMempoolFunc)(void *userdata,
212 MempoolIterData *iter,
213 const TaskParallelTLS *__restrict tls);
224void BLI_task_parallel_mempool(struct BLI_mempool *mempool,
225 void *userdata,
227 const TaskParallelSettings *settings);
228
231{
232 memset(settings, 0, sizeof(*settings));
233 settings->use_threading = true;
234 /* Use default heuristic to define actual chunk size. */
235 settings->min_iter_per_thread = 0;
236}
237
239{
240 memset(settings, 0, sizeof(*settings));
241 settings->use_threading = true;
242}
243
249
252/* -------------------------------------------------------------------- */
321struct TaskGraph;
322struct TaskNode;
323
324typedef void (*TaskGraphNodeRunFunction)(void *__restrict task_data);
326
327struct TaskGraph *BLI_task_graph_create(void);
328void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph);
329void BLI_task_graph_free(struct TaskGraph *task_graph);
330struct TaskNode *BLI_task_graph_node_create(struct TaskGraph *task_graph,
332 void *user_data,
334bool BLI_task_graph_node_push_work(struct TaskNode *task_node);
335void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node);
336
339/* -------------------------------------------------------------------- */
369void BLI_task_isolate(void (*func)(void *userdata), void *userdata);
370
373#ifdef __cplusplus
374}
375#endif
376
377#endif
#define BLI_INLINE
void BLI_task_scheduler_init(void)
int BLI_task_scheduler_num_threads(void)
eTaskPriority
Definition BLI_task.h:55
@ TASK_PRIORITY_LOW
Definition BLI_task.h:56
@ TASK_PRIORITY_HIGH
Definition BLI_task.h:57
void(* TaskParallelReduceFunc)(const void *__restrict userdata, void *__restrict chunk_join, void *__restrict chunk)
Definition BLI_task.h:155
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:413
void(* TaskGraphNodeFreeFunction)(void *task_data)
Definition BLI_task.h:325
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:516
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)
struct MempoolIterData MempoolIterData
Definition BLI_task.h:209
void BLI_task_isolate(void(*func)(void *userdata), void *userdata)
bool BLI_task_pool_current_canceled(TaskPool *pool)
Definition task_pool.cc:501
TaskPool * BLI_task_pool_create_no_threads(void *userdata)
Definition task_pool.cc:421
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:471
struct TaskParallelTLS TaskParallelTLS
void BLI_task_pool_cancel(TaskPool *pool)
Definition task_pool.cc:486
void BLI_task_scheduler_exit(void)
ThreadMutex * BLI_task_pool_user_mutex(TaskPool *pool)
Definition task_pool.cc:521
bool BLI_task_graph_node_push_work(struct TaskNode *task_node)
void(* TaskParallelInitFunc)(const void *__restrict userdata, void *__restrict chunk)
Definition BLI_task.h:153
TaskPool * BLI_task_pool_create_background(void *userdata, eTaskPriority priority)
Definition task_pool.cc:399
TaskPool * BLI_task_pool_create_background_serial(void *userdata, eTaskPriority priority)
Definition task_pool.cc:426
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:149
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:394
void BLI_task_graph_free(struct TaskGraph *task_graph)
struct TaskGraph * BLI_task_graph_create(void)
int BLI_task_parallel_thread_id(const TaskParallelTLS *tls)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition BLI_task.h:230
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:238
void(* TaskGraphNodeRunFunction)(void *__restrict task_data)
Definition BLI_task.h:324
void(* TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task.h:62
void(* TaskParallelMempoolFunc)(void *userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict tls)
Definition BLI_task.h:211
void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:431
void(* TaskParallelFreeFunc)(const void *__restrict userdata, void *__restrict chunk)
Definition BLI_task.h:159
struct TaskParallelSettings TaskParallelSettings
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:450
pthread_mutex_t ThreadMutex
Definition BLI_threads.h:83
static PyObject * free_func(PyObject *, PyObject *value)
Definition python.cpp:224
void * task_data
Definition task_graph.cc:47
TaskParallelReduceFunc func_reduce
Definition BLI_task.h:185
TaskParallelFreeFunc func_free
Definition BLI_task.h:187
TaskParallelInitFunc func_init
Definition BLI_task.h:180
size_t userdata_chunk_size
Definition BLI_task.h:173
void * userdata_chunk
Definition BLI_task.h:146
void * userdata
Definition task_pool.cc:153
function< void(void)> TaskRunFunction
Definition task.h:19