Program Listing for File error.hpp#

Return to documentation for file (python/morpheus/morpheus/_lib/include/morpheus/utilities/error.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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 <cuda.h>
#include <cuda_runtime_api.h>

#include <stdexcept>
#include <string>

namespace morpheus {
struct LogicError : public std::logic_error
{
    LogicError(char const* const message) : std::logic_error(message) {}

    LogicError(std::string const& message) : std::logic_error(message) {}
};
struct CudaError : public std::runtime_error
{
    CudaError(std::string const& message) : std::runtime_error(message) {}
};
}  // namespace morpheus

#define STRINGIFY_DETAIL(x) #x
#define MORPHEUS_STRINGIFY(x) STRINGIFY_DETAIL(x)

#define MORPHEUS_EXPECTS(cond, reason) \
    (!!(cond))                         \
        ? static_cast<void>(0)         \
        : throw morpheus::LogicError("Morpheus failure at: " __FILE__ ":" MORPHEUS_STRINGIFY(__LINE__) ": " reason)

#define MORPHEUS_FAIL(reason) \
    throw morpheus::LogicError("Morpheus failure at: " __FILE__ ":" MORPHEUS_STRINGIFY(__LINE__) ": " reason)

namespace morpheus::detail {

inline void throw_cuda_error(cudaError_t error, const char* file, unsigned int line)
{
    throw morpheus::CudaError(std::string{"CUDA error encountered at: " + std::string{file} + ":" +
                                          std::to_string(line) + ": " + std::to_string(error) + " " +
                                          cudaGetErrorName(error) + " " + cudaGetErrorString(error)});
}
}  // namespace morpheus::detail

#define CUDA_TRY(call)                                                      \
    do                                                                      \
    {                                                                       \
        cudaError_t const status = (call);                                  \
        if (cudaSuccess != status)                                          \
        {                                                                   \
            cudaGetLastError();                                             \
            morpheus::detail::throw_cuda_error(status, __FILE__, __LINE__); \
        }                                                                   \
    } while (0);

#define CHECK_CUDA(stream)                       \
    do                                           \
    {                                            \
        CUDA_TRY(cudaStreamSynchronize(stream)); \
        CUDA_TRY(cudaPeekAtLastError());         \
    } while (0);