Blender V5.3
BLI_task_c.hh
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#pragma once
6
7#include <string.h> /* for memset() */
8
12
13#include "BLI_threads.hh"
14#include "BLI_utildefines.hh"
15
16namespace blender {
17
18struct BLI_mempool;
19
20/* -------------------------------------------------------------------- */
29
35void BLI_task_scheduler_init(bool use_flush_denormals_to_zero = true);
38
40
41/* -------------------------------------------------------------------- */
58
63
64struct TaskPool;
65typedef void (*TaskRunFunction)(TaskPool *__restrict pool, void *taskdata);
66typedef void (*TaskFreeFunction)(TaskPool *__restrict pool, void *taskdata);
67
74TaskPool *BLI_task_pool_create(void *userdata, eTaskPriority priority);
75
81
89
96
101
102void BLI_task_pool_free(TaskPool *pool);
103
105 TaskRunFunction run,
106 void *taskdata,
107 bool free_taskdata,
108 TaskFreeFunction freedata);
109
114
119
121
122/* -------------------------------------------------------------------- */
125
136
137typedef void (*TaskParallelRangeFunc)(void *__restrict userdata,
138 int iter,
139 const TaskParallelTLS *__restrict tls);
140
141typedef void (*TaskParallelInitFunc)(const void *__restrict userdata, void *__restrict chunk);
142
143typedef void (*TaskParallelReduceFunc)(const void *__restrict userdata,
144 void *__restrict chunk_join,
145 void *__restrict chunk);
146
147typedef void (*TaskParallelFreeFunc)(const void *__restrict userdata, void *__restrict chunk);
148
150 /* Whether caller allows to do threading of the particular range.
151 * Usually set by some equation, which forces threading off when threading
152 * overhead becomes higher than speed benefit.
153 * BLI_task_parallel_range() by itself will always use threading when range
154 * is higher than a chunk size. As in, threading will always be performed.
155 */
157 /* Each instance of looping chunks will get a copy of this data
158 * (similar to OpenMP's firstprivate).
159 */
160 void *userdata_chunk; /* Pointer to actual data. */
161 size_t userdata_chunk_size; /* Size of that data. */
162 /* Function called from calling thread once whole range have been
163 * processed.
164 */
165 /* Function called to initialize user data chunk,
166 * typically to allocate data, freed by `func_free`.
167 */
169 /* Function called to join user data chunk into another, to reduce
170 * the result to the original userdata_chunk memory.
171 * The reduce functions should have no side effects, so that they
172 * can be run on any thread. */
174 /* Function called to free data created by TaskParallelRangeFunc. */
176 /* Minimum allowed number of range iterators to be handled by a single
177 * thread. This allows to achieve following:
178 * - Reduce amount of threading overhead.
179 * - Partially occupy thread pool with ranges which are computationally
180 * expensive, but which are smaller than amount of available threads.
181 * For example, it's possible to multi-thread [0 .. 64] range into 4
182 * thread which will be doing 16 iterators each.
183 * This is a preferred way to tell scheduler when to start threading than
184 * having a global use_threading switch based on just range size.
185 */
187};
188
190
191void BLI_task_parallel_range(int start,
192 int stop,
193 void *userdata,
195 const TaskParallelSettings *settings);
196
197struct MempoolIterData;
198
199typedef void (*TaskParallelMempoolFunc)(void *userdata,
200 MempoolIterData *iter,
201 const TaskParallelTLS *__restrict tls);
212void BLI_task_parallel_mempool(struct BLI_mempool *mempool,
213 void *userdata,
215 const TaskParallelSettings *settings);
216
219{
220 memset(settings, 0, sizeof(*settings));
221 settings->use_threading = true;
222 /* Use default heuristic to define actual chunk size. */
223 settings->min_iter_per_thread = 0;
224}
225
227{
228 memset(settings, 0, sizeof(*settings));
229 settings->use_threading = true;
230}
231
236int BLI_task_parallel_thread_id(const TaskParallelTLS *tls);
237
239
240/* -------------------------------------------------------------------- */
308
309struct TaskGraph;
310struct TaskNode;
311
312typedef void (*TaskGraphNodeRunFunction)(void *__restrict task_data);
313typedef void (*TaskGraphNodeFreeFunction)(void *task_data);
314
316void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph);
317void BLI_task_graph_free(struct TaskGraph *task_graph);
318struct TaskNode *BLI_task_graph_node_create(struct TaskGraph *task_graph,
320 void *user_data,
321 TaskGraphNodeFreeFunction free_func);
322bool BLI_task_graph_node_push_work(struct TaskNode *task_node);
323void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node);
324
326
327/* -------------------------------------------------------------------- */
356
357void BLI_task_isolate(void (*func)(void *userdata), void *userdata);
358
360
361} // namespace blender
#define BLI_INLINE
BMesh Iterators.
struct TaskGraph * BLI_task_graph_create()
Definition task_graph.cc:98
void(*) TaskParallelRangeFunc(void *__restrict userdata, int iter, const TaskParallelTLS *__restrict tls)
bool BLI_task_graph_node_push_work(struct TaskNode *task_node)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
void(*) TaskParallelFreeFunc(const void *__restrict userdata, void *__restrict chunk)
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
@ TASK_PRIORITY_LOW
Definition BLI_task_c.hh:60
TaskPool * BLI_task_pool_create_suspended(void *userdata, eTaskPriority priority)
Definition task_pool.cc:423
int BLI_task_scheduler_num_threads()
void BLI_task_graph_free(struct TaskGraph *task_graph)
struct TaskNode * BLI_task_graph_node_create(struct TaskGraph *task_graph, TaskGraphNodeRunFunction run, void *user_data, TaskGraphNodeFreeFunction free_func)
void BLI_task_scheduler_exit()
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
TaskPool * BLI_task_pool_create_no_threads(void *userdata)
Definition task_pool.cc:431
void BLI_task_graph_work_and_wait(struct TaskGraph *task_graph)
BLI_INLINE void BLI_parallel_mempool_settings_defaults(TaskParallelSettings *settings)
void BLI_task_pool_work_and_wait(TaskPool *pool)
Definition task_pool.cc:455
void * BLI_task_pool_user_data(TaskPool *pool)
Definition task_pool.cc:460
void(*) TaskGraphNodeRunFunction(void *__restrict task_data)
TaskPool * BLI_task_pool_create_background_serial(void *userdata, eTaskPriority priority)
Definition task_pool.cc:436
void BLI_task_parallel_mempool(struct BLI_mempool *mempool, void *userdata, TaskParallelMempoolFunc func, const TaskParallelSettings *settings)
void(*) TaskParallelInitFunc(const void *__restrict userdata, void *__restrict chunk)
void(*) TaskRunFunction(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task_c.hh:65
int BLI_task_parallel_thread_id(const TaskParallelTLS *tls)
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
void(*) TaskFreeFunction(TaskPool *__restrict pool, void *taskdata)
Definition BLI_task_c.hh:66
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
void BLI_task_isolate(void(*func)(void *userdata), void *userdata)
void(*) TaskParallelReduceFunc(const void *__restrict userdata, void *__restrict chunk_join, void *__restrict chunk)
void BLI_task_scheduler_init(bool use_flush_denormals_to_zero=true)
void(*) TaskGraphNodeFreeFunction(void *task_data)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
void(*) TaskParallelMempoolFunc(void *userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict tls)
void BLI_task_graph_edge_create(struct TaskNode *from_node, struct TaskNode *to_node)
TaskPool * BLI_task_pool_create_background(void *userdata, eTaskPriority priority)
Definition task_pool.cc:409
TaskParallelReduceFunc func_reduce
TaskParallelInitFunc func_init
TaskParallelFreeFunc func_free