nvcodec: Add support runtime CUDA kernel source compilation

Add util functions for runtime CUDA kernel source compilation
using NVRTC library. Like other nvcodec dependent libraries,
NVRTC library will be loaded via g_module_open.

Note that the NVRTC library naming is not g_module_open friendly
on Windows.
(i.e., nvrtc64_{CUDA major version}{CUDA minor version}.dll).
So users can specify the dll name using GST_NVCODEC_NVRTC_LIBNAME
environment.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1633>
This commit is contained in:
Seungha Yang 2019-10-16 22:42:24 +09:00 committed by GStreamer Merge Bot
parent dd995bd9d4
commit 4cc73ff9d6
6 changed files with 416 additions and 0 deletions

105
sys/nvcodec/gstcudanvrtc.c Normal file
View file

@ -0,0 +1,105 @@
/* 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 "gstcudanvrtc.h"
GST_DEBUG_CATEGORY_STATIC (gst_cuda_nvrtc_debug);
#define GST_CAT_DEFAULT gst_cuda_nvrtc_debug
static void
_init_debug (void)
{
static volatile gsize once_init = 0;
if (g_once_init_enter (&once_init)) {
GST_DEBUG_CATEGORY_INIT (gst_cuda_nvrtc_debug, "cudanvrtc", 0,
"CUDA runtime compiler");
g_once_init_leave (&once_init, 1);
}
}
gchar *
gst_cuda_nvrtc_compile (const gchar * source)
{
nvrtcProgram prog;
nvrtcResult ret;
const gchar *opts[] = { "--gpu-architecture=compute_30" };
gsize ptx_size;
gchar *ptx = NULL;
g_return_val_if_fail (source != NULL, FALSE);
_init_debug ();
GST_TRACE ("CUDA kernel source \n%s", source);
ret = NvrtcCreateProgram (&prog, source, NULL, 0, NULL, NULL);
if (ret != NVRTC_SUCCESS) {
GST_ERROR ("couldn't create nvrtc program, ret %d", ret);
return NULL;
}
ret = NvrtcCompileProgram (prog, 1, opts);
if (ret != NVRTC_SUCCESS) {
gsize log_size;
GST_ERROR ("couldn't compile nvrtc program, ret %d", ret);
if (NvrtcGetProgramLogSize (prog, &log_size) == NVRTC_SUCCESS &&
log_size > 0) {
gchar *compile_log = g_alloca (log_size);
if (NvrtcGetProgramLog (prog, compile_log) == NVRTC_SUCCESS) {
GST_ERROR ("nvrtc compile log %s", compile_log);
}
}
goto error;
}
ret = NvrtcGetPTXSize (prog, &ptx_size);
if (ret != NVRTC_SUCCESS) {
GST_ERROR ("unknown ptx size, ret %d", ret);
goto error;
}
ptx = g_malloc0 (ptx_size);
ret = NvrtcGetPTX (prog, ptx);
if (ret != NVRTC_SUCCESS) {
GST_ERROR ("couldn't get ptx, ret %d", ret);
g_free (ptx);
goto error;
}
NvrtcDestroyProgram (&prog);
GST_TRACE ("compiled CUDA PTX %s\n", ptx);
return ptx;
error:
NvrtcDestroyProgram (&prog);
return NULL;
}

View file

@ -0,0 +1,34 @@
/* 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_CUDA_NVRTC_H__
#define __GST_CUDA_NVRTC_H__
#include <gst/gst.h>
#include "gstcudaloader.h"
#include "gstnvrtcloader.h"
G_BEGIN_DECLS
G_GNUC_INTERNAL
gchar * gst_cuda_nvrtc_compile (const gchar * source);
G_END_DECLS
#endif /* __GST_CUDA_NVRTC_H__ */

View file

@ -0,0 +1,177 @@
/* 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 "gstnvrtcloader.h"
#include "gstcudaloader.h"
#include <gmodule.h>
#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;
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;
static GstNvCodecNvrtcVtahle gst_nvrtc_vtable = { 0, };
gboolean
gst_nvrtc_load_library (void)
{
GModule *module = NULL;
gchar *filename = NULL;
const gchar *filename_env;
const gchar *fname;
gint cuda_version;
GstNvCodecNvrtcVtahle *vtable;
if (gst_nvrtc_vtable.loaded)
return 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);
#else
/* (major version * 1000) + (minor version * 10) */
filename = g_strdup_printf (NVRTC_LIBNAME, cuda_version / 1000,
(cuda_version % 1000) / 10);
#endif
module = g_module_open (filename, G_MODULE_BIND_LAZY);
fname = filename;
}
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);
}
nvrtcResult
NvrtcGetPTXSize (nvrtcProgram prog, size_t * ptxSizeRet)
{
g_assert (gst_nvrtc_vtable.NvrtcGetPTXSize != NULL);
return gst_nvrtc_vtable.NvrtcGetPTXSize (prog, ptxSizeRet);
}
nvrtcResult
NvrtcGetProgramLog (nvrtcProgram prog, char *log)
{
g_assert (gst_nvrtc_vtable.NvrtcGetProgramLog != NULL);
return gst_nvrtc_vtable.NvrtcGetProgramLog (prog, log);
}
nvrtcResult
NvrtcGetProgramLogSize (nvrtcProgram prog, size_t * logSizeRet)
{
g_assert (gst_nvrtc_vtable.NvrtcGetProgramLogSize != NULL);
return gst_nvrtc_vtable.NvrtcGetProgramLogSize (prog, logSizeRet);
}

View file

@ -0,0 +1,64 @@
/* 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__
#include <gst/gst.h>
#include <nvrtc.h>
G_BEGIN_DECLS
G_GNUC_INTERNAL
gboolean gst_nvrtc_load_library (void);
G_GNUC_INTERNAL
nvrtcResult NvrtcCompileProgram (nvrtcProgram prog,
int numOptions,
const char** options);
G_GNUC_INTERNAL
nvrtcResult NvrtcCreateProgram (nvrtcProgram* prog,
const char* src,
const char* name,
int numHeaders,
const char** headers,
const char** includeNames);
G_GNUC_INTERNAL
nvrtcResult NvrtcDestroyProgram (nvrtcProgram* prog);
G_GNUC_INTERNAL
nvrtcResult NvrtcGetPTX (nvrtcProgram prog,
char* ptx);
G_GNUC_INTERNAL
nvrtcResult NvrtcGetPTXSize (nvrtcProgram prog,
size_t* ptxSizeRet);
G_GNUC_INTERNAL
nvrtcResult NvrtcGetProgramLog (nvrtcProgram prog,
char* log);
G_GNUC_INTERNAL
nvrtcResult NvrtcGetProgramLogSize (nvrtcProgram prog,
size_t* logSizeRet);
G_END_DECLS
#endif /* __GST_NVRTC_LOADER_H__ */

View file

@ -17,6 +17,8 @@ nvcodec_sources = [
'gstcudabasetransform.c',
'gstcudadownload.c',
'gstcudaupload.c',
'gstcudanvrtc.c',
'gstnvrtcloader.c',
]
if get_option('nvcodec').disabled()

34
sys/nvcodec/stub/nvrtc.h Normal file
View file

@ -0,0 +1,34 @@
/* NVRTC stub header
*
* 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_STUB_H__
#define __GST_NVRTC_STUB_H__
#include <gst/gst.h>
G_BEGIN_DECLS
typedef struct _nvrtcProgram * nvrtcProgram;
typedef enum {
NVRTC_SUCCESS = 0,
} nvrtcResult;
G_END_DECLS
#endif /* __GST_NVRTC_STUB_H__ */