ErrorCheckMacros.h#

Fully qualified name: public/src/operator/include/ErrorCheckMacros.h

File members: public/src/operator/include/ErrorCheckMacros.h

/*
 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
 *
 * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
 * property and proprietary rights in and to this material, related
 * documentation and any modifications thereto. Any use, reproduction,
 * disclosure or distribution of this material and related documentation
 * without an express license agreement from NVIDIA CORPORATION or
 * its affiliates is strictly prohibited.
 */

#ifndef ERROR_CHECK_MACROS_H
#define ERROR_CHECK_MACROS_H

#include <cuda_runtime.h>
#include <cupva_host.h>
#include <nvcv/Status.h>

#ifdef __cplusplus
#    include <cstdio>
#    include <cstring>
#    include <stdexcept>
#else
#    include <stdio.h>
#endif

#define CUPVA_CHECK_ERROR_GOTO(__v, __e, __l)                                         \
    __e = __v;                                                                        \
    if (__e != CUPVA_ERROR_NONE)                                                      \
    {                                                                                 \
        const char *msgBuffer;                                                        \
        CupvaGetLastError(&msgBuffer);                                                \
        printf("CUPVA error: %d at line %d in function %s\n", (__v), __LINE__, #__v); \
        printf("Error message: %s\n", msgBuffer);                                     \
        goto __l;                                                                     \
    }

#define NVCV_CHECK_ERROR_GOTO(__v, __e, __l)                                         \
    __e = __v;                                                                       \
    if (__e != NVCV_SUCCESS)                                                         \
    {                                                                                \
        char msgBuffer[1024];                                                        \
        nvcvPeekAtLastErrorMessage(msgBuffer, sizeof(msgBuffer));                    \
        printf("NVCV error: %d at line %d in function %s\n", (__v), __LINE__, #__v); \
        printf("Error message: %s\n", msgBuffer);                                    \
        goto __l;                                                                    \
    }

#define CUDA_CHECK_ERROR_GOTO(__v, __e, __l)                                         \
    __e = __v;                                                                       \
    if (__e != cudaSuccess)                                                          \
    {                                                                                \
        printf("CUDA error: %d at line %d in function %s\n", (__v), __LINE__, #__v); \
        printf("Error message: %s\n", cudaGetErrorString(__e));                      \
        goto __l;                                                                    \
    }

#ifdef __cplusplus

inline void CupvaCheckError(cupvaError_t err, const char *file, const int line, void (*cleanup_callback)() = nullptr)
{
    if (err != CUPVA_ERROR_NONE)
    {
        if (cleanup_callback != nullptr)
        {
            cleanup_callback();
        }

        const char *msgBuffer;
        CupvaGetLastError(&msgBuffer);

        char message[512];
        snprintf(message, sizeof(message), "CUPVA error returned at %s:%d, Error code: %d (%s)", file, line, err,
                 msgBuffer);

        throw std::runtime_error(message);
    }
}

inline void NvcvCheckError(NVCVStatus status, const char *file, const int line, void (*cleanup_callback)() = nullptr)
{
    if (status != NVCV_SUCCESS)
    {
        if (cleanup_callback != nullptr)
        {
            cleanup_callback();
        }

        char message[512];
        snprintf(message, sizeof(message), "NVCV error returned at %s:%d, Error code: %d", file, line, status);

        throw std::runtime_error(message);
    }
}

#    define CUPVA_CHECK_ERROR(val, ...) CupvaCheckError((val), __FILE__, __LINE__, ##__VA_ARGS__)

#    define NVCV_CHECK_ERROR(val, ...) NvcvCheckError((val), __FILE__, __LINE__, ##__VA_ARGS__)

#endif

#endif /* ERROR_CHECK_MACROS_H */