Skip to content

Commit

Permalink
tool(space-time-stack): demangle name
Browse files Browse the repository at this point in the history
  • Loading branch information
romintomasetti committed Nov 6, 2023
1 parent 2ddedef commit b12c60a
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 119 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ 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 "Build 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 @@ -263,6 +264,12 @@ if(KokkosTools_ENABLE_EXAMPLES)
endif()
endif()

# Build 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
1 change: 1 addition & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"CMAKE_BUILD_TYPE" : "Release",
"CMAKE_CXX_STANDARD" : "17",
"KokkosTools_ENABLE_EXAMPLES" : "ON",
"KokkosTools_ENABLE_TESTS" : "ON",
"KokkosTools_ENABLE_SINGLE" : "ON",
"KokkosTools_ENABLE_MPI" : "ON"
}
Expand Down
67 changes: 67 additions & 0 deletions common/SpaceHandle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef KOKKOSTOOLS_COMMON_SPACEHANDLE_HPP
#define KOKKOSTOOLS_COMMON_SPACEHANDLE_HPP

#include "impl/Kokkos_Profiling_C_Interface.h"

#include <ostream>
#include <string>

namespace KokkosTools
{
//! A @c Kokkos space type has a unique name.
using SpaceHandle = Kokkos_Profiling_SpaceHandle;

//! Supported @c Kokkos spaces.
enum Space {
HOST = 0,
CUDA = 1,
HIP = 2,
SYCL = 3,
OMPT = 4
};

//! Number of supported spaces (size of @ref Space).
constexpr size_t NSPACES = 5;

//! Get @ref Space from @ref SpaceHandle.
Space get_space(SpaceHandle const& handle)
{
switch(handle.name[0])
{
// Only 'CUDA' space starts with 'C'.
case 'C':
return Space::CUDA;
// Only 'SYCL' space starts with 'S'.
case 'S':
return Space::SYCL;
// Only 'OpenMPTarget' starts with 'O'.
case 'O':
return Space::OMPT;
// Otherwise, it's either 'HIP' or 'HOST'.
case 'H':
if(handle.name[1] == 'I') return Space::HIP;
else return Space::HOST;
default:
std::abort();
}
}

//! Get the name of a @ref Space.
const char* get_space_name(const Space space) {
switch (space) {
case Space::HOST: return "HOST";
case Space::CUDA: return "CUDA";
case Space::SYCL: return "SYCL";
case Space::OMPT: return "OpenMPTarget";
case Space::HIP : return "HIP";
}
std::abort();
}

std::ostream& operator<<(std::ostream& out, const Space space) {
return out << get_space_name(space);
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_SPACEHANDLE_HPP
55 changes: 55 additions & 0 deletions common/utils_demangle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef KOKKOSTOOLS_COMMON_UTILS_DEMANGLE
#define KOKKOSTOOLS_COMMON_UTILS_DEMANGLE

#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
{

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 || 0 == demangledKernelName) {
if (demangledKernelName != NULL) {
free(demangledKernelName);
}
} else {
free(kernelName);
kernelName = demangledKernelName;
}
#endif // HAVE_GCC_ABI_DEMANGLE
return kernelName;
}

inline std::string demangleName(const std::string& mangledName )
{
#if defined(HAVE_GCC_ABI_DEMANGLE)
int status;
char* _demangledName = abi::__cxa_demangle(mangledName.c_str (), 0, 0, &status);
if (status != 0 || 0 == _demangledName) {
if (_demangledName != NULL) {
free (_demangledName);
}
return mangledName;
}
std::string demangledName (_demangledName);
free (_demangledName); // We have to free this before we return!
return demangledName;
#else
return mangledName;
#endif
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_UTILS_DEMANGLE
35 changes: 35 additions & 0 deletions common/utils_time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef KOKKOSTOOLS_COMMON_UTILS_TIME_HPP
#define KOKKOSTOOLS_COMMON_UTILS_TIME_HPP

#include <chrono>
#include <sys/time.h>

namespace KokkosTools
{
struct Now {
using impl_t = std::chrono::time_point<std::chrono::high_resolution_clock>;
impl_t impl;
};

inline Now now() {
Now t;
t.impl = std::chrono::high_resolution_clock::now();
return t;
}

inline uint64_t operator-(Now b, Now a) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(b.impl - a.impl)
.count();
}

//! @todo Use @c chrono instead.
inline double seconds() {
struct timeval now;
gettimeofday(&now, NULL);

return (double)(now.tv_sec + (now.tv_usec * 1.0e-6));
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_UTILS_TIME_HPP
7 changes: 4 additions & 3 deletions debugging/kernel-logger/kp_kernel_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#include <limits>
#include <cstring>

#include "../../common/SpaceHandle.hpp"

std::vector<std::string> regions;
static uint64_t uniqID;
struct SpaceHandle {
char name[64];
};

using SpaceHandle = KokkosTools::SpaceHandle;

void kokkosp_print_region_stack_indent(const int level) {
printf("KokkosP: ");
Expand Down
4 changes: 2 additions & 2 deletions profiling/all/impl/Kokkos_Profiling_C_Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ enum Kokkos_Tools_OptimizationType {
Kokkos_Tools_Maximize
};

struct Kokkos_Tools_OptimzationGoal {
struct Kokkos_Tools_OptimizationGoal {
size_t type_id;
enum Kokkos_Tools_OptimizationType goal;
};
Expand Down Expand Up @@ -218,7 +218,7 @@ typedef void (*Kokkos_Tools_contextBeginFunction)(const size_t);
typedef void (*Kokkos_Tools_contextEndFunction)(
const size_t, struct Kokkos_Tools_VariableValue);
typedef void (*Kokkos_Tools_optimizationGoalDeclarationFunction)(
const size_t, const struct Kokkos_Tools_OptimzationGoal goal);
const size_t, const struct Kokkos_Tools_OptimizationGoal goal);

struct Kokkos_Profiling_EventSet {
Kokkos_Profiling_initFunction init;
Expand Down
2 changes: 1 addition & 1 deletion profiling/all/impl/Kokkos_Profiling_Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ using ValueType = Kokkos_Tools_VariableInfo_ValueType;
using CandidateValueType = Kokkos_Tools_VariableInfo_CandidateValueType;
using SetOrRange = Kokkos_Tools_VariableInfo_SetOrRange;
using VariableInfo = Kokkos_Tools_VariableInfo;
using OptimizationGoal = Kokkos_Tools_OptimzationGoal;
using OptimizationGoal = Kokkos_Tools_OptimizationGoal;
using TuningString = Kokkos_Tools_Tuning_String;
using VariableValue = Kokkos_Tools_VariableValue;

Expand Down
33 changes: 4 additions & 29 deletions profiling/chrome-tracing/kp_chrome_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <unistd.h>

#include "kp_core.hpp"
#include "../../common/SpaceHandle.hpp"
#include "../../common/utils_time.hpp"

#if USE_MPI
#include <mpi.h>
Expand All @@ -42,33 +44,6 @@
namespace KokkosTools {
namespace ChromeTracing {

enum Space { SPACE_HOST, SPACE_CUDA };

Space get_space(SpaceHandle const &handle) {
switch (handle.name[0]) {
case 'H': return SPACE_HOST;
case 'C': return SPACE_CUDA;
}
abort();
return SPACE_HOST;
}

struct Now {
typedef std::chrono::time_point<std::chrono::high_resolution_clock> Impl;
Impl impl;
};

Now now() {
Now t;
t.impl = std::chrono::high_resolution_clock::now();
return t;
}

uint64_t operator-(Now b, Now a) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(b.impl - a.impl)
.count();
}

enum StackKind {
STACK_FOR,
STACK_REDUCE,
Expand Down Expand Up @@ -175,11 +150,11 @@ struct State {
std::string frame_name;
frame_name += dst_name;
frame_name += " SPACE ";
frame_name += (dst == SPACE_HOST) ? 'H' : 'D';
frame_name += (dst == Space::HOST) ? 'H' : 'D';
frame_name += " COPYFROM ";
frame_name += src_name;
frame_name += " SPACE ";
frame_name += (src == SPACE_HOST) ? 'H' : 'D';
frame_name += (src == Space::HOST) ? 'H' : 'D';
frame_name += " LENGTH ";
frame_name += std::to_string(len);
frame_name += " ";
Expand Down
6 changes: 3 additions & 3 deletions profiling/papi-connector/kp_papi_connector_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <sys/time.h>
#include <cstring>

#include "../../common/SpaceHandle.hpp"

#include "papi.h"

struct SpaceHandle {
char name[64];
};
using SpaceHandle = KokkosTools::SpaceHandle;

/* stack for parallel_for */
std::stack<std::string> parallel_for_name;
Expand Down
36 changes: 5 additions & 31 deletions profiling/simple-kernel-timer/kp_kernel_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,14 @@
#define _H_KOKKOSP_KERNEL_INFO

#include <stdio.h>
#include <sys/time.h>
#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"
#include "../../common/utils_time.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);

return (double)(now.tv_sec + (now.tv_usec * 1.0e-6));
}

enum KernelExecutionType {
PARALLEL_FOR = 0,
PARALLEL_REDUCE = 1,
Expand Down Expand Up @@ -82,12 +56,12 @@ class KernelPerformanceInfo {
}

void addFromTimer() {
addTime(seconds() - startTime);
addTime(KokkosTools::seconds() - startTime);

incrementCount();
}

void startTimer() { startTime = seconds(); }
void startTimer() { startTime = KokkosTools::seconds(); }

uint64_t getCallCount() const { return callCount; }

Expand Down Expand Up @@ -120,7 +94,7 @@ class KernelPerformanceInfo {
copy(kernelName, &entry[nextIndex], kernelNameLength);
kernelName[kernelNameLength] = '\0';

kernelName = demangleName(kernelName);
kernelName = KokkosTools::demangleName(kernelName);

nextIndex += kernelNameLength;

Expand Down
Loading

0 comments on commit b12c60a

Please sign in to comment.