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

Refactor error.hpp out of detail #1439

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
97 changes: 3 additions & 94 deletions include/rmm/detail/error.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2024, NVIDIA CORPORATION.
wence- marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,105 +16,14 @@

#pragma once

#include <rmm/error.hpp>

#include <cuda_runtime_api.h>

#include <cassert>
#include <iostream>
#include <stdexcept>
#include <string>

namespace rmm {

/**
* @brief Exception thrown when logical precondition is violated.
*
* @ingroup errors
*
* This exception should not be thrown directly and is instead thrown by the
* RMM_EXPECTS macro.
*
*/
struct logic_error : public std::logic_error {
using std::logic_error::logic_error;
};

/**
* @brief Exception thrown when a CUDA error is encountered.
*
* @ingroup errors
*
*/
struct cuda_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* @brief Exception thrown when an RMM allocation fails
*
* @ingroup errors
*
*/
class bad_alloc : public std::bad_alloc {
public:
/**
* @brief Constructs a bad_alloc with the error message.
*
* @param msg Message to be associated with the exception
*/
bad_alloc(const char* msg) : _what{std::string{std::bad_alloc::what()} + ": " + msg} {}

/**
* @brief Constructs a bad_alloc with the error message.
*
* @param msg Message to be associated with the exception
*/
bad_alloc(std::string const& msg) : bad_alloc{msg.c_str()} {}

/**
* @briefreturn{The explanatory string}
*/
[[nodiscard]] const char* what() const noexcept override { return _what.c_str(); }

private:
std::string _what;
};

/**
* @brief Exception thrown when RMM runs out of memory
*
* @ingroup errors
*
* This error should only be thrown when we know for sure a resource is out of memory.
*/
class out_of_memory : public bad_alloc {
public:
/**
* @brief Constructs an out_of_memory with the error message.
*
* @param msg Message to be associated with the exception
*/
out_of_memory(const char* msg) : bad_alloc{std::string{"out_of_memory: "} + msg} {}

/**
* @brief Constructs an out_of_memory with the error message.
*
* @param msg Message to be associated with the exception
*/
out_of_memory(std::string const& msg) : out_of_memory{msg.c_str()} {}
};

/**
* @brief Exception thrown when attempting to access outside of a defined range
*
* @ingroup errors
*
*/
class out_of_range : public std::out_of_range {
using std::out_of_range::out_of_range;
};

} // namespace rmm

#define STRINGIFY_DETAIL(x) #x
#define RMM_STRINGIFY(x) STRINGIFY_DETAIL(x)

Expand Down
112 changes: 112 additions & 0 deletions include/rmm/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
harrism marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <stdexcept>
#include <string>

namespace rmm {

/**
* @brief Exception thrown when logical precondition is violated.
*
* @ingroup errors
*
* This exception should not be thrown directly and is instead thrown by the
* RMM_EXPECTS macro.
*
*/
struct logic_error : public std::logic_error {
using std::logic_error::logic_error;
};

/**
* @brief Exception thrown when a CUDA error is encountered.
*
* @ingroup errors
*
*/
struct cuda_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* @brief Exception thrown when an RMM allocation fails
*
* @ingroup errors
*
*/
class bad_alloc : public std::bad_alloc {
public:
/**
* @brief Constructs a bad_alloc with the error message.
*
* @param msg Message to be associated with the exception
*/
bad_alloc(const char* msg) : _what{std::string{std::bad_alloc::what()} + ": " + msg} {}

/**
* @brief Constructs a bad_alloc with the error message.
*
* @param msg Message to be associated with the exception
*/
bad_alloc(std::string const& msg) : bad_alloc{msg.c_str()} {}

/**
* @briefreturn{The explanatory string}
*/
[[nodiscard]] const char* what() const noexcept override { return _what.c_str(); }

private:
std::string _what;
};

/**
* @brief Exception thrown when RMM runs out of memory
*
* @ingroup errors
*
* This error should only be thrown when we know for sure a resource is out of memory.
*/
class out_of_memory : public bad_alloc {
public:
/**
* @brief Constructs an out_of_memory with the error message.
*
* @param msg Message to be associated with the exception
*/
out_of_memory(const char* msg) : bad_alloc{std::string{"out_of_memory: "} + msg} {}

/**
* @brief Constructs an out_of_memory with the error message.
*
* @param msg Message to be associated with the exception
*/
out_of_memory(std::string const& msg) : out_of_memory{msg.c_str()} {}
};

/**
* @brief Exception thrown when attempting to access outside of a defined range
*
* @ingroup errors
*
*/
class out_of_range : public std::out_of_range {
using std::out_of_range::out_of_range;
};

} // namespace rmm
Loading