lcls2
C++ API: public headers of xtcdata, psalg, psdaq and psana
Loading...
Searching...
No Matches
GpuAsyncLib.hh File Reference

CUDA and GPU Async helpers (error checks, DataDev, CudaContext, FPGA and GPU memory mapping, DMA target selection, GPUTimer) from axi-pcie-devel. More...

#include "Utils.h"
#include <string>
#include "psdaq/aes-stream-drivers/GpuAsync.h"
#include <assert.h>
#include <stdint.h>
#include <cuda.h>
#include <cuda_runtime.h>

Classes

class  DataDev
 Wraps a data_dev device so it can be automatically freed. More...
 
class  CudaContext
 Wraps a cuda context and handles some initialization for you. More...
 
struct  GpuDmaBuffer_t
 Describes FPGA memory that's mapped via RDMA to the GPU. More...
 
struct  GpuBufferState_t
 A read buffer and a write buffer on the GPU, mapped to the FPGA by gpuInitBufferState(). More...
 
struct  GPUTimer
 Class for timing various things. More...
 

Macros

#define deviceFunc
 
#define globalFunc
 Empty when not compiling with nvcc; global under nvcc.
 
#define hostFunc
 Empty when not compiling with nvcc; host under nvcc.
 
#define NOARG   ""
 Empty string literal put in front of the optional message, so that the message argument is never empty (per the code comment).
 
#define chkFatal(rc, ...)   checkError((rc), #rc, __FILE__, __LINE__, true, NOARG __VA_ARGS__)
 Check a CUDA driver or runtime result rc with checkError(), logging any failure with the optional message and aborting.
 
#define chkError(rc, ...)   checkError((rc), #rc, __FILE__, __LINE__, false, NOARG __VA_ARGS__)
 Check a CUDA driver or runtime result rc with checkError(), logging any failure with the optional message without aborting; evaluates to true on failure.
 

Enumerations

enum  DmaTgt_t { TGT_CPU =0x0 , TGT_GPU =0x1 , TGT_ERR =-1u }
 Functions to get and set the DMA destination. More...
 

Functions

bool checkError (CUresult status, const char *func, const char *file, int line, bool crash=true, const char *msg="")
 If status is not CUDA_SUCCESS, log file, line, the error name and description and msg, then abort if crash is true.
 
bool checkError (cudaError status, const char *func, const char *file, int line, bool crash=true, const char *msg="")
 If status is not cudaSuccess, log file, line, the error name and description and msg, then abort if crash is true.
 
template<typename T , int COLS = 16>
static void dumpGpuMem (CUdeviceptr devicePtr, size_t sizeInBytes, void *buf=nullptr, FILE *stream=stdout)
 Dumps a buffer from the GPU to a stream.
 
static void show_buf (CUdeviceptr dptr, size_t size, CUstream stream=0)
 Simple utility to display a device buffer.
 
int gpuMapHostFpgaMem (GpuDmaBuffer_t *outmem, int fd, uint64_t offset, size_t size)
 Maps FPGA memory to the host and allows GPU access to it This is the "standard" way of doing DMA with CUDA.
 
int gpuMapFpgaMem (GpuDmaBuffer_t *outmem, int fd, uint64_t offset, size_t size, int write)
 Maps GPU memory to the FPGA using RDMA This function uses gpuAddNvidiaMemory to give the FPGA access to some pages of memory located on the GPU.
 
void gpuUnmapFpgaMem (GpuDmaBuffer_t *mem)
 Unmaps memory, clears out the pointer and size.
 
int gpuInitBufferState (GpuBufferState_t *b, int fd, size_t bufSize)
 Allocates and inits buffer pairs on the GPU for RDMA operation.
 
void gpuDestroyBufferState (GpuBufferState_t *b)
 Unmap and free both buffers of b with gpuUnmapFpgaMem().
 
struct __attribute__ ((packed)) AxiWrDesc64_t
 64-bit write AXI descriptor data
 
deviceFunc AxiWrDesc64_t UnpackAxiWriteDescriptor (const void *data)
 Return the 8 bytes at data as an AxiWrDesc64_t.
 
DmaTgt_t dmaTgtGet (const DataDev &)
 Read the AXI stream demux select register of the GPU Async core and return TGT_CPU or TGT_GPU for its value, or TGT_ERR for any other value.
 
void dmaTgtSet (const DataDev &, DmaTgt_t)
 Write tgt to the AXI stream demux select register of the GPU Async core; a write error is only reported with perror().
 
void dmaIdxReset (const DataDev &)
 Function to reset the DMA buffer round-robin index.
 

Detailed Description

CUDA and GPU Async helpers (error checks, DataDev, CudaContext, FPGA and GPU memory mapping, DMA target selection, GPUTimer) from axi-pcie-devel.

In psdaq they are built only into the pgpread_gpu tool.

Macro Definition Documentation

◆ chkError

#define chkError (   rc,
  ... 
)    checkError((rc), #rc, __FILE__, __LINE__, false, NOARG __VA_ARGS__)

Check a CUDA driver or runtime result rc with checkError(), logging any failure with the optional message without aborting; evaluates to true on failure.

◆ chkFatal

#define chkFatal (   rc,
  ... 
)    checkError((rc), #rc, __FILE__, __LINE__, true, NOARG __VA_ARGS__)

Check a CUDA driver or runtime result rc with checkError(), logging any failure with the optional message and aborting.

◆ deviceFunc

#define deviceFunc

Company : SLAC National Accelerator Laboratory

Description: Library and utilities for CUDA and the GPU Async protocol.

This file is part of 'axi-pcie-devel'. It is subject to the license terms in the LICENSE.txt file found in the top-level directory of this distribution and at: https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html. No part of 'axi-pcie-devel', including this file, may be copied, modified, propagated, or distributed except according to the terms contained in

the LICENSE.txt file.

◆ globalFunc

#define globalFunc

Empty when not compiling with nvcc; global under nvcc.

◆ hostFunc

#define hostFunc

Empty when not compiling with nvcc; host under nvcc.

◆ NOARG

#define NOARG   ""

Empty string literal put in front of the optional message, so that the message argument is never empty (per the code comment).

Enumeration Type Documentation

◆ DmaTgt_t

enum DmaTgt_t

Functions to get and set the DMA destination.

Parameters
fdThe file descriptor of the PGP PCIe device
modeThe enumerated value of the destination
Enumerator
TGT_CPU 

Value 0: destination CPU.

TGT_GPU 

Value 1: destination GPU.

TGT_ERR 

Returned by dmaTgtGet() for any other register value.

Function Documentation

◆ __attribute__()

struct __attribute__ ( (packed)  )

64-bit write AXI descriptor data

Overflow bit

Continue bit

◆ checkError() [1/2]

bool checkError ( cudaError  status,
const char *  func,
const char *  file,
int  line,
bool  crash = true,
const char *  msg = "" 
)

If status is not cudaSuccess, log file, line, the error name and description and msg, then abort if crash is true.

Returns true on failure, else false; func is not used.

◆ checkError() [2/2]

bool checkError ( CUresult  status,
const char *  func,
const char *  file,
int  line,
bool  crash = true,
const char *  msg = "" 
)

If status is not CUDA_SUCCESS, log file, line, the error name and description and msg, then abort if crash is true.

Returns true on failure, else false; func is not used.

◆ dmaIdxReset()

void dmaIdxReset ( const DataDev &  )

Function to reset the DMA buffer round-robin index.

Parameters
fdThe file descriptor of the PGP PCIe device

◆ dmaTgtGet()

DmaTgt_t dmaTgtGet ( const DataDev &  )

Read the AXI stream demux select register of the GPU Async core and return TGT_CPU or TGT_GPU for its value, or TGT_ERR for any other value.

A read error is only reported with perror().

◆ dmaTgtSet()

void dmaTgtSet ( const DataDev &  ,
DmaTgt_t   
)

Write tgt to the AXI stream demux select register of the GPU Async core; a write error is only reported with perror().

◆ dumpGpuMem()

template<typename T , int COLS = 16>
static void dumpGpuMem ( CUdeviceptr  devicePtr,
size_t  sizeInBytes,
void *  buf = nullptr,
FILE *  stream = stdout 
)
inlinestatic

Dumps a buffer from the GPU to a stream.

Parameters
devicePtrPointer to the block of memory to dump on the GPU
sizeInBytes
bufThe copy buffer (assumed to be sizeInBytes in size). If NULL, one will be allocated
streamThe stream to dump to

◆ gpuDestroyBufferState()

void gpuDestroyBufferState ( GpuBufferState_t *  b)

Unmap and free both buffers of b with gpuUnmapFpgaMem().

◆ gpuInitBufferState()

int gpuInitBufferState ( GpuBufferState_t *  b,
int  fd,
size_t  bufSize 
)

Allocates and inits buffer pairs on the GPU for RDMA operation.

Parameters
bBuffer state to init
fdThe file descriptor of the DataDev device to allocate for
bufSizeThe size of the buffers to allocate
Returns
0 for success, -1 on error

◆ gpuMapFpgaMem()

int gpuMapFpgaMem ( GpuDmaBuffer_t *  outmem,
int  fd,
uint64_t  offset,
size_t  size,
int  write 
)

Maps GPU memory to the FPGA using RDMA This function uses gpuAddNvidiaMemory to give the FPGA access to some pages of memory located on the GPU.

This is the other method of doing DMA, and it requires custom driver code and a very specific PCIe topology. Regardless, RDMA allows for low-latency, high-bandwidth DMA transfers between two devices on the bus, without needing to interact with the CPU.

Parameters
outmem
fdFile descriptor of the FPGA device
offsetOffset of the register block
sizeSize of the register block
writeIf 1, this will be writable

◆ gpuMapHostFpgaMem()

int gpuMapHostFpgaMem ( GpuDmaBuffer_t *  outmem,
int  fd,
uint64_t  offset,
size_t  size 
)

Maps FPGA memory to the host and allows GPU access to it This is the "standard" way of doing DMA with CUDA.

cuMemHostRegister essentially just gives CUDA access to some pages and allows the GPU to read/write to them. Unlike RDMA, this doesn't require any custom code on the kernel side, this will work with any (properly aligned) buffer given, whether it's IO memory or not. Again unlike RDMA, cuMemHostRegister has more latency and lower throughput, since a DMA transfer must occur between the GPU <-> HOST <-> FPGA

Parameters
outmem
fdFile descriptor of the FPGA device
offsetOffset of the register block
sizeSize of the register block
Returns
0 for success

◆ gpuUnmapFpgaMem()

void gpuUnmapFpgaMem ( GpuDmaBuffer_t *  mem)

Unmaps memory, clears out the pointer and size.

◆ show_buf()

static void show_buf ( CUdeviceptr  dptr,
size_t  size,
CUstream  stream = 0 
)
static

Simple utility to display a device buffer.

◆ UnpackAxiWriteDescriptor()

deviceFunc AxiWrDesc64_t UnpackAxiWriteDescriptor ( const void *  data)
inline

Return the 8 bytes at data as an AxiWrDesc64_t.