2020-03-20 22:10:19 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2020 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nvidia-cuda.hpp"
|
2020-11-05 05:39:01 +00:00
|
|
|
#include "util/util-logging.hpp"
|
|
|
|
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
|
|
|
#include <mutex>
|
|
|
|
#include "warning-enable.hpp"
|
|
|
|
|
2020-11-05 05:39:01 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ST_PREFIX "<%s> "
|
|
|
|
#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ST_PREFIX "<nvidia::cuda::cuda> "
|
|
|
|
#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__)
|
|
|
|
#endif
|
2020-03-20 22:10:19 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2021-06-08 02:42:43 +00:00
|
|
|
#define ST_CUDA_NAME "nvcuda.dll"
|
2020-03-20 22:10:19 +00:00
|
|
|
#else
|
2021-06-08 02:42:43 +00:00
|
|
|
#define ST_CUDA_NAME "libcuda.so.1"
|
2020-03-20 22:10:19 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL(NAME) \
|
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME)); \
|
|
|
|
if (!NAME) \
|
|
|
|
throw std::runtime_error("Failed to load '" #NAME "' from '" ST_CUDA_NAME "'."); \
|
2020-03-20 22:10:19 +00:00
|
|
|
}
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL_OPT(NAME) \
|
2021-04-26 01:50:17 +00:00
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME)); \
|
|
|
|
if (!NAME) \
|
|
|
|
D_LOG_WARNING("Loading of optional symbol '" #NAME "' failed.", 0); \
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL_EX(NAME, OVERRIDE) \
|
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#OVERRIDE)); \
|
|
|
|
if (!NAME) \
|
|
|
|
throw std::runtime_error("Failed to load '" #NAME "' from '" ST_CUDA_NAME "'."); \
|
2020-03-20 22:10:19 +00:00
|
|
|
}
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL_OPT_EX(NAME, OVERRIDE) \
|
2021-04-26 01:50:17 +00:00
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#OVERRIDE)); \
|
|
|
|
if (!NAME) \
|
|
|
|
D_LOG_WARNING("Loading of optional symbol '" #NAME "' failed.", 0); \
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL_V2(NAME) \
|
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME "_v2")); \
|
|
|
|
if (!NAME) \
|
|
|
|
throw std::runtime_error("Failed to load '" #NAME "' from '" ST_CUDA_NAME "'."); \
|
2020-03-20 22:10:19 +00:00
|
|
|
}
|
2021-06-08 02:42:43 +00:00
|
|
|
#define P_CUDA_LOAD_SYMBOL_OPT_V2(NAME) \
|
2021-04-26 01:50:17 +00:00
|
|
|
{ \
|
|
|
|
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME "_v2")); \
|
|
|
|
if (!NAME) \
|
|
|
|
D_LOG_WARNING("Loading of optional symbol '" #NAME "' failed.", 0); \
|
|
|
|
}
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-06-16 10:16:35 +00:00
|
|
|
streamfx::nvidia::cuda::cuda::~cuda()
|
2020-11-05 05:39:01 +00:00
|
|
|
{
|
|
|
|
D_LOG_DEBUG("Finalizing... (Addr: 0x%" PRIuPTR ")", this);
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:16:35 +00:00
|
|
|
streamfx::nvidia::cuda::cuda::cuda() : _library()
|
2020-03-20 22:10:19 +00:00
|
|
|
{
|
2021-04-26 01:50:17 +00:00
|
|
|
int32_t cuda_version = 0;
|
|
|
|
|
2021-05-01 16:31:13 +00:00
|
|
|
D_LOG_DEBUG("Initializing... (Addr: 0x%" PRIuPTR ")", this);
|
2020-11-05 05:39:01 +00:00
|
|
|
|
2021-06-08 02:42:43 +00:00
|
|
|
_library = streamfx::util::library::load(std::string_view(ST_CUDA_NAME));
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
{ // 1. Load critical initialization functions.
|
|
|
|
// Initialization
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuInit);
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
// Version Management
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuDriverGetVersion);
|
2021-04-26 01:50:17 +00:00
|
|
|
}
|
2020-03-20 22:10:19 +00:00
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
{ // 2. Get the CUDA Driver version and log it.
|
2021-04-26 00:34:19 +00:00
|
|
|
if (cuDriverGetVersion(&cuda_version) == result::SUCCESS) {
|
|
|
|
int32_t major = cuda_version / 1000;
|
|
|
|
int32_t minor = (cuda_version % 1000) / 10;
|
|
|
|
int32_t patch = (cuda_version % 10);
|
|
|
|
D_LOG_INFO("Driver reported CUDA version: %" PRId32 ".%" PRId32 ".%" PRId32, major, minor, patch);
|
|
|
|
} else {
|
|
|
|
D_LOG_WARNING("Failed to query NVIDIA CUDA Driver for version.", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 01:50:17 +00:00
|
|
|
{ // 3. Load remaining functions.
|
|
|
|
// Device Management
|
2021-06-17 09:43:28 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuDeviceGetName);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuDeviceGetLuid);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuDeviceGetUuid);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Primary Context Management
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuDevicePrimaryCtxRetain);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuDevicePrimaryCtxRelease);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuDevicePrimaryCtxSetFlags);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Context Management
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuCtxCreate);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuCtxDestroy);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuCtxPushCurrent);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuCtxPopCurrent);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuCtxGetCurrent);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuCtxSetCurrent);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuCtxGetStreamPriorityRange);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuCtxSynchronize);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Module Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Memory Management
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemAlloc);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemAllocPitch);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemFree);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuMemcpy);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemcpy2D);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemcpy2DAsync);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuArrayGetDescriptor);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyAtoA);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyAtoD);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyAtoH);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyAtoHAsync);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyDtoA);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyDtoD);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyDtoH);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyDtoHAsync);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyHtoA);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyHtoAAsync);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyHtoD);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemcpyHtoDAsync);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemHostGetDevicePointer);
|
2021-09-28 01:46:50 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuMemsetD8);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuMemsetD8Async);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemsetD16);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuMemsetD16Async);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT_V2(cuMemsetD32);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuMemsetD32Async);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Virtual Memory Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Stream Ordered Memory Allocator
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Unified Addressing
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Stream Management
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuStreamCreate);
|
|
|
|
P_CUDA_LOAD_SYMBOL_V2(cuStreamDestroy);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuStreamSynchronize);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuStreamCreateWithPriority);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuStreamGetPriority);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Event Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// External Resource Interoperability (CUDA 11.1+)
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Stream Memory Operations
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Execution Control
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Graph Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Occupancy
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Texture Object Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Surface Object Management
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Peer Context Memory Access
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Graphics Interoperability
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuGraphicsMapResources);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuGraphicsSubResourceGetMappedArray);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuGraphicsUnmapResources);
|
|
|
|
P_CUDA_LOAD_SYMBOL(cuGraphicsUnregisterResource);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Driver Entry Point Access
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Profiler Control
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// OpenGL Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// VDPAU Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// EGL Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
// Direct3D9 Interoperability
|
|
|
|
// - Not yet needed.
|
|
|
|
|
|
|
|
// Direct3D10 Interoperability
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuD3D10GetDevice);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuGraphicsD3D10RegisterResource);
|
2021-04-26 01:50:17 +00:00
|
|
|
|
|
|
|
// Direct3D11 Interoperability
|
2021-06-08 02:42:43 +00:00
|
|
|
P_CUDA_LOAD_SYMBOL(cuD3D11GetDevice);
|
|
|
|
P_CUDA_LOAD_SYMBOL_OPT(cuGraphicsD3D11RegisterResource);
|
2021-04-26 01:50:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-20 22:10:19 +00:00
|
|
|
// Initialize CUDA
|
|
|
|
cuInit(0);
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:16:35 +00:00
|
|
|
int32_t streamfx::nvidia::cuda::cuda::version()
|
2021-04-26 02:05:44 +00:00
|
|
|
{
|
|
|
|
int32_t v = 0;
|
|
|
|
cuDriverGetVersion(&v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:16:35 +00:00
|
|
|
std::shared_ptr<streamfx::nvidia::cuda::cuda> streamfx::nvidia::cuda::cuda::get()
|
2020-06-14 23:10:02 +00:00
|
|
|
{
|
2021-06-16 10:16:35 +00:00
|
|
|
static std::weak_ptr<streamfx::nvidia::cuda::cuda> instance;
|
|
|
|
static std::mutex lock;
|
2020-06-14 23:10:02 +00:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> ul(lock);
|
2020-11-05 05:39:01 +00:00
|
|
|
if (instance.expired()) {
|
2021-06-16 10:16:35 +00:00
|
|
|
auto hard_instance = std::make_shared<streamfx::nvidia::cuda::cuda>();
|
2020-11-05 05:39:01 +00:00
|
|
|
instance = hard_instance;
|
|
|
|
return hard_instance;
|
2020-06-14 23:10:02 +00:00
|
|
|
}
|
2020-11-05 05:39:01 +00:00
|
|
|
return instance.lock();
|
2020-06-14 23:10:02 +00:00
|
|
|
}
|