Skip to content

Commit 2989aa8

Browse files
authored
Merge pull request #4 from Compaile/gcc_clang_tbb_fix
tbb gcc clang fix and added fallback
2 parents 61510e9 + 1d20ad1 commit 2989aa8

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CTRACK is a powerful tool that can be seamlessly integrated into both developmen
77
## Features
88

99
- Single header file
10-
- No dependencies
10+
- No dependencies (optional tbb for non msvc to use paralell result calculation)
1111
- Easy to use (just 1 line per function you want to track)
1212
- Minimal overhead (can record tens of millions events per second)
1313
- Optimized for multi-threaded environments
@@ -168,6 +168,9 @@ CTRACK is a header-only library, which means you can start using it by simply in
168168

169169
This method is straightforward and doesn't require any additional setup or build process.
170170

171+
Note: If you are using a compiler which needs TBB for C++ standard parallel algorithms, you need to link to -ltbb. You can always fall back to sequential result calculation by setting
172+
CTRACK_DISABLE_EXECUTION_POLICY. The recording will be unchanged, but the printing/calculating of the stats will be a bit slower.
173+
171174
### 2. CMake Package
172175

173176
For projects using CMake, CTRACK can be installed and used as a CMake package. This method provides better integration with your build system and makes it easier to manage dependencies.
@@ -191,6 +194,11 @@ To use CTRACK as a CMake package:
191194
target_link_libraries(your_target PRIVATE CTRACK::CTRACK)
192195
```
193196

197+
Note: If you are using a compiler which needs TBB for C++ standard parallel algorithms, you need to link to tbb.
198+
```target_link_libraries( your_target PRIVATE TBB::tbb ) ```
199+
You can always fall back to sequential result calculation by setting
200+
CTRACK_DISABLE_EXECUTION_POLICY. The recording will be unchanged, but the printing/calculating of the stats will be a bit slower.
201+
194202
For more detailed examples of how to use CTRACK with CMake, please refer to the `examples` directory in the CTRACK repository.
195203

196204
Choose the installation method that best fits your project's needs and structure. Both methods provide full access to CTRACK's features and capabilities.

examples/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
option(DISABLE_PAR "Disable parallel processing" OFF)
2+
if(NOT MSVC AND NOT DISABLE_PAR)
3+
# For other compilers, check for TBB
4+
find_package(TBB QUIET)
5+
if(TBB_FOUND)
6+
message(STATUS "TBB found. Enabling parallel execution.")
7+
else()
8+
message(STATUS "TBB not found. Disabling parallel execution.")
9+
set(DISABLE_PAR ON)
10+
endif()
11+
elseif(DISABLE_PAR)
12+
message(STATUS "DISABLE_PAR set. Disabling parallel execution.")
13+
endif()
14+
15+
16+
# Function to enable parallel execution on non msvc
17+
function(enable_parallel_execution target)
18+
if(DISABLE_PAR)
19+
target_compile_definitions(${target} PRIVATE CTRACK_DISABLE_EXECUTION_POLICY)
20+
elseif(NOT MSVC)
21+
target_link_libraries(${target} PRIVATE TBB::tbb)
22+
endif()
23+
endfunction()
24+
125
# Create executables for each example
226
add_executable(basic_singlethreaded basic_singlethreaded.cpp)
327
add_executable(multithreaded_prime_counter multithreaded_prime_counter.cpp)
428
add_executable(ctrack_overhead_test ctrack_overhead_test.cpp)
529
add_executable(high_variance_pi_estimation high_variance_pi_estimation.cpp)
630
add_executable(complex_multithreaded_puzzle complex_multithreaded_puzzle.cpp)
731

32+
#check for paralell
33+
enable_parallel_execution(basic_singlethreaded)
34+
enable_parallel_execution(multithreaded_prime_counter)
35+
enable_parallel_execution(ctrack_overhead_test)
36+
enable_parallel_execution(high_variance_pi_estimation)
37+
enable_parallel_execution(complex_multithreaded_puzzle)
38+
839
# Link the ctrack library to each example
940
target_link_libraries(basic_singlethreaded PRIVATE ctrack)
1041
target_link_libraries(multithreaded_prime_counter PRIVATE ctrack)

include/ctrack.hpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717
#include <algorithm>
1818
#include <set>
1919
#include <numeric>
20+
#ifndef CTRACK_DISABLE_EXECUTION_POLICY
2021
#include <execution>
22+
#endif
2123
#include <vector>
2224
#include <iomanip>
2325
#include <fstream>
2426
#include <filesystem>
2527
#include <sstream>
28+
#include <atomic>
29+
#include <cmath>
2630

2731
#define CTRACK_VERSION_MAJOR 1
2832
#define CTRACK_VERSION_MINOR 0
29-
#define CTRACK_VERSION_PATCH 0
33+
#define CTRACK_VERSION_PATCH 1
3034

3135
// Helper macro to convert a numeric value to a string
3236
#define STRINGIFY(x) #x
@@ -44,14 +48,18 @@
4448
namespace ctrack {
4549

4650
inline namespace CTRACK_VERSION_NAMESPACE {
47-
51+
#ifndef CTRACK_DISABLE_EXECUTION_POLICY
4852
constexpr auto execution_policy = std::execution::par_unseq;
53+
#define OPT_EXEC_POLICY execution_policy,
54+
#else
55+
#define OPT_EXEC_POLICY
56+
#endif
4957

5058
template<typename T, typename Field>
5159
auto sum_field(const std::vector<T>& vec, Field T::* field) {
5260
using FieldType = std::decay_t<decltype(std::declval<T>().*field)>;
5361
return std::transform_reduce(
54-
execution_policy,
62+
OPT_EXEC_POLICY
5563
vec.begin(), vec.end(),
5664
FieldType{},
5765
std::plus<>(),
@@ -63,7 +71,7 @@ namespace ctrack {
6371
auto sum_squared_field(const std::vector<T>& values, Field T::* field) {
6472
using FieldType = std::decay_t<decltype(std::declval<T>().*field)>;
6573
return std::transform_reduce(
66-
execution_policy,
74+
OPT_EXEC_POLICY
6775
values.begin(), values.end(),
6876
FieldType{},
6977
std::plus<>(),
@@ -76,7 +84,7 @@ namespace ctrack {
7684
template<typename T, typename Field>
7785
double calculate_std_dev_field(std::vector<T>& values, Field T::* field, const double mean) {
7886
double res = std::transform_reduce(
79-
execution_policy,
87+
OPT_EXEC_POLICY
8088
values.begin(), values.end(),
8189
0.0,
8290
std::plus<>(),
@@ -105,7 +113,7 @@ namespace ctrack {
105113

106114
template <typename StructType, typename MemberType>
107115
void order_pointer_vector_by_field(std::vector<StructType*>& vec, MemberType StructType::* member, bool asc = true) {
108-
std::sort(execution_policy, vec.begin(), vec.end(),
116+
std::sort(OPT_EXEC_POLICY vec.begin(), vec.end(),
109117
[member, asc](const StructType* a, const StructType* b) {
110118
if (asc)
111119
return (a->*member) < (b->*member);
@@ -117,7 +125,7 @@ namespace ctrack {
117125
template <typename T>
118126
size_t countAllEvents(const std::deque<std::vector<T>>& events) {
119127
return std::transform_reduce(
120-
execution_policy,
128+
OPT_EXEC_POLICY
121129
events.begin(), events.end(),
122130
size_t(0),
123131
std::plus<>(),
@@ -393,7 +401,7 @@ namespace ctrack {
393401
std::vector<Simple_Event> simple_events{};
394402
simple_events.resize(events.size());
395403
std::transform(
396-
execution_policy,
404+
OPT_EXEC_POLICY
397405
events.begin(), events.end(),
398406
simple_events.begin(),
399407
[](const Event& event) {
@@ -409,7 +417,7 @@ namespace ctrack {
409417
std::vector<Simple_Event> simple_events{};
410418
simple_events.resize(events.size());
411419
std::transform(
412-
execution_policy,
420+
OPT_EXEC_POLICY
413421
events.begin(), events.end(),
414422
simple_events.begin(),
415423
[](const Event* event) {
@@ -482,7 +490,7 @@ namespace ctrack {
482490
return;
483491

484492
auto all_events_simple = create_simple_events(all_events);
485-
std::sort(execution_policy, all_events_simple.begin(), all_events_simple.end(), cmp_simple_event_by_duration_asc);
493+
std::sort(OPT_EXEC_POLICY all_events_simple.begin(), all_events_simple.end(), cmp_simple_event_by_duration_asc);
486494
all_cnt = static_cast<unsigned int> (all_events_simple.size());
487495
const double factor = (1.0 / static_cast<double>(all_cnt));
488496

@@ -540,19 +548,19 @@ namespace ctrack {
540548

541549
auto center_child_events_simple = load_child_events_simple(center_events_simple, events_map, child_graph);
542550

543-
std::sort(execution_policy, center_events_simple.begin(), center_events_simple.end(), cmp_simple_event_by_start_time_asc);
551+
std::sort(OPT_EXEC_POLICY center_events_simple.begin(), center_events_simple.end(), cmp_simple_event_by_start_time_asc);
544552
center_grouped = sorted_create_grouped_simple_events(center_events_simple);
545553
center_time_active = sum_field(center_grouped, &Simple_Event::duration);
546554

547-
std::sort(execution_policy, center_child_events_simple.begin(), center_child_events_simple.end(), cmp_simple_event_by_start_time_asc);
555+
std::sort(OPT_EXEC_POLICY center_child_events_simple.begin(), center_child_events_simple.end(), cmp_simple_event_by_start_time_asc);
548556
auto center_child_events_grouped = sorted_create_grouped_simple_events(center_child_events_simple);
549557
center_time_active_exclusive = center_time_active - sum_field(center_child_events_grouped, &Simple_Event::duration);
550558

551-
std::sort(execution_policy, all_events_simple.begin(), all_events_simple.end(), cmp_simple_event_by_start_time_asc);
559+
std::sort(OPT_EXEC_POLICY all_events_simple.begin(), all_events_simple.end(), cmp_simple_event_by_start_time_asc);
552560
all_grouped = sorted_create_grouped_simple_events(all_events_simple);
553561
all_time_active = sum_field(all_grouped, &Simple_Event::duration);
554562

555-
std::sort(execution_policy, all_child_events_simple.begin(), all_child_events_simple.end(), cmp_simple_event_by_start_time_asc);
563+
std::sort(OPT_EXEC_POLICY all_child_events_simple.begin(), all_child_events_simple.end(), cmp_simple_event_by_start_time_asc);
556564
auto all_child_events_grouped = sorted_create_grouped_simple_events(all_child_events_simple);
557565
all_time_active_exclusive = all_time_active - sum_field(all_child_events_grouped, &Simple_Event::duration);
558566
}
@@ -721,7 +729,7 @@ namespace ctrack {
721729
}
722730
}
723731

724-
std::sort(execution_policy, grouped_events.begin(), grouped_events.end(), cmp_simple_event_by_start_time_asc);
732+
std::sort(OPT_EXEC_POLICY grouped_events.begin(), grouped_events.end(), cmp_simple_event_by_start_time_asc);
725733
auto all_grouped = sorted_create_grouped_simple_events(grouped_events);
726734
sum_time_active_exclusive = sum_field(all_grouped, &Simple_Event::duration);
727735

0 commit comments

Comments
 (0)