UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
cuda_wrapper.cu
Go to the documentation of this file.
1
12/*
13 * Copyright (c) 2013-2024 CESNET z.s.p.o.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, is permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 *
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * 3. Neither the name of CESNET nor the names of its contributors may be
28 * used to endorse or promote products derived from this software without
29 * specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
33 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
34 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
35 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45#include <stdio.h>
46#include <stdlib.h>
47
48#include "cuda_runtime.h"
49#include "cuda_wrapper.h"
50
52
53static inline int map_cuda_error(cudaError_t cuda_error) {
54 return (int) cuda_error;
55};
56
57static inline const char * map_error_string(int error) {
58
59 return cudaGetErrorString((cudaError_t) error);
60};
61
62static inline enum cudaMemcpyKind map_cuda_memcpy_kind(int our_kind) {
63 struct kind_mapping {
64 enum cudaMemcpyKind kind;
65 int our_kind;
66 };
67 struct kind_mapping mapping[] = {
69 { cudaMemcpyDeviceToHost, CUDA_WRAPPER_MEMCPY_DEVICE_TO_HOST },
70 };
71
72 int i;
73 for (i = 0; i < sizeof(mapping)/sizeof(struct kind_mapping); ++i) {
74 if (our_kind == mapping[i].our_kind) {
75 return mapping[i].kind;
76 }
77 }
78
79 abort(); // should not reach here
80};
81
82int cuda_wrapper_free(void *buffer)
83{
84 return map_cuda_error(cudaFree(buffer));
85}
86
87int cuda_wrapper_free_host(void *buffer)
88{
89 return map_cuda_error(cudaFreeHost(buffer));
90}
91
92int cuda_wrapper_host_alloc(void **pHost, size_t size, unsigned int flags)
93{
94 return map_cuda_error(cudaHostAlloc(pHost, size, flags));
95}
96
97int cuda_wrapper_malloc(void **buffer, size_t data_len)
98{
99 return map_cuda_error(cudaMalloc(buffer, data_len));
100}
101
102int cuda_wrapper_malloc_host(void **buffer, size_t data_len)
103{
104 return map_cuda_error(cudaMallocHost(buffer, data_len));
105}
106
107int cuda_wrapper_memcpy(void *dst, const void *src,
108 size_t count, int kind)
109{
110 return map_cuda_error(
111 cudaMemcpy(dst, src, count,
112 map_cuda_memcpy_kind(kind)));
113}
114
116{
118}
119
124
125const char *cuda_wrapper_get_error_string(int error)
126{
127 return map_error_string(error);
128}
129
131{
132 return map_cuda_error(
133 cudaSetDevice(index));
134}
135
138{
139 int device_count = 0;
140 if (cudaGetDeviceCount(&device_count) != cudaSuccess) {
141 fprintf(stderr, "Cannot get number of CUDA devices: %s\n", cudaGetErrorString(cudaGetLastError()));
142 return;
143 }
144 if (device_count == 0) {
145 fprintf(stderr, "There is no device supporting CUDA.\n");
146 } else {
147 printf("There %s %d devices supporting CUDA:\n", device_count == 1 ? "is" : "are", device_count);
148 }
149
150 for ( int device_id = 0; device_id < device_count; device_id++ ) {
151 struct cudaDeviceProp device_properties;
152 if (cudaGetDeviceProperties(&device_properties, device_id) != cudaSuccess) {
153 fprintf(stderr, "Cannot get number of CUDA device #%d properties: %s\n", device_id, cudaGetErrorString(cudaGetLastError()));
154 continue;
155 }
156
157 printf("\nDevice #%d: \"%s\"\n", device_id, device_properties.name);
158 printf(" Compute capability: %d.%d\n", device_properties.major, device_properties.minor);
159 printf(" Total amount of global memory: %zu kB\n", device_properties.totalGlobalMem / 1024);
160 printf(" Total amount of constant memory: %zu kB\n", device_properties.totalGlobalMem / 1024);
161 printf(" Total amount of shared memory per block: %zu kB\n", device_properties.sharedMemPerBlock / 1024);
162 printf(" Total number of registers available per block: %d\n", device_properties.regsPerBlock);
163 printf(" Multiprocessors: %d\n", device_properties.multiProcessorCount);
164 }
165}
166
167void
169{
170 // to allow "cuda-memcheck --leak-check full"
171 if (cudaDeviceReset() != cudaSuccess) {
172 fprintf(stderr, "cudaDeviceReset failed!\n");
173 }
174}
const char unsigned int flags
static const struct codec_param mapping[AC_COUNT]
Definition libavcodec.c:87
#define cudaMemcpy
#define cudaSuccess
#define cudaFree
#define cudaMemcpyHostToDevice
#define cudaGetLastError
#define cudaHostAlloc
#define cudaMalloc
#define cudaFreeHost
#define cudaError_t
#define cudaGetErrorString
const char * cuda_wrapper_get_error_string(int error)
int cuda_wrapper_set_device(int index)
int cuda_wrapper_malloc_host(void **buffer, size_t data_len)
int cuda_wrapper_free_host(void *buffer)
int cuda_wrapper_host_alloc(void **pHost, size_t size, unsigned int flags)
void * cuda_wrapper_stream_t
int cuda_wrapper_free(void *buffer)
static const char * map_error_string(int error)
const char * cuda_wrapper_last_error_string(void)
int cuda_wrapper_memcpy(void *dst, const void *src, size_t count, int kind)
void cuda_wrapper_print_devices_info(void)
adapted from gpujpeg_print_devices_info()
int cuda_wrapper_get_last_error(void)
static enum cudaMemcpyKind map_cuda_memcpy_kind(int our_kind)
static int map_cuda_error(cudaError_t cuda_error)
void cuda_wrapper_device_reset(void)
int cuda_wrapper_malloc(void **buffer, size_t data_len)
This file contais wrapper around CUDA functions.
#define CUDA_WRAPPER_MEMCPY_HOST_TO_DEVICE
#define CUDA_WRAPPER_MEMCPY_DEVICE_TO_HOST
int count
Definition hd-rum.c:44