Skip to content

Commit ed79eed

Browse files
authored
[XPTI] Additional TBB dependency removal (#10486)
Semantic and correctness testing of the XPTI framework relies on std::for_each parallel execution policy. However, this adds an install dependency of the TBB development package. To remove this inconvenience, the semantic tests that used std::for_each have been modified to use the same thread pool functionality adopted by the performance tests. --------- Signed-off-by: Vasanth Tovinkere <[email protected]>
1 parent 07c60c8 commit ed79eed

File tree

4 files changed

+150
-134
lines changed

4 files changed

+150
-134
lines changed

xptifw/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ if (NOT DEFINED XPTI_DIR) # don't overwrite if already set
1818
endif()
1919
endif()
2020

21-
# Create a soft option for enabling the use of TBB
22-
option(XPTI_ENABLE_TBB "Enable TBB in the framework" OFF)
23-
2421
option(XPTI_ENABLE_WERROR OFF)
2522
option(XPTI_BUILD_SAMPLES OFF)
2623
option(XPTI_BUILD_BENCHMARK OFF)

xptifw/basic_test/parallel_test.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//
8+
#pragma once
9+
#include "external/BS_thread_pool.hpp"
10+
11+
// Macro to parallelize a loop.
12+
#define PARALLEL_FOR(tp, func, lower, upper) \
13+
{ \
14+
int NumWorkers = tp.get_thread_count(); \
15+
int Step = (upper - lower + 1) / NumWorkers; \
16+
for (int i = 0; i < NumWorkers; ++i) { \
17+
int Min = lower + i * Step; \
18+
int Max = std::min<int>((lower + (i + 1) * Step), upper); \
19+
auto Ret = tp.submit(func, Min, Max); \
20+
} \
21+
tp.wait_for_tasks(); \
22+
}

xptifw/basic_test/performance_tests.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,14 @@
1616
//
1717
// Copyright (c) 2023 Barak Shoshany. Licensed under the MIT license.
1818
#include "cl_processor.hpp"
19-
#include "external/BS_thread_pool.hpp"
19+
#include "parallel_test.h"
2020
#include "xpti/xpti_trace_framework.h"
2121

2222
#include <algorithm>
2323
#include <atomic>
2424
#include <chrono>
2525
#include <random>
2626

27-
// Macro to parallelize a loop.
28-
#define PARALLEL_FOR(tp, func, lower, upper) \
29-
{ \
30-
int NumWorkers = tp.get_thread_count(); \
31-
int Step = (upper - lower + 1) / NumWorkers; \
32-
for (int i = 0; i < NumWorkers; ++i) { \
33-
int Min = lower + i * Step; \
34-
int Max = std::min<int>((lower + (i + 1) * Step), upper); \
35-
auto Ret = tp.submit(func, Min, Max); \
36-
} \
37-
tp.wait_for_tasks(); \
38-
}
39-
4027
namespace test {
4128
void registerCallbacks(uint8_t sid);
4229
namespace performance {

xptifw/basic_test/semantic_tests.cpp

Lines changed: 127 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// expected results.
1111
//--------------------------------------------------------------------------
1212
#include "cl_processor.hpp"
13+
#include "parallel_test.h"
1314
#include "xpti/xpti_trace_framework.h"
1415

1516
#include <atomic>
@@ -125,6 +126,7 @@ void TestCorrectness::runStringTableTestThreads(
125126
(double)(Strings.size() + LookupCount + DuplicateCount) /
126127
(NumStrings * 3) * 100;
127128
} else { // Multi-threaded run
129+
BS::thread_pool tp(NumThreads);
128130
std::vector<char *> Strings;
129131
std::vector<xpti::string_id_t> IDs;
130132
Strings.resize(NumStrings);
@@ -139,36 +141,39 @@ void TestCorrectness::runStringTableTestThreads(
139141

140142
{
141143
int Count = 0;
142-
std::vector<int> MRange(NumStrings, 0);
143-
std::for_each(MRange.begin(), MRange.end(),
144-
[&](int &n) { n += Count++; });
145-
146-
std::for_each(
147-
std::execution::par, MRange.begin(), MRange.end(), [&](int &i) {
148-
char *TableStrRef = nullptr;
149-
std::string StrName = "Function" + std::to_string(i);
150-
IDs[i] = xptiRegisterString(StrName.c_str(), &TableStrRef);
151-
Strings[i] = TableStrRef;
152-
});
153-
154-
std::for_each(std::execution::par, MRange.begin(), MRange.end(),
155-
[&](int &i) {
156-
const char *TableStrRef = xptiLookupString(IDs[i]);
157-
if (TableStrRef == Strings[i])
158-
++LookupCount;
159-
});
144+
145+
auto RegisterString = [&](int min, int max) {
146+
for (int i = min; i < max; ++i) {
147+
char *TableStrRef = nullptr;
148+
std::string StrName = "Function" + std::to_string(i);
149+
IDs[i] = xptiRegisterString(StrName.c_str(), &TableStrRef);
150+
Strings[i] = TableStrRef;
151+
}
152+
};
153+
PARALLEL_FOR(tp, RegisterString, 0, NumStrings);
154+
155+
auto LookupString = [&](int min, int max) {
156+
for (int i = min; i < max; ++i) {
157+
const char *TableStrRef = xptiLookupString(IDs[i]);
158+
if (TableStrRef == Strings[i])
159+
++LookupCount;
160+
}
161+
};
162+
PARALLEL_FOR(tp, LookupString, 0, NumStrings);
160163
ModelRow[(int)STColumns::Lookups] = LookupCount;
161164

162-
std::for_each(std::execution::par, MRange.begin(), MRange.end(),
163-
[&](int &i) {
164-
char *TableStrRef = nullptr;
165-
std::string StrName = "Function" + std::to_string(i);
166-
xpti::string_id_t id =
167-
xptiRegisterString(StrName.c_str(), &TableStrRef);
168-
if (StrName == TableStrRef && id == IDs[i] &&
169-
TableStrRef == Strings[i])
170-
++DuplicateCount;
171-
});
165+
auto CheckLookup = [&](int min, int max) {
166+
for (int i = min; i < max; ++i) {
167+
char *TableStrRef = nullptr;
168+
std::string StrName = "Function" + std::to_string(i);
169+
xpti::string_id_t id =
170+
xptiRegisterString(StrName.c_str(), &TableStrRef);
171+
if (StrName == TableStrRef && id == IDs[i] &&
172+
TableStrRef == Strings[i])
173+
++DuplicateCount;
174+
}
175+
};
176+
PARALLEL_FOR(tp, CheckLookup, 0, NumStrings);
172177
ModelRow[(int)STColumns::DuplicateInserts] = DuplicateCount;
173178

174179
ModelRow[(int)STColumns::PassRate] =
@@ -266,10 +271,8 @@ void TestCorrectness::runTracepointTestThreads(int RunNo, int NumThreads,
266271
(TracepointCount * 4) * 100;
267272
} else {
268273

274+
BS::thread_pool tp(NumThreads);
269275
int Count = 0;
270-
std::vector<size_t> MRange(TracepointCount, 0);
271-
std::for_each(MRange.begin(), MRange.end(),
272-
[&](size_t &n) { n += Count++; });
273276

274277
std::vector<xpti::payload_t *> Payloads;
275278
std::vector<int64_t> UIds;
@@ -285,51 +288,57 @@ void TestCorrectness::runTracepointTestThreads(int RunNo, int NumThreads,
285288
std::atomic<int> LookupCount = {0}, DuplicateCount = {0},
286289
PayloadCount = {0};
287290

288-
std::for_each(
289-
std::execution::par, MRange.begin(), MRange.end(), [&](size_t &i) {
290-
std::string fn = "Function" + std::to_string(i);
291-
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)i,
292-
(int)i % 80, (void *)i);
293-
xpti::trace_event_data_t *Ev = xptiMakeEvent(
294-
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
295-
xpti::trace_activity_type_t::active, &MInstanceID);
296-
if (Ev) {
297-
UIds[i] = Ev->unique_id;
298-
Payloads[i] = Ev->reserved.payload;
299-
Events[i] = Ev;
300-
}
301-
});
291+
auto MakeEvent = [&](int min, int max) {
292+
for (size_t i = min; i < max; ++i) {
293+
std::string fn = "Function" + std::to_string(i);
294+
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)i,
295+
(int)i % 80, (void *)i);
296+
xpti::trace_event_data_t *Ev = xptiMakeEvent(
297+
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
298+
xpti::trace_activity_type_t::active, &MInstanceID);
299+
if (Ev) {
300+
UIds[i] = Ev->unique_id;
301+
Payloads[i] = Ev->reserved.payload;
302+
Events[i] = Ev;
303+
}
304+
}
305+
};
306+
PARALLEL_FOR(tp, MakeEvent, 0, TracepointCount);
302307
ModelRow[(int)TPColumns::Insertions] = (long double)Events.size();
303308

304-
std::for_each(std::execution::par, MRange.begin(), MRange.end(),
305-
[&](size_t &i) {
306-
std::string fn = "Function" + std::to_string(i);
307-
const xpti::trace_event_data_t *Ev = xptiFindEvent(UIds[i]);
308-
if (Ev && Ev->unique_id == UIds[i])
309-
LookupCount++;
310-
});
309+
auto EventLookup = [&](int min, int max) {
310+
for (size_t i = min; i < max; ++i) {
311+
std::string fn = "Function" + std::to_string(i);
312+
const xpti::trace_event_data_t *Ev = xptiFindEvent(UIds[i]);
313+
if (Ev && Ev->unique_id == UIds[i])
314+
LookupCount++;
315+
}
316+
};
317+
PARALLEL_FOR(tp, EventLookup, 0, TracepointCount);
311318
ModelRow[(int)TPColumns::Lookups] = LookupCount;
312319

313-
std::for_each(
314-
std::execution::par, MRange.begin(), MRange.end(), [&](size_t &i) {
315-
std::string fn = "Function" + std::to_string(i);
316-
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)i,
317-
(int)i % 80, (void *)i);
318-
xpti::trace_event_data_t *Ev = xptiMakeEvent(
319-
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
320-
xpti::trace_activity_type_t::active, &MInstanceID);
321-
if (Ev) {
322-
if (Ev->unique_id == UIds[i]) {
323-
++DuplicateCount;
324-
}
325-
xpti::payload_t *RP = Ev->reserved.payload;
326-
if (Ev->unique_id == UIds[i] && RP &&
327-
std::string(RP->name) == std::string(P.name) &&
328-
std::string(RP->source_file) == std::string(P.source_file) &&
329-
RP->line_no == P.line_no && RP->column_no == P.column_no)
330-
++PayloadCount;
320+
auto CheckEvents = [&](int min, int max) {
321+
for (size_t i = min; i < max; ++i) {
322+
std::string fn = "Function" + std::to_string(i);
323+
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)i,
324+
(int)i % 80, (void *)i);
325+
xpti::trace_event_data_t *Ev = xptiMakeEvent(
326+
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
327+
xpti::trace_activity_type_t::active, &MInstanceID);
328+
if (Ev) {
329+
if (Ev->unique_id == UIds[i]) {
330+
++DuplicateCount;
331331
}
332-
});
332+
xpti::payload_t *RP = Ev->reserved.payload;
333+
if (Ev->unique_id == UIds[i] && RP &&
334+
std::string(RP->name) == std::string(P.name) &&
335+
std::string(RP->source_file) == std::string(P.source_file) &&
336+
RP->line_no == P.line_no && RP->column_no == P.column_no)
337+
++PayloadCount;
338+
}
339+
}
340+
};
341+
PARALLEL_FOR(tp, CheckEvents, 0, TracepointCount);
333342
ModelRow[(int)TPColumns::DuplicateInserts] = DuplicateCount;
334343
ModelRow[(int)TPColumns::PayloadLookup] = PayloadCount;
335344
ModelRow[(int)TPColumns::PassRate] =
@@ -426,60 +435,61 @@ void TestCorrectness::runNotificationTestThreads(
426435
ModelRow[(int)NColumns::Notifications] = (long double)Acc;
427436
ModelRow[(int)NColumns::PassRate] = (long double)(Acc) / (NotifyCount)*100;
428437
} else {
438+
439+
BS::thread_pool tp(NumThreads);
429440
std::atomic<int> NotifyCount = {0};
430441

431442
int Count = 0;
432-
std::vector<size_t> MRange(CallbackCount, 0);
433-
std::for_each(MRange.begin(), MRange.end(),
434-
[&](size_t &n) { n += Count++; });
435-
436-
std::for_each(std::execution::par_unseq, MRange.begin(), MRange.end(),
437-
[&](size_t &i) {
438-
if (i >= TPCount)
439-
return;
440-
441-
size_t Index = (int)i;
442-
std::string fn = "Function" + std::to_string(i);
443-
xpti::payload_t P =
444-
xpti::payload_t(fn.c_str(), MSource, (int)Index,
445-
(int)Index % 80, (void *)(i % 10));
446-
xpti::trace_event_data_t *Ev = xptiMakeEvent(
447-
fn.c_str(), &P,
448-
(uint16_t)xpti::trace_event_type_t::algorithm,
449-
xpti::trace_activity_type_t::active, &MInstanceID);
450-
if (Ev) {
451-
UIds[Index] = Ev->unique_id;
452-
Payloads[Index] = Ev->reserved.payload;
453-
Events[Index] = Ev;
454-
}
455-
++NotifyCount;
456-
});
443+
444+
auto MakeEvents = [&](int min, int max) {
445+
for (size_t i = min; i < max; ++i) {
446+
if (i >= TPCount)
447+
return;
448+
449+
size_t Index = (int)i;
450+
std::string fn = "Function" + std::to_string(i);
451+
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)Index,
452+
(int)Index % 80, (void *)(i % 10));
453+
xpti::trace_event_data_t *Ev = xptiMakeEvent(
454+
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
455+
xpti::trace_activity_type_t::active, &MInstanceID);
456+
if (Ev) {
457+
UIds[Index] = Ev->unique_id;
458+
Payloads[Index] = Ev->reserved.payload;
459+
Events[Index] = Ev;
460+
}
461+
++NotifyCount;
462+
}
463+
};
464+
PARALLEL_FOR(tp, MakeEvents, 0, CallbackCount);
457465

458466
std::string RowTitle = "Threads " + std::to_string(NumThreads);
459467
auto &ModelRow = Model.addRow(RunNo, RowTitle);
460468
ModelRow[(int)NColumns::Threads] = NumThreads;
461469

462-
std::for_each(
463-
std::execution::par, MRange.begin(), MRange.end(), [&](size_t &i) {
464-
if (i < TPCount)
465-
return;
466-
467-
size_t Index = (int)i % TPCount;
468-
void *Address = (void *)(Index % 10);
469-
std::string fn = "Function" + std::to_string(Index);
470-
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)Index,
471-
(int)Index % 80, Address);
472-
xpti::trace_event_data_t *Ev = xptiMakeEvent(
473-
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
474-
xpti::trace_activity_type_t::active, &MInstanceID);
475-
if (Ev && Ev->unique_id == UIds[Index]) {
476-
uint8_t TP = (Index % 10) + 1;
477-
uint16_t TPType = (uint16_t)(TP << 1);
478-
xpti::framework::scoped_notify ev("xpti", TPType, nullptr, Ev,
479-
MInstanceID, nullptr);
480-
NotifyCount++;
481-
}
482-
});
470+
auto NotifyCallbacks = [&](int min, int max) {
471+
for (size_t i = min; i < max; ++i) {
472+
if (i < TPCount)
473+
return;
474+
475+
size_t Index = (int)i % TPCount;
476+
void *Address = (void *)(Index % 10);
477+
std::string fn = "Function" + std::to_string(Index);
478+
xpti::payload_t P = xpti::payload_t(fn.c_str(), MSource, (int)Index,
479+
(int)Index % 80, Address);
480+
xpti::trace_event_data_t *Ev = xptiMakeEvent(
481+
fn.c_str(), &P, (uint16_t)xpti::trace_event_type_t::algorithm,
482+
xpti::trace_activity_type_t::active, &MInstanceID);
483+
if (Ev && Ev->unique_id == UIds[Index]) {
484+
uint8_t TP = (Index % 10) + 1;
485+
uint16_t TPType = (uint16_t)(TP << 1);
486+
xpti::framework::scoped_notify ev("xpti", TPType, nullptr, Ev,
487+
MInstanceID, nullptr);
488+
NotifyCount++;
489+
}
490+
}
491+
};
492+
PARALLEL_FOR(tp, NotifyCallbacks, 0, CallbackCount);
483493

484494
uint64_t Acc = 0;
485495
for (int i = 0; i < TPCount; ++i) {

0 commit comments

Comments
 (0)