Blender V5.3
task_iterator.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdlib>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_mempool.hh"
16#include "BLI_mempool_private.h"
17#include "BLI_task_c.hh"
18
19namespace blender {
20
21/* -------------------------------------------------------------------- */
24
25/* Allows to avoid using `malloc` for userdata_chunk in tasks, when small enough. */
26#define MALLOCA(_size) ((_size) <= 8192) ? alloca(_size) : MEM_new_uninitialized((_size), __func__)
27#define MALLOCA_FREE(_mem, _size) \
28 if (((_mem) != nullptr) && ((_size) > 8192)) { \
29 MEM_delete_void(_mem); \
30 } \
31 ((void)0)
32
34
35/* -------------------------------------------------------------------- */
38
43
44static void parallel_mempool_func(TaskPool *__restrict pool, void *taskdata)
45{
46 ParallelMempoolState *__restrict state = static_cast<ParallelMempoolState *>(
48 BLI_mempool_threadsafe_iter *iter = &(static_cast<ParallelMempoolTaskData *>(taskdata))->ts_iter;
49 TaskParallelTLS *tls = &(static_cast<ParallelMempoolTaskData *>(taskdata))->tls;
50
51 MempoolIterData *item;
52 while ((item = static_cast<MempoolIterData *>(mempool_iter_threadsafe_step(iter))) != nullptr) {
53 state->func(state->userdata, item, tls);
54 }
55}
56
58 void *userdata,
60 const TaskParallelSettings *settings)
61{
62 if (BLI_mempool_len(mempool) == 0) [[unlikely]] {
63 return;
64 }
65
66 void *userdata_chunk = settings->userdata_chunk;
67 const size_t userdata_chunk_size = settings->userdata_chunk_size;
68 void *userdata_chunk_array = nullptr;
69 const bool use_userdata_chunk = (userdata_chunk_size != 0) && (userdata_chunk != nullptr);
70
71 if (!settings->use_threading) {
72 TaskParallelTLS tls = {nullptr};
73 if (use_userdata_chunk) {
74 if (settings->func_init != nullptr) {
75 settings->func_init(userdata, userdata_chunk);
76 }
77 tls.userdata_chunk = userdata_chunk;
78 }
79
81 BLI_mempool_iternew(mempool, &iter);
82
83 void *item;
84 while ((item = BLI_mempool_iterstep(&iter))) {
85 func(userdata, static_cast<MempoolIterData *>(item), &tls);
86 }
87
88 if (use_userdata_chunk) {
89 if (settings->func_free != nullptr && userdata_chunk != nullptr) {
90 /* `func_free` should only free data that was created during execution of `func`. */
91 settings->func_free(userdata, userdata_chunk);
92 }
93 }
94
95 return;
96 }
97
100 const int threads_num = BLI_task_scheduler_num_threads();
101
102 /* The idea here is to prevent creating task for each of the loop iterations
103 * and instead have tasks which are evenly distributed across CPU cores and
104 * pull next item to be crunched using the threaded-aware BLI_mempool_iter.
105 */
106 const int tasks_num = threads_num + 2;
107
108 state.userdata = userdata;
109 state.func = func;
110
111 if (use_userdata_chunk) {
112 userdata_chunk_array = MALLOCA(userdata_chunk_size * tasks_num);
113 }
114
116 mempool, size_t(tasks_num));
117
118 for (int i = 0; i < tasks_num; i++) {
119 void *userdata_chunk_local = nullptr;
120 if (use_userdata_chunk) {
121 userdata_chunk_local = static_cast<char *>(userdata_chunk_array) + (userdata_chunk_size * i);
122 memcpy(userdata_chunk_local, userdata_chunk, userdata_chunk_size);
123 if (settings->func_init != nullptr) {
124 settings->func_init(userdata, userdata_chunk_local);
125 }
126 }
127 mempool_iterator_data[i].tls.userdata_chunk = userdata_chunk_local;
128
129 /* Use this pool's pre-allocated tasks. */
131 task_pool, parallel_mempool_func, &mempool_iterator_data[i], false, nullptr);
132 }
133
135 BLI_task_pool_free(task_pool);
136
137 if (use_userdata_chunk) {
138 if ((settings->func_free != nullptr) || (settings->func_reduce != nullptr)) {
139 for (int i = 0; i < tasks_num; i++) {
140 if (settings->func_reduce) {
141 settings->func_reduce(
142 userdata, userdata_chunk, mempool_iterator_data[i].tls.userdata_chunk);
143 }
144 if (settings->func_free && mempool_iterator_data[i].tls.userdata_chunk != nullptr) {
145 settings->func_free(userdata, mempool_iterator_data[i].tls.userdata_chunk);
146 }
147 }
148 }
149 MALLOCA_FREE(userdata_chunk_array, userdata_chunk_size * tasks_num);
150 }
151
152 mempool_iter_threadsafe_destroy(mempool_iterator_data);
153}
154
155#undef MALLOCA
156#undef MALLOCA_FREE
157
159
160} // namespace blender
Read Blender memory allocation and freeing.
const int state
BMesh Iterators.
@ TASK_PRIORITY_HIGH
Definition BLI_task_c.hh:61
int BLI_task_scheduler_num_threads()
ParallelMempoolTaskData * mempool_iter_threadsafe_create(BLI_mempool *pool, const size_t iter_num)
void mempool_iter_threadsafe_destroy(ParallelMempoolTaskData *iter_arr)
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 BLI_task_parallel_mempool(struct BLI_mempool *mempool, void *userdata, TaskParallelMempoolFunc func, const TaskParallelSettings *settings)
void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter) ATTR_NONNULL()
void * BLI_mempool_iterstep(BLI_mempool_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_task_pool_free(TaskPool *pool)
Definition task_pool.cc:441
TaskPool * BLI_task_pool_create(void *userdata, eTaskPriority priority)
Definition task_pool.cc:404
int BLI_mempool_len(const BLI_mempool *pool) ATTR_NONNULL(1)
void BLI_task_pool_push(TaskPool *pool, TaskRunFunction run, void *taskdata, bool free_taskdata, TaskFreeFunction freedata)
Definition task_pool.cc:446
static void parallel_mempool_func(TaskPool *__restrict pool, void *taskdata)
void(*) TaskParallelMempoolFunc(void *userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict tls)
void * mempool_iter_threadsafe_step(BLI_mempool_threadsafe_iter *ts_iter)
TaskParallelMempoolFunc func
TaskParallelReduceFunc func_reduce
TaskParallelInitFunc func_init
TaskParallelFreeFunc func_free
#define MALLOCA_FREE(_mem, _size)
#define MALLOCA(_size)