3#include <cuda_runtime.h>
4#include <cuda/std/expected>
9#include <source_location>
28 static_cast<uint32_t
>(location.line()),
29 static_cast<uint32_t
>(location.column()),
77 std::variant<CudaError, IoError, FastxParseError, InvalidArgumentError, ResourceError>
kind;
83 [[nodiscard]]
const std::string&
message() const noexcept {
85 [](
const auto& error) ->
const std::string& {
return error.message; },
kind
90 return std::get_if<CudaError>(&
kind);
94 return std::get_if<IoError>(&
kind);
98 return std::get_if<FastxParseError>(&
kind);
102 return std::get_if<InvalidArgumentError>(&
kind);
106 return std::get_if<ResourceError>(&
kind);
109 [[nodiscard]]
static Error
110 cuda(cudaError_t code, std::source_location location = std::source_location::current()) {
115 std::format(
"{}: {}", site.to_string(), cudaGetErrorString(code)),
124 const std::string location_text = site.
to_string();
127 std::format(
"{}: {}", location_text, detail),
152struct [[nodiscard]]
Result : cuda::std::expected<T, Error> {
153 using cuda::std::expected<T,
Error>::expected;
156 return Result(std::move(value));
160 return Result(cuda::std::unexpect, std::move(error));
165struct [[nodiscard]]
Result<void> : cuda::std::expected<void, Error> {
166 using cuda::std::expected<void,
Error>::expected;
173 return Result(cuda::std::unexpect, std::move(error));
180 std::fputs(result.error().message().c_str(), stderr);
181 std::fputc(
'\n', stderr);
189 std::source_location location = std::source_location::current()
191 if (error == cudaSuccess) {
195 std::fputs(err.
message().c_str(), stderr);
196 std::fputc(
'\n', stderr);
204 return std::move(*
result);
213 return cuda::std::unexpected<Error>(
Error{
error});
219[[nodiscard]]
inline cuda::std::unexpected<Error>
Err(
Error error) {
220 return cuda::std::unexpected<Error>(std::move(error));
229[[nodiscard]]
inline Result<void>
230cuda_try(cudaError_t error, std::source_location location = std::source_location::current()) {
231 if (error == cudaSuccess) {
246#define CUSBF_TRY(expr) \
248 auto _cusbf_result = (expr); \
249 if (!_cusbf_result) { \
250 return ::cusbf::detail::propagate_error(_cusbf_result.error()); \
252 ::cusbf::detail::try_unwrap_success(_cusbf_result); \
256#define CUSBF_UNWRAP(expr) \
258 auto _cusbf_result = (expr); \
259 if (!_cusbf_result) { \
260 throw std::runtime_error(_cusbf_result.error().message()); \
262 ::cusbf::detail::try_unwrap_success(_cusbf_result); \
266#define CUSBF_CUDA_ABORT(expr) ::cusbf::cuda_abort_on_error((expr))
269#define CUSBF_CUDA_TRY(expr) CUSBF_TRY(::cusbf::cuda_try((expr)))
T try_unwrap_success(Result< T > &result)
cuda::std::unexpected< Error > propagate_error(const Error &error)
Copies error for propagation (avoids moving out of expected::error()).
consteval bool separatorPositionAlwaysEncodesInvalid(char *input, uint64_t separatorPosition, uint64_t index)
Recursively tests whether placing the separator byte at any position in an input of valid bytes alway...
Result< void > cuda_try(cudaError_t error, std::source_location location=std::source_location::current())
Checks a CUDA runtime call and returns an error on failure.
void cuda_abort_on_error(cudaError_t error, std::source_location location=std::source_location::current())
Terminates the process when error is not cudaSuccess (for RAII teardown only).
void require_void(const Result< void > &result)
Aborts when a Result is unsuccessful (benchmarks and tests).
cuda::std::unexpected< Error > Err(Error error)
Failure return; converts to any Result<T> via cuda::std::unexpected.
Result< void > Ok() noexcept
Success return for Result<void>; same as return {}.
ErrorCategory
Error category for Result failures.
Error payload carried in Result on failure.
const FastxParseError * as_fastx_parse() const noexcept
static Error invalid_argument(std::string message)
static Error cuda(cudaError_t code, std::source_location location=std::source_location::current())
std::variant< CudaError, IoError, FastxParseError, InvalidArgumentError, ResourceError > kind
const ResourceError * as_resource() const noexcept
const CudaError * as_cuda() const noexcept
ErrorCategory category() const noexcept
static Error io(std::string message)
const IoError * as_io() const noexcept
static Error resource(std::string message)
const std::string & message() const noexcept
const InvalidArgumentError * as_invalid_argument() const noexcept
static Error fastx_parse(SourceLocation site, std::string_view detail)
static Result err(Error error)
Fallible API result: cuda::std::expected<T, Error> with cuSBF factories.
static Result ok(T value)
static Result err(Error error)
File, line, and column for a C++ call site or FASTX input position.
static SourceLocation from(std::source_location location)
static SourceLocation fastx(std::string_view file, uint32_t line, uint32_t column)
std::string to_string() const