Skip to content

Commit 1a19fe3

Browse files
added object_pool test
1 parent 03f7337 commit 1a19fe3

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ add_executable(utility unittest/utility.cpp)
160160
target_link_libraries(utility ${PROJECT_NAME} Threads::Threads)
161161
target_include_directories(utility PRIVATE ${PROJECT_SOURCE_DIR}/doctest)
162162
add_test(passive_vector ${TF_UTEST_DIR}/utility -tc=PassiveVector)
163+
add_test(object_pool ${TF_UTEST_DIR}/utility -tc=ObjectPool)
163164
add_test(singular_alloc ${TF_UTEST_DIR}/utility -tc=SingularAllocator)
164165

165166
# unittest for taskflow
@@ -168,6 +169,7 @@ target_link_libraries(taskflow_test_tmp ${PROJECT_NAME} Threads::Threads)
168169
target_include_directories(taskflow_test_tmp PRIVATE ${PROJECT_SOURCE_DIR}/doctest)
169170
set_target_properties(taskflow_test_tmp PROPERTIES OUTPUT_NAME "taskflow")
170171
add_test(builder ${TF_UTEST_DIR}/taskflow -tc=Builder)
172+
add_test(creation ${TF_UTEST_DIR}/taskflow -tc=Creation)
171173
add_test(dispatch ${TF_UTEST_DIR}/taskflow -tc=Dispatch)
172174
add_test(multiple_runs ${TF_UTEST_DIR}/taskflow -tc=MultipleRuns)
173175
add_test(parallel_for ${TF_UTEST_DIR}/taskflow -tc=ParallelFor)

taskflow/core/executor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 2019/06/11 - modified by Tsung-Wei Huang
22
// - fixed the bug in calling observer while the user
33
// may clear the data
4+
// - added object pool for nodes
45
//
56
// 2019/05/17 - modified by Chun-Xun Lin
67
// - moved topology to taskflow

unittest/taskflow.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,44 @@ TEST_CASE("Builder" * doctest::timeout(300)) {
176176
}
177177
}
178178

179+
// --------------------------------------------------------
180+
// Testcase: Creation
181+
// --------------------------------------------------------
182+
TEST_CASE("Creation" * doctest::timeout(300)) {
183+
184+
std::vector<int> dummy(1024, -1);
185+
186+
auto create_taskflow = [&] () {
187+
for(int i=0; i<1024; ++i) {
188+
tf::Taskflow tf;
189+
tf.parallel_for(dummy.begin(), dummy.end(), [] (int) {});
190+
}
191+
};
192+
193+
SUBCASE("One") {
194+
create_taskflow();
195+
}
196+
197+
SUBCASE("Two") {
198+
std::thread t1(create_taskflow);
199+
std::thread t2(create_taskflow);
200+
t1.join();
201+
t2.join();
202+
}
203+
204+
SUBCASE("Four") {
205+
std::thread t1(create_taskflow);
206+
std::thread t2(create_taskflow);
207+
std::thread t3(create_taskflow);
208+
std::thread t4(create_taskflow);
209+
t1.join();
210+
t2.join();
211+
t3.join();
212+
t4.join();
213+
}
214+
215+
}
216+
179217
// --------------------------------------------------------
180218
// Testcase: Run
181219
// --------------------------------------------------------

unittest/utility.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,81 @@ TEST_CASE("PassiveVector" * doctest::timeout(300)) {
183183
}
184184
}
185185

186+
// --------------------------------------------------------
187+
// Testcase: Pool
188+
// --------------------------------------------------------
189+
TEST_CASE("ObjectPool" * doctest::timeout(300)) {
190+
191+
auto fork = [&] (unsigned N) {
192+
const int M = 2048 * N;
193+
std::atomic<int> counter = M;
194+
std::atomic<int> recycle = M;
195+
std::mutex mutex;
196+
std::vector<int*> objects;
197+
std::vector<std::thread> threads;
198+
199+
// allocate
200+
for(unsigned t=1; t<=N; ++t) {
201+
threads.emplace_back([&] () {
202+
while(1) {
203+
if(int c = --counter; c < 0) {
204+
break;
205+
}
206+
else {
207+
auto ptr = tf::per_thread_object_pool<int>().get(c);
208+
std::scoped_lock lock(mutex);
209+
objects.push_back(ptr);
210+
}
211+
}
212+
});
213+
}
214+
for(auto& thread : threads) {
215+
thread.join();
216+
}
217+
threads.clear();
218+
219+
REQUIRE(objects.size() == M);
220+
221+
auto sum = std::accumulate(objects.begin(), objects.end(), 0,
222+
[] (int s, int* v) { return s + *v; }
223+
);
224+
225+
REQUIRE(sum == (M-1)*M / 2);
226+
227+
// recycle
228+
for(unsigned t=1; t<=N; ++t) {
229+
threads.emplace_back([&] () {
230+
while(1) {
231+
if(int r = --recycle; r < 0) {
232+
break;
233+
}
234+
else {
235+
std::scoped_lock lock(mutex);
236+
REQUIRE(!objects.empty());
237+
tf::per_thread_object_pool<int>().recycle(objects.back());
238+
objects.pop_back();
239+
}
240+
}
241+
});
242+
}
243+
for(auto& thread : threads) {
244+
thread.join();
245+
}
246+
threads.clear();
247+
248+
REQUIRE(objects.size() == 0);
249+
};
250+
251+
SUBCASE("OneThread") { fork(1); }
252+
SUBCASE("TwoThread") { fork(2); }
253+
SUBCASE("ThreeThreads") { fork(3); }
254+
SUBCASE("FourThreads") { fork(4); }
255+
SUBCASE("FiveThreads") { fork(5); }
256+
SUBCASE("SixThreads") { fork(6); }
257+
SUBCASE("SevenThreads") { fork(7); }
258+
SUBCASE("EightThreads") { fork(8); }
259+
}
260+
186261
// --------------------------------------------------------
187262
// Testcase: SingularAllocator
188263
// --------------------------------------------------------

0 commit comments

Comments
 (0)