1. Usage#

This section describes the operation of the cuFile APIs.

Because the functionality is part of the CUDA Driver C API, the APIs use the cuFile prefix and camel case motif of the CUDA Driver.

  • All APIs are thread-safe.

  • The fork system call should not be used after the library is initialized. The behavior of the APIs after the fork system call is undefined in the child process.

  • The APIs with GPU buffers should be called in a valid CUDA context and stream if applicable.

  • All APIs are issued from the CPU, not the GPU.

Note

Starting from CUDA toolkit 12.2 (GDS version 1.7.x) release cuFile APIs support memory allocated on GPU device as well as host memory. peer to peer transfer using GPUDirect is supported to and from device memory on supported file system and hardware configurations. The APIs will refer to this memory address as buffer pointer unless the API specifically applies to a particular type of memory.

1.1. Dynamic Interactions#

The following describes the dynamic interactions between the cuFile APIs.

Some of the cuFile APIs are optional. If they are not called proactively, their actions will occur reactively:

If cuFile{DriverOpen, HandleRegister, BufRegister} `` is called on a driver, file, or buffer, respectively that has been opened or registered by a previous ``cuFile* API call, this will result in an error. Calling cuFile{BufDeregister, HandleDeregister, DriverClose} on a buffer, file, or driver, respectively that has never been opened or registered by a previous cuFile* API call results in an error. For these errors, the output parameters of the APIs are left in an undefined state, and there are no other side effects.

  • cuFileDriverOpen explicitly causes driver initialization.

    Its use is optional. If it is not used, driver initialization happens implicitly at the first use of the cuFile{HandleRegister, Read, Write, BufRegister} APIs.

  • (Mandatory) cuFileHandleRegister turns an OS-specific file descriptor into a CUfileHandle_t and performs checking on the GDS supportability based on the mount point and the way that the file was opened.

  • cuFileBufRegister explicitly registers a memory buffer.

    If this API is not called, an internal registered memory is used if required on the first time the buffer is used, for example, in cuFile{Read, Write}.

  • cuFile{BufDeregister, HandleDeregister} explicitly frees a buffer and file resources, respectively.

    If this API is not called, the buffer and resources are implicitly freed when the driver is closed using cuFileDriverClose.

  • cuFileDriverClose explicitly frees driver resources.

    If this API is not called, the driver resources are implicitly freed when dlclose() is performed on the library handle or when the process is terminated.

Note

In GDS v1.20 (CUDA 13.5), cuFile and cuObject have separate configuration files and environment variables but share a runtime within a process. The first successful initialization, whether explicit or implicit, determines which configuration is loaded. Subsequent initialization calls reuse that configuration without loading the other interface’s configuration, until the shared runtime is fully shut down.

Calling cuFileDriverOpen() after cuObject has initialized the shared runtime logs a warning and continues using the cuObject configuration.

Direct application use of both cuFile and cuObject APIs in the same process is not supported in this release. This restriction does not apply to cuObject’s internal use of cuFile. Applications requiring independent cuFile and cuObject configurations must use separate processes.

1.2. Driver, File, and Buffer Management#

This section describes the overall workflow to manage the driver, the file, and buffer management:

  1. Call cuFileDriverOpen() to initialize the state of the critical performance path.

  2. Allocate GPU memory with cudaMalloc, cudaMallocManaged, cuMem* APIs or host memory using cudaMallocHost, malloc or mmap.

  3. To register the buffer, call cuFileBufRegister to initialize the buffer state of the critical performance path.

  4. Complete the following IO workflow:

    1. For Linux, open a file with POSIX open.

    2. Call cuFileHandleRegister to wrap an existing file descriptor in an OS-agnostic CUfileHandle_t. This step evaluates the suitability of the file state and the file mount for GDS and initializes the file state of the critical performance path.

    3. Call IO APIs such as cuFileRead/cuFileWrite on an existing cuFile handle and existing buffer.

      • If the cuFileBufRegister has not been previously called on the buffer pointer, cuFileRead/cuFileWrite will use internal registered buffers when required.

      • Not using cuFileBufRegister might not be performant for small IO sizes.

      • Refer to the GPUDirect Best Practices Guide for more information.

    4. Unless an error condition is returned, the IO is performed successfully.

  5. Call cuFileBufDeregister to free the buffer-specific cuFile state.

  6. Call cuFileHandleDeregister to free the file-specific cuFile state.

  7. Call cuFileDriverClose to free up the cuFile state.

Note

Not using the cuFileDeregister and cuFileDriverClose APIs (steps 5, 6, and 7) might unnecessarily consume resources, as shown by tools such as valgrind. The best practice is to always call these APIs in the application cleanup paths.

1.3. cuFile Compatibility Mode#

Use Cases

cuFile APIs can be used in different scenarios:

  • Developers building GPUDirect Storage applications with cuFile APIs, but don’t have the supported hardware configurations.

  • Developers building applications running on GPU cards that have CUDA compute capability > 6, but don’t have BAR space exposed.

  • Deployments where nvidia-fs.ko is not loaded or cannot be loaded.

  • Deployments where the Linux distribution does not support GPUDirect Storage.

  • Deployments where the filesystem may be not supported with GPUDirect Storage.

  • Deployments where the network links are not enabled with RDMA support.

  • Deployment where the configuration is not optimal for GPUDirect Storage.

Behavior

The cuFile library provides a mechanism for cuFile reads and writes to use compatibility mode using POSIX pread, pwrite, and aio_submit APIS respectively to host memory and copying to GPU memory when applicable. The behavior of compatibility mode with cuFile APIs is determined by the following configuration parameters.

Configuration Option (default)

cuFile IO Behavior

“allow_compat_mode”: true

If true, falls back to using compatibility mode when the library detects that the buffer file descriptor opened cannot use GPUDirect Storage.

“force_compat_mode”: false

If true, this option can be used to force all IO to use compatibility mode. Alternatively the admin can unload the nvidia_fs.ko or not expose the character devices in the docker container environment.

“gds_rdma_write_support”: true

If false, forces compatibility mode to be used for writes even when the underlying file system is capable of performing GPUDirect Storage writes. Note: If the option is “false”, this option will override and disable any filesystem-specific option to enable RDMA writes.

“posix_unaligned_writes” : false

If true, forces compatibility mode to be used for writes where the file offset and/or IO size is not aligned to Page Boundary (4KB).

“lustre:posix_gds_min_kb” : 0

For a lustre filesystem, if greater than 0, compatibility mode is used for IO sizes between [1 - posix_gds_min_kb] specified in kB.

Note: This option will force posix mode even if allow_compat_mode is set to false.

“weka:rdma_write_support” : false

If this option is false, all writes to WekaFS will use compatibility mode.

Note: If the option is set to false, cuFile library will use the posix path even if the allow_compat_mode option is true or false.

“gpfs:gds_write_support” : false

If this option is false, all writes to IBM Spectrum Scale will use compatibility mode.

Note: If the option is set to false, cuFile library will use the posix path even if the allow_compat_mode option is true or false.

“rdma_dynamic_routing”: false,

“rdma_dynamic_routing_order”: [ “ “SYS_MEM” ]

If rdma_dynamic_routing is set to true and rdma_dynamic_routing_order is set to [SYS_MEM], then all IO for DFS will use compatibility mode.

In addition to the above configuration options, compatibility mode will be used as a fallback option for following use cases.

Use Case

cuFile IO Behavior

No BAR1 memory in GPU.

Use compatibility mode.

For wekaFS or IBM Spectrum Scale mounts: If there are no rdma_dev_addr_list specified, or failure to register MR with ib device.

Use compatibility mode.

Bounce buffers cannot be allocated in GPU memory.

Use compatibility mode.

For WekaFS and IBM Spectrum Scale: If the kernel returns -ENOTSUP for GPUDirect Storage read/write.

Retry the IO operation internally using compatibility mode.

cuFile Stream and cuFile Batch APIs on IBM Spectrum Scale or WekaFS

All Async and batch operations will internally use compatibility mode IO.

The nvidia_fs.ko driver is not loaded.

All IO operations will use compatibility mode.

Limitations

  • Compatible mode does not work in cases where the GPUs have CUDA compute capability less than 6.

  • GDS Compat mode has been tested and works with GDS enabled file systems and environments. It has not been tested to work on all other filesystems.