Program Listing for File cuda_rtc.hpp
↰ Return to documentation for file (include/holoscan/utils/cuda/cuda_rtc.hpp)
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 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.
*/
#ifndef HOLOSCAN_UTILS_CUDA_CUDA_RTC_HPP
#define HOLOSCAN_UTILS_CUDA_CUDA_RTC_HPP
#include <cuda.h>
#include <vector_types.h>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "holoscan/utils/cuda/nullable_pointer.hpp"
namespace holoscan {
#define CudaCheck(FUNC) \
{ \
const CUresult result = FUNC; \
if (result != CUDA_SUCCESS) { \
const char* error_name = ""; \
cuGetErrorName(result, &error_name); \
const char* error_string = ""; \
cuGetErrorString(result, &error_string); \
std::stringstream buf; \
buf << "[" << __FILE__ << ":" << __LINE__ << "] CUDA driver error " << result << " (" \
<< error_name << "): " << error_string; \
throw std::runtime_error(buf.str().c_str()); \
} \
}
class CudaFunctionLauncher {
public:
CudaFunctionLauncher(const char* source, const std::vector<std::string>& functions,
const std::vector<std::string>& options = std::vector<std::string>());
~CudaFunctionLauncher();
template <class... TYPES>
void launch(const std::string& name, const dim3& grid, CUstream stream, TYPES... args) const {
void* args_array[] = {reinterpret_cast<void*>(&args)...};
launch_internal(name, grid, nullptr, stream, args_array);
}
template <class... TYPES>
void launch(const std::string& name, const dim3& grid, const dim3& block, CUstream stream,
TYPES... args) const {
void* args_array[] = {reinterpret_cast<void*>(&args)...};
launch_internal(name, grid, &block, stream, args_array);
}
private:
void launch_internal(const std::string& name, const dim3& grid, const dim3* block,
CUstream stream, void** args) const;
CUmodule module_ = nullptr;
struct LaunchParams {
dim3 block_dim_;
CUfunction function_;
};
std::unordered_map<std::string, LaunchParams> functions_;
};
using UniqueCUdeviceptr =
std::unique_ptr<Nullable<CUdeviceptr>, Nullable<CUdeviceptr>::Deleter<CUresult, &cuMemFree>>;
template <typename T, CUresult func(T)>
struct Deleter {
typedef T pointer;
void operator()(T value) const { func(value); }
};
using UniqueCUhostptr = std::unique_ptr<void, Deleter<void*, &cuMemFreeHost>>;
using UniqueCUevent = std::unique_ptr<CUevent, Deleter<CUevent, &cuEventDestroy>>;
using UniqueCUstream = std::unique_ptr<CUstream, Deleter<CUstream, &cuStreamDestroy>>;
class CudaContextScopedPush {
public:
explicit CudaContextScopedPush(CUcontext cuda_context);
CudaContextScopedPush() = delete;
~CudaContextScopedPush();
private:
const CUcontext cuda_context_;
};
} // namespace holoscan
#endif/* HOLOSCAN_UTILS_CUDA_CUDA_RTC_HPP */