Skip to content

Commit 6e1961f

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 1faa557 commit 6e1961f

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

test/ipcFixtures.hpp

Lines changed: 15 additions & 13 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,8 @@ struct umfIpcTest : umf_test::test,
162163
closeCount(0) {}
163164
};
164165

165-
static constexpr int NTHREADS = 10;
166+
unsigned int numThreads;
167+
static constexpr int CNTHREADS = 10;
166168
stats_type stat;
167169
MemoryAccessor *memAccessor = nullptr;
168170

@@ -188,9 +190,9 @@ struct umfIpcTest : umf_test::test,
188190
ptrs.push_back(ptr);
189191
}
190192

191-
std::array<std::vector<umf_ipc_handle_t>, NTHREADS> ipcHandles;
193+
std::array<std::vector<umf_ipc_handle_t>, CNTHREADS> ipcHandles;
192194

193-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
195+
umf_test::syncthreads_barrier syncthreads(numThreads);
194196

195197
auto getHandlesFn = [shuffle, &ipcHandles, &ptrs,
196198
&syncthreads](size_t tid) {
@@ -212,7 +214,7 @@ struct umfIpcTest : umf_test::test,
212214
}
213215
};
214216

215-
umf_test::parallel_exec(NTHREADS, getHandlesFn);
217+
umf_test::parallel_exec(numThreads, getHandlesFn);
216218

217219
auto putHandlesFn = [&ipcHandles, &syncthreads](size_t tid) {
218220
syncthreads();
@@ -222,7 +224,7 @@ struct umfIpcTest : umf_test::test,
222224
}
223225
};
224226

225-
umf_test::parallel_exec(NTHREADS, putHandlesFn);
227+
umf_test::parallel_exec(numThreads, putHandlesFn);
226228

227229
for (void *ptr : ptrs) {
228230
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -246,7 +248,7 @@ struct umfIpcTest : umf_test::test,
246248
ptrs.push_back(ptr);
247249
}
248250

249-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
251+
umf_test::syncthreads_barrier syncthreads(numThreads);
250252

251253
auto getPutHandlesFn = [shuffle, &ptrs, &syncthreads](size_t) {
252254
// Each thread gets a copy of the pointers to shuffle them
@@ -268,7 +270,7 @@ struct umfIpcTest : umf_test::test,
268270
}
269271
};
270272

271-
umf_test::parallel_exec(NTHREADS, getPutHandlesFn);
273+
umf_test::parallel_exec(numThreads, getPutHandlesFn);
272274

273275
for (void *ptr : ptrs) {
274276
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -302,13 +304,13 @@ struct umfIpcTest : umf_test::test,
302304
ipcHandles.push_back(ipcHandle);
303305
}
304306

305-
std::array<std::vector<void *>, NTHREADS> openedIpcHandles;
307+
std::array<std::vector<void *>, CNTHREADS> openedIpcHandles;
306308
umf_ipc_handler_handle_t ipcHandler = nullptr;
307309
ret = umfPoolGetIPCHandler(pool.get(), &ipcHandler);
308310
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
309311
ASSERT_NE(ipcHandler, nullptr);
310312

311-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
313+
umf_test::syncthreads_barrier syncthreads(numThreads);
312314

313315
auto openHandlesFn = [shuffle, &ipcHandles, &openedIpcHandles,
314316
&syncthreads, ipcHandler](size_t tid) {
@@ -329,7 +331,7 @@ struct umfIpcTest : umf_test::test,
329331
}
330332
};
331333

332-
umf_test::parallel_exec(NTHREADS, openHandlesFn);
334+
umf_test::parallel_exec(numThreads, openHandlesFn);
333335

334336
auto closeHandlesFn = [&openedIpcHandles, &syncthreads](size_t tid) {
335337
syncthreads();
@@ -339,7 +341,7 @@ struct umfIpcTest : umf_test::test,
339341
}
340342
};
341343

342-
umf_test::parallel_exec(NTHREADS, closeHandlesFn);
344+
umf_test::parallel_exec(numThreads, closeHandlesFn);
343345

344346
for (auto ipcHandle : ipcHandles) {
345347
ret = umfPutIPCHandle(ipcHandle);
@@ -386,7 +388,7 @@ struct umfIpcTest : umf_test::test,
386388
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
387389
ASSERT_NE(ipcHandler, nullptr);
388390

389-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
391+
umf_test::syncthreads_barrier syncthreads(numThreads);
390392

391393
auto openCloseHandlesFn = [shuffle, &ipcHandles, &syncthreads,
392394
ipcHandler](size_t) {
@@ -408,7 +410,7 @@ struct umfIpcTest : umf_test::test,
408410
}
409411
};
410412

411-
umf_test::parallel_exec(NTHREADS, openCloseHandlesFn);
413+
umf_test::parallel_exec(numThreads, openCloseHandlesFn);
412414

413415
for (auto ipcHandle : ipcHandles) {
414416
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>
@@ -82,13 +83,15 @@ struct umfPoolTest : umf_test::test,
8283
test::SetUp();
8384

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

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

8992
umf_test::pool_unique_handle_t pool;
9093

91-
static constexpr int NTHREADS = 5;
94+
int numThreads;
9295
static constexpr std::array<int, 7> nonAlignedAllocSizes = {5, 7, 23, 55,
9396
80, 119, 247};
9497
};
@@ -281,7 +284,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) {
281284
};
282285

283286
std::vector<std::thread> threads;
284-
for (int i = 0; i < NTHREADS; i++) {
287+
for (int i = 0; i < numThreads; i++) {
285288
threads.emplace_back(poolMalloc, allocSize, pool.get());
286289
}
287290

@@ -300,7 +303,7 @@ TEST_P(umfPoolTest, multiThreadedpow2AlignedAlloc) {
300303
};
301304

302305
std::vector<std::thread> threads;
303-
for (int i = 0; i < NTHREADS; i++) {
306+
for (int i = 0; i < numThreads; i++) {
304307
threads.emplace_back(poolpow2AlignedAlloc, pool.get());
305308
}
306309

@@ -335,7 +338,7 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) {
335338
};
336339

337340
std::vector<std::thread> threads;
338-
for (int i = 0; i < NTHREADS; i++) {
341+
for (int i = 0; i < numThreads; i++) {
339342
threads.emplace_back(poolRealloc, allocSize, multiplier, pool.get());
340343
}
341344

@@ -366,7 +369,7 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) {
366369
};
367370

368371
std::vector<std::thread> threads;
369-
for (int i = 0; i < NTHREADS; i++) {
372+
for (int i = 0; i < numThreads; i++) {
370373
threads.emplace_back(poolCalloc, num, sizeof(int), pool.get());
371374
}
372375

@@ -392,7 +395,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
392395
};
393396

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

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: 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>
@@ -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)