6.32. 库管理

本节介绍 CUDA 运行时应用程序编程接口的库管理功能。

函数

__host__cudaError_t cudaKernelSetAttributeForDevice ( cudaKernel_t kernel, cudaFuncAttribute attr, int  value, int  device )
设置有关内核的信息。
__host__cudaError_t cudaLibraryEnumerateKernels ( cudaKernel_t* kernels, unsigned int  numKernels, cudaLibrary_t lib )
检索库中的内核句柄。
__host__cudaError_t cudaLibraryGetGlobal ( void** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
返回全局设备指针。
__host__cudaError_t cudaLibraryGetKernel ( cudaKernel_t* pKernel, cudaLibrary_t library, const char* name )
返回内核句柄。
__host__cudaError_t cudaLibraryGetKernelCount ( unsigned int* count, cudaLibrary_t lib )
返回库中内核的数量。
__host__cudaError_t cudaLibraryGetManaged ( void** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
返回指向托管内存的指针。
__host__cudaError_t cudaLibraryGetUnifiedFunction ( void** fptr, cudaLibrary_t library, const char* symbol )
返回指向统一函数的指针。
__host__cudaError_t cudaLibraryLoadData ( cudaLibrary_t* library, const void* code, cudaJitOption ** jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, cudaLibraryOption ** libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载具有指定代码和选项的库。
__host__cudaError_t cudaLibraryLoadFromFile ( cudaLibrary_t* library, const char* fileName, cudaJitOption ** jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, cudaLibraryOption ** libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载具有指定文件和选项的库。
__host__cudaError_t cudaLibraryUnload ( cudaLibrary_t library )
卸载库。

函数

__host__cudaError_t cudaKernelSetAttributeForDevice ( cudaKernel_t kernel, cudaFuncAttribute attr, int  value, int  device )
设置有关内核的信息。
参数
kernel
- 要设置属性的内核
attr
- 请求的属性
value
- 要设置的值
device
- 要设置属性的设备
描述

此调用设置指定属性的值attr在内核上kernel对于请求的设备device为指定的整数值value。 如果成功设置了属性的新值,则此函数返回 cudaSuccess。 如果设置失败,则此调用将返回错误。 并非所有属性都可以设置值。 尝试在只读属性上设置值将导致错误 (cudaErrorInvalidValue)

请注意,使用 cudaFuncSetAttribute() 设置的属性将覆盖由此 API 设置的属性,而不管对 cudaFuncSetAttribute() 的调用是在此 API 调用之前还是之后进行的。 由于这一点以及下面提到的更严格的锁定要求,建议此调用在初始化路径期间使用,而不是在访问的每个线程上kernel例如在内核启动时或关键路径上。

的有效值是attr

注意

与传统的对等 API cudaFuncSetAttribute() 相比,此 API 具有更严格的锁定要求,这是由于设备范围的语义。 如果多个线程尝试同时在同一设备上设置同一属性,则属性设置将取决于 OS 调度程序和内存一致性选择的交错。

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cudaLibraryGetKernel, cudaLaunchKernel, cudaFuncSetAttribute, cuKernelSetAttribute

__host__cudaError_t cudaLibraryEnumerateKernels ( cudaKernel_t* kernels, unsigned int  numKernels, cudaLibrary_t lib )
检索库中的内核句柄。
参数
kernels
- 内核句柄返回到的缓冲区
numKernels
- 可以返回到缓冲区的最大内核句柄数
lib
- 要查询的库
描述

返回最大数量的kernels内核句柄,最多numKernelslib中。 当库卸载时,返回的内核句柄将失效。

另请参阅

cudaLibraryGetKernelCount, cuLibraryEnumerateKernels

__host__cudaError_t cudaLibraryGetGlobal ( void** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
返回全局设备指针。
参数
dptr
- 返回请求库的全局设备指针
bytes
- 返回全局大小(以字节为单位)
library
- 要从中检索全局变量的库
name
- 要检索的全局变量的名称
描述

返回最大数量的*dptr*bytes具有名称的全局变量的基指针和大小name对于请求的库library和当前设备。 如果请求的名称没有全局变量name存在,则调用返回 cudaErrorSymbolNotFound。 参数之一dptrbytes(而不是两者)可以为 NULL,在这种情况下将被忽略。 返回的dptr不能传递给符号 API,例如 cudaMemcpyToSymbol, cudaMemcpyFromSymbol, cudaGetSymbolAddresscudaGetSymbolSize

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cudaLibraryGetManaged, cuLibraryGetGlobal

__host__cudaError_t cudaLibraryGetKernel ( cudaKernel_t* pKernel, cudaLibrary_t library, const char* name )
返回内核句柄。
参数
pKernel
- 返回的内核句柄
library
- 要从中检索内核的库
name
- 要检索的内核的名称
描述

返回最大数量的pKernel具有名称的内核的句柄name位于库library中。 如果未找到内核句柄,则调用返回 cudaErrorSymbolNotFound

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cuLibraryGetKernel

__host__cudaError_t cudaLibraryGetKernelCount ( unsigned int* count, cudaLibrary_t lib )
返回库中内核的数量。
参数
count
- 在库中找到的内核数
lib
- 要查询的库
描述

返回最大数量的count中的内核数lib.

另请参阅

cudaLibraryEnumerateKernels, cudaLibraryLoadFromFile, cudaLibraryLoadData, cuLibraryGetKernelCount

__host__cudaError_t cudaLibraryGetManaged ( void** dptr, size_t* bytes, cudaLibrary_t library, const char* name )
返回指向托管内存的指针。
参数
dptr
- 返回的指向托管内存的指针
bytes
- 返回的内存大小(以字节为单位)
library
- 要从中检索托管内存的库
name
- 要检索的托管内存的名称
描述

返回最大数量的*dptr*bytes具有名称的托管内存的基指针和大小name对于请求的库library。 如果没有具有请求名称的托管内存name存在,则调用返回 cudaErrorSymbolNotFound。 参数之一dptrbytes(而不是两者)可以为 NULL,在这种情况下将被忽略。 请注意,库的托管内存library在设备之间共享,并在加载库时注册。 返回的dptr不能传递给符号 API,例如 cudaMemcpyToSymbol, cudaMemcpyFromSymbol, cudaGetSymbolAddresscudaGetSymbolSize

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cudaLibraryGetGlobal, cuLibraryGetManaged

__host__cudaError_t cudaLibraryGetUnifiedFunction ( void** fptr, cudaLibrary_t library, const char* symbol )
返回指向统一函数的指针。
参数
fptr
- 返回的指向统一函数的指针
library
- 从中检索函数指针内存的库
symbol
- 要检索的函数指针的名称
描述

返回最大数量的*fptr指向由以下项表示的统一函数的函数指针symbol。如果名称为symbol的统一函数不存在,则调用返回 cudaErrorSymbolNotFound。如果系统中不存在具有属性 cudaDeviceProp::unifiedFunctionPointers 的设备,则调用可能返回 cudaErrorSymbolNotFound

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cudaLibraryUnload, cuLibraryGetUnifiedFunction

__host__cudaError_t cudaLibraryLoadData ( cudaLibrary_t* library, const void* code, cudaJitOption ** jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, cudaLibraryOption ** libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载具有指定代码和选项的库。
参数
library
- 返回的库
code
- 要加载的代码
jitOptions
- JIT 的选项
jitOptionsValues
- JIT 的选项值
numJitOptions
- 选项数量
libraryOptions
- 加载的选项
libraryOptionValues
- 加载的选项值
numLibraryOptions
- 加载的选项数量
描述

接受一个指针code并根据应用程序定义的库加载模式加载相应的库library

  • 如果模块加载设置为 EAGER,通过 “模块加载” 中描述的环境变量,library则在调用时和将来上下文创建时,急切地加载到所有上下文中,直到使用 cudaLibraryUnload() 卸载库。

  • 如果环境变量设置为 LAZY,library则不会立即加载到所有现有上下文中,并且仅在上下文需要函数时(例如内核启动)才加载。

这些环境变量在 CUDA 编程指南的 “CUDA 环境变量” 部分中描述。

codecode可以是 nvcc 输出的 cubin 或 fatbin,或者是由 nvcc 输出或手写的 NULL 终止的 PTX。当进行单独编译时,fatbin 也应包含可重定位代码。另请参阅 nvrtc (https://docs.nvda.net.cn/cuda/nvrtc/index.html), nvjitlink (https://docs.nvda.net.cn/cuda/nvjitlink/index.html), 和 nvfatbin (https://docs.nvda.net.cn/cuda/nvfatbin/index.html) 的文档,以获取有关在运行时生成可加载代码的更多信息。

选项通过数组 jitOptions 传递,jitOptions任何相应的参数都在 jitOptionsValues 中传递。jitOptionsValuesJIT 选项的总数通过 numJitOptions 提供。numJitOptions任何输出都将通过 jitOptionsValues 返回。jitOptionsValues.

库加载选项通过数组 libraryOptions 传递。libraryOptions任何相应的参数都在 jitOptionsValues 中传递。libraryOptionValues库加载选项的总数通过 numLibraryOptions 提供。numLibraryOptions.

另请参阅

cudaLibraryLoadFromFile, cudaLibraryUnload, cuLibraryLoadData

__host__cudaError_t cudaLibraryLoadFromFile ( cudaLibrary_t* library, const char* fileName, cudaJitOption ** jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, cudaLibraryOption ** libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载具有指定文件和选项的库。
参数
library
- 返回的库
fileName
- 要从中加载的文件
jitOptions
- JIT 的选项
jitOptionsValues
- JIT 的选项值
numJitOptions
- 选项数量
libraryOptions
- 加载的选项
libraryOptionValues
- 加载的选项值
numLibraryOptions
- 加载的选项数量
描述

接受一个指针code并根据应用程序定义的库加载模式加载相应的库library

  • 如果模块加载设置为 EAGER,通过 “模块加载” 中描述的环境变量,library则在调用时和将来上下文创建时,急切地加载到所有上下文中,直到使用 cudaLibraryUnload() 卸载库。

  • 如果环境变量设置为 LAZY,library则不会立即加载到所有现有上下文中,并且仅在上下文需要函数时(例如内核启动)才加载。

这些环境变量在 CUDA 编程指南的 “CUDA 环境变量” 部分中描述。

该文件应为 nvcc 输出的 cubin 文件、nvcc 输出或手写的 PTX 文件,或 nvcc 输出的 fatbin 文件。当进行单独编译时,fatbin 也应包含可重定位代码。另请参阅 nvrtc (https://docs.nvda.net.cn/cuda/nvrtc/index.html), nvjitlink (https://docs.nvda.net.cn/cuda/nvjitlink/index.html), 和 nvfatbin (https://docs.nvda.net.cn/cuda/nvfatbin/index.html) 的文档,以获取有关在运行时生成可加载代码的更多信息。

选项通过数组 jitOptions 传递,jitOptions任何相应的参数都在 jitOptionsValues 中传递。jitOptionsValues选项总数通过 numJitOptions 提供。numJitOptions任何输出都将通过 jitOptionsValues 返回。jitOptionsValues.

库加载选项通过数组 libraryOptions 传递。libraryOptions任何相应的参数都在 jitOptionsValues 中传递。libraryOptionValues库加载选项的总数通过 numLibraryOptions 提供。numLibraryOptions.

另请参阅

cudaLibraryLoadData, cudaLibraryUnload, cuLibraryLoadFromFile

__host__cudaError_t cudaLibraryUnload ( cudaLibrary_t library )
卸载库。
参数
library
- 要卸载的库
描述

卸载使用以下项指定的库library

另请参阅

cudaLibraryLoadData, cudaLibraryLoadFromFile, cuLibraryUnload