2 * Copyright (C) <2018-2019> Seungha Yang <seungha.yang@navercorp.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gstcudaloader.h"
25 #include "gstcudacontext.h"
26 #include "gstcudautils.h"
28 GST_DEBUG_CATEGORY_STATIC (gst_cuda_context_debug);
29 #define GST_CAT_DEFAULT gst_cuda_context_debug
31 /* store all context object with weak ref */
32 static GList *context_list = NULL;
33 G_LOCK_DEFINE_STATIC (list_lock);
41 #define DEFAULT_DEVICE_ID -1
43 struct _GstCudaContextPrivate
51 GHashTable *accessible_peer;
54 #define gst_cuda_context_parent_class parent_class
55 G_DEFINE_TYPE_WITH_PRIVATE (GstCudaContext, gst_cuda_context, GST_TYPE_OBJECT);
57 static void gst_cuda_context_set_property (GObject * object, guint prop_id,
58 const GValue * value, GParamSpec * pspec);
59 static void gst_cuda_context_get_property (GObject * object, guint prop_id,
60 GValue * value, GParamSpec * pspec);
61 static void gst_cuda_context_constructed (GObject * object);
62 static void gst_cuda_context_finalize (GObject * object);
63 static void gst_cuda_context_weak_ref_notify (gpointer data,
64 GstCudaContext * context);
65 static void gst_cuda_context_enable_peer_access (GstCudaContext * context,
66 GstCudaContext * peer);
69 gst_cuda_context_class_init (GstCudaContextClass * klass)
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
73 gobject_class->set_property = gst_cuda_context_set_property;
74 gobject_class->get_property = gst_cuda_context_get_property;
75 gobject_class->constructed = gst_cuda_context_constructed;
76 gobject_class->finalize = gst_cuda_context_finalize;
78 g_object_class_install_property (gobject_class, PROP_DEVICE_ID,
79 g_param_spec_int ("cuda-device-id", "Cuda Device ID",
80 "Set the GPU device to use for operations (-1 = auto)",
81 -1, G_MAXINT, DEFAULT_DEVICE_ID,
82 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
84 GST_DEBUG_CATEGORY_INIT (gst_cuda_context_debug,
85 "cudacontext", 0, "CUDA Context");
89 gst_cuda_context_init (GstCudaContext * context)
91 GstCudaContextPrivate *priv = gst_cuda_context_get_instance_private (context);
94 priv->device_id = DEFAULT_DEVICE_ID;
95 priv->accessible_peer = g_hash_table_new (g_direct_hash, g_direct_equal);
101 gst_cuda_context_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec)
104 GstCudaContext *context = GST_CUDA_CONTEXT (object);
105 GstCudaContextPrivate *priv = context->priv;
109 priv->device_id = g_value_get_int (value);
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 gst_cuda_context_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec)
121 GstCudaContext *context = GST_CUDA_CONTEXT (object);
122 GstCudaContextPrivate *priv = context->priv;
126 g_value_set_int (value, priv->device_id);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 gst_cuda_context_constructed (GObject * object)
137 static gsize once = 0;
138 GstCudaContext *context = GST_CUDA_CONTEXT (object);
139 GstCudaContextPrivate *priv = context->priv;
140 CUcontext cuda_ctx, old_ctx;
142 CUdevice cdev = 0, cuda_dev = -1;
145 gint min = 0, maj = 0;
150 if (g_once_init_enter (&once)) {
151 if (CuInit (0) != CUDA_SUCCESS) {
152 GST_ERROR_OBJECT (context, "Failed to cuInit");
155 g_once_init_leave (&once, ret);
161 if (!gst_cuda_result (CuDeviceGetCount (&dev_count)) || dev_count == 0) {
162 GST_WARNING ("No CUDA devices detected");
166 for (i = 0; i < dev_count; ++i) {
167 if (gst_cuda_result (CuDeviceGet (&cdev, i)) &&
168 gst_cuda_result (CuDeviceGetName (name, sizeof (name), cdev)) &&
169 gst_cuda_result (CuDeviceGetAttribute (&maj,
170 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cdev)) &&
171 gst_cuda_result (CuDeviceGetAttribute (&min,
172 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cdev)) &&
173 gst_cuda_result (CuDeviceGetAttribute (&tex_align,
174 CU_DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT, cdev))) {
175 GST_INFO ("GPU #%d supports NVENC: %s (%s) (Compute SM %d.%d)", i,
176 (((maj << 4) + min) >= 0x30) ? "yes" : "no", name, maj, min);
177 if (priv->device_id == -1 || priv->device_id == cdev) {
178 priv->device_id = cuda_dev = cdev;
179 priv->tex_align = tex_align;
185 if (cuda_dev == -1) {
186 GST_WARNING ("Device with id %d does not exist", priv->device_id);
190 GST_DEBUG ("Creating cuda context for device index %d", cuda_dev);
192 if (!gst_cuda_result (CuCtxCreate (&cuda_ctx, 0, cuda_dev))) {
193 GST_WARNING ("Failed to create CUDA context for cuda device %d", cuda_dev);
197 if (!gst_cuda_result (CuCtxPopCurrent (&old_ctx))) {
201 GST_INFO ("Created CUDA context %p with device-id %d", cuda_ctx, cuda_dev);
203 priv->context = cuda_ctx;
204 priv->device = cuda_dev;
207 g_object_weak_ref (G_OBJECT (object),
208 (GWeakNotify) gst_cuda_context_weak_ref_notify, NULL);
209 for (iter = context_list; iter; iter = g_list_next (iter)) {
210 GstCudaContext *peer = (GstCudaContext *) iter->data;
212 /* EnablePeerAccess is unidirectional */
213 gst_cuda_context_enable_peer_access (context, peer);
214 gst_cuda_context_enable_peer_access (peer, context);
217 context_list = g_list_append (context_list, context);
218 G_UNLOCK (list_lock);
221 /* must be called with list_lock taken */
223 gst_cuda_context_enable_peer_access (GstCudaContext * context,
224 GstCudaContext * peer)
226 GstCudaContextPrivate *priv = context->priv;
227 GstCudaContextPrivate *peer_priv = peer->priv;
228 CUdevice device = priv->device;
229 CUdevice other_dev = peer_priv->device;
233 cuda_ret = CuDeviceCanAccessPeer (&can_access, device, other_dev);
235 if (!gst_cuda_result (cuda_ret) || !can_access) {
236 GST_DEBUG_OBJECT (context,
237 "Peer access to %" GST_PTR_FORMAT " is not allowed", peer);
241 gst_cuda_context_push (context);
242 if (gst_cuda_result (CuCtxEnablePeerAccess (peer_priv->context, 0))) {
243 GST_DEBUG_OBJECT (context, "Enable peer access to %" GST_PTR_FORMAT, peer);
244 g_hash_table_add (priv->accessible_peer, peer);
247 gst_cuda_context_pop (NULL);
251 gst_cuda_context_weak_ref_notify (gpointer data, GstCudaContext * context)
256 context_list = g_list_remove (context_list, context);
258 /* disable self -> peer access */
259 if (context->priv->accessible_peer) {
262 g_hash_table_iter_init (&iter, context->priv->accessible_peer);
263 if (gst_cuda_context_push (context)) {
264 while (g_hash_table_iter_next (&iter, &key, NULL)) {
265 GstCudaContext *peer = GST_CUDA_CONTEXT (key);
266 CUcontext peer_handle = gst_cuda_context_get_handle (peer);
267 GST_DEBUG_OBJECT (context,
268 "Disable peer access to %" GST_PTR_FORMAT, peer);
269 gst_cuda_result (CuCtxDisablePeerAccess (peer_handle));
271 gst_cuda_context_pop (NULL);
274 g_hash_table_destroy (context->priv->accessible_peer);
275 context->priv->accessible_peer = NULL;
278 /* disable peer -> self access */
279 for (iter = context_list; iter; iter = g_list_next (iter)) {
280 GstCudaContext *other = (GstCudaContext *) iter->data;
281 GstCudaContextPrivate *other_priv = other->priv;
282 CUcontext self_handle;
284 if (!other_priv->accessible_peer)
287 if (g_hash_table_lookup (other_priv->accessible_peer, context)) {
288 if (gst_cuda_context_push (other)) {
289 self_handle = gst_cuda_context_get_handle (context);
290 GST_DEBUG_OBJECT (other,
291 "Disable peer access to %" GST_PTR_FORMAT, context);
292 gst_cuda_result (CuCtxDisablePeerAccess (self_handle));
293 gst_cuda_context_pop (NULL);
296 g_hash_table_remove (other_priv->accessible_peer, context);
299 G_UNLOCK (list_lock);
303 gst_cuda_context_finalize (GObject * object)
305 GstCudaContext *context = GST_CUDA_CONTEXT_CAST (object);
306 GstCudaContextPrivate *priv = context->priv;
309 GST_DEBUG_OBJECT (context, "Destroying CUDA context %p", priv->context);
310 gst_cuda_result (CuCtxDestroy (priv->context));
313 G_OBJECT_CLASS (parent_class)->finalize (object);
317 * gst_cuda_context_new:
318 * @device_id: device-id for creating #GstCudaContext or -1 for auto selection
320 * Create #GstCudaContext with given device_id. If the @device_id was not -1
321 * but was out of range (e.g., exceed the number of device),
322 * #GstCudaContext will not be created.
324 * Returns: a new #GstCudaContext or %NULL on failure
327 gst_cuda_context_new (gint device_id)
329 GstCudaContext *self =
330 g_object_new (GST_TYPE_CUDA_CONTEXT, "cuda-device-id", device_id, NULL);
332 gst_object_ref_sink (self);
334 if (!self->priv->context) {
335 GST_ERROR ("Failed to create CUDA context");
336 gst_clear_object (&self);
343 * gst_cuda_context_push:
344 * @ctx: a #GstCudaContext to push current thread
346 * Pushes the given @ctx onto the CPU thread's stack of current contexts.
347 * The specified context becomes the CPU thread's current context,
348 * so all CUDA functions that operate on the current context are affected.
350 * Returns: %TRUE if @ctx was pushed without error.
353 gst_cuda_context_push (GstCudaContext * ctx)
355 g_return_val_if_fail (ctx, FALSE);
356 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), FALSE);
358 return gst_cuda_result (CuCtxPushCurrent (ctx->priv->context));
362 * gst_cuda_context_pop:
364 * Pops the current CUDA context from CPU thread
366 * Returns: %TRUE if @ctx was pushed without error.
369 gst_cuda_context_pop (CUcontext * cuda_ctx)
371 return gst_cuda_result (CuCtxPopCurrent (cuda_ctx));
375 * gst_cuda_context_get_handle:
376 * @ctx: a #GstCudaContext
378 * Get CUDA device context. Caller must not modify and/or destroy
379 * returned device context.
381 * Returns: the #CUcontext of @ctx
384 gst_cuda_context_get_handle (GstCudaContext * ctx)
386 g_return_val_if_fail (ctx, NULL);
387 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), NULL);
389 return ctx->priv->context;
393 * gst_cuda_context_get_texture_alignment:
394 * @ctx: a #GstCudaContext
396 * Get required texture alignment by device
398 * Returns: the #CUcontext of @ctx
401 gst_cuda_context_get_texture_alignment (GstCudaContext * ctx)
403 g_return_val_if_fail (ctx, 0);
404 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), 0);
406 return ctx->priv->tex_align;
410 * gst_cuda_context_can_access_peer:
411 * @ctx: a #GstCudaContext
412 * @peer: a #GstCudaContext
414 * Query whether @ctx can access any memory which belongs to @peer directly.
416 * Returns: %TRUE if @ctx can access @peer directly
419 gst_cuda_context_can_access_peer (GstCudaContext * ctx, GstCudaContext * peer)
421 gboolean ret = FALSE;
423 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), FALSE);
424 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (peer), FALSE);
427 if (ctx->priv->accessible_peer &&
428 g_hash_table_lookup (ctx->priv->accessible_peer, peer)) {
431 G_UNLOCK (list_lock);