2. cuFile API Specification#
This section provides information about the cuFile APIs that are used from the CPU to enable applications and frameworks.
2.1. Data Types#
2.1.1. Declarations and Definitions#
Here are the relevant cuFile enums and their descriptions.
typedef struct CUfileError {
CUfileOpError err; // cufile error
enum CUresult cu_err; // for CUDA-specific errors
} CUfileError_t;
/**
* error macros to inspect error status of type CUfileOpError
*/
#define IS_CUFILE_ERR(err) \
(abs((err)) > CUFILEOP_BASE_ERR)
#define CUFILE_ERRSTR(err) \
cufileop_status_error(static_cast<CUfileOpError>(abs((err))))
#define IS_CUDA_ERR(status) \
((status).err == CU_FILE_CUDA_DRIVER_ERROR)
#define CU_FILE_CUDA_ERR(status) ((status).cu_
The following enum and two structures enable broader cross-OS support:
enum CUfileFileHandleType {
CU_FILE_HANDLE_TYPE_OPAQUE_FD = 1, /* linux based fd */
CU_FILE_HANDLE_TYPE_OPAQUE_WIN32 = 2, /* windows based handle */
CU_FILE_HANDLE_TYPE_USERSPACE_FS = 3, /* userspace based FS */
};
typedef struct CUfileDescr_t {
CUfileFileHandleType type; /* type of file being registered */
union {
int fd; /* Linux */
void *handle; /* Windows */
} handle;
const CUfileFSOps_t *fs_ops; /* file system operation table */
} CUfileDescr_t;
/* cuFile handle type */
typedef void* CUfileHandle_t;
typedef struct cufileRDMAInfo
{
int version;
int desc_len;
const char *desc_str;
} cufileRDMAInfo_t;
typedef struct CUfileFSOps {
/* NULL means discover using fstat */
const char* (*fs_type) (void *handle);
/* list of host addresses to use, NULL means no restriction */
int (*getRDMADeviceList)(void *handle, sockaddr_t **hostaddrs);
/* -1 no pref */
int (*getRDMADevicePriority)(void *handle, char*, size_t,
loff_t, sockaddr_t* hostaddr);
/* NULL means try VFS */
ssize_t (*read) (void *handle, char*, size_t, loff_t, cufileRDMAInfo_t*);
ssize_t (*write) (void *handle, const char *, size_t, loff_t , cufileRDMAInfo_t*);
} CUfileFSOps_t;
typedef enum CUfileDriverStatusFlags {
CU_FILE_LUSTRE_SUPPORTED = 0, /*!< Support for DDN LUSTRE */
CU_FILE_WEKAFS_SUPPORTED = 1, /*!< Support for WEKAFS */
CU_FILE_NFS_SUPPORTED = 2, /*!< Support for NFS */
CU_FILE_GPFS_SUPPORTED = 3, /*! < Support for GPFS */
CU_FILE_NVME_SUPPORTED = 4, /*!< Support for NVMe */
CU_FILE_NVMEOF_SUPPORTED = 5, /*!< Support for NVMeOF */
CU_FILE_SCSI_SUPPORTED = 6, /*!< Support for SCSI */
CU_FILE_SCALEFLUX_CSD_SUPPORTED = 7, /*!< Support for Scaleflux CSD*/
CU_FILE_NVMESH_SUPPORTED = 8, /*!< Support for NVMesh Block Dev*/
CU_FILE_BEEGFS_SUPPORTED = 9, /*!< Support for BeeGFS */
//10 is reserved for YRCloudFile
CU_FILE_NVME_P2P_SUPPORTED = 11, /*!< Do not use this macro. This is deprecated now */
CU_FILE_SCATEFS_SUPPORTED = 12, /*!< Support for ScateFS */
CU_FILE_VIRTIOFS_SUPPORTED = 13, /*!<Support for VirtioFS */
CU_FILE_MAX_TARGET_TYPES /*!<Maximum FS supported */
} CUfileDriverStatusFlags_t;
Note: CU_FILE_NVME_P2P_SUPPORTED is now deprecated and should not be used to check P2PDMA support for NVMe devices.
enum CUfileDriverControlFlags {
CU_FILE_USE_POLL_MODE = 0, /*!< use POLL mode. properties.use_poll_mode*/
CU_FILE_ALLOW_COMPAT_MODE = 1 /*!< allow COMPATIBILITY mode. properties.allow_compat_mode*/
};
typedef enum CUfileFeatureFlags {
CU_FILE_DYN_ROUTING_SUPPORTED =0,
CU_FILE_BATCH_IO_SUPPORTED = 1,
CU_FILE_STREAMS_SUPPORTED = 2,
CU_FILE_PARALLEL_IO_SUPPORTED = 3,
CU_FILE_P2P_SUPPORTED = 4
} CUfileFeatureFlags_t;;
typedef enum CUfileP2PFlags {
CUFILE_P2PDMA = 0, /*!< Support for PCI P2PDMA */
CUFILE_NVFS = 1, /*!< Support for nvidia-fs */
CUFILE_DMABUF = 2, /*!< Support for DMA Buffer */
CUFILE_C2C = 3, /*!< Support for Chip-to-Chip (Grace-based systems) */
CUFILE_NVIDIA_PEERMEM = 4 /*!< Only for IBM Spectrum Scale and WekaFS */
}CUfileP2PFlags_t;
/* P2P Flag constants for use with cuFileDriverSetP2PFlags */
#define CU_FILE_P2P_FLAG_PCI_P2PDMA ((CUfileP2PFlags_t)(1 << CUFILE_P2PDMA))
#define CU_FILE_P2P_FLAG_NVFS ((CUfileP2PFlags_t)(1 << CUFILE_NVFS))
#define CU_FILE_P2P_FLAG_DMABUF ((CUfileP2PFlags_t)(1 << CUFILE_DMABUF))
#define CU_FILE_P2P_FLAG_C2C ((CUfileP2PFlags_t)(1 << CUFILE_C2C))
/* cuFileDriverGetProperties describes this structure's members */
typedef struct CUfileDrvProps {
struct {
unsigned int major_version;
unsigned int minor_version;
size_t poll_thresh_size;
size_t max_direct_io_size;
unsigned int dstatusflags;
unsigned int dcontrolflags;
} nvfs;
CUfileFeatureFlags_t fflags;
unsigned int max_device_cache_size;
unsigned int per_buffer_cache_size;
unsigned int max_pinned_memory_size;
unsigned int max_batch_io_timeout_msecs;
} CUfileDrvProps_t;
/* Parameter block for async cuFile IO */
/* Batch APIs use an array of these */
/* Status must be CU_FILE_WAITING when submitted, and is
updated when enqueued and when complete, so this user-allocated
structure is live until the operation completes. */
typedef enum CUFILEStatus_enum {
CUFILE_WAITING = 0x000001, /* required value prior to submission */
CUFILE_PENDING = 0x000002, /* once enqueued */
CUFILE_INVALID = 0x000004, /* request was ill-formed or could not be enqueued */
CUFILE_CANCELED = 0x000008, /* request successfully canceled */
CUFILE_COMPLETE = 0x0000010, /* request successfully completed */
CUFILE_TIMEOUT = 0x0000020, /* request timed out */
CUFILE_FAILED = 0x0000040 /* unable to complete */
}CUfileStatus_t;
typedef enum cufileBatchMode {
CUFILE_BATCH = 1,
} CUfileBatchMode_t;
typedef struct CUfileIOParams {
CUfileBatchMode_t mode; // Must be the very first field.
union {
struct {
void *devPtr_base;
off_t file_offset;
off_t devPtr_offset;
size_t size;
}batch;
}u;
CUfileHandle_t fh;
CUfileOpcode_t opcode;
void *cookie;
} CUfileIOParams_t;
typedef struct CUfileIOEvents {
void *cookie;
CUfileStatus_t status; /* status of the operation */
size_t ret; /* -ve error or amount of I/O done. */
} CUfileIOEvents_t;
The following data types are used to collect stats using the cuFile stats APIs which are discussed later in this document:
/**
* @brief Counter structure for tracking operation successes and failures
*/
typedef struct CUfileOpCounter {
uint64_t ok; // Number of successful operations
uint64_t err; // Number of failed operations
} CUfileOpCounter_t;
/**
* @brief Level 1 Statistics: Basic IO and operation counters
*/
typedef struct CUfileStatsLevel1 {
// Operation counters
CUfileOpCounter_t read_ops; // Read operations
CUfileOpCounter_t write_ops; // Write operations
CUfileOpCounter_t hdl_register_ops; // Handle register operations
CUfileOpCounter_t hdl_deregister_ops; // Handle deregister operations
CUfileOpCounter_t buf_register_ops; // Buffer register operations
CUfileOpCounter_t buf_deregister_ops; // Buffer deregister operations
// Basic IO statistics
uint64_t read_bytes; // Total bytes read
uint64_t write_bytes; // Total bytes written
uint64_t read_bw_bytes_per_sec; // Read bandwidth (bytes/sec)
uint64_t write_bw_bytes_per_sec; // Write bandwidth (bytes/sec)
uint64_t read_lat_avg_us; // Average read latency (microseconds)
uint64_t write_lat_avg_us; // Average write latency (microseconds)
// Operations per second
uint64_t read_ops_per_sec; // Read operations per second
uint64_t write_ops_per_sec; // Write operations per second
// Latency sums
uint64_t read_lat_sum_us; // Sum of read latencies
uint64_t write_lat_sum_us; // Sum of write latencies
// Batch operations counters
CUfileOpCounter_t batch_submit_ops; // Batch submit operations
CUfileOpCounter_t batch_complete_ops; // Batch complete operations
CUfileOpCounter_t batch_setup_ops; // Batch setup operations
CUfileOpCounter_t batch_cancel_ops; // Batch cancel operations
CUfileOpCounter_t batch_destroy_ops; // Batch destroy operations
// Batch queue counters
CUfileOpCounter_t batch_enqueued_ops; // Batch enqueue operations
CUfileOpCounter_t batch_posix_enqueued_ops; // POSIX batch enqueue operations
CUfileOpCounter_t batch_processed_ops; // Batch process operations
CUfileOpCounter_t batch_posix_processed_ops; // POSIX batch process operations
// Batch submission type counters
CUfileOpCounter_t batch_nvfs_submit_ops; // NVFS batch submit operations
CUfileOpCounter_t batch_p2p_submit_ops; // P2P batch submit operations
CUfileOpCounter_t batch_aio_submit_ops; // AIO batch submit operations
CUfileOpCounter_t batch_iouring_submit_ops; // IO_URING batch submit operations
CUfileOpCounter_t batch_mixed_io_submit_ops; // Mixed IO batch submit operations
CUfileOpCounter_t batch_total_submit_ops; // Total batch submit operations
// Batch operations statistics
uint64_t batch_read_bytes; // Total batch read bytes
uint64_t batch_write_bytes; // Total batch write bytes
uint64_t batch_read_bw_bytes; // Batch read bandwidth
uint64_t batch_write_bw_bytes; // Batch write bandwidth
uint64_t batch_submit_lat_avg_us; // Avg batch submit latency
uint64_t batch_completion_lat_avg_us; // Avg batch completion latency
uint64_t batch_submit_ops_per_sec; // Batch submit operations per second
uint64_t batch_complete_ops_per_sec; // Batch complete operations per second
uint64_t batch_submit_lat_sum_us; // Sum of batch submit latencies
uint64_t batch_completion_lat_sum_us; // Sum of batch completion latencies
uint64_t last_batch_read_bytes; // Last batch read bytes
uint64_t last_batch_write_bytes; // Last batch write bytes
} CUfileStatsLevel1_t;
/**
* @brief Level 2 Statistics: Includes Level 1 plus size histograms and detailed metrics
*/
typedef struct CUfileStatsLevel2 {
// Basic statistics (Level 1)
CUfileStatsLevel1_t basic;
// IO size histograms
uint64_t read_size_kb_hist[32]; // Histogram of read sizes
uint64_t write_size_kb_hist[32]; // Histogram of write sizes
} CUfileStatsLevel2_t;
/**
* @brief Per-GPU statistics structure used in Level 3
*/
typedef struct CUfilePerGpuStats {
char uuid[CUFILE_GPU_UUID_LEN]; // GPU UUID
// Read operations
uint64_t read_bytes; // Total bytes read
uint64_t read_bw_bytes_per_sec; // Read bandwidth in bytes per second
uint64_t read_utilization; // Read utilization percentage
uint64_t read_duration_us; // Read operation duration
uint64_t n_total_reads; // Total number of reads
uint64_t n_p2p_reads; // Number of PCIe P2PDMA reads
uint64_t n_nvfs_reads; // Number of nvidia-fs reads
uint64_t n_posix_reads; // Number of POSIX reads
uint64_t n_unaligned_reads; // Number of unaligned reads
uint64_t n_dr_reads; // Number of reads using dynamic routing
uint64_t n_sparse_regions; // Number of sparse regions
uint64_t n_inline_regions; // Number of inline regions
uint64_t n_reads_err; // Number of read errors
// Write operations
uint64_t writes_bytes; // Total bytes written
uint64_t write_bw_bytes_per_sec; // Write bandwidth in bytes per second
uint64_t write_utilization; // Write utilization percentage
uint64_t write_duration_us; // Write operation duration
uint64_t n_total_writes; // Total number of writes
uint64_t n_p2p_writes; // Number of PCIe P2PDMA writes
uint64_t n_nvfs_writes; // Number of nvidia-fs writes
uint64_t n_posix_writes; // Number of POSIX writes
uint64_t n_unaligned_writes; // Number of unaligned writes
uint64_t n_dr_writes; // Number of writes using dynamic routing
uint64_t n_writes_err; // Number of write errors
// Buffer registration statistics
uint64_t n_mmap; // Number of buffer registrations
uint64_t n_mmap_ok; // Successful registrations
uint64_t n_mmap_err; // Failed registrations
uint64_t n_mmap_free; // Number of buffer deregistrations
uint64_t reg_bytes; // Total bytes registered
} CUfilePerGpuStats_t;
/**
* @brief Level 3 Statistics: Includes Level 2 plus per-GPU and subsystem statistics
*/
typedef struct CUfileStatsLevel3 {
// Detailed statistics (Level 2)
CUfileStatsLevel2_t detailed;
// Number of GPUs detected
uint32_t num_gpus;
// Per-GPU statistics (array for each GPU)
CUfilePerGpuStats_t per_gpu_stats[16]; // Using the maxGpus constant value from cufile_stats.h
} CUfileStatsLevel3_t;
The following enum values specify which parameter to set or query when calling the cuFileSetParameter* and cuFileGetParameter* APIs.
typedef enum CUFileSizeTConfigParameter_t {
CUFILE_PARAM_PROFILE_STATS,
CUFILE_PARAM_EXECUTION_MAX_IO_QUEUE_DEPTH,
CUFILE_PARAM_EXECUTION_MAX_IO_THREADS,
CUFILE_PARAM_EXECUTION_MIN_IO_THRESHOLD_SIZE_KB,
CUFILE_PARAM_EXECUTION_MAX_REQUEST_PARALLELISM,
CUFILE_PARAM_PROPERTIES_MAX_DIRECT_IO_SIZE_KB,
CUFILE_PARAM_PROPERTIES_MAX_DEVICE_CACHE_SIZE_KB,
CUFILE_PARAM_PROPERTIES_PER_BUFFER_CACHE_SIZE_KB,
CUFILE_PARAM_PROPERTIES_MAX_DEVICE_PINNED_MEM_SIZE_KB,
CUFILE_PARAM_PROPERTIES_IO_BATCHSIZE,
CUFILE_PARAM_POLLTHRESHOLD_SIZE_KB,
CUFILE_PARAM_PROPERTIES_BATCH_IO_TIMEOUT_MS,
} CUFileSizeTConfigParameter_t;
typedef enum CUFileBoolConfigParameter_t {
CUFILE_PARAM_PROPERTIES_USE_POLL_MODE,
CUFILE_PARAM_PROPERTIES_ALLOW_COMPAT_MODE,
CUFILE_PARAM_FORCE_COMPAT_MODE,
CUFILE_PARAM_FS_MISC_API_CHECK_AGGRESSIVE,
CUFILE_PARAM_EXECUTION_PARALLEL_IO,
CUFILE_PARAM_PROFILE_NVTX, //Do not use this macro. This is deprecated now.
CUFILE_PARAM_PROPERTIES_ALLOW_SYSTEM_MEMORY,
CUFILE_PARAM_USE_PCIP2PDMA,
CUFILE_PARAM_PREFER_IO_URING,
CUFILE_PARAM_FORCE_ODIRECT_MODE,
CUFILE_PARAM_SKIP_TOPOLOGY_DETECTION,
CUFILE_PARAM_STREAM_MEMOPS_BYPASS,
} CUFileBoolConfigParameter_t;
typedef enum CUFileStringConfigParameter_t {
CUFILE_PARAM_LOGGING_LEVEL,
CUFILE_PARAM_ENV_LOGFILE_PATH,
CUFILE_PARAM_LOG_DIR,
} CUFileStringConfigParameter_t;
Note: CUFILE_PARAM_PROFILE_NVTX is now deprecated and attempting to set/get this property will result in a failure.
2.1.2. Typedefs#
cuFile typedefs:
typedef struct CUfileDescr CUfileDescr_t
typedef struct CUfileError CUfileError_t
typedef struct CUfileDrvProps CUfileDrvProps_t
typedef enum CUfileFeatureFlags CUfileFeatureFlags_t
typedef enum CUfileDriverStatusFlags_enum CUfileDriverStatusFlags_t
typedef enum CUfileDriverControlFlags_enum CUfileDriverControlFlags_t
typedef struct CUfileIOParams CUfileIOParams_t
typedef enum CUfileBatchOpcode CUfileBatchOpcode_t
2.1.3. Enumerations#
cuFile enums:
enum CUfileOpcode_enumThis is the cuFile operation code for batch mode.
OpCode |
Value |
Description |
|---|---|---|
|
0 |
Batch Read |
|
1 |
Batch Write |
/* cuFile Batch IO operation kind */ enum CUfileOpcode { CU_FILE_READ, CU_FILE_WRITE, };
enum CUfileStatusThe cuFile Status codes for batch mode.
Status |
Value |
Description |
|---|---|---|
|
0x01 |
The initial value. |
|
0x02 |
Set once enqueued into the driver. |
|
0x04 |
Invalid parameters. |
|
0x08 |
Request successfully canceled. |
|
0x10 |
Successfully completed. |
|
0x20 |
The operation has timed out. |
|
0x40 |
IO has failed. |
enum CUfileOpErrorThe cuFile Operation error types.
All error code values, other than
CU_FILE_SUCCESS, are considered failures that might leave the output and input parameter values of APIs in an undefined state.These values cannot have any side effects on the file system, the application process, and the larger system.
Note
cuFile-specific errors will be greater than
CUFILEOP_BASE_ERRto enable users to distinguish between POSIX errors and cuFile errors.#define CUFILEOP_BASE_ERR 5000
Note
Data path errors are captured via standard error codes by using errno. The APIs will return -1 on error.
2.2. cuFile Driver APIs#
The following cuFile APIs are used to initialize, finalize, query, and tune settings for the cuFile system.
/* Initialize the cuFile infrastructure */
CUfileError_t cuFileDriverOpen();
/* Finalize the cuFile system */
CUfileError_t cuFileDriverClose();
/* Query capabilities based on current versions, installed functionality */
CUfileError_t cuFileGetDriverProperties(CUfileDrvProps_t *props);
/*API to set whether the Read/Write APIs use polling to do IO operations */
CUfileError_t cuFileDriverSetPollMode(bool poll, size_t poll_threshold_size);
/*API to set max IO size(KB) used by the library to talk to nvidia-fs driver */
CUfileError_t cuFileDriverSetMaxDirectIOSize(size_t max_direct_io_size);
/* API to set maximum GPU memory reserved per device by the library for internal buffering */
CUfileError_t cuFileDriverSetMaxCacheSize(size_t max_cache_size);
/* Sets maximum buffer space that is pinned in KB for use by cuFileBufRegister
CUfileError_t cuFileDriverSetMaxPinnedMemSize(size_t
max_pinned_memory_size);
/* Retrieves the cuFile library version. */
CUfileError_t cuFileGetVersion(int *version);
Note
Refer to the error_and_properties.cc sample for usage.
2.3. cuFile Synchronous IO APIs#
The core cuFile IO APIs are the read and write functions.
ssize_t cuFileRead(CUfileHandle_t fh, void *bufPtr_base, size_t size, off_t file_offset, off_t devPtr_offset);
ssize_t cuFileWrite(CUfileHandle_t fh, const void *bufPtr_base, size_t size, off_t file_offset, off_t devPtr_offset);
The starting offset of the buffer on the device or host is determined by a base (bufPtr_base) and offset
(bufPtr_offset). This offset is distinct from the offset in the file.
Note
To use the registered buffer, the bufPtr_base must be the buffer pointer used to register during cuFileBufRegister.
Otherwise cuFileRead and cuFileWrite APIs may use internal memory buffers for GPUDirect Storage peer to peer operations.
Note
The default behavior for all paths where GDS is not supported is for the cuFile IO API to attempt IO using file
system supported posix mode APIs when properties.allow_compat_mode is set to true. In order to disable cuFile
APIs falling back to posix APIs for unsupported GDS paths, properties.allow_compat_mode in the /etc/cufile.json
file should be set to false.
Note
Refer to the runtime_rw_integrity.cc sample for usage.
2.4. cuFile File Handle APIs#
Here is some information about the cuFile Handle APIs.
The cuFileHandleRegister API makes a file descriptor or handle that is known to the cuFile subsystem by using an
OS-agnostic interface. The API returns an opaque handle that is owned by the cuFile subsystem.
To conserve memory, the cuFileHandleDeregister API is used to release cuFile-related memory objects. Using only
the POSIX close will not clean up resources that were used by cuFile. Additionally, the clean up of cuFile objects
associated with the files that were operated on in the cuFile context will occur at cuFileDriverClose.
CUfileError_t cuFileHandleRegister(CUfileHandle_t *fh, CUFileDescr_t *descr);
void cuFileHandleDeregister(CUfileHandle_t fh);
Note
Refer to the runtime_rw_integrity.cc sample for usage.
2.5. cuFile Buffer APIs#
The cuFileBufRegister API incurs a significant performance cost, so registration costs should be amortized
where possible. Developers must ensure that buffers are registered up front and off the critical path.
The cuFileBufRegister API is optional. If this is not used, instead of pinning the user’s memory, cuFile-managed
and internally pinned buffers are used.
The cuFileBufDeregister API is used to optimally clean up cuFile-related memory objects, but CUDA currently
has no analog to cuFileBufDeregister. The cleaning up of objects associated with the buffers operated on in
the cuFile context occurs at cuFileDriverClose. If explicit APIs are used, the incurred errors are reported
immediately, but if the operations of these explicit APIs are performed implicitly, error reporting and handling
are less clear.
CUfileError_t cuFileBufRegister(const void *devPtr_base, size_t size, int flags);
CUfileError_t cuFileBufDeregister(const void *devPtr_base);
Note
Refer to the devmem_offset_write.cc sample for usage.
2.6. cuFile Stream APIs#
Operations that are enqueued with cuFile Stream APIs are FIFO ordered with respect to other work on the stream and must be completed before continuing with the next action in the stream.
CUfileError_t cuFileReadAsync(CUfileHandle_t fh, void *bufPtr_base,
size_t *size_p, off_t *file_offset_p, off_t *bufPtr_offset_p,
ssize_t *bytes_read_p, CUStream stream);
CUfileError_t cuFileWriteAsync(CUfileHandle_t fh, void *bufPtr_base,
size_t *size_p, off_t *file_offset_p, off_t *bufPtr_offse_pt,
ssize_t *bytes_written_p, CUstream stream);
Note
Refer to the samples in 4_cuFile_Async for usage.
2.7. cuFile Batch APIs#
Batch APIs are submitted synchronously, but executed asynchronously with respect to host thread.
These operations can be submitted on different files, different locations in the same file, or a mix.
Completion of IO can be checked asynchronously using a status API in the same host thread or in a
different thread. The cuFileBatchIOGetStatus API takes an array of CUfileIOEvents_t and
minimum number of elements to poll for, which describes the IO action, status, errors, and bytes
transacted for each instance. The bytes transacted field is valid only when the status indicates a
successful completion. For any batch I/O size bigger than the bounce buffer size (properties.per_buffer_cache_size_kb),
cuFile uses compatible mode.
Note
Refer to the samples in 3_cuFile_Batch for usage.