Forked from mkolod/redirect_streams_and_cuda_checks.cu
Created
September 25, 2020 21:34
-
-
Save sandeepkumar-skb/75e16e4e1d13f38f4f0039644cebe766 to your computer and use it in GitHub Desktop.
Redirect Streams and CUDA checks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <limits.h> | |
#include <unistd.h> | |
#include <csignal> | |
#include <cstdlib> | |
#include <fstream> | |
#include <iostream> | |
#include <iostream> | |
#include <sstream> | |
#include <stdexcept> | |
#include <cuda.h> | |
#include <cuda_runtime_api.h> | |
namespace { | |
struct StaticBlock { | |
std::fstream file; | |
char* log_path = nullptr; | |
StaticBlock() { | |
char* hostname = new char[255]; | |
gethostname(hostname, 255); | |
const char* path = std::getenv("KERNEL_DEBUG_PATH"); | |
if (path != "") { | |
log_path = | |
const_cast<char*>((std::string(path) + "_" + | |
std::string(static_cast<const char*>(hostname)) + | |
"_" + std::to_string(getpid())) | |
.c_str()); | |
file.open(log_path, std::ios::out); | |
std::streambuf* stream_buffer_cout = std::cout.rdbuf(); | |
std::streambuf* stream_buffer_file = file.rdbuf(); | |
std::cout.rdbuf(stream_buffer_file); | |
std::cerr.rdbuf(stream_buffer_file); | |
} | |
} | |
void signalHandler(int signum) { | |
if (log_path != nullptr) { | |
file.close(); | |
} | |
exit(signum); | |
} | |
}; | |
[[MAYBE_UNUSED]] static StaticBlock staticBlock; | |
void signalWrapper(int signum) { staticBlock.signalHandler(signum); } | |
for (const int& i : {SIGTERM, SIGABRT, SIGINT, SIGSEGV, SIGKILL}) { | |
signal(i, &signalWrapper); | |
} | |
} // namespace | |
inline void gpuAssert(cudaError_t code, const char* file, int line) { | |
if (code != cudaSuccess) { | |
std::stringstream ss; | |
ss << "GPU error: " << cudaGetErrorString(code) << ", in file " << file | |
<< ", line: " << line; | |
std::cout << ss.str() << std::endl; | |
throw std::runtime_error(ss.str()); | |
} | |
} | |
#define gpuErrchk(ans) \ | |
{ gpuAssert((ans), __FILE__, __LINE__); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment