From efb86b712d540e132fb68058b7a7fba0fc8be247 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 23 Nov 2017 21:14:43 +0100 Subject: Add a new parallel looper for MemPool items to BLI_task. It merely uses the new thread-safe iterators system of mempool, quite straight forward. Note that to avoid possible confusion with two void pointers as parameters of the callback, a dummy opaque struct pointer is used instead for the second parameter (pointer generated by iteration over mempool), callback functions must explicitely convert it to expected real type. Also added a basic gtest for this new feature. --- tests/gtests/blenlib/BLI_task_test.cc | 76 +++++++++++++++++++++++++++++++++++ tests/gtests/blenlib/CMakeLists.txt | 2 + 2 files changed, 78 insertions(+) create mode 100644 tests/gtests/blenlib/BLI_task_test.cc (limited to 'tests/gtests') diff --git a/tests/gtests/blenlib/BLI_task_test.cc b/tests/gtests/blenlib/BLI_task_test.cc new file mode 100644 index 00000000000..e6464164ecb --- /dev/null +++ b/tests/gtests/blenlib/BLI_task_test.cc @@ -0,0 +1,76 @@ +/* Apache License, Version 2.0 */ + +#include "testing/testing.h" +#include + +#include "atomic_ops.h" + +extern "C" { +#include "BLI_mempool.h" +#include "BLI_task.h" +#include "BLI_utildefines.h" +}; + +#define NUM_ITEMS 10000 + +static void task_mempool_iter_func(void *userdata, MempoolIterData *item) { + int *data = (int *)item; + int *count = (int *)userdata; + + EXPECT_TRUE(data != NULL); + + *data += 1; + atomic_sub_and_fetch_uint32((uint32_t *)count, 1); +} + +TEST(task, MempoolIter) +{ + int *data[NUM_ITEMS]; + BLI_mempool *mempool = BLI_mempool_create(sizeof(*data[0]), NUM_ITEMS, 32, BLI_MEMPOOL_ALLOW_ITER); + + int i; + + /* 'Randomly' add and remove some items from mempool, to create a non-homogenous one. */ + int num_items = 0; + for (i = 0; i < NUM_ITEMS; i++) { + data[i] = (int *)BLI_mempool_alloc(mempool); + *data[i] = i - 1; + num_items++; + } + + for (i = 0; i < NUM_ITEMS; i += 3) { + BLI_mempool_free(mempool, data[i]); + data[i] = NULL; + num_items--; + } + + for (i = 0; i < NUM_ITEMS; i += 7) { + if (data[i] == NULL) { + data[i] = (int *)BLI_mempool_alloc(mempool); + *data[i] = i - 1; + num_items++; + } + } + + for (i = 0; i < NUM_ITEMS - 5; i += 23) { + for (int j = 0; j < 5; j++) { + if (data[i + j] != NULL) { + BLI_mempool_free(mempool, data[i + j]); + data[i + j] = NULL; + num_items--; + } + } + } + + BLI_task_parallel_mempool(mempool, &num_items, task_mempool_iter_func, true); + + /* Those checks should ensure us all items of the mempool were processed once, and only once - as expected. */ + EXPECT_EQ(num_items, 0); + for (i = 0; i < NUM_ITEMS; i++) { + if (data[i] != NULL) { + EXPECT_EQ(*data[i], i); + } + } + + BLI_mempool_destroy(mempool); +} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index eff67f053e6..715b689387f 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -27,6 +27,7 @@ set(INC ../../../source/blender/blenlib ../../../source/blender/makesdna ../../../intern/guardedalloc + ../../../intern/atomic ) include_directories(${INC}) @@ -55,6 +56,7 @@ BLENDER_TEST(BLI_polyfill2d "bf_blenlib") BLENDER_TEST(BLI_stack "bf_blenlib") BLENDER_TEST(BLI_string "bf_blenlib") BLENDER_TEST(BLI_string_utf8 "bf_blenlib") +BLENDER_TEST(BLI_task "bf_blenlib") BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib") -- cgit v1.2.3