The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
Gaudi::CUDA::DeviceArray< T > Class Template Reference

#include </builds/gaudi/Gaudi/GaudiCUDA/src/CUDADeviceArray.h>

Collaboration diagram for Gaudi::CUDA::DeviceArray< T >:

Public Member Functions

 DeviceArray (Stream &stream, std::size_t len)
 Construct DeviceArray local to a Stream (i.e. Algorithm). May not be stored in whiteboard.
 DeviceArray (DeviceArrayGlobalTag_t globalTag, std::size_t len)
 Construct global DeviceArray (may be stored in whiteboard).
 ~DeviceArray ()
 Destructor.
template<HostRange< T > R>
DeviceArray & operator= (const R &src)
 Copy from a HostRange.
DeviceArray & operator= (const T &src)
 Copy from a host value (for size 1).
DeviceArray & operator= (const DeviceArray &rhs)
 Copy from another DeviceArray You can copy between arrays in the same stream, or between a local and a global DeviceArray.
template<HostRange< T > R>
void toHost (R &dest)
 Copy to a HostRange.
void toHost (T &dest)
 Copy to a host value (for size 1).
T * devPtr ()
 Return raw device pointer.
T * operator& ()
 Alias for devPtr.
T * operator-> ()
 Allow access to struct members.
template<HostRange< T > R>
DeviceArray< T > & operator= (const R &src)

Private Attributes

T * m_ptr
const std::size_t m_len
const std::size_t m_size
Stream *const m_stream

Detailed Description

template<TriviallyCopyable T>
class Gaudi::CUDA::DeviceArray< T >

Definition at line 44 of file CUDADeviceArray.h.

Constructor & Destructor Documentation

◆ DeviceArray() [1/2]

template<TriviallyCopyable T>
Gaudi::CUDA::DeviceArray< T >::DeviceArray ( Stream & stream,
std::size_t len )

Construct DeviceArray local to a Stream (i.e. Algorithm). May not be stored in whiteboard.

Definition at line 100 of file CUDADeviceArray.h.

101 : m_ptr( static_cast<T*>( Detail::allocateWithStream( len * sizeof( T ), stream ) ) )
102 , m_len( len )
103 , m_size( len * sizeof( T ) )
104 , m_stream( &stream ) {
105 stream.registerDependency();
106 }
const std::size_t m_size
const std::size_t m_len

◆ DeviceArray() [2/2]

template<TriviallyCopyable T>
Gaudi::CUDA::DeviceArray< T >::DeviceArray ( DeviceArrayGlobalTag_t globalTag,
std::size_t len )

Construct global DeviceArray (may be stored in whiteboard).

Definition at line 109 of file CUDADeviceArray.h.

110 : m_ptr( static_cast<T*>(
111 Detail::allocateNoStream( len * sizeof( T ), globalTag.hasAsyncParent ? globalTag.asyncPtr : nullptr ) ) )
112 , m_len( len )
113 , m_size( len * sizeof( T ) )
114 , m_stream( nullptr ) {}

◆ ~DeviceArray()

template<TriviallyCopyable T>
Gaudi::CUDA::DeviceArray< T >::~DeviceArray ( )

Destructor.

Definition at line 117 of file CUDADeviceArray.h.

117 {
118 if ( m_stream == nullptr ) {
120 } else {
122 m_stream->removeDependency();
123 }
124 }
void freeNoStream(void *ptr)
void freeWithStream(void *ptr, Stream &stream)

Member Function Documentation

◆ devPtr()

template<TriviallyCopyable T>
T * Gaudi::CUDA::DeviceArray< T >::devPtr ( )
inline

Return raw device pointer.

Definition at line 69 of file CUDADeviceArray.h.

69{ return m_ptr; };

◆ operator&()

template<TriviallyCopyable T>
T * Gaudi::CUDA::DeviceArray< T >::operator& ( )
inline

Alias for devPtr.

Definition at line 71 of file CUDADeviceArray.h.

71{ return m_ptr; }

◆ operator->()

template<TriviallyCopyable T>
T * Gaudi::CUDA::DeviceArray< T >::operator-> ( )
inline

Allow access to struct members.

Definition at line 73 of file CUDADeviceArray.h.

73{ return m_ptr; }

◆ operator=() [1/4]

template<TriviallyCopyable T>
DeviceArray< T > & Gaudi::CUDA::DeviceArray< T >::operator= ( const DeviceArray< T > & rhs)

Copy from another DeviceArray You can copy between arrays in the same stream, or between a local and a global DeviceArray.

You cannot copy between arrays in different streams.

Definition at line 149 of file CUDADeviceArray.h.

149 {
150 if ( this == &rhs ) { return *this; }
151 if ( m_stream != nullptr && rhs.m_stream != nullptr && m_stream != rhs.m_stream ) {
152 m_stream->parent()->error()
153 << "Device to device copies between DeviceArrays on different streams are not allowed!" << endmsg;
154 throw GaudiException( "Device to device copy with mismatched streams", "CUDADeviceArrayException",
156 }
157 if ( m_len != rhs.m_len ) {
158 if ( m_stream != nullptr ) {
159 m_stream->parent()->error() << "Device to device copy with mismatched sizes: " << rhs.m_len << " and " << m_len
160 << endmsg;
161 throw GaudiException( "Device to device copy with mismatched sizes", "CUDADeviceArrayException",
163 }
164 if ( rhs.m_stream != nullptr ) {
165 rhs.m_stream->parent()->error() << "Device to device copy with mismatched sizes: " << rhs.m_len << " and "
166 << m_len << endmsg;
167 throw GaudiException( "Device to device copy with mismatched sizes", "CUDADeviceArrayException",
169 }
170 // We want the error message so we throw an exception
171 throw GaudiException( "Device to device copy with mismatched sizes", "CUDADeviceArrayException",
173 }
174
175 // Do the copy
176 if ( m_stream != nullptr ) {
178 } else if ( rhs.m_stream != nullptr ) {
180 } else {
182 }
183 return *this;
184 }
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
const Gaudi::Algorithm * parent()
Access the parent algorithm.
void copyDeviceToDeviceNoStream(void *destDevPtr, const void *srcDevPtr, std::size_t size)
void copyDeviceToDeviceWithStream(void *destDevPtr, const void *srcDevPtr, std::size_t size, Stream &stream)

◆ operator=() [2/4]

template<TriviallyCopyable T>
template<HostRange< T > R>
DeviceArray< T > & Gaudi::CUDA::DeviceArray< T >::operator= ( const R & src)

Definition at line 128 of file CUDADeviceArray.h.

128 {
129 // Guard against mismatched sizes
130 if ( std::ranges::size( src ) != m_len ) {
131 if ( m_stream != nullptr ) {
132 m_stream->parent()->error() << "Host to device copy with mismatched sizes: " << std::ranges::size( src )
133 << " on host and " << m_len << " on device" << endmsg;
134 throw GaudiException( "Host to device copy with mismatched sizes", "CUDADeviceArrayException",
136 } // We want the error message so we throw an exception
137 throw GaudiException( "Host to device copy with mismatched sizes", "CUDADeviceArrayException",
139 }
140 if ( m_stream == nullptr ) {
142 return *this;
143 }
145 return *this;
146 }
void copyHostToDeviceNoStream(void *devPtr, const void *hstPtr, std::size_t size)
void copyHostToDeviceWithStream(void *devPtr, const void *hstPtr, std::size_t size, Stream &stream)

◆ operator=() [3/4]

template<TriviallyCopyable T>
template<HostRange< T > R>
DeviceArray & Gaudi::CUDA::DeviceArray< T >::operator= ( const R & src)

Copy from a HostRange.

◆ operator=() [4/4]

template<TriviallyCopyable T>
DeviceArray & Gaudi::CUDA::DeviceArray< T >::operator= ( const T & src)
inline

Copy from a host value (for size 1).

Definition at line 57 of file CUDADeviceArray.h.

57{ return this->operator=( std::span<T, 1>( &src, 1 ) ); }
DeviceArray & operator=(const R &src)
Copy from a HostRange.

◆ toHost() [1/2]

template<TriviallyCopyable T>
template<HostRange< T > R>
void Gaudi::CUDA::DeviceArray< T >::toHost ( R & dest)

Copy to a HostRange.

Definition at line 188 of file CUDADeviceArray.h.

188 {
189 // Guard against mismatched sizes
190 if ( std::ranges::size( dest ) != m_len ) {
191 if ( m_stream != nullptr ) {
192 m_stream->parent()->error() << "Device to host copy with mismatched sizes: " << m_len << " on device and "
193 << std::ranges::size( dest ) << " on host" << endmsg;
194 throw GaudiException( "Device to host copy with mismatched sizes", "CUDADeviceArrayException",
196 } // We want the error message so we throw an exception
197 throw GaudiException( "Device to host copy with mismatched sizes", "CUDADeviceArrayException",
199 }
200 if ( m_stream == nullptr ) {
202 return;
203 }
205 }
void copyDeviceToHostWithStream(void *hstPtr, const void *devPtr, std::size_t size, Stream &stream)
void copyDeviceToHostNoStream(void *hstPtr, const void *devPtr, std::size_t size)

◆ toHost() [2/2]

template<TriviallyCopyable T>
void Gaudi::CUDA::DeviceArray< T >::toHost ( T & dest)

Copy to a host value (for size 1).

Definition at line 208 of file CUDADeviceArray.h.

208 {
210 this->toHost( dest_span );
211 }
void toHost(R &dest)
Copy to a HostRange.

Member Data Documentation

◆ m_len

template<TriviallyCopyable T>
const std::size_t Gaudi::CUDA::DeviceArray< T >::m_len
private

Definition at line 77 of file CUDADeviceArray.h.

◆ m_ptr

template<TriviallyCopyable T>
T* Gaudi::CUDA::DeviceArray< T >::m_ptr
private

Definition at line 76 of file CUDADeviceArray.h.

◆ m_size

template<TriviallyCopyable T>
const std::size_t Gaudi::CUDA::DeviceArray< T >::m_size
private

Definition at line 78 of file CUDADeviceArray.h.

◆ m_stream

template<TriviallyCopyable T>
Stream* const Gaudi::CUDA::DeviceArray< T >::m_stream
private

Definition at line 79 of file CUDADeviceArray.h.


The documentation for this class was generated from the following file: