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 Dec 21, 2023
1 parent 6dae155 commit 205edcb
Show file tree
Hide file tree
Showing 24 changed files with 357 additions and 239 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 @@ -264,6 +265,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",
"KokkosTools_ENABLE_PAPI" : "ON"
Expand Down
53 changes: 53 additions & 0 deletions common/FrameType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef KOKKOSTOOLS_COMMON_FRAMETYPE_HPP
#define KOKKOSTOOLS_COMMON_FRAMETYPE_HPP

#include <ostream>
#include <string>

namespace KokkosTools {

enum FrameType {
PARALLEL_FOR = 0,
PARALLEL_REDUCE = 1,
PARALLEL_SCAN = 2,
REGION = 3,
COPY = 4
};

//! A @ref FrameType::REGION is not a kernel.
inline constexpr bool is_a_kernel(const FrameType type) {
switch (type) {
case PARALLEL_FOR : return true;
case PARALLEL_REDUCE: return true;
case PARALLEL_SCAN : return true;
case REGION : return false;
case COPY : return true;
default: throw type;
}
}

inline std::string to_string(const FrameType t) {
switch (t) {
case PARALLEL_FOR : return "PARALLEL_FOR";
case PARALLEL_REDUCE: return "PARALLEL_REDUCE";
case PARALLEL_SCAN : return "PARALLEL_SCAN";
case REGION : return "REGION";
case COPY : return "COPY";
default: throw t;
}
}

inline std::ostream &operator<<(std::ostream &os, const FrameType kind) {
switch (kind) {
case PARALLEL_FOR : os << "[for]"; break;
case PARALLEL_REDUCE: os << "[reduce]"; break;
case PARALLEL_SCAN : os << "[scan]"; break;
case REGION : os << "[region]"; break;
case COPY : os << "[copy]"; break;
};
return os;
}

} // namespace KokkosTools

#endif // KOKKOSTOOLS_COMMON_FRAMETYPE_HPP
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
71 changes: 14 additions & 57 deletions profiling/chrome-tracing/kp_chrome_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <unistd.h>

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

#if USE_MPI
#include <mpi.h>
Expand All @@ -42,60 +45,14 @@
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,
STACK_SCAN,
STACK_REGION,
STACK_COPY
};

std::ostream &operator<<(std::ostream &os, StackKind kind) {
switch (kind) {
case STACK_FOR: os << "[for]"; break;
case STACK_REDUCE: os << "[reduce]"; break;
case STACK_SCAN: os << "[scan]"; break;
case STACK_REGION: os << "[region]"; break;
case STACK_COPY: os << "[copy]"; break;
};
return os;
}

struct StackNode {
std::string name;
StackKind kind;
FrameType kind;
int rank;
double total_runtime;
Now start_time;
Now base_time;
StackNode(std::string &&name_in, StackKind kind_in, int r, Now b)
StackNode(std::string &&name_in, FrameType kind_in, int r, Now b)
: name(std::move(name_in)),
kind(kind_in),
rank(r),
Expand Down Expand Up @@ -144,7 +101,7 @@ struct State {
}

~State() { outfile << "]\n"; }
void begin_frame(const char *name, StackKind kind) {
void begin_frame(const char *name, FrameType kind) {
std::string name_str(name);
current_stack.emplace_back(std::move(name_str), kind, my_mpi_rank,
my_base_time);
Expand All @@ -163,27 +120,27 @@ struct State {
current_stack.pop_back();
}

std::uint64_t begin_kernel(const char *name, StackKind kind) {
std::uint64_t begin_kernel(const char *name, FrameType kind) {
begin_frame(name, kind);
return 0;
}
void end_kernel(std::uint64_t) { end_frame(); }
void push_region(const char *name) { begin_frame(name, STACK_REGION); }
void push_region(const char *name) { begin_frame(name, FrameType::REGION); }
void pop_region() { end_frame(); }
void begin_deep_copy(Space dst, const char *dst_name, const void *, Space src,
const char *src_name, const void *, std::uint64_t len) {
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 += " ";
begin_frame(frame_name.c_str(), STACK_COPY);
begin_frame(frame_name.c_str(), FrameType::COPY);
}
void end_deep_copy() { end_frame(); }
};
Expand All @@ -206,19 +163,19 @@ void kokkosp_finalize_library() {
void kokkosp_begin_parallel_for(const char *name, std::uint32_t devid,
std::uint64_t *kernid) {
(void)devid;
*kernid = global_state->begin_kernel(name, STACK_FOR);
*kernid = global_state->begin_kernel(name, FrameType::PARALLEL_FOR);
}

void kokkosp_begin_parallel_reduce(const char *name, std::uint32_t devid,
std::uint64_t *kernid) {
(void)devid;
*kernid = global_state->begin_kernel(name, STACK_REDUCE);
*kernid = global_state->begin_kernel(name, FrameType::PARALLEL_REDUCE);
}

void kokkosp_begin_parallel_scan(const char *name, std::uint32_t devid,
std::uint64_t *kernid) {
(void)devid;
*kernid = global_state->begin_kernel(name, STACK_SCAN);
*kernid = global_state->begin_kernel(name, FrameType::PARALLEL_SCAN);
}

void kokkosp_end_parallel_for(std::uint64_t kernid) {
Expand Down
Loading

0 comments on commit 205edcb

Please sign in to comment.