Skip to content

Commit

Permalink
space-time-stack: allow demangled names
Browse files Browse the repository at this point in the history
  • Loading branch information
romintomasetti committed Jan 4, 2024
1 parent 0becae4 commit 4dacc69
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 57 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ option(KokkosTools_ENABLE_MPI "Enable MPI support" OFF)
option(KokkosTools_ENABLE_CALIPER "Enable building Caliper library" OFF)
option(KokkosTools_ENABLE_APEX "Enable building Apex library" OFF)
option(KokkosTools_ENABLE_EXAMPLES "Build examples" OFF)
option(KokkosTools_ENABLE_TESTS "Enable tests" OFF)

# Advanced settings
option(KokkosTools_REUSE_KOKKOS_COMPILER "Set the compiler and flags based on installed Kokkos settings" OFF)
mark_as_advanced(KokkosTools_REUSE_KOKKOS_COMPILER)
Expand Down Expand Up @@ -116,6 +118,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/profiling/all)
set(COMMON_HEADERS_PATH ${CMAKE_CURRENT_BINARY_DIR}/common)
include_directories(${COMMON_HEADERS_PATH})

# Allow all tools to include any file.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(SINGLELIB_PROFILERS "" CACHE STRING "" FORCE)

# Export settings
Expand Down Expand Up @@ -264,6 +269,12 @@ if(KokkosTools_ENABLE_EXAMPLES)
endif()
endif()

# Tests
if(KokkosTools_ENABLE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

# Install exports
install(TARGETS ${EXPORT_TARGETS} EXPORT ${EXPORT_NAME})
install(EXPORT ${EXPORT_NAME}
Expand Down
3 changes: 2 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"KokkosTools_ENABLE_EXAMPLES" : "ON",
"KokkosTools_ENABLE_SINGLE" : "ON",
"KokkosTools_ENABLE_MPI" : "ON",
"KokkosTools_ENABLE_PAPI" : "ON"
"KokkosTools_ENABLE_PAPI" : "ON",
"KokkosTools_ENABLE_TESTS" : "ON"
}
},
{
Expand Down
74 changes: 74 additions & 0 deletions common/utils/demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP

#include <string>

#if defined(__GXX_ABI_VERSION)
#define HAVE_GCC_ABI_DEMANGLE
#endif

#if defined(HAVE_GCC_ABI_DEMANGLE)
#include <cxxabi.h>
#endif // HAVE_GCC_ABI_DEMANGLE

namespace KokkosTools {

//! Demangle @p mangled_name.
inline std::string demangleName(const std::string_view mangled_name) {
#if defined(HAVE_GCC_ABI_DEMANGLE)
int status = 0;

char* demangled_name =
abi::__cxa_demangle(mangled_name.data(), nullptr, nullptr, &status);

if (demangled_name) {
std::string ret(demangled_name);
std::free(demangled_name);
return ret;
}
#endif
return std::string(mangled_name);
}

/**
* @brief Demangle @p mangled_name.
*
* This function supports @c Kokkos convention from
* @c Kokkos::Impl::ParallelConstructName.
*
* For instance, a kernel launched with a tag would appear as
* "<functor type>/<tag type>".
*/
inline std::string demangleNameKokkos(const std::string_view mangled_name) {
if (size_t pos = mangled_name.find('/', 0);
pos != std::string_view::npos && pos > 0) {
/// An explicit copy of the first part of the string is needed, because
/// @c abi::__cxa_demangle will parse the pointer until its NULL-terminated.
return demangleName(std::string(mangled_name.substr(0, pos)))
.append("/")
.append(
demangleName(mangled_name.substr(pos + 1, mangled_name.size())));
} else {
return demangleName(mangled_name);
}
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE_HPP
2 changes: 1 addition & 1 deletion profiling/simple-kernel-timer/kp_json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char* argv[]) {
KernelPerformanceInfo* new_kernel =
new KernelPerformanceInfo("", PARALLEL_FOR);
if (new_kernel->readFromFile(the_file)) {
if (strlen(new_kernel->getName()) > 0) {
if (!new_kernel->getName().empty()) {
int kernelIndex = find_index(kernelInfo, new_kernel->getName());

if (kernelIndex > -1) {
Expand Down
59 changes: 13 additions & 46 deletions profiling/simple-kernel-timer/kp_kernel_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,10 @@
#include <string>
#include <cstring>

#if defined(__GXX_ABI_VERSION)
#define HAVE_GCC_ABI_DEMANGLE
#endif

#if defined(HAVE_GCC_ABI_DEMANGLE)
#include <cxxabi.h>
#endif // HAVE_GCC_ABI_DEMANGLE
#include "common/utils/demangle.hpp"

namespace KokkosTools::KernelTimer {

inline char* demangleName(char* kernelName) {
#if defined(HAVE_GCC_ABI_DEMANGLE)
int status = -1;
char* demangledKernelName =
abi::__cxa_demangle(kernelName, NULL, NULL, &status);
if (status == 0) {
free(kernelName);
kernelName = demangledKernelName;
}
#endif // HAVE_GCC_ABI_DEMANGLE
return kernelName;
}

inline double seconds() {
struct timeval now;
gettimeofday(&now, NULL);
Expand All @@ -62,15 +43,7 @@ enum KernelExecutionType {
class KernelPerformanceInfo {
public:
KernelPerformanceInfo(std::string kName, KernelExecutionType kernelType)
: kType(kernelType) {
kernelName = (char*)malloc(sizeof(char) * (kName.size() + 1));
strcpy(kernelName, kName.c_str());

callCount = 0;
time = 0;
}

~KernelPerformanceInfo() { free(kernelName); }
: kernelName(std::move(kName)), kType(kernelType) {}

KernelExecutionType getKernelType() const { return kType; }

Expand All @@ -95,7 +68,7 @@ class KernelPerformanceInfo {

double getTimeSq() { return timeSq; }

char* getName() const { return kernelName; }
const std::string& getName() const { return kernelName; }

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

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

if (strlen(kernelName) > 0) {
free(kernelName);
}

kernelName = (char*)malloc(sizeof(char) * (kernelNameLength + 1));
copy(kernelName, &entry[nextIndex], kernelNameLength);
kernelName[kernelNameLength] = '\0';
this->kernelName = std::string(&entry[nextIndex], kernelNameLength);

kernelName = demangleName(kernelName);
kernelName = demangleNameKokkos(kernelName);

nextIndex += kernelNameLength;

Expand Down Expand Up @@ -152,7 +119,7 @@ class KernelPerformanceInfo {
}

void writeToBinaryFile(FILE* output) {
const uint32_t kernelNameLen = (uint32_t)strlen(kernelName);
const uint32_t kernelNameLen = kernelName.size();
const uint32_t recordLen = sizeof(uint32_t) + sizeof(char) * kernelNameLen +
sizeof(uint64_t) + sizeof(double) +
sizeof(double) + sizeof(uint32_t);
Expand All @@ -163,7 +130,7 @@ class KernelPerformanceInfo {
copy(&entry[nextIndex], (char*)&kernelNameLen, sizeof(kernelNameLen));
nextIndex += sizeof(kernelNameLen);

copy(&entry[nextIndex], kernelName, kernelNameLen);
copy(&entry[nextIndex], kernelName.c_str(), kernelNameLen);
nextIndex += kernelNameLen;

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

fprintf(output, "%s\"kernel-name\" : \"%s\",\n", indentBuffer,
kernelName);
kernelName.c_str());
// fprintf(output, "%s\"region\" : \"%s\",\n", indentBuffer,
// regionName);
fprintf(output, "%s\"call-count\" : %llu,\n", indentBuffer,
Expand All @@ -216,12 +183,12 @@ class KernelPerformanceInfo {
}
}

char* kernelName;
std::string kernelName;
// const char* regionName;
uint64_t callCount;
double time;
double timeSq;
double startTime;
uint64_t callCount = 0;
double time = 0;
double timeSq = 0;
double startTime = 0;
KernelExecutionType kType;
};

Expand Down
10 changes: 5 additions & 5 deletions profiling/simple-kernel-timer/kp_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, char* argv[]) {
KernelPerformanceInfo* new_kernel =
new KernelPerformanceInfo("", PARALLEL_FOR);
if (new_kernel->readFromFile(the_file)) {
if (strlen(new_kernel->getName()) > 0) {
if (!new_kernel->getName().empty()) {
int kernelIndex = find_index(kernelInfo, new_kernel->getName());

if (kernelIndex > -1) {
Expand Down Expand Up @@ -97,7 +97,7 @@ int main(int argc, char* argv[]) {
if (kernelInfo[i]->getKernelType() != REGION) continue;
if (fixed_width)
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -112,7 +112,7 @@ int main(int argc, char* argv[]) {
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
else
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -139,7 +139,7 @@ int main(int argc, char* argv[]) {
if (kernelInfo[i]->getKernelType() == REGION) continue;
if (fixed_width)
printf("- %100s\n%11s%c%15.5f%c%12" PRIu64 "%c%15.5f%c%7.3f%c%7.3f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand All @@ -154,7 +154,7 @@ int main(int argc, char* argv[]) {
(kernelInfo[i]->getTime() / totalExecuteTime) * 100.0);
else
printf("- %s\n%s%c%f%c%" PRIu64 "%c%f%c%f%c%f\n",
kernelInfo[i]->getName(),
kernelInfo[i]->getName().c_str(),
(kernelInfo[i]->getKernelType() == PARALLEL_FOR)
? (" (ParFor) ")
: ((kernelInfo[i]->getKernelType() == PARALLEL_REDUCE)
Expand Down
6 changes: 3 additions & 3 deletions profiling/simple-kernel-timer/kp_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ inline bool compareKernelPerformanceInfo(KernelPerformanceInfo* left,
};

inline int find_index(const std::vector<KernelPerformanceInfo*>& kernels,
const char* kernelName) {
for (unsigned int i = 0; i < kernels.size(); i++) {
if (strcmp(kernels[i]->getName(), kernelName) == 0) {
const std::string& kernelName) {
for (unsigned int i = 0; i < kernels.size(); ++i) {
if (kernels[i]->getName() == kernelName) {
return i;
}
}
Expand Down
4 changes: 3 additions & 1 deletion profiling/space-time-stack/kp_space_time_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <algorithm>
#include <cstring>

#include "common/utils/demangle.hpp"

#include "kp_core.hpp"

#if USE_MPI
Expand Down Expand Up @@ -741,7 +743,7 @@ struct State {
}

void begin_frame(const char* name, StackKind kind) {
std::string name_str(name);
std::string name_str(demangleNameKokkos(name));
stack_frame = stack_frame->get_child(std::move(name_str), kind);
stack_frame->begin();
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(space-time-stack)
18 changes: 18 additions & 0 deletions tests/space-time-stack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A function to add such "simple" tests in 'tests/CMakeLists.txt' might be a good option.
add_executable(test_space_time_stack_demangling)
target_sources(
test_space_time_stack_demangling
PRIVATE
test_demangling.cpp
)
target_link_libraries(
test_space_time_stack_demangling
PRIVATE
Kokkos::kokkos kokkostools
)
add_test(
NAME test_space_time_stack_demangling
COMMAND $<TARGET_FILE:test_space_time_stack_demangling>
--kokkos-tools-libs=$<TARGET_FILE:kp_space_time_stack>
--kokkos-tools-args=1e-9
)
Loading

0 comments on commit 4dacc69

Please sign in to comment.