Skip to content

Commit

Permalink
Add debugging utility RAII guard for printting scopes (#2555)
Browse files Browse the repository at this point in the history
  • Loading branch information
zasdfgbnm authored Mar 8, 2023
1 parent 2ee05db commit a86f9b0
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions third_party/nvfuser/csrc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,62 @@ std::string toDelimitedString(
return toDelimitedString(vec.begin(), vec.end(), delim);
}

template <int64_t index, int64_t stop, int64_t step, typename func_t>
void unrolled_for(func_t fun) {
if constexpr (index < stop) {
fun(std::integral_constant<int64_t, index>());
unrolled_for<index + step, stop>(fun);
}
}

template <int64_t index, int64_t stop, typename func_t>
void unrolled_for(func_t fun) {
unrolled_for<index, stop, 1>(fun);
}

template <int64_t stop, typename func_t>
void unrolled_for(func_t fun) {
unrolled_for<0, stop>(fun);
}

template <typename... Args>
std::string toDelimitedString(
const std::tuple<Args...>& args,
std::string delim = ", ") {
std::stringstream ss;
bool first_val = true;
unrolled_for<sizeof...(Args)>([&](auto i) {
if (!first_val) {
ss << delim;
}
auto item = std::get<decltype(i)::value>(args);
ss << Printer<decltype(item)>::toString(item);
first_val = false;
});
return ss.str();
}

template <typename... Args>
class DebugPrintScope {
public:
DebugPrintScope(std::string name, Args... args) : name_(std::move(name)) {
std::cout << "Entering " << name_ << "("
<< toDelimitedString(std::forward_as_tuple(args...)) << ")"
<< std::endl;
}
~DebugPrintScope() {
std::cout << "Leaving " << name_ << std::endl;
}

private:
std::string name_;
};

// Note: ##__VA_ARGS__ is not C++ stardard, but it should work on gcc and clang.
// Compared to __VA_ARGS__, ##__VA_ARGS__ automatically remove the preceding
// comma when empty, allowing empty variadic parameters. If using other
// compiler, please use DebugPrintScope directly without this macro.
#define DEBUG_PRINT_SCOPE(...) \
DebugPrintScope _debug_print_scope(__func__, ##__VA_ARGS__)

} // namespace nvfuser

0 comments on commit a86f9b0

Please sign in to comment.