The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
Gaudi::CUDA::Detail Namespace Reference

Functions

void * allocateWithStream (std::size_t size, Stream &stream)
void * allocateNoStream (std::size_t size, Gaudi::AsynchronousAlgorithm *parent)
void freeWithStream (void *ptr, Stream &stream)
void freeNoStream (void *ptr)
void copyHostToDeviceWithStream (void *devPtr, const void *hstPtr, std::size_t size, Stream &stream)
void copyHostToDeviceNoStream (void *devPtr, const void *hstPtr, std::size_t size)
void copyDeviceToHostWithStream (void *hstPtr, const void *devPtr, std::size_t size, Stream &stream)
void copyDeviceToHostNoStream (void *hstPtr, const void *devPtr, std::size_t size)
void copyDeviceToDeviceWithStream (void *destDevPtr, const void *srcDevPtr, std::size_t size, Stream &stream)
void copyDeviceToDeviceNoStream (void *destDevPtr, const void *srcDevPtr, std::size_t size)

Function Documentation

◆ allocateNoStream()

void * Gaudi::CUDA::Detail::allocateNoStream ( std::size_t size,
Gaudi::AsynchronousAlgorithm * parent )

Definition at line 72 of file CUDADeviceArray.cpp.

72 {
73 void* devPtr = nullptr;
74 cudaError_t err = cudaSuccess;
75 do {
76 err = cudaMalloc( &devPtr, size );
77 if ( err == cudaSuccess ) { break; }
78 if ( err == cudaErrorMemoryAllocation ) {
79 cudaGetLastError();
80 // If called from an AsynchronousAlgorithm, wait as in the with stream variant
81 // Otherwise, the thread should sleep
82 if ( parent != nullptr ) {
83 parent
84 ->decorateSuspension( [&]() {
85 std::unique_lock lck( gpu_mem_mtx );
86 gpu_mem_cv.wait( lck );
88 } )
89 .orThrow( "Error supending while waiting for memory", DEVARREXC );
90 } else {
91 std::this_thread::sleep_for( 100ms );
92 }
93 } else {
94 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
95 }
96 } while ( err == cudaErrorMemoryAllocation );
97 return devPtr;
98 }
StatusCode decorateSuspension(std::function< StatusCode()> f) const
Decorate a direct call to a suspending (or possibly suspending) function with the necessary pre- and ...
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100

◆ allocateWithStream()

void * Gaudi::CUDA::Detail::allocateWithStream ( std::size_t size,
Stream & stream )

Definition at line 48 of file CUDADeviceArray.cpp.

48 {
49 const Gaudi::AsynchronousAlgorithm* parent = stream.asyncParent();
50 void* devPtr = nullptr;
51 cudaError_t err = cudaSuccess;
52 do {
53 err = cudaMallocAsync( &devPtr, size, stream );
54 if ( err == cudaSuccess ) { break; }
55 if ( err == cudaErrorMemoryAllocation && parent != nullptr ) {
56 // Waiting and retrying only works if we're in an asynchronous algorithm
57 cudaGetLastError();
58 parent
59 ->decorateSuspension( [&]() {
60 std::unique_lock lck( gpu_mem_mtx );
61 gpu_mem_cv.wait( lck );
63 } )
64 .orThrow( "Error suspending while waiting for memory", DEVARREXC );
65 } else {
66 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
67 }
68 } while ( err == cudaErrorMemoryAllocation );
69 return devPtr;
70 }
Base class for asynchronous algorithms.
Define general base for Gaudi exception.

◆ copyDeviceToDeviceNoStream()

void Gaudi::CUDA::Detail::copyDeviceToDeviceNoStream ( void * destDevPtr,
const void * srcDevPtr,
std::size_t size )

Definition at line 155 of file CUDADeviceArray.cpp.

155 {
156 cudaError_t err = cudaMemcpy( destDevPtr, srcDevPtr, size, cudaMemcpyDeviceToDevice );
157 if ( err != cudaSuccess ) {
158 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
159 }
160 }

◆ copyDeviceToDeviceWithStream()

void Gaudi::CUDA::Detail::copyDeviceToDeviceWithStream ( void * destDevPtr,
const void * srcDevPtr,
std::size_t size,
Stream & stream )

Definition at line 148 of file CUDADeviceArray.cpp.

148 {
149 cudaError_t err = cudaMemcpyAsync( destDevPtr, srcDevPtr, size, cudaMemcpyDeviceToDevice, stream );
150 if ( err != cudaSuccess ) {
151 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
152 }
153 }

◆ copyDeviceToHostNoStream()

void Gaudi::CUDA::Detail::copyDeviceToHostNoStream ( void * hstPtr,
const void * devPtr,
std::size_t size )

Definition at line 141 of file CUDADeviceArray.cpp.

141 {
142 cudaError_t err = cudaMemcpy( hstPtr, devPtr, size, cudaMemcpyDeviceToHost );
143 if ( err != cudaSuccess ) {
144 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
145 }
146 }

◆ copyDeviceToHostWithStream()

void Gaudi::CUDA::Detail::copyDeviceToHostWithStream ( void * hstPtr,
const void * devPtr,
std::size_t size,
Stream & stream )

Definition at line 132 of file CUDADeviceArray.cpp.

132 {
133 cudaError_t err = cudaMemcpyAsync( hstPtr, devPtr, size, cudaMemcpyDeviceToHost, stream );
134 if ( err != cudaSuccess ) {
135 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
136 }
137 // await stream to avoid deleting host memory before copy is done
138 stream.await().orThrow( "Await error", DEVARREXC );
139 }

◆ copyHostToDeviceNoStream()

void Gaudi::CUDA::Detail::copyHostToDeviceNoStream ( void * devPtr,
const void * hstPtr,
std::size_t size )

Definition at line 125 of file CUDADeviceArray.cpp.

125 {
126 cudaError_t err = cudaMemcpy( devPtr, hstPtr, size, cudaMemcpyHostToDevice );
127 if ( err != cudaSuccess ) {
128 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
129 }
130 }

◆ copyHostToDeviceWithStream()

void Gaudi::CUDA::Detail::copyHostToDeviceWithStream ( void * devPtr,
const void * hstPtr,
std::size_t size,
Stream & stream )

Definition at line 116 of file CUDADeviceArray.cpp.

116 {
117 cudaError_t err = cudaMemcpyAsync( devPtr, hstPtr, size, cudaMemcpyHostToDevice, stream );
118 if ( err != cudaSuccess ) {
119 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
120 }
121 // await stream to avoid deleting host memory before copy is done
122 stream.await().orThrow( "Await error", DEVARREXC );
123 }

◆ freeNoStream()

void Gaudi::CUDA::Detail::freeNoStream ( void * ptr)

Definition at line 108 of file CUDADeviceArray.cpp.

108 {
109 cudaError_t err = cudaFree( ptr );
110 if ( err != cudaSuccess ) {
111 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
112 }
113 gpu_mem_cv.notify_all();
114 }

◆ freeWithStream()

void Gaudi::CUDA::Detail::freeWithStream ( void * ptr,
Stream & stream )

Definition at line 100 of file CUDADeviceArray.cpp.

100 {
101 cudaError_t err = cudaFreeAsync( ptr, stream );
102 if ( err != cudaSuccess ) {
103 throw GaudiException( err_fmt( err, __FILE__, __LINE__ ), DEVARREXC, StatusCode::FAILURE );
104 }
105 gpu_mem_cv.notify_all();
106 }