diff options
author | Campbell Barton <ideasman42@gmail.com> | 2021-06-09 15:49:45 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2021-06-09 17:55:04 +0300 |
commit | 14f3b2cdad2d7b00ce3ea27bf8eb0087ac2a6cbd (patch) | |
tree | 889d20ad5cf627057bb619e18ec47d39cabafc99 /source/blender/blenlib/intern | |
parent | f546b0800b9121b24b1292f1ec602ed9964d1848 (diff) |
BLI_task: add TLS support to BLI_task_parallel_mempool
Support thread local storage for BLI_task_parallel_mempool,
as well as support for the reduce and free callbacks.
mempool_iter_threadsafe_* functions have been moved into a private
header thats only shared between task_iterator.c and BLI_mempool.c
so the TLS can be made part of the iterator array without having to
rely on passing in struct offsets.
Add test task.MempoolIterTLS that ensures reduce and free
are working as expected.
Reviewed By: mont29
Ref D11548
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool.c | 27 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool_private.h | 45 | ||||
-rw-r--r-- | source/blender/blenlib/intern/task_iterator.c | 81 |
3 files changed, 121 insertions, 32 deletions
diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index 75f934c1fb8..cf0021609b5 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -36,7 +36,8 @@ #include "BLI_utildefines.h" -#include "BLI_mempool.h" /* own include */ +#include "BLI_mempool.h" /* own include */ +#include "BLI_mempool_private.h" /* own include */ #include "MEM_guardedalloc.h" @@ -558,33 +559,31 @@ void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter) * * See BLI_task_parallel_mempool implementation for detailed usage example. */ -BLI_mempool_iter *BLI_mempool_iter_threadsafe_create(BLI_mempool *pool, const size_t num_iter) +ParallelMempoolTaskData *mempool_iter_threadsafe_create(BLI_mempool *pool, const size_t num_iter) { BLI_assert(pool->flag & BLI_MEMPOOL_ALLOW_ITER); - BLI_mempool_iter *iter_arr = MEM_mallocN(sizeof(*iter_arr) * num_iter, __func__); + ParallelMempoolTaskData *iter_arr = MEM_mallocN(sizeof(*iter_arr) * num_iter, __func__); BLI_mempool_chunk **curchunk_threaded_shared = MEM_mallocN(sizeof(void *), __func__); - BLI_mempool_iternew(pool, iter_arr); - - *curchunk_threaded_shared = iter_arr->curchunk; - iter_arr->curchunk_threaded_shared = curchunk_threaded_shared; + BLI_mempool_iternew(pool, &iter_arr->iter); + *curchunk_threaded_shared = iter_arr->iter.curchunk; + iter_arr->iter.curchunk_threaded_shared = curchunk_threaded_shared; for (size_t i = 1; i < num_iter; i++) { - iter_arr[i] = iter_arr[0]; - *curchunk_threaded_shared = iter_arr[i].curchunk = ((*curchunk_threaded_shared) ? - (*curchunk_threaded_shared)->next : - NULL); + iter_arr[i].iter = iter_arr[0].iter; + *curchunk_threaded_shared = iter_arr[i].iter.curchunk = + ((*curchunk_threaded_shared) ? (*curchunk_threaded_shared)->next : NULL); } return iter_arr; } -void BLI_mempool_iter_threadsafe_free(BLI_mempool_iter *iter_arr) +void mempool_iter_threadsafe_destroy(ParallelMempoolTaskData *iter_arr) { - BLI_assert(iter_arr->curchunk_threaded_shared != NULL); + BLI_assert(iter_arr->iter.curchunk_threaded_shared != NULL); - MEM_freeN(iter_arr->curchunk_threaded_shared); + MEM_freeN(iter_arr->iter.curchunk_threaded_shared); MEM_freeN(iter_arr); } diff --git a/source/blender/blenlib/intern/BLI_mempool_private.h b/source/blender/blenlib/intern/BLI_mempool_private.h new file mode 100644 index 00000000000..60994624ecd --- /dev/null +++ b/source/blender/blenlib/intern/BLI_mempool_private.h @@ -0,0 +1,45 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2008 Blender Foundation. + * All rights reserved. + */ + +#pragma once + +/** \file + * \ingroup bli + * + * Shared logic for #BLI_task_parallel_mempool to create a threaded iterator, + * without exposing the these functions publicly. + */ + +#include "BLI_compiler_attrs.h" + +#include "BLI_mempool.h" +#include "BLI_task.h" + +typedef struct ParallelMempoolTaskData { + BLI_mempool_iter iter; + TaskParallelTLS tls; +} ParallelMempoolTaskData; + +ParallelMempoolTaskData *mempool_iter_threadsafe_create(BLI_mempool *pool, const size_t num_iter) + ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); +void mempool_iter_threadsafe_destroy(ParallelMempoolTaskData *iter_arr) ATTR_NONNULL(); + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/blenlib/intern/task_iterator.c b/source/blender/blenlib/intern/task_iterator.c index 463d3675cbe..3914a074d11 100644 --- a/source/blender/blenlib/intern/task_iterator.c +++ b/source/blender/blenlib/intern/task_iterator.c @@ -29,6 +29,7 @@ #include "BLI_listbase.h" #include "BLI_math.h" #include "BLI_mempool.h" +#include "BLI_mempool_private.h" #include "BLI_task.h" #include "BLI_threads.h" @@ -348,9 +349,6 @@ void BLI_task_parallel_listbase(ListBase *listbase, task_parallel_iterator_do(settings, &state); } -#undef MALLOCA -#undef MALLOCA_FREE - typedef struct ParallelMempoolState { void *userdata; TaskParallelMempoolFunc func; @@ -359,11 +357,12 @@ typedef struct ParallelMempoolState { static void parallel_mempool_func(TaskPool *__restrict pool, void *taskdata) { ParallelMempoolState *__restrict state = BLI_task_pool_user_data(pool); - BLI_mempool_iter *iter = taskdata; - MempoolIterData *item; + BLI_mempool_iter *iter = &((ParallelMempoolTaskData *)taskdata)->iter; + TaskParallelTLS *tls = &((ParallelMempoolTaskData *)taskdata)->tls; + MempoolIterData *item; while ((item = BLI_mempool_iterstep(iter)) != NULL) { - state->func(state->userdata, item); + state->func(state->userdata, item, tls); } } @@ -373,16 +372,14 @@ static void parallel_mempool_func(TaskPool *__restrict pool, void *taskdata) * \param mempool: The iterable BLI_mempool to loop over. * \param userdata: Common userdata passed to all instances of \a func. * \param func: Callback function. - * \param use_threading: If \a true, actually split-execute loop in threads, - * else just do a sequential for loop - * (allows caller to use any kind of test to switch on parallelization or not). + * \param settings: See public API doc of TaskParallelSettings for description of all settings. * * \note There is no static scheduling here. */ void BLI_task_parallel_mempool(BLI_mempool *mempool, void *userdata, TaskParallelMempoolFunc func, - const bool use_threading) + const TaskParallelSettings *settings) { TaskPool *task_pool; ParallelMempoolState state; @@ -392,14 +389,34 @@ void BLI_task_parallel_mempool(BLI_mempool *mempool, return; } - if (!use_threading) { + void *userdata_chunk = settings->userdata_chunk; + const size_t userdata_chunk_size = settings->userdata_chunk_size; + void *userdata_chunk_local = NULL; + void *userdata_chunk_array = NULL; + const bool use_userdata_chunk = (userdata_chunk_size != 0) && (userdata_chunk != NULL); + + if (!settings->use_threading) { + TaskParallelTLS tls = {NULL}; + if (use_userdata_chunk) { + userdata_chunk_local = MALLOCA(userdata_chunk_size); + memcpy(userdata_chunk_local, userdata_chunk, userdata_chunk_size); + tls.userdata_chunk = userdata_chunk_local; + } + BLI_mempool_iter iter; BLI_mempool_iternew(mempool, &iter); - for (void *item = BLI_mempool_iterstep(&iter); item != NULL; - item = BLI_mempool_iterstep(&iter)) { - func(userdata, item); + void *item; + while ((item = BLI_mempool_iterstep(&iter))) { + func(userdata, item, &tls); } + + if (settings->func_free != NULL) { + /* `func_free` should only free data that was created during execution of `func`. */ + settings->func_free(userdata, userdata_chunk_local); + } + + MALLOCA_FREE(userdata_chunk_local, userdata_chunk_size); return; } @@ -415,16 +432,44 @@ void BLI_task_parallel_mempool(BLI_mempool *mempool, state.userdata = userdata; state.func = func; - BLI_mempool_iter *mempool_iterators = BLI_mempool_iter_threadsafe_create(mempool, - (size_t)num_tasks); + if (use_userdata_chunk) { + userdata_chunk_array = MALLOCA(userdata_chunk_size * num_tasks); + } + + ParallelMempoolTaskData *mempool_iterator_data = mempool_iter_threadsafe_create( + mempool, (size_t)num_tasks); for (i = 0; i < num_tasks; i++) { + if (use_userdata_chunk) { + userdata_chunk_local = (char *)userdata_chunk_array + (userdata_chunk_size * i); + memcpy(userdata_chunk_local, userdata_chunk, userdata_chunk_size); + } + mempool_iterator_data[i].tls.userdata_chunk = userdata_chunk_local; + /* Use this pool's pre-allocated tasks. */ - BLI_task_pool_push(task_pool, parallel_mempool_func, &mempool_iterators[i], false, NULL); + BLI_task_pool_push(task_pool, parallel_mempool_func, &mempool_iterator_data[i], false, NULL); } BLI_task_pool_work_and_wait(task_pool); BLI_task_pool_free(task_pool); - BLI_mempool_iter_threadsafe_free(mempool_iterators); + if (use_userdata_chunk) { + if ((settings->func_free != NULL) || (settings->func_reduce != NULL)) { + for (i = 0; i < num_tasks; i++) { + if (settings->func_reduce) { + settings->func_reduce( + userdata, userdata_chunk, mempool_iterator_data[i].tls.userdata_chunk); + } + if (settings->func_free) { + settings->func_free(userdata, mempool_iterator_data[i].tls.userdata_chunk); + } + } + } + MALLOCA_FREE(userdata_chunk_array, userdata_chunk_size * num_tasks); + } + + mempool_iter_threadsafe_destroy(mempool_iterator_data); } + +#undef MALLOCA +#undef MALLOCA_FREE |