Skip to content

Commit 1068151

Browse files
committed
Set NTHREADS to utils_get_num_cores() at minimum
Set NTHREADS to utils_get_num_cores() at minimum and rename NTHREADS to numThreads. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent b1ca59d commit 1068151

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

test/ipcFixtures.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct umfIpcTest : umf_test::test,
9393
providerParamsDestroy = provider_params_destroy;
9494
memAccessor = accessor;
9595
openedIpcCacheSize = getOpenedIpcCacheSize();
96+
numThreads = std::max(10, (int)utils_get_num_cores());
9697
}
9798

9899
void TearDown() override { test::TearDown(); }
@@ -162,7 +163,7 @@ struct umfIpcTest : umf_test::test,
162163
closeCount(0) {}
163164
};
164165

165-
static constexpr int NTHREADS = 10;
166+
unsigned int numThreads;
166167
stats_type stat;
167168
MemoryAccessor *memAccessor = nullptr;
168169

@@ -188,9 +189,9 @@ struct umfIpcTest : umf_test::test,
188189
ptrs.push_back(ptr);
189190
}
190191

191-
std::array<std::vector<umf_ipc_handle_t>, NTHREADS> ipcHandles;
192+
std::vector<std::vector<umf_ipc_handle_t>> ipcHandles(numThreads);
192193

193-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
194+
umf_test::syncthreads_barrier syncthreads(numThreads);
194195

195196
auto getHandlesFn = [shuffle, &ipcHandles, &ptrs,
196197
&syncthreads](size_t tid) {
@@ -212,7 +213,7 @@ struct umfIpcTest : umf_test::test,
212213
}
213214
};
214215

215-
umf_test::parallel_exec(NTHREADS, getHandlesFn);
216+
umf_test::parallel_exec(numThreads, getHandlesFn);
216217

217218
auto putHandlesFn = [&ipcHandles, &syncthreads](size_t tid) {
218219
syncthreads();
@@ -222,7 +223,7 @@ struct umfIpcTest : umf_test::test,
222223
}
223224
};
224225

225-
umf_test::parallel_exec(NTHREADS, putHandlesFn);
226+
umf_test::parallel_exec(numThreads, putHandlesFn);
226227

227228
for (void *ptr : ptrs) {
228229
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -246,7 +247,7 @@ struct umfIpcTest : umf_test::test,
246247
ptrs.push_back(ptr);
247248
}
248249

249-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
250+
umf_test::syncthreads_barrier syncthreads(numThreads);
250251

251252
auto getPutHandlesFn = [shuffle, &ptrs, &syncthreads](size_t) {
252253
// Each thread gets a copy of the pointers to shuffle them
@@ -268,7 +269,7 @@ struct umfIpcTest : umf_test::test,
268269
}
269270
};
270271

271-
umf_test::parallel_exec(NTHREADS, getPutHandlesFn);
272+
umf_test::parallel_exec(numThreads, getPutHandlesFn);
272273

273274
for (void *ptr : ptrs) {
274275
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -302,13 +303,13 @@ struct umfIpcTest : umf_test::test,
302303
ipcHandles.push_back(ipcHandle);
303304
}
304305

305-
std::array<std::vector<void *>, NTHREADS> openedIpcHandles;
306+
std::vector<std::vector<umf_ipc_handle_t>> openedIpcHandles(numThreads);
306307
umf_ipc_handler_handle_t ipcHandler = nullptr;
307308
ret = umfPoolGetIPCHandler(pool.get(), &ipcHandler);
308309
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
309310
ASSERT_NE(ipcHandler, nullptr);
310311

311-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
312+
umf_test::syncthreads_barrier syncthreads(numThreads);
312313

313314
auto openHandlesFn = [shuffle, &ipcHandles, &openedIpcHandles,
314315
&syncthreads, ipcHandler](size_t tid) {
@@ -325,11 +326,11 @@ struct umfIpcTest : umf_test::test,
325326
umf_result_t ret =
326327
umfOpenIPCHandle(ipcHandler, ipcHandle, &ptr);
327328
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
328-
openedIpcHandles[tid].push_back(ptr);
329+
openedIpcHandles[tid].push_back((umf_ipc_data_t *)ptr);
329330
}
330331
};
331332

332-
umf_test::parallel_exec(NTHREADS, openHandlesFn);
333+
umf_test::parallel_exec(numThreads, openHandlesFn);
333334

334335
auto closeHandlesFn = [&openedIpcHandles, &syncthreads](size_t tid) {
335336
syncthreads();
@@ -339,7 +340,7 @@ struct umfIpcTest : umf_test::test,
339340
}
340341
};
341342

342-
umf_test::parallel_exec(NTHREADS, closeHandlesFn);
343+
umf_test::parallel_exec(numThreads, closeHandlesFn);
343344

344345
for (auto ipcHandle : ipcHandles) {
345346
ret = umfPutIPCHandle(ipcHandle);
@@ -386,7 +387,7 @@ struct umfIpcTest : umf_test::test,
386387
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
387388
ASSERT_NE(ipcHandler, nullptr);
388389

389-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
390+
umf_test::syncthreads_barrier syncthreads(numThreads);
390391

391392
auto openCloseHandlesFn = [shuffle, &ipcHandles, &syncthreads,
392393
ipcHandler](size_t) {
@@ -408,7 +409,7 @@ struct umfIpcTest : umf_test::test,
408409
}
409410
};
410411

411-
umf_test::parallel_exec(NTHREADS, openCloseHandlesFn);
412+
umf_test::parallel_exec(numThreads, openCloseHandlesFn);
412413

413414
for (auto ipcHandle : ipcHandles) {
414415
ret = umfPutIPCHandle(ipcHandle);

test/poolFixtures.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef UMF_TEST_POOL_FIXTURES_HPP
66
#define UMF_TEST_POOL_FIXTURES_HPP 1
77

8+
#include <algorithm>
89
#include <array>
910
#include <cstring>
1011
#include <functional>
@@ -83,13 +84,15 @@ struct umfPoolTest : umf_test::test,
8384
test::SetUp();
8485

8586
pool = poolCreateExtUnique(this->GetParam());
87+
#undef max
88+
numThreads = std::max(5, (int)utils_get_num_cores());
8689
}
8790

8891
void TearDown() override { test::TearDown(); }
8992

9093
umf_test::pool_unique_handle_t pool;
9194

92-
static constexpr int NTHREADS = 5;
95+
int numThreads;
9396
static constexpr std::array<int, 7> nonAlignedAllocSizes = {5, 7, 23, 55,
9497
80, 119, 247};
9598
};
@@ -282,7 +285,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) {
282285
};
283286

284287
std::vector<std::thread> threads;
285-
for (int i = 0; i < NTHREADS; i++) {
288+
for (int i = 0; i < numThreads; i++) {
286289
threads.emplace_back(poolMalloc, allocSize, pool.get());
287290
}
288291

@@ -301,7 +304,7 @@ TEST_P(umfPoolTest, multiThreadedpow2AlignedAlloc) {
301304
};
302305

303306
std::vector<std::thread> threads;
304-
for (int i = 0; i < NTHREADS; i++) {
307+
for (int i = 0; i < numThreads; i++) {
305308
threads.emplace_back(poolpow2AlignedAlloc, pool.get());
306309
}
307310

@@ -336,7 +339,7 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) {
336339
};
337340

338341
std::vector<std::thread> threads;
339-
for (int i = 0; i < NTHREADS; i++) {
342+
for (int i = 0; i < numThreads; i++) {
340343
threads.emplace_back(poolRealloc, allocSize, multiplier, pool.get());
341344
}
342345

@@ -367,7 +370,7 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) {
367370
};
368371

369372
std::vector<std::thread> threads;
370-
for (int i = 0; i < NTHREADS; i++) {
373+
for (int i = 0; i < numThreads; i++) {
371374
threads.emplace_back(poolCalloc, num, sizeof(int), pool.get());
372375
}
373376

@@ -393,7 +396,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
393396
};
394397

395398
std::vector<std::thread> threads;
396-
for (int i = 0; i < NTHREADS; i++) {
399+
for (int i = 0; i < numThreads; i++) {
397400
threads.emplace_back(poolMalloc, (rand() % 16) * 8, pool.get());
398401
}
399402

test/test_base_alloc.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8+
#include <algorithm>
89
#include <cstdio>
910
#include <cstdlib>
1011
#include <thread>
@@ -17,9 +18,9 @@
1718
using umf_test::test;
1819

1920
TEST_F(test, baseAllocMultiThreadedAllocMemset) {
20-
static constexpr int NTHREADS = 10;
2121
static constexpr int ITERATIONS = 1000;
2222
static constexpr int ALLOCATION_SIZE = 16;
23+
int numThreads = std::max(10, (int)utils_get_num_cores());
2324

2425
auto pool = std::shared_ptr<umf_ba_pool_t>(umf_ba_create(ALLOCATION_SIZE),
2526
umf_ba_destroy);
@@ -43,7 +44,7 @@ TEST_F(test, baseAllocMultiThreadedAllocMemset) {
4344
};
4445

4546
std::vector<std::thread> threads;
46-
for (int i = 0; i < NTHREADS; i++) {
47+
for (int i = 0; i < numThreads; i++) {
4748
threads.emplace_back(poolAlloc, i, pool.get());
4849
}
4950

test/test_base_alloc_linear.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8+
#include <algorithm>
89
#include <cstdio>
910
#include <cstdlib>
1011
#include <thread>
@@ -50,9 +51,9 @@ TEST_F(test, baseAllocLinearPoolContainsPointer) {
5051
}
5152

5253
TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
53-
static constexpr int NTHREADS = 10;
5454
static constexpr int ITERATIONS = 1000;
5555
static constexpr int MAX_ALLOCATION_SIZE = 1024;
56+
int numThreads = std::max(10, (int)utils_get_num_cores());
5657

5758
srand(0);
5859

@@ -94,7 +95,7 @@ TEST_F(test, baseAllocLinearMultiThreadedAllocMemset) {
9495
};
9596

9697
std::vector<std::thread> threads;
97-
for (int i = 0; i < NTHREADS; i++) {
98+
for (int i = 0; i < numThreads; i++) {
9899
threads.emplace_back(poolAlloc, i, pool.get());
99100
}
100101

0 commit comments

Comments
 (0)