cuda: Hide runtime compiler related header and symbols

That's already abstracted via gst_cuda_nvrtc_compile() method
and therefore, we do not need to expose such symbols yet.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2884>
This commit is contained in:
Seungha Yang 2022-08-14 21:49:27 +09:00
parent d9bd870db4
commit 75e8f80999
7 changed files with 198 additions and 309 deletions

View file

@ -1,7 +1,6 @@
#pragma once
#include "cuda-prelude.h"
#include <nvrtc.h>
#include <cuda.h>
#include <cudaGL.h>
@ -202,37 +201,4 @@ CUresult CUDAAPI CuD3D11GetDevices(unsigned int * pCudaDeviceCount,
CUD3D11DeviceList deviceList);
#endif
/* nvrtc.h */
GST_CUDA_API
nvrtcResult NvrtcCompileProgram (nvrtcProgram prog,
int numOptions,
const char** options);
GST_CUDA_API
nvrtcResult NvrtcCreateProgram (nvrtcProgram* prog,
const char* src,
const char* name,
int numHeaders,
const char** headers,
const char** includeNames);
GST_CUDA_API
nvrtcResult NvrtcDestroyProgram (nvrtcProgram* prog);
GST_CUDA_API
nvrtcResult NvrtcGetPTX (nvrtcProgram prog,
char* ptx);
GST_CUDA_API
nvrtcResult NvrtcGetPTXSize (nvrtcProgram prog,
size_t* ptxSizeRet);
GST_CUDA_API
nvrtcResult NvrtcGetProgramLog (nvrtcProgram prog,
char* log);
GST_CUDA_API
nvrtcResult NvrtcGetProgramLogSize (nvrtcProgram prog,
size_t* logSizeRet);
G_END_DECLS

View file

@ -23,23 +23,208 @@
#include "cuda-gst.h"
#include "gstcudanvrtc.h"
#include "gstcudaloader.h"
#include <nvrtc.h>
#include <gmodule.h>
GST_DEBUG_CATEGORY_STATIC (gst_cuda_nvrtc_debug);
#define GST_CAT_DEFAULT gst_cuda_nvrtc_debug
static void
_init_debug (void)
#ifndef G_OS_WIN32
#define NVRTC_LIBNAME "libnvrtc.so"
#else
#define NVRTC_LIBNAME "nvrtc64_%d%d_0.dll"
#endif
#define LOAD_SYMBOL(name,func) G_STMT_START { \
if (!g_module_symbol (module, G_STRINGIFY (name), (gpointer *) &vtable->func)) { \
GST_ERROR ("Failed to load '%s' from %s, %s", G_STRINGIFY (name), fname, g_module_error()); \
goto error; \
} \
} G_STMT_END;
/* *INDENT-OFF* */
typedef struct _GstCudaNvrtcVTable
{
static gsize once_init = 0;
gboolean loaded;
if (g_once_init_enter (&once_init)) {
nvrtcResult (*NvrtcCompileProgram) (nvrtcProgram prog, int numOptions,
const char **options);
nvrtcResult (*NvrtcCreateProgram) (nvrtcProgram * prog, const char *src,
const char *name, int numHeaders, const char **headers,
const char **includeNames);
nvrtcResult (*NvrtcDestroyProgram) (nvrtcProgram * prog);
nvrtcResult (*NvrtcGetPTX) (nvrtcProgram prog, char *ptx);
nvrtcResult (*NvrtcGetPTXSize) (nvrtcProgram prog, size_t * ptxSizeRet);
nvrtcResult (*NvrtcGetProgramLog) (nvrtcProgram prog, char *log);
nvrtcResult (*NvrtcGetProgramLogSize) (nvrtcProgram prog,
size_t * logSizeRet);
} GstCudaNvrtcVTable;
/* *INDENT-ON* */
static GstCudaNvrtcVTable gst_cuda_nvrtc_vtable = { 0, };
static gboolean
gst_cuda_nvrtc_load_library_once (void)
{
GModule *module = NULL;
gchar *filename = NULL;
const gchar *filename_env;
const gchar *fname;
gint cuda_version;
GstCudaNvrtcVTable *vtable;
CuDriverGetVersion (&cuda_version);
fname = filename_env = g_getenv ("GST_CUDA_NVRTC_LIBNAME");
if (filename_env)
module = g_module_open (filename_env, G_MODULE_BIND_LAZY);
if (!module) {
#ifndef G_OS_WIN32
filename = g_strdup (NVRTC_LIBNAME);
fname = filename;
module = g_module_open (filename, G_MODULE_BIND_LAZY);
#else
/* XXX: On Windows, minor version of nvrtc library might not be exactly
* same as CUDA library */
{
gint cuda_major_version = cuda_version / 1000;
gint cuda_minor_version = (cuda_version % 1000) / 10;
gint minor_version;
for (minor_version = cuda_minor_version; minor_version >= 0;
minor_version--) {
g_free (filename);
filename = g_strdup_printf (NVRTC_LIBNAME, cuda_major_version,
minor_version);
fname = filename;
module = g_module_open (filename, G_MODULE_BIND_LAZY);
if (module) {
GST_INFO ("%s is available", filename);
break;
}
GST_DEBUG ("Couldn't open library %s", filename);
}
}
#endif
}
if (module == NULL) {
GST_WARNING ("Could not open library %s, %s", filename, g_module_error ());
g_free (filename);
return FALSE;
}
vtable = &gst_cuda_nvrtc_vtable;
LOAD_SYMBOL (nvrtcCompileProgram, NvrtcCompileProgram);
LOAD_SYMBOL (nvrtcCreateProgram, NvrtcCreateProgram);
LOAD_SYMBOL (nvrtcDestroyProgram, NvrtcDestroyProgram);
LOAD_SYMBOL (nvrtcGetPTX, NvrtcGetPTX);
LOAD_SYMBOL (nvrtcGetPTXSize, NvrtcGetPTXSize);
LOAD_SYMBOL (nvrtcGetProgramLog, NvrtcGetProgramLog);
LOAD_SYMBOL (nvrtcGetProgramLogSize, NvrtcGetProgramLogSize);
vtable->loaded = TRUE;
g_free (filename);
return TRUE;
error:
g_module_close (module);
g_free (filename);
return FALSE;
}
/**
* gst_cuda_nvrtc_load_library:
*
* Loads the nvrtc library.
*
* Returns: %TRUE if the library could be loaded, %FALSE otherwise
*
* Since: 1.22
*/
gboolean
gst_cuda_nvrtc_load_library (void)
{
static gsize init_once = 0;
if (g_once_init_enter (&init_once)) {
GST_DEBUG_CATEGORY_INIT (gst_cuda_nvrtc_debug, "cudanvrtc", 0,
"CUDA runtime compiler");
g_once_init_leave (&once_init, 1);
if (gst_cuda_load_library ())
gst_cuda_nvrtc_load_library_once ();
g_once_init_leave (&init_once, 1);
}
return gst_cuda_nvrtc_vtable.loaded;
}
/* *INDENT-OFF* */
static nvrtcResult
NvrtcCompileProgram (nvrtcProgram prog, int numOptions, const char **options)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcCompileProgram != NULL);
return gst_cuda_nvrtc_vtable.NvrtcCompileProgram (prog, numOptions, options);
}
static nvrtcResult
NvrtcCreateProgram (nvrtcProgram * prog, const char *src, const char *name,
int numHeaders, const char **headers, const char **includeNames)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcCreateProgram != NULL);
return gst_cuda_nvrtc_vtable.NvrtcCreateProgram (prog, src, name, numHeaders,
headers, includeNames);
}
static nvrtcResult
NvrtcDestroyProgram (nvrtcProgram * prog)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcDestroyProgram != NULL);
return gst_cuda_nvrtc_vtable.NvrtcDestroyProgram (prog);
}
static nvrtcResult
NvrtcGetPTX (nvrtcProgram prog, char *ptx)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcGetPTX != NULL);
return gst_cuda_nvrtc_vtable.NvrtcGetPTX (prog, ptx);
}
static nvrtcResult
NvrtcGetPTXSize (nvrtcProgram prog, size_t *ptxSizeRet)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcGetPTXSize != NULL);
return gst_cuda_nvrtc_vtable.NvrtcGetPTXSize (prog, ptxSizeRet);
}
static nvrtcResult
NvrtcGetProgramLog (nvrtcProgram prog, char *log)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcGetProgramLog != NULL);
return gst_cuda_nvrtc_vtable.NvrtcGetProgramLog (prog, log);
}
static nvrtcResult
NvrtcGetProgramLogSize (nvrtcProgram prog, size_t *logSizeRet)
{
g_assert (gst_cuda_nvrtc_vtable.NvrtcGetProgramLogSize != NULL);
return gst_cuda_nvrtc_vtable.NvrtcGetProgramLogSize (prog, logSizeRet);
}
/* *INDENT-ON* */
/**
* gst_cuda_nvrtc_compile:
* @source: Source code to compile
@ -57,9 +242,11 @@ gst_cuda_nvrtc_compile (const gchar * source)
gchar *ptx = NULL;
int driverVersion;
g_return_val_if_fail (source != NULL, FALSE);
g_return_val_if_fail (source != NULL, NULL);
_init_debug ();
if (!gst_cuda_nvrtc_load_library ()) {
return NULL;
}
GST_TRACE ("CUDA kernel source \n%s", source);

View file

@ -27,11 +27,12 @@
#include "cuda-prelude.h"
#include <gst/gst.h>
#include "gstcudaloader.h"
#include "gstnvrtcloader.h"
G_BEGIN_DECLS
GST_CUDA_API
gboolean gst_cuda_nvrtc_load_library (void);
GST_CUDA_API
gchar * gst_cuda_nvrtc_compile (const gchar * source);

View file

@ -1,224 +0,0 @@
/* GStreamer
* Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cuda-gst.h"
#include "gstnvrtcloader.h"
#include "gstcudaloader.h"
#include <gmodule.h>
GST_DEBUG_CATEGORY (gst_nvrtcloader_debug);
#define GST_CAT_DEFAULT gst_nvrtcloader_debug
#ifndef G_OS_WIN32
#define NVRTC_LIBNAME "libnvrtc.so"
#else
#define NVRTC_LIBNAME "nvrtc64_%d%d_0.dll"
#endif
#define LOAD_SYMBOL(name,func) G_STMT_START { \
if (!g_module_symbol (module, G_STRINGIFY (name), (gpointer *) &vtable->func)) { \
GST_ERROR ("Failed to load '%s' from %s, %s", G_STRINGIFY (name), fname, g_module_error()); \
goto error; \
} \
} G_STMT_END;
/* *INDENT-OFF* */
typedef struct _GstNvCodecNvrtcVtahle
{
gboolean loaded;
nvrtcResult (*NvrtcCompileProgram) (nvrtcProgram prog, int numOptions,
const char **options);
nvrtcResult (*NvrtcCreateProgram) (nvrtcProgram * prog, const char *src,
const char *name, int numHeaders, const char **headers,
const char **includeNames);
nvrtcResult (*NvrtcDestroyProgram) (nvrtcProgram * prog);
nvrtcResult (*NvrtcGetPTX) (nvrtcProgram prog, char *ptx);
nvrtcResult (*NvrtcGetPTXSize) (nvrtcProgram prog, size_t * ptxSizeRet);
nvrtcResult (*NvrtcGetProgramLog) (nvrtcProgram prog, char *log);
nvrtcResult (*NvrtcGetProgramLogSize) (nvrtcProgram prog,
size_t * logSizeRet);
} GstNvCodecNvrtcVtahle;
/* *INDENT-ON* */
static GstNvCodecNvrtcVtahle gst_nvrtc_vtable = { 0, };
/**
* gst_nvrtc_load_library:
*
* Loads the nvrtc library.
*
* Returns: %TRUE if the library could be loaded, %FALSE otherwise
*
* Since: 1.22
*/
gboolean
gst_nvrtc_load_library (void)
{
GModule *module = NULL;
gchar *filename = NULL;
const gchar *filename_env;
const gchar *fname;
gint cuda_version;
GstNvCodecNvrtcVtahle *vtable;
static gsize debug_initialized = FALSE;
if (gst_nvrtc_vtable.loaded)
return TRUE;
if (g_once_init_enter (&debug_initialized)) {
GST_DEBUG_CATEGORY_INIT (gst_nvrtcloader_debug, "nvrtcloader", 0,
"nvrtcloader");
g_once_init_leave (&debug_initialized, TRUE);
}
CuDriverGetVersion (&cuda_version);
fname = filename_env = g_getenv ("GST_NVCODEC_NVRTC_LIBNAME");
if (filename_env)
module = g_module_open (filename_env, G_MODULE_BIND_LAZY);
if (!module) {
#ifndef G_OS_WIN32
filename = g_strdup (NVRTC_LIBNAME);
fname = filename;
module = g_module_open (filename, G_MODULE_BIND_LAZY);
#else
/* XXX: On Windows, minor version of nvrtc library might not be exactly
* same as CUDA library */
{
gint cuda_major_version = cuda_version / 1000;
gint cuda_minor_version = (cuda_version % 1000) / 10;
gint minor_version;
for (minor_version = cuda_minor_version; minor_version >= 0;
minor_version--) {
g_free (filename);
filename = g_strdup_printf (NVRTC_LIBNAME, cuda_major_version,
minor_version);
fname = filename;
module = g_module_open (filename, G_MODULE_BIND_LAZY);
if (module) {
GST_INFO ("%s is available", filename);
break;
}
GST_DEBUG ("Couldn't open library %s", filename);
}
}
#endif
}
if (module == NULL) {
GST_WARNING ("Could not open library %s, %s", filename, g_module_error ());
g_free (filename);
return FALSE;
}
vtable = &gst_nvrtc_vtable;
LOAD_SYMBOL (nvrtcCompileProgram, NvrtcCompileProgram);
LOAD_SYMBOL (nvrtcCreateProgram, NvrtcCreateProgram);
LOAD_SYMBOL (nvrtcDestroyProgram, NvrtcDestroyProgram);
LOAD_SYMBOL (nvrtcGetPTX, NvrtcGetPTX);
LOAD_SYMBOL (nvrtcGetPTXSize, NvrtcGetPTXSize);
LOAD_SYMBOL (nvrtcGetProgramLog, NvrtcGetProgramLog);
LOAD_SYMBOL (nvrtcGetProgramLogSize, NvrtcGetProgramLogSize);
vtable->loaded = TRUE;
g_free (filename);
return TRUE;
error:
g_module_close (module);
g_free (filename);
return FALSE;
}
nvrtcResult
NvrtcCompileProgram (nvrtcProgram prog, int numOptions, const char **options)
{
g_assert (gst_nvrtc_vtable.NvrtcCompileProgram != NULL);
return gst_nvrtc_vtable.NvrtcCompileProgram (prog, numOptions, options);
}
nvrtcResult
NvrtcCreateProgram (nvrtcProgram * prog, const char *src, const char *name,
int numHeaders, const char **headers, const char **includeNames)
{
g_assert (gst_nvrtc_vtable.NvrtcCreateProgram != NULL);
return gst_nvrtc_vtable.NvrtcCreateProgram (prog, src, name, numHeaders,
headers, includeNames);
}
nvrtcResult
NvrtcDestroyProgram (nvrtcProgram * prog)
{
g_assert (gst_nvrtc_vtable.NvrtcDestroyProgram != NULL);
return gst_nvrtc_vtable.NvrtcDestroyProgram (prog);
}
nvrtcResult
NvrtcGetPTX (nvrtcProgram prog, char *ptx)
{
g_assert (gst_nvrtc_vtable.NvrtcGetPTX != NULL);
return gst_nvrtc_vtable.NvrtcGetPTX (prog, ptx);
}
/* *INDENT-OFF* */
nvrtcResult
NvrtcGetPTXSize (nvrtcProgram prog, size_t * ptxSizeRet)
{
g_assert (gst_nvrtc_vtable.NvrtcGetPTXSize != NULL);
return gst_nvrtc_vtable.NvrtcGetPTXSize (prog, ptxSizeRet);
}
/* *INDENT-ON* */
nvrtcResult
NvrtcGetProgramLog (nvrtcProgram prog, char *log)
{
g_assert (gst_nvrtc_vtable.NvrtcGetProgramLog != NULL);
return gst_nvrtc_vtable.NvrtcGetProgramLog (prog, log);
}
/* *INDENT-OFF* */
nvrtcResult
NvrtcGetProgramLogSize (nvrtcProgram prog, size_t * logSizeRet)
{
g_assert (gst_nvrtc_vtable.NvrtcGetProgramLogSize != NULL);
return gst_nvrtc_vtable.NvrtcGetProgramLogSize (prog, logSizeRet);
}
/* *INDENT-ON* */

View file

@ -1,38 +0,0 @@
/* GStreamer
* Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_NVRTC_LOADER_H__
#define __GST_NVRTC_LOADER_H__
#ifndef GST_USE_UNSTABLE_API
#warning "The Cuda library from gst-plugins-bad is unstable API and may change in future."
#warning "You can define GST_USE_UNSTABLE_API to avoid this warning."
#endif
#include "cuda-prelude.h"
#include <gst/gst.h>
#include <nvrtc.h>
G_BEGIN_DECLS
GST_CUDA_API
gboolean gst_nvrtc_load_library (void);
G_END_DECLS
#endif /* __GST_NVRTC_LOADER_H__ */

View file

@ -5,7 +5,6 @@ cuda_sources = [
'gstcudamemory.c',
'gstcudabufferpool.c',
'gstcudanvrtc.c',
'gstnvrtcloader.c',
]
cuda_headers = [
@ -16,7 +15,6 @@ cuda_headers = [
'gstcudamemory.h',
'gstcudanvrtc.h',
'gstcudautils.h',
'gstnvrtcloader.h',
]
gstcuda_dep = dependency('', required : false)

View file

@ -22,7 +22,6 @@
#endif
#include <gst/cuda/gstcudaloader.h>
#include <gst/cuda/gstnvrtcloader.h>
#include <gst/cuda/gstcudanvrtc.h>
#include "gstcudafilter.h"
@ -40,7 +39,7 @@ gst_cuda_filter_plugin_init (GstPlugin * plugin)
{
gchar *test_ptx = NULL;
if (!gst_nvrtc_load_library ())
if (!gst_cuda_nvrtc_load_library ())
return;
test_ptx = gst_cuda_nvrtc_compile (nvrtc_test_source);