Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CudaBuffers.h
Go to the documentation of this file.
1// @(#)root/tmva/tmva/dnn:$Id$
2// Author: Simon Pfreundschuh 07/08/16
3
4/*************************************************************************
5 * Copyright (C) 2016, Simon Pfreundschuh *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12////////////////////////////////////////////////////
13// Device and host buffer for CUDA architectures. //
14////////////////////////////////////////////////////
15
16#ifndef TMVA_DNN_ARCHITECTURES_CUDA_CUDABUFFERS
17#define TMVA_DNN_ARCHITECTURES_CUDA_CUDABUFFERS
18
19#include "cuda.h"
20#include "cuda_runtime.h"
21
22#include <memory>
23
24namespace TMVA {
25namespace DNN {
26
27template<typename AFloat>
28class TCudaDeviceBuffer;
29
30/** TCudaHostBuffer
31 *
32 * Wrapper class for pinned memory buffers on the host. Uses
33 * std::shared_pointer with custom destructor to ensure consistent
34 * memory management and allow for easy copying/moving of the
35 * buffers. Copying is asynchronous and will set the cudaStream of the
36 * device buffer so that subsequent computations on the device buffer
37 * can be performed on the same stream.
38 *
39 * \tparam AFloat The floating point type to be stored in the buffers.
40 */
41template<typename AFloat>
43{
44private:
45
46 size_t fOffset; ///< Offset for sub-buffers
47 size_t fSize;
48 mutable cudaStream_t fComputeStream; ///< cudaStream for data transfer
49 std::shared_ptr<AFloat *> fHostPointer; ///< Pointer to the buffer data
50
51 // Custom destructor required to free pinned host memory using cudaFree.
53 {
54 TDestructor() = default;
55 TDestructor(const TDestructor &) = default;
56 TDestructor( TDestructor &&) = default;
57 TDestructor & operator=(const TDestructor &) = default;
59 void operator()(AFloat ** devicePointer);
61
63
64public:
65
66 TCudaHostBuffer(size_t size);
67 TCudaHostBuffer(AFloat *);
68 TCudaHostBuffer() = default;
69 TCudaHostBuffer(const TCudaHostBuffer &) = default;
73
74 /** Return sub-buffer of the current buffer. */
76 /** Sets the entire buffer to a constant value */
77 void SetConstVal(const AFloat constVal);
78
79 inline operator AFloat * () const { return data(); }
80 AFloat * data() const;
81
82 inline AFloat & operator[](size_t index);
83 inline AFloat operator[](size_t index) const;
84
85 size_t GetSize() const {return fSize;}
86
87};
88
89/** TCudaDeviceBuffer
90 *
91 * Service class for on-device memory buffers. Uses
92 * std::shared_pointer with custom destructor to ensure consistent
93 * memory management and allow for easy copying/moving. A device
94 * buffer has an associated CUDA compute stream , which is used for
95 * implicit synchronization of data transfers.
96 *
97 * \tparam AFloat The floating point type to be stored in the buffers.
98 */
99template<typename AFloat>
101{
102private:
103
104 size_t fOffset; ///< Offset for sub-buffers
105 size_t fSize;
106 cudaStream_t fComputeStream; ///< cudaStream for data transfer
107 std::shared_ptr<AFloat *> fDevicePointer; ///< Pointer to the buffer data
108
109 // Custom destructor required to free pinned host memory using cudaFree.
111 {
112 TDestructor() = default;
113 TDestructor(const TDestructor &) = default;
114 TDestructor( TDestructor &&) = default;
115 TDestructor & operator=(const TDestructor &) = default;
117 void operator()(AFloat ** devicePointer);
120
121public:
122
123 TCudaDeviceBuffer(size_t size);
124 TCudaDeviceBuffer(size_t size, cudaStream_t stream);
125 TCudaDeviceBuffer(AFloat *, size_t size, cudaStream_t stream);
126 TCudaDeviceBuffer() = default;
131
132 /** Return sub-buffer of the current buffer. */
134 /** Convert to raw device data pointer.*/
135 inline operator AFloat * () const { return data(); }
136 AFloat * data() const;
137
138 void CopyFrom(const TCudaHostBuffer<AFloat> &) const;
139 void CopyTo(const TCudaHostBuffer<AFloat> &) const;
140
141 size_t GetSize() const {return fSize;}
142 cudaStream_t GetComputeStream() const {return fComputeStream;}
143 void SetComputeStream(cudaStream_t stream) {fComputeStream = stream;}
144
145 size_t GetUseCount() const { return fDevicePointer.use_count(); }
146
147};
148
149//
150// Inline Functions.
151//______________________________________________________________________________
152
153template<typename AFloat>
155{
156 return (*fHostPointer + fOffset)[index];
157}
158
159template<typename AFloat>
161{
162 return (*fHostPointer + fOffset)[index];
163}
164
165
166} // namespace DNN
167} // namespace TMVA
168#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
size_t fOffset
Offset for sub-buffers.
void SetComputeStream(cudaStream_t stream)
void CopyFrom(const TCudaHostBuffer< AFloat > &) const
void CopyTo(const TCudaHostBuffer< AFloat > &) const
struct TMVA::DNN::TCudaDeviceBuffer::TDestructor fDestructor
TCudaDeviceBuffer GetSubBuffer(size_t offset, size_t size)
Return sub-buffer of the current buffer.
TCudaDeviceBuffer & operator=(TCudaDeviceBuffer &&)=default
cudaStream_t fComputeStream
cudaStream for data transfer
TCudaDeviceBuffer & operator=(const TCudaDeviceBuffer &)=default
std::shared_ptr< AFloat * > fDevicePointer
Pointer to the buffer data.
TCudaDeviceBuffer(TCudaDeviceBuffer &&)=default
cudaStream_t GetComputeStream() const
TCudaDeviceBuffer(const TCudaDeviceBuffer &)=default
AFloat & operator[](size_t index)
struct TMVA::DNN::TCudaHostBuffer::TDestructor fDestructor
size_t fOffset
Offset for sub-buffers.
Definition CudaBuffers.h:46
TCudaHostBuffer GetSubBuffer(size_t offset, size_t size)
Return sub-buffer of the current buffer.
TCudaHostBuffer & operator=(const TCudaHostBuffer &)=default
TCudaHostBuffer(TCudaHostBuffer &&)=default
cudaStream_t fComputeStream
cudaStream for data transfer
Definition CudaBuffers.h:48
void SetConstVal(const AFloat constVal)
Sets the entire buffer to a constant value.
TCudaHostBuffer(const TCudaHostBuffer &)=default
std::shared_ptr< AFloat * > fHostPointer
Pointer to the buffer data.
Definition CudaBuffers.h:49
TCudaHostBuffer & operator=(TCudaHostBuffer &&)=default
create variable transformations
TDestructor(const TDestructor &)=default
void operator()(AFloat **devicePointer)
TDestructor & operator=(TDestructor &&)=default
TDestructor & operator=(const TDestructor &)=default
void operator()(AFloat **devicePointer)
TDestructor(const TDestructor &)=default
TDestructor(TDestructor &&)=default
TDestructor & operator=(TDestructor &&)=default
TDestructor & operator=(const TDestructor &)=default