Skip to content

Commit 4270945

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 856058d commit 4270945

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
@@ -119,6 +119,7 @@ struct umfIpcTest : umf_test::test,
119119
providerParamsDestroy = provider_params_destroy;
120120
memAccessor = accessor;
121121
openedIpcCacheSize = getOpenedIpcCacheSize();
122+
numThreads = std::max(10, (int)utils_get_num_cores());
122123
}
123124

124125
void TearDown() override { test::TearDown(); }
@@ -188,7 +189,7 @@ struct umfIpcTest : umf_test::test,
188189
closeCount(0) {}
189190
};
190191

191-
static constexpr int NTHREADS = 10;
192+
unsigned int numThreads;
192193
stats_type stat;
193194
MemoryAccessor *memAccessor = nullptr;
194195

@@ -214,9 +215,9 @@ struct umfIpcTest : umf_test::test,
214215
ptrs.push_back(ptr);
215216
}
216217

217-
std::array<std::vector<umf_ipc_handle_t>, NTHREADS> ipcHandles;
218+
std::vector<std::vector<umf_ipc_handle_t>> ipcHandles(numThreads);
218219

219-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
220+
umf_test::syncthreads_barrier syncthreads(numThreads);
220221

221222
auto getHandlesFn = [shuffle, &ipcHandles, &ptrs,
222223
&syncthreads](size_t tid) {
@@ -238,7 +239,7 @@ struct umfIpcTest : umf_test::test,
238239
}
239240
};
240241

241-
umf_test::parallel_exec(NTHREADS, getHandlesFn);
242+
umf_test::parallel_exec(numThreads, getHandlesFn);
242243

243244
auto putHandlesFn = [&ipcHandles, &syncthreads](size_t tid) {
244245
syncthreads();
@@ -248,7 +249,7 @@ struct umfIpcTest : umf_test::test,
248249
}
249250
};
250251

251-
umf_test::parallel_exec(NTHREADS, putHandlesFn);
252+
umf_test::parallel_exec(numThreads, putHandlesFn);
252253

253254
for (void *ptr : ptrs) {
254255
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -272,7 +273,7 @@ struct umfIpcTest : umf_test::test,
272273
ptrs.push_back(ptr);
273274
}
274275

275-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
276+
umf_test::syncthreads_barrier syncthreads(numThreads);
276277

277278
auto getPutHandlesFn = [shuffle, &ptrs, &syncthreads](size_t) {
278279
// Each thread gets a copy of the pointers to shuffle them
@@ -294,7 +295,7 @@ struct umfIpcTest : umf_test::test,
294295
}
295296
};
296297

297-
umf_test::parallel_exec(NTHREADS, getPutHandlesFn);
298+
umf_test::parallel_exec(numThreads, getPutHandlesFn);
298299

299300
for (void *ptr : ptrs) {
300301
umf_result_t ret = umfPoolFree(pool.get(), ptr);
@@ -328,13 +329,13 @@ struct umfIpcTest : umf_test::test,
328329
ipcHandles.push_back(ipcHandle);
329330
}
330331

331-
std::array<std::vector<void *>, NTHREADS> openedIpcHandles;
332+
std::vector<std::vector<umf_ipc_handle_t>> openedIpcHandles(numThreads);
332333
umf_ipc_handler_handle_t ipcHandler = nullptr;
333334
ret = umfPoolGetIPCHandler(pool.get(), &ipcHandler);
334335
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
335336
ASSERT_NE(ipcHandler, nullptr);
336337

337-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
338+
umf_test::syncthreads_barrier syncthreads(numThreads);
338339

339340
auto openHandlesFn = [shuffle, &ipcHandles, &openedIpcHandles,
340341
&syncthreads, ipcHandler](size_t tid) {
@@ -351,11 +352,11 @@ struct umfIpcTest : umf_test::test,
351352
umf_result_t ret =
352353
umfOpenIPCHandle(ipcHandler, ipcHandle, &ptr);
353354
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
354-
openedIpcHandles[tid].push_back(ptr);
355+
openedIpcHandles[tid].push_back((umf_ipc_data_t *)ptr);
355356
}
356357
};
357358

358-
umf_test::parallel_exec(NTHREADS, openHandlesFn);
359+
umf_test::parallel_exec(numThreads, openHandlesFn);
359360

360361
auto closeHandlesFn = [&openedIpcHandles, &syncthreads](size_t tid) {
361362
syncthreads();
@@ -365,7 +366,7 @@ struct umfIpcTest : umf_test::test,
365366
}
366367
};
367368

368-
umf_test::parallel_exec(NTHREADS, closeHandlesFn);
369+
umf_test::parallel_exec(numThreads, closeHandlesFn);
369370

370371
for (auto ipcHandle : ipcHandles) {
371372
ret = umfPutIPCHandle(ipcHandle);
@@ -412,7 +413,7 @@ struct umfIpcTest : umf_test::test,
412413
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
413414
ASSERT_NE(ipcHandler, nullptr);
414415

415-
umf_test::syncthreads_barrier syncthreads(NTHREADS);
416+
umf_test::syncthreads_barrier syncthreads(numThreads);
416417

417418
auto openCloseHandlesFn = [shuffle, &ipcHandles, &syncthreads,
418419
ipcHandler](size_t) {
@@ -434,7 +435,7 @@ struct umfIpcTest : umf_test::test,
434435
}
435436
};
436437

437-
umf_test::parallel_exec(NTHREADS, openCloseHandlesFn);
438+
umf_test::parallel_exec(numThreads, openCloseHandlesFn);
438439

439440
for (auto ipcHandle : ipcHandles) {
440441
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>
@@ -81,13 +82,15 @@ struct umfPoolTest : umf_test::test,
8182
test::SetUp();
8283

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

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

8891
umf_test::pool_unique_handle_t pool;
8992

90-
static constexpr int NTHREADS = 5;
93+
int numThreads;
9194
static constexpr std::array<int, 7> nonAlignedAllocSizes = {5, 7, 23, 55,
9295
80, 119, 247};
9396
};
@@ -285,7 +288,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFree) {
285288
};
286289

287290
std::vector<std::thread> threads;
288-
for (int i = 0; i < NTHREADS; i++) {
291+
for (int i = 0; i < numThreads; i++) {
289292
threads.emplace_back(poolMalloc, allocSize, pool.get());
290293
}
291294

@@ -304,7 +307,7 @@ TEST_P(umfPoolTest, multiThreadedpow2AlignedAlloc) {
304307
};
305308

306309
std::vector<std::thread> threads;
307-
for (int i = 0; i < NTHREADS; i++) {
310+
for (int i = 0; i < numThreads; i++) {
308311
threads.emplace_back(poolpow2AlignedAlloc, pool.get());
309312
}
310313

@@ -339,7 +342,7 @@ TEST_P(umfPoolTest, multiThreadedReallocFree) {
339342
};
340343

341344
std::vector<std::thread> threads;
342-
for (int i = 0; i < NTHREADS; i++) {
345+
for (int i = 0; i < numThreads; i++) {
343346
threads.emplace_back(poolRealloc, allocSize, multiplier, pool.get());
344347
}
345348

@@ -370,7 +373,7 @@ TEST_P(umfPoolTest, multiThreadedCallocFree) {
370373
};
371374

372375
std::vector<std::thread> threads;
373-
for (int i = 0; i < NTHREADS; i++) {
376+
for (int i = 0; i < numThreads; i++) {
374377
threads.emplace_back(poolCalloc, num, sizeof(int), pool.get());
375378
}
376379

@@ -396,7 +399,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
396399
};
397400

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

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)