Forge: CUDACopy.hpp Source File

Forge

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

CUDACopy.hpp
Go to the documentation of this file.
1 /*******************************************************
2 * Copyright (c) 2015-2019, ArrayFire
3 * All rights reserved.
4 *
5 * This file is distributed under 3-clause BSD license.
6 * The complete license agreement can be obtained at:
7 * http://arrayfire.com/licenses/BSD-3-Clause
8 ********************************************************/
9
10 #ifndef __CUDA_DATA_COPY_H__
11 #define __CUDA_DATA_COPY_H__
12
13 #include <cuda_gl_interop.h>
14 #include <cstdio>
15
16 static void handleCUDAError(cudaError_t err, const char *file, int line)
17 {
18 if (err != cudaSuccess) {
19 printf( "%s in %s at line %d\n", cudaGetErrorString(err), file, line);
20 exit(EXIT_FAILURE);
21 }
22 }
23
24 #define CUDA_ERROR_CHECK(err) (handleCUDAError(err, __FILE__, __LINE__ ))
25
26 namespace fg
27 {
28
29 template<typename T>
30 void copy(fg::Image& out, const T * devicePtr)
31 {
32 cudaGraphicsResource *cudaPBOResource;
33 CUDA_ERROR_CHECK(cudaGraphicsGLRegisterBuffer(&cudaPBOResource, out.pbo(), cudaGraphicsMapFlagsWriteDiscard));
34
35 size_t num_bytes;
36 T* pboDevicePtr = NULL;
37
38 CUDA_ERROR_CHECK(cudaGraphicsMapResources(1, &cudaPBOResource, 0));
39 CUDA_ERROR_CHECK(cudaGraphicsResourceGetMappedPointer((void **)&pboDevicePtr, &num_bytes, cudaPBOResource));
40 CUDA_ERROR_CHECK(cudaMemcpy(pboDevicePtr, devicePtr, num_bytes, cudaMemcpyDeviceToDevice));
41 CUDA_ERROR_CHECK(cudaGraphicsUnmapResources(1, &cudaPBOResource, 0));
42 CUDA_ERROR_CHECK(cudaGraphicsUnregisterResource(cudaPBOResource));
43 }
44
45 /*
46 * Below functions takes any renderable forge object that has following member functions
47 * defined
48 *
49 * `unsigned Renderable::vbo() const;`
50 * `unsigned Renderable::size() const;`
51 *
52 * Currently fg::Plot, fg::Histogram objects in Forge library fit the bill
53 */
54 template<class Renderable, typename T>
55 void copy(Renderable& out, const T * devicePtr)
56 {
57 cudaGraphicsResource *cudaVBOResource;
58 CUDA_ERROR_CHECK(cudaGraphicsGLRegisterBuffer(&cudaVBOResource, out.vbo(), cudaGraphicsMapFlagsWriteDiscard));
59
60 size_t num_bytes;
61 T* vboDevicePtr = NULL;
62
63 CUDA_ERROR_CHECK(cudaGraphicsMapResources(1, &cudaVBOResource, 0));
64 CUDA_ERROR_CHECK(cudaGraphicsResourceGetMappedPointer((void **)&vboDevicePtr, &num_bytes, cudaVBOResource));
65 CUDA_ERROR_CHECK(cudaMemcpy(vboDevicePtr, devicePtr, num_bytes, cudaMemcpyDeviceToDevice));
66 CUDA_ERROR_CHECK(cudaGraphicsUnmapResources(1, &cudaVBOResource, 0));
67 CUDA_ERROR_CHECK(cudaGraphicsUnregisterResource(cudaVBOResource));
68 }
69
70 }
71
72 #endif //__CUDA_DATA_COPY_H__
fg
Definition: CPUCopy.hpp:13
fg::copy
void copy(fg::Image &out, const T *dataPtr)
Definition: CPUCopy.hpp:17
handleCUDAError
static void handleCUDAError(cudaError_t err, const char *file, int line)
Definition: CUDACopy.hpp:16
fg::Image::pbo
FGAPI unsigned pbo() const
Get the OpenGL Pixel Buffer Object identifier.
CUDA_ERROR_CHECK
#define CUDA_ERROR_CHECK(err)
Definition: CUDACopy.hpp:24
fg::Image
Definition: image.h:25