Skip to content

Commit d30ca08

Browse files
space-time-stack: allow demangled names
1 parent 0becae4 commit d30ca08

File tree

11 files changed

+224
-57
lines changed

11 files changed

+224
-57
lines changed

CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ option(KokkosTools_ENABLE_MPI "Enable MPI support" OFF)
4545
option(KokkosTools_ENABLE_CALIPER "Enable building Caliper library" OFF)
4646
option(KokkosTools_ENABLE_APEX "Enable building Apex library" OFF)
4747
option(KokkosTools_ENABLE_EXAMPLES "Build examples" OFF)
48+
option(KokkosTools_ENABLE_TESTS "Enable tests" OFF)
49+
4850
# Advanced settings
4951
option(KokkosTools_REUSE_KOKKOS_COMPILER "Set the compiler and flags based on installed Kokkos settings" OFF)
5052
mark_as_advanced(KokkosTools_REUSE_KOKKOS_COMPILER)
@@ -116,6 +118,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/profiling/all)
116118
set(COMMON_HEADERS_PATH ${CMAKE_CURRENT_BINARY_DIR}/common)
117119
include_directories(${COMMON_HEADERS_PATH})
118120

121+
# Allow all tools to include any file.
122+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
123+
119124
set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE)
120125

121126
# Export settings
@@ -264,6 +269,12 @@ if(KokkosTools_ENABLE_EXAMPLES)
264269
endif()
265270
endif()
266271

272+
# Tests
273+
if(KokkosTools_ENABLE_TESTS)
274+
enable_testing()
275+
add_subdirectory(tests)
276+
endif()
277+
267278
# Install exports
268279
install(TARGETS ${EXPORT_TARGETS} EXPORT ${EXPORT_NAME})
269280
install(EXPORT ${EXPORT_NAME}

CMakePresets.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"KokkosTools_ENABLE_EXAMPLES" : "ON",
1111
"KokkosTools_ENABLE_SINGLE" : "ON",
1212
"KokkosTools_ENABLE_MPI" : "ON",
13-
"KokkosTools_ENABLE_PAPI" : "ON"
13+
"KokkosTools_ENABLE_PAPI" : "ON",
14+
"KokkosTools_ENABLE_TESTS" : "ON"
1415
}
1516
},
1617
{

common/utils/demangle.hpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//@HEADER
2+
// ************************************************************************
3+
//
4+
// Kokkos v. 4.0
5+
// Copyright (2022) National Technology & Engineering
6+
// Solutions of Sandia, LLC (NTESS).
7+
//
8+
// Under the terms of Contract DE-NA0003525 with NTESS,
9+
// the U.S. Government retains certain rights in this software.
10+
//
11+
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12+
// See https://kokkos.org/LICENSE for license information.
13+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14+
//
15+
//@HEADER
16+
17+
#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
18+
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
19+
20+
#include <string>
21+
22+
#if defined(__GXX_ABI_VERSION)
23+
#define HAVE_GCC_ABI_DEMANGLE
24+
#endif
25+
26+
#if defined(HAVE_GCC_ABI_DEMANGLE)
27+
#include <cxxabi.h>
28+
#endif // HAVE_GCC_ABI_DEMANGLE
29+
30+
namespace KokkosTools {
31+
32+
//! Demangle @p mangled_name.
33+
inline std::string demangleName(const std::string_view& mangled_name) {
34+
#if defined(HAVE_GCC_ABI_DEMANGLE)
35+
int status = 0;
36+
37+
char* demangled_name =
38+
abi::__cxa_demangle(mangled_name.data(), nullptr, nullptr, &status);
39+
40+
if (demangled_name) {
41+
std::string ret(demangled_name);
42+
std::free(demangled_name);
43+
return ret;
44+
}
45+
#endif
46+
return std::string(mangled_name);
47+
}
48+
49+
/**
50+
* @brief Demangle @p mangled_name.
51+
*
52+
* This function supports @c Kokkos convention from
53+
* @c Kokkos::Impl::ParallelConstructName.
54+
*
55+
* For instance, a kernel launched with a tag would appear as
56+
* "<functor type>/<tag type>".
57+
*/
58+
inline std::string demangleNameKokkos(const std::string_view& mangled_name) {
59+
if (size_t pos = mangled_name.find('/', 0);
60+
pos != std::string_view::npos && pos > 0) {
61+
/// An explicit copy of the first part of the string is needed, because
62+
/// @c abi::__cxa_demangle will parse the pointer until its NULL-terminated.
63+
return demangleName(std::string(mangled_name.substr(0, pos)))
64+
.append("/")
65+
.append(
66+
demangleName(mangled_name.substr(pos + 1, mangled_name.size())));
67+
} else {
68+
return demangleName(mangled_name);
69+
}
70+
}
71+
72+
} // namespace KokkosTools
73+
74+
#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP

profiling/simple-kernel-timer/kp_json_writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char* argv[]) {
8383
KernelPerformanceInfo* new_kernel =
8484
new KernelPerformanceInfo("", PARALLEL_FOR);
8585
if (new_kernel->readFromFile(the_file)) {
86-
if (strlen(new_kernel->getName()) > 0) {
86+
if (!new_kernel->getName().empty()) {
8787
int kernelIndex = find_index(kernelInfo, new_kernel->getName());
8888

8989
if (kernelIndex > -1) {

profiling/simple-kernel-timer/kp_kernel_info.h

+13-46
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,10 @@
2222
#include <string>
2323
#include <cstring>
2424

25-
#if defined(__GXX_ABI_VERSION)
26-
#define HAVE_GCC_ABI_DEMANGLE
27-
#endif
28-
29-
#if defined(HAVE_GCC_ABI_DEMANGLE)
30-
#include <cxxabi.h>
31-
#endif // HAVE_GCC_ABI_DEMANGLE
25+
#include "common/utils/demangle.hpp"
3226

3327
namespace KokkosTools::KernelTimer {
3428

35-
inline char* demangleName(char* kernelName) {
36-
#if defined(HAVE_GCC_ABI_DEMANGLE)
37-
int status = -1;
38-
char* demangledKernelName =
39-
abi::__cxa_demangle(kernelName, NULL, NULL, &status);
40-
if (status == 0) {
41-
free(kernelName);
42-
kernelName = demangledKernelName;
43-
}
44-
#endif // HAVE_GCC_ABI_DEMANGLE
45-
return kernelName;
46-
}
47-
4829
inline double seconds() {
4930
struct timeval now;
5031
gettimeofday(&now, NULL);
@@ -62,15 +43,7 @@ enum KernelExecutionType {
6243
class KernelPerformanceInfo {
6344
public:
6445
KernelPerformanceInfo(std::string kName, KernelExecutionType kernelType)
65-
: kType(kernelType) {
66-
kernelName = (char*)malloc(sizeof(char) * (kName.size() + 1));
67-
strcpy(kernelName, kName.c_str());
68-
69-
callCount = 0;
70-
time = 0;
71-
}
72-
73-
~KernelPerformanceInfo() { free(kernelName); }
46+
: kernelName(std::move(kName)), kType(kernelType) {}
7447

7548
KernelExecutionType getKernelType() const { return kType; }
7649

@@ -95,7 +68,7 @@ class KernelPerformanceInfo {
9568

9669
double getTimeSq() { return timeSq; }
9770

98-
char* getName() const { return kernelName; }
71+
const std::string& getName() const { return kernelName; }
9972

10073
void addCallCount(const uint64_t newCalls) { callCount += newCalls; }
10174

@@ -112,15 +85,9 @@ class KernelPerformanceInfo {
11285
copy((char*)&kernelNameLength, &entry[nextIndex], sizeof(kernelNameLength));
11386
nextIndex += sizeof(kernelNameLength);
11487

115-
if (strlen(kernelName) > 0) {
116-
free(kernelName);
117-
}
118-
119-
kernelName = (char*)malloc(sizeof(char) * (kernelNameLength + 1));
120-
copy(kernelName, &entry[nextIndex], kernelNameLength);
121-
kernelName[kernelNameLength] = '\0';
88+
this->kernelName = std::string(&entry[nextIndex], kernelNameLength);
12289

123-
kernelName = demangleName(kernelName);
90+
kernelName = demangleNameKokkos(kernelName);
12491

12592
nextIndex += kernelNameLength;
12693

@@ -152,7 +119,7 @@ class KernelPerformanceInfo {
152119
}
153120

154121
void writeToBinaryFile(FILE* output) {
155-
const uint32_t kernelNameLen = (uint32_t)strlen(kernelName);
122+
const uint32_t kernelNameLen = kernelName.size();
156123
const uint32_t recordLen = sizeof(uint32_t) + sizeof(char) * kernelNameLen +
157124
sizeof(uint64_t) + sizeof(double) +
158125
sizeof(double) + sizeof(uint32_t);
@@ -163,7 +130,7 @@ class KernelPerformanceInfo {
163130
copy(&entry[nextIndex], (char*)&kernelNameLen, sizeof(kernelNameLen));
164131
nextIndex += sizeof(kernelNameLen);
165132

166-
copy(&entry[nextIndex], kernelName, kernelNameLen);
133+
copy(&entry[nextIndex], kernelName.c_str(), kernelNameLen);
167134
nextIndex += kernelNameLen;
168135

169136
copy(&entry[nextIndex], (char*)&callCount, sizeof(callCount));
@@ -191,7 +158,7 @@ class KernelPerformanceInfo {
191158
snprintf(indentBuffer, 256, "%s ", indent);
192159

193160
fprintf(output, "%s\"kernel-name\" : \"%s\",\n", indentBuffer,
194-
kernelName);
161+
kernelName.c_str());
195162
// fprintf(output, "%s\"region\" : \"%s\",\n", indentBuffer,
196163
// regionName);
197164
fprintf(output, "%s\"call-count\" : %llu,\n", indentBuffer,
@@ -216,12 +183,12 @@ class KernelPerformanceInfo {
216183
}
217184
}
218185

219-
char* kernelName;
186+
std::string kernelName;
220187
// const char* regionName;
221-
uint64_t callCount;
222-
double time;
223-
double timeSq;
224-
double startTime;
188+
uint64_t callCount = 0;
189+
double time = 0;
190+
double timeSq = 0;
191+
double startTime = 0;
225192
KernelExecutionType kType;
226193
};
227194

profiling/simple-kernel-timer/kp_reader.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int main(int argc, char* argv[]) {
6464
KernelPerformanceInfo* new_kernel =
6565
new KernelPerformanceInfo("", PARALLEL_FOR);
6666
if (new_kernel->readFromFile(the_file)) {
67-
if (strlen(new_kernel->getName()) > 0) {
67+
if (!new_kernel->getName().empty()) {
6868
int kernelIndex = find_index(kernelInfo, new_kernel->getName());
6969

7070
if (kernelIndex > -1) {
@@ -97,7 +97,7 @@ int main(int argc, char* argv[]) {
9797
if (kernelInfo[i]->getKernelType() != REGION) continue;
9898
if (fixed_width)
9999
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
100-
kernelInfo[i]->getName(),
100+
kernelInfo[i]->getName().c_str(),
101101
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
102102
? (" (ParFor) ")
103103
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -112,7 +112,7 @@ int main(int argc, char* argv[]) {
112112
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
113113
else
114114
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
115-
kernelInfo[i]->getName(),
115+
kernelInfo[i]->getName().c_str(),
116116
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
117117
? (" (ParFor) ")
118118
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -139,7 +139,7 @@ int main(int argc, char* argv[]) {
139139
if (kernelInfo[i]->getKernelType() == REGION) continue;
140140
if (fixed_width)
141141
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
142-
kernelInfo[i]->getName(),
142+
kernelInfo[i]->getName().c_str(),
143143
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
144144
? (" (ParFor) ")
145145
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
@@ -154,7 +154,7 @@ int main(int argc, char* argv[]) {
154154
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
155155
else
156156
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
157-
kernelInfo[i]->getName(),
157+
kernelInfo[i]->getName().c_str(),
158158
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
159159
? (" (ParFor) ")
160160
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)

profiling/simple-kernel-timer/kp_shared.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ inline bool compareKernelPerformanceInfo(KernelPerformanceInfo* left,
4242
};
4343

4444
inline int find_index(const std::vector<KernelPerformanceInfo*>& kernels,
45-
const char* kernelName) {
46-
for (unsigned int i = 0; i < kernels.size(); i++) {
47-
if (strcmp(kernels[i]->getName(), kernelName) == 0) {
45+
const std::string& kernelName) {
46+
for (unsigned int i = 0; i < kernels.size(); ++i) {
47+
if (kernels[i]->getName() == kernelName) {
4848
return i;
4949
}
5050
}

profiling/space-time-stack/kp_space_time_stack.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <algorithm>
3232
#include <cstring>
3333

34+
#include "common/utils/demangle.hpp"
35+
3436
#include "kp_core.hpp"
3537

3638
#if USE_MPI
@@ -741,7 +743,7 @@ struct State {
741743
}
742744

743745
void begin_frame(const char* name, StackKind kind) {
744-
std::string name_str(name);
746+
std::string name_str(demangleNameKokkos(name));
745747
stack_frame = stack_frame->get_child(std::move(name_str), kind);
746748
stack_frame->begin();
747749
}

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(space-time-stack)

tests/space-time-stack/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A function to add such "simple" tests in 'tests/CMakeLists.txt' might be a good option.
2+
add_executable(test_space_time_stack_demangling)
3+
target_sources(
4+
test_space_time_stack_demangling
5+
PRIVATE
6+
test_demangling.cpp
7+
)
8+
target_link_libraries(
9+
test_space_time_stack_demangling
10+
PRIVATE
11+
Kokkos::kokkos kokkostools
12+
)
13+
add_test(
14+
NAME test_space_time_stack_demangling
15+
COMMAND $<TARGET_FILE:test_space_time_stack_demangling>
16+
--kokkos-tools-libs=$<TARGET_FILE:kp_space_time_stack>
17+
--kokkos-tools-args=1e-9
18+
)

0 commit comments

Comments
 (0)