nvidia/cuda: Use util::library and remove CUDA library argument

The CUDA library is always available as a singleton, so it does not make sense for it to be passed in. Instead we can simply grab it from the singleton and use it as it is, which makes the code easier to maintain and automates certain code.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-11-05 06:39:01 +01:00
parent b4a229e26f
commit 98f711523e
10 changed files with 51 additions and 50 deletions

View file

@ -31,13 +31,7 @@
#endif
#endif
nvidia::cuda::context::context(std::shared_ptr<::nvidia::cuda::cuda> cuda)
: _cuda(cuda), _ctx(), _has_device(false), _device()
{
if (!cuda)
throw std::invalid_argument("cuda");
}
nvidia::cuda::context::context() : _cuda(::nvidia::cuda::cuda::get()), _ctx(), _has_device(false), _device() {}
nvidia::cuda::context::~context()
{
if (_has_device) {
@ -47,7 +41,7 @@ nvidia::cuda::context::~context()
}
#ifdef WIN32
nvidia::cuda::context::context(std::shared_ptr<::nvidia::cuda::cuda> cuda, ID3D11Device* device) : context(cuda)
nvidia::cuda::context::context(ID3D11Device* device) : context()
{
using namespace nvidia::cuda;

View file

@ -31,13 +31,13 @@ namespace nvidia::cuda {
::nvidia::cuda::device_t _device;
private:
context(std::shared_ptr<::nvidia::cuda::cuda> cuda);
context();
public:
~context();
#ifdef WIN32
context(std::shared_ptr<::nvidia::cuda::cuda> cuda, ID3D11Device* device);
context(ID3D11Device* device);
#endif
::nvidia::cuda::context_t get();

View file

@ -20,13 +20,11 @@
#include "nvidia-cuda-gs-texture.hpp"
#include "obs/gs/gs-helper.hpp"
nvidia::cuda::gstexture::gstexture(std::shared_ptr<nvidia::cuda::cuda> cuda, std::shared_ptr<gs::texture> texture)
: _cuda(cuda), _texture(texture), _resource(), _is_mapped(false), _pointer()
nvidia::cuda::gstexture::gstexture(std::shared_ptr<gs::texture> texture)
: _cuda(::nvidia::cuda::cuda::get()), _texture(texture), _resource(), _is_mapped(false), _pointer()
{
if (!texture)
throw std::invalid_argument("texture");
if (!cuda)
throw std::invalid_argument("cuda");
gs::context gctx;
int dev_type = gs_get_device_type();

View file

@ -35,7 +35,7 @@ namespace nvidia::cuda {
std::shared_ptr<nvidia::cuda::stream> _stream;
public:
gstexture(std::shared_ptr<nvidia::cuda::cuda> cuda, std::shared_ptr<gs::texture> texture);
gstexture(std::shared_ptr<gs::texture> texture);
~gstexture();
array_t map(std::shared_ptr<nvidia::cuda::stream> stream);

View file

@ -20,10 +20,9 @@
#include "nvidia-cuda-memory.hpp"
#include <stdexcept>
nvidia::cuda::memory::memory(std::shared_ptr<::nvidia::cuda::cuda> cuda, std::size_t size)
: _cuda(cuda), _pointer(), _size(size)
nvidia::cuda::memory::memory(size_t size) : _cuda(::nvidia::cuda::cuda::get()), _pointer(), _size(size)
{
::nvidia::cuda::result res = _cuda->cuMemAlloc(&_pointer, size);
::nvidia::cuda::result res = _cuda->cuMemAlloc(&_pointer, _size);
switch (res) {
case ::nvidia::cuda::result::SUCCESS:
break;
@ -36,7 +35,6 @@ nvidia::cuda::memory::~memory()
{
_cuda->cuMemFree(_pointer);
}
nvidia::cuda::device_ptr_t nvidia::cuda::memory::get()
{
return _pointer;

View file

@ -26,10 +26,10 @@ namespace nvidia::cuda {
class memory {
std::shared_ptr<::nvidia::cuda::cuda> _cuda;
device_ptr_t _pointer;
std::size_t _size;
size_t _size;
public:
memory(std::shared_ptr<::nvidia::cuda::cuda> cuda, std::size_t size);
memory(size_t size);
~memory();
device_ptr_t get();

View file

@ -20,9 +20,7 @@
#include "nvidia-cuda-stream.hpp"
#include <stdexcept>
nvidia::cuda::stream::stream(std::shared_ptr<::nvidia::cuda::cuda> cuda, ::nvidia::cuda::stream_flags flags,
int32_t priority)
: _cuda(cuda)
nvidia::cuda::stream::stream(::nvidia::cuda::stream_flags flags, int32_t priority) : _cuda(::nvidia::cuda::cuda::get())
{
nvidia::cuda::result res;
if (priority == 0) {

View file

@ -27,9 +27,8 @@ namespace nvidia::cuda {
::nvidia::cuda::stream_t _stream;
public:
stream(std::shared_ptr<::nvidia::cuda::cuda> cuda,
::nvidia::cuda::stream_flags flags = ::nvidia::cuda::stream_flags::DEFAULT, int32_t priority = 0);
~stream();
stream(::nvidia::cuda::stream_flags flags = ::nvidia::cuda::stream_flags::DEFAULT, int32_t priority = 0);
::nvidia::cuda::stream_t get();
};

View file

@ -18,8 +18,22 @@
*/
#include "nvidia-cuda.hpp"
#include "common.hpp"
#include <mutex>
#include "util/util-logging.hpp"
#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
#if defined(_WIN32) || defined(_WIN64)
#define CUDA_NAME "nvcuda.dll"
@ -29,28 +43,33 @@
#define CUDA_LOAD_SYMBOL(NAME) \
{ \
NAME = reinterpret_cast<decltype(NAME)>(os_dlsym(_library, #NAME)); \
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME)); \
if (!NAME) \
throw std::runtime_error("Failed to load '" #NAME "' from '" CUDA_NAME "'."); \
}
#define CUDA_LOAD_SYMBOL_V2(NAME) \
{ \
NAME = reinterpret_cast<decltype(NAME)>(os_dlsym(_library, #NAME "_v2")); \
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#NAME "_v2")); \
if (!NAME) \
throw std::runtime_error("Failed to load '" #NAME "' from '" CUDA_NAME "'."); \
}
#define CUDA_LOAD_SYMBOL_EX(NAME, OVERRIDE) \
{ \
NAME = reinterpret_cast<decltype(NAME)>(os_dlsym(_library, #OVERRIDE)); \
NAME = reinterpret_cast<decltype(NAME)>(_library->load_symbol(#OVERRIDE)); \
if (!NAME) \
throw std::runtime_error("Failed to load '" #NAME "' from '" CUDA_NAME "'."); \
}
nvidia::cuda::cuda::cuda()
nvidia::cuda::cuda::~cuda()
{
_library = os_dlopen(CUDA_NAME);
if (!_library)
throw std::runtime_error("Failed to load '" CUDA_NAME "'.");
D_LOG_DEBUG("Finalizing... (Addr: 0x%" PRIuPTR ")", this);
}
nvidia::cuda::cuda::cuda() : _library()
{
D_LOG_DEBUG("Initialization... (Addr: 0x%" PRIuPTR ")", this);
_library = util::library::load(std::string_view(CUDA_NAME));
// Initialization
CUDA_LOAD_SYMBOL(cuInit);
@ -117,19 +136,16 @@ nvidia::cuda::cuda::cuda()
cuInit(0);
}
nvidia::cuda::cuda::~cuda()
{
os_dlclose(_library);
}
std::shared_ptr<nvidia::cuda::cuda> nvidia::cuda::cuda::get()
{
static std::shared_ptr<nvidia::cuda::cuda> instance;
static std::mutex lock;
static std::weak_ptr<nvidia::cuda::cuda> instance;
static std::mutex lock;
std::unique_lock<std::mutex> ul(lock);
if (!instance) {
instance = std::make_shared<nvidia::cuda::cuda>();
if (instance.expired()) {
auto hard_instance = std::make_shared<nvidia::cuda::cuda>();
instance = hard_instance;
return hard_instance;
}
return instance;
return instance.lock();
}

View file

@ -18,10 +18,9 @@
*/
#pragma once
#include "common.hpp"
#include <cstddef>
#include <functional>
#include <memory>
#include "util/util-bitmask.hpp"
#include "util/util-library.hpp"
#ifdef WIN32
#pragma warning(push)
@ -128,8 +127,7 @@ namespace nvidia::cuda {
};
class cuda {
private:
void* _library;
std::shared_ptr<util::library> _library;
public:
cuda();
@ -395,7 +393,7 @@ namespace nvidia::cuda {
ID3D11Resource* d3dresource, uint32_t flags);
#endif
public:
static std::shared_ptr<cuda> get();
static std::shared_ptr<::nvidia::cuda::cuda> get();
};
} // namespace nvidia::cuda