19#ifndef TMVA_DNN_ARCHITECTURES_CUDA_CUDAMATRIX
20#define TMVA_DNN_ARCHITECTURES_CUDA_CUDAMATRIX
23#include "cuda_runtime.h"
25#include "curand_kernel.h"
30#define CUDACHECK(ans) {cudaError((ans), __FILE__, __LINE__); }
38inline void cudaError(cudaError_t code,
const char *
file,
int line,
bool abort=
true);
53template<
typename AFloat>
97template<
typename AFloat>
190 if (code != cudaSuccess)
192 fprintf(stderr,
"CUDA Error: %s %s %d\n", cudaGetErrorString(code),
file,
line);
193 if (abort) exit(code);
198template<
typename AFloat>
200 : fDevicePointer(devicePointer)
206template<
typename AFloat>
210 cudaMemcpy(& buffer, fDevicePointer,
sizeof(AFloat),
211 cudaMemcpyDeviceToHost);
216template<
typename AFloat>
220 cudaMemcpyDeviceToDevice);
224template<
typename AFloat>
227 AFloat buffer = value;
228 cudaMemcpy(fDevicePointer, & buffer,
sizeof(AFloat),
229 cudaMemcpyHostToDevice);
233template<
typename AFloat>
237 cudaMemcpy(& buffer, fDevicePointer,
sizeof(AFloat),
238 cudaMemcpyDeviceToHost);
240 cudaMemcpy(fDevicePointer, & buffer,
sizeof(AFloat),
241 cudaMemcpyHostToDevice);
245template<
typename AFloat>
249 cudaMemcpy(& buffer, fDevicePointer,
sizeof(AFloat),
250 cudaMemcpyDeviceToHost);
252 cudaMemcpy(fDevicePointer, & buffer,
sizeof(AFloat),
253 cudaMemcpyHostToDevice);
257template<
typename AFloat>
260 return fElementBuffer.GetComputeStream();
264template<
typename AFloat>
267 return fElementBuffer.SetComputeStream(stream);
271template<
typename AFloat>
275 cudaEventCreateWithFlags(&event, cudaEventDisableTiming);
276 cudaEventRecord(event,
A.GetComputeStream());
277 cudaStreamWaitEvent(fElementBuffer.GetComputeStream(), event, 0);
278 cudaEventDestroy(event);
282template<
typename AFloat>
285 AFloat buffer = value;
286 cudaMemcpy(fDeviceReturn, & buffer,
sizeof(AFloat), cudaMemcpyHostToDevice);
290template<
typename AFloat>
294 cudaMemcpy(& buffer, fDeviceReturn,
sizeof(AFloat), cudaMemcpyDeviceToHost);
299template<
typename AFloat>
302 AFloat * elementPointer = fElementBuffer;
303 elementPointer += j * fNRows + i;
void operator-=(AFloat value)
TCudaDeviceReference(AFloat *devicePointer)
void operator=(const TCudaDeviceReference &other)
void operator+=(AFloat value)
TCudaDeviceBuffer< AFloat > fElementBuffer
TCudaMatrix & operator=(const TCudaMatrix &)=default
static curandState_t * fCurandStates
TCudaMatrix(const TMatrixT< AFloat > &)
static AFloat GetDeviceReturn()
Transfer the value in the device return buffer to the host.
void SetComputeStream(cudaStream_t stream)
static AFloat * fDeviceReturn
Buffer for kernel return values.
cudaStream_t GetComputeStream() const
size_t GetNoElements() const
void InitializeCuda()
Initializes all shared devices resource and makes sure that a sufficient number of curand states are ...
TCudaDeviceReference< AFloat > operator()(size_t i, size_t j) const
Access to elements of device matrices provided through TCudaDeviceReference class.
static AFloat * GetDeviceReturnPointer()
Return device pointer to the device return buffer.
const cublasHandle_t & GetCublasHandle() const
static AFloat * fOnes
Vector used for summations of columns.
static void ResetDeviceReturn(AFloat value=0.0)
Set the return buffer on the device to the specified value.
const AFloat * GetDataPointer() const
TCudaMatrix(TCudaDeviceBuffer< AFloat > buffer, size_t m, size_t n)
static size_t fNCurandStates
TCudaMatrix(const TCudaMatrix &)=default
void Synchronize(const TCudaMatrix &) const
Blocking synchronization with the associated compute stream, if it's not the default stream.
static AFloat * GetOnes()
static cublasHandle_t fCublasHandle
static size_t fInstances
Current number of matrix instances.
TCudaMatrix(size_t i, size_t j)
TCudaMatrix & operator=(TCudaMatrix &&)=default
void InitializeCurandStates()
AFloat * GetDataPointer()
static size_t fNOnes
Current length of the one vector.
TCudaMatrix(TCudaMatrix &&)=default
static curandState_t * GetCurandStatesPointer()
void Print(Option_t *name="") const
Print the matrix as a table of elements.
void cudaError(cudaError_t code, const char *file, int line, bool abort=true)
Function to check cuda return code.
Abstract ClassifierFactory template that handles arbitrary types.