Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cudf::cuda_error #10630

Merged
merged 12 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions cpp/include/cudf/utilities/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,25 @@ struct logic_error : public std::logic_error {
* @brief Exception thrown when a CUDA error is encountered.
*/
struct cuda_error : public std::runtime_error {
cuda_error(std::string const& message) : std::runtime_error(message) {}
cuda_error(std::string const& message, cudaError_t const& error)
: std::runtime_error(message), _cudaError(error)
{
}
cudaError_t error_code() { return _cudaError; }

protected:
cudaError_t _cudaError;
};

struct cudart_error : public cuda_error {
cudart_error(std::string const& message, cudaError_t const& error) : cuda_error(message, error) {}
};
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved

struct sticky_cuda_error : public cuda_error {
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved
sticky_cuda_error(std::string const& message, cudaError_t const& error)
: cuda_error(message, error)
{
}
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved
};
/** @} */

Expand Down Expand Up @@ -101,9 +119,16 @@ namespace detail {

inline void throw_cuda_error(cudaError_t error, const char* file, unsigned int line)
{
throw cudf::cuda_error(std::string{"CUDA error encountered at: " + std::string{file} + ":" +
std::to_string(line) + ": " + std::to_string(error) + " " +
cudaGetErrorName(error) + " " + cudaGetErrorString(error)});
cudaGetLastError();
jrhemstad marked this conversation as resolved.
Show resolved Hide resolved
auto const last = cudaGetLastError();
auto const msg = std::string{"CUDA error encountered at: " + std::string{file} + ":" +
std::to_string(line) + ": " + std::to_string(error) + " " +
cudaGetErrorName(error) + " " + cudaGetErrorString(error)};
if (error == last && last == cudaDeviceSynchronize()) {
throw sticky_cuda_error{"Sticky " + msg, error};
} else {
throw cudart_error{msg, error};
}
}
} // namespace detail
} // namespace cudf
Expand All @@ -115,13 +140,10 @@ inline void throw_cuda_error(cudaError_t error, const char* file, unsigned int l
* cudaSuccess, invokes cudaGetLastError() to clear the error and throws an
* exception detailing the CUDA error that occurred
*/
#define CUDF_CUDA_TRY(call) \
do { \
cudaError_t const status = (call); \
if (cudaSuccess != status) { \
cudaGetLastError(); \
cudf::detail::throw_cuda_error(status, __FILE__, __LINE__); \
} \
#define CUDF_CUDA_TRY(call) \
do { \
cudaError_t const status = (call); \
if (cudaSuccess != status) { cudf::detail::throw_cuda_error(status, __FILE__, __LINE__); } \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semicolon after the while(0) should be removed to ensure all uses of the CUDF_CUDA_TRY macro are terminated with a semi-colon.

} while (0);

/**
Expand Down
12 changes: 9 additions & 3 deletions cpp/include/cudf_test/cudf_gtest.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -117,8 +117,14 @@ struct TypeList<Types<TYPES...>> {
#define CUDF_EXPECT_THROW_MESSAGE(x, msg) \
EXPECT_THROW_MESSAGE(x, cudf::logic_error, "cuDF failure at:", msg)

#define CUDA_EXPECT_THROW_MESSAGE(x, msg) \
EXPECT_THROW_MESSAGE(x, cudf::cuda_error, "CUDA error encountered at:", msg)
#define CUDART_EXPECT_THROW_MESSAGE(x, msg) \
EXPECT_THROW_MESSAGE(x, cudf::cudart_error, "CUDA error encountered at:", msg)

#define STICKY_CUDA_EXPECT_THROW_MESSAGE(x, msg) \
EXPECT_THROW_MESSAGE(x, cudf::sticky_cuda_error, "Sticky CUDA error encountered at:", msg)

#define STICKY_CUDA_EXPECT_THROW_MESSAGE_1(x, msg) \
EXPECT_THROW_MESSAGE(x, cudf::cuda_error, "Sticky CUDA error encountered at:", msg)

/**
* @brief test macro to be expected as no exception.
Expand Down
17 changes: 9 additions & 8 deletions cpp/tests/error/error_handling_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ TEST(ExpectsTest, TryCatch)

TEST(CudaTryTest, Error)
{
CUDA_EXPECT_THROW_MESSAGE(CUDF_CUDA_TRY(cudaErrorLaunchFailure),
"cudaErrorLaunchFailure unspecified launch failure");
CUDART_EXPECT_THROW_MESSAGE(CUDF_CUDA_TRY(cudaErrorLaunchFailure),
"cudaErrorLaunchFailure unspecified launch failure");
}

TEST(CudaTryTest, Success) { EXPECT_NO_THROW(CUDF_CUDA_TRY(cudaSuccess)); }

TEST(CudaTryTest, TryCatch)
{
CUDA_EXPECT_THROW_MESSAGE(CUDF_CUDA_TRY(cudaErrorMemoryAllocation),
"cudaErrorMemoryAllocation out of memory");
CUDART_EXPECT_THROW_MESSAGE(CUDF_CUDA_TRY(cudaErrorMemoryAllocation),
"cudaErrorMemoryAllocation out of memory");
}

TEST(StreamCheck, success) { EXPECT_NO_THROW(CUDF_CHECK_CUDA(0)); }
Expand All @@ -67,7 +68,7 @@ TEST(StreamCheck, FailedKernel)
#ifdef NDEBUG
stream.synchronize();
#endif
EXPECT_THROW(CUDF_CHECK_CUDA(stream.value()), cudf::cuda_error);
EXPECT_THROW(CUDF_CHECK_CUDA(stream.value()), cudf::cudart_error);
}

TEST(StreamCheck, CatchFailedKernel)
Expand All @@ -78,9 +79,9 @@ TEST(StreamCheck, CatchFailedKernel)
#ifndef NDEBUG
stream.synchronize();
#endif
CUDA_EXPECT_THROW_MESSAGE(CUDF_CHECK_CUDA(stream.value()),
"cudaErrorInvalidConfiguration "
"invalid configuration argument");
CUDART_EXPECT_THROW_MESSAGE(CUDF_CHECK_CUDA(stream.value()),
"cudaErrorInvalidConfiguration "
"invalid configuration argument");
}

#ifndef NDEBUG
Expand Down