6.12. 库管理

本节介绍底层CUDA驱动程序应用程序编程接口的库管理功能。

Functions

CUresult cuKernelGetAttribute ( int* pi, CUfunction_attribute attrib, CUkernel kernel, CUdevice dev )
Returns information about a kernel.
CUresult cuKernelGetFunction ( CUfunction* pFunc, CUkernel kernel )
Returns a function handle.
CUresult cuKernelGetLibrary ( CUlibrary* pLib, CUkernel kernel )
Returns a library handle.
CUresult cuKernelGetName ( const char** name, CUkernel hfunc )
Returns the function name for a CUkernel handle.
CUresult cuKernelGetParamInfo ( CUkernel kernel, size_t paramIndex, size_t* paramOffset, size_t* paramSize )
Returns the offset and size of a kernel parameter in the device-side parameter layout.
CUresult cuKernelSetAttribute ( CUfunction_attribute attrib, int  val, CUkernel kernel, CUdevice dev )
Sets information about a kernel.
CUresult cuKernelSetCacheConfig ( CUkernel kernel, CUfunc_cache config, CUdevice dev )
Sets the preferred cache configuration for a device kernel.
CUresult cuLibraryEnumerateKernels ( CUkernel* kernels, unsigned int  numKernels, CUlibrary lib )
Retrieve the kernel handles within a library.
CUresult cuLibraryGetGlobal ( CUdeviceptr* dptr, size_t* bytes, CUlibrary library, const char* name )
Returns a global device pointer.
CUresult cuLibraryGetKernel ( CUkernel* pKernel, CUlibrary library, const char* name )
Returns a kernel handle.
CUresult cuLibraryGetKernelCount ( unsigned int* count, CUlibrary lib )
Returns the number of kernels within a library.
CUresult cuLibraryGetManaged ( CUdeviceptr* dptr, size_t* bytes, CUlibrary library, const char* name )
Returns a pointer to managed memory.
CUresult cuLibraryGetModule ( CUmodule* pMod, CUlibrary library )
Returns a module handle.
CUresult cuLibraryGetUnifiedFunction ( void** fptr, CUlibrary library, const char* symbol )
Returns a pointer to a unified function.
CUresult cuLibraryLoadData ( CUlibrary* library, const void* code, CUjit_option* jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, CUlibraryOption* libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
Load a library with specified code and options.
CUresult cuLibraryLoadFromFile ( CUlibrary* library, const char* fileName, CUjit_option* jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, CUlibraryOption* libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
Load a library with specified file and options.
CUresult cuLibraryUnload ( CUlibrary library )
Unloads a library.

Functions

CUresult cuKernelGetAttribute ( int* pi, CUfunction_attribute attrib, CUkernel kernel, CUdevice dev )
返回有关内核的信息。
参数
pi
- Returned attribute value
attrib
- Attribute requested
kernel
- Kernel to query attribute of
dev
- Device to query attribute of
描述

*pi中返回设备dev上内核kernel的属性attrib的整数值。支持的属性包括:

Note:

如果另一个线程同时尝试使用cuKernelSetAttribute()在同一设备上设置相同属性,根据操作系统调度器选择的内存交错访问和内存一致性,属性查询将返回旧值或新值。

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuKernelSetAttribute, cuLibraryGetKernel, cuLaunchKernel, cuKernelGetFunction, cuLibraryGetModule, cuModuleGetFunction, cuFuncGetAttribute

CUresult cuKernelGetFunction ( CUfunction* pFunc, CUkernel kernel )
返回一个函数句柄。
参数
pFunc
- Returned function handle
kernel
- Kernel to retrieve function for the requested context
描述

pFunc中返回当前上下文中请求的内核kernel对应的函数句柄。如果未找到函数句柄,该调用将返回CUDA_ERROR_NOT_FOUND

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuLibraryGetKernel, cuLibraryGetModule, cuModuleGetFunction

CUresult cuKernelGetLibrary ( CUlibrary* pLib, CUkernel kernel )
返回一个库句柄。
参数
pLib
- Returned library handle
kernel
- Kernel to retrieve library handle
描述

pLib中返回请求的内核kernel对应的库句柄

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuLibraryGetKernel

CUresult cuKernelGetName ( const char** name, CUkernel hfunc )
返回CUDA内核句柄对应的函数名称。
参数
name
- The returned name of the function
hfunc
- The function handle to retrieve the name for
描述

返回与内核句柄hfunc关联的函数名称到**name中。函数名称以空终止字符串形式返回。返回的名称仅在内核句柄有效时有效。 如果库被卸载或重新加载,必须再次调用API以获取更新后的名称。如果函数未声明为具有C链接,此API可能会返回一个混淆的名称。如果**namehfunc为NULL,则返回CUDA_ERROR_INVALID_VALUE

Note:

请注意,此函数也可能返回之前异步启动的错误代码。

CUresult cuKernelGetParamInfo ( CUkernel kernel, size_t paramIndex, size_t* paramOffset, size_t* paramSize )
返回设备端参数布局中内核参数的偏移量和大小。
参数
kernel
- The kernel to query
paramIndex
- The parameter index to query
paramOffset
- Returns the offset into the device-side parameter layout at which the parameter resides
paramSize
- Optionally returns the size of the parameter in the device-side parameter layout
描述

查询kernel参数列表中位于paramIndex处的内核参数,并分别在paramOffsetparamSize中返回该参数在设备端参数布局中的偏移量和大小。此信息可用于通过cudaGraphKernelNodeSetParam()cudaGraphKernelNodeUpdatesApply()从设备端更新内核节点参数。paramIndex必须小于kernel所接受的参数数量。如果仅需要参数偏移量,可以将paramSize设置为NULL。

Note:

请注意,此函数也可能返回之前异步启动的错误代码。

另请参阅:

cuFuncGetParamInfo

CUresult cuKernelSetAttribute ( CUfunction_attribute attrib, int  val, CUkernel kernel, CUdevice dev )
设置有关内核的信息。
参数
attrib
- Attribute requested
val
- Value to set
kernel
- Kernel to set attribute of
dev
- Device to set attribute of
描述

此调用将指定设备dev上内核kernel的属性attrib设置为由val指定的整数值。如果成功设置属性新值,该函数将返回CUDA_SUCCESS。若设置失败,此调用将返回错误。并非所有属性都可设置值。尝试为只读属性设置值将导致错误(CUDA_ERROR_INVALID_VALUE)。

请注意,使用cuFuncSetAttribute()设置的属性将覆盖此API设置的属性,无论调用cuFuncSetAttribute()是在此API调用之前还是之后。但是,cuKernelGetAttribute()将始终返回由此API设置的属性值。

支持的属性包括:

Note:

与旧版API cuFuncSetAttribute()相比,该API由于涉及设备级语义,具有更严格的锁定要求。如果多个线程同时尝试在同一设备上设置相同属性,则属性设置结果将取决于操作系统调度器选择的时间交错顺序和内存一致性。

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuKernelGetAttribute, cuLibraryGetKernel, cuLaunchKernel, cuKernelGetFunction, cuLibraryGetModule, cuModuleGetFunction, cuFuncSetAttribute

CUresult cuKernelSetCacheConfig ( CUkernel kernel, CUfunc_cache config, CUdevice dev )
为设备内核设置首选的缓存配置。
参数
kernel
- Kernel to configure cache for
config
- Requested cache configuration
dev
- Device to set attribute of
描述

在使用相同硬件资源的L1缓存和共享内存的设备上,这通过config为请求的设备dev上的设备内核kernel设置首选的缓存配置。这只是一个偏好设置。驱动程序会尽可能使用请求的配置,但必要时可以自由选择不同的配置来执行kernel。通过cuCtxSetCacheConfig()设置的任何上下文范围内的首选项将被此每个内核的设置覆盖。

请注意,使用cuFuncSetCacheConfig()设置的属性将覆盖此API设置的属性,无论调用cuFuncSetCacheConfig()是在此API调用之前还是之后进行的。

在L1缓存和共享内存大小固定的设备上,此设置不起作用。

使用与最近偏好设置不同的偏好启动内核可能会插入一个设备端同步点。

支持的缓存配置包括:

Note:

与旧版API cuFuncSetCacheConfig()相比,该API由于涉及设备级语义,具有更严格的锁定要求。如果多个线程同时尝试在同一设备上设置配置,缓存配置的设置将取决于操作系统调度器选择的交错执行顺序和内存一致性。

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuLibraryGetKernel, cuKernelGetFunction, cuLibraryGetModule, cuModuleGetFunction, cuFuncSetCacheConfig, cuCtxSetCacheConfig, cuLaunchKernel

CUresult cuLibraryEnumerateKernels ( CUkernel* kernels, unsigned int  numKernels, CUlibrary lib )
检索库中的内核句柄。
参数
kernels
- Buffer where the kernel handles are returned to
numKernels
- Maximum number of kernel handles may be returned to the buffer
lib
- Library to query from
描述

kernels中返回库lib内最多numKernels个内核句柄。当库被卸载时,返回的内核句柄将失效。

另请参阅:

cuLibraryGetKernelCount

CUresult cuLibraryGetGlobal ( CUdeviceptr* dptr, size_t* bytes, CUlibrary library, const char* name )
返回一个全局设备指针。
参数
dptr
- Returned global device pointer for the requested context
bytes
- Returned global size in bytes
library
- Library to retrieve global from
name
- Name of global to retrieve
描述

*dptr*bytes中返回指定库library和当前上下文中名为name的全局变量的基指针和大小。如果请求的名称name不存在对应的全局变量,该调用将返回CUDA_ERROR_NOT_FOUND。参数dptrbytes(不能同时为NULL)可以为NULL,此时该参数将被忽略。

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuLibraryGetModule, cuModuleGetGlobal

CUresult cuLibraryGetKernel ( CUkernel* pKernel, CUlibrary library, const char* name )
返回一个内核句柄。
参数
pKernel
- Returned kernel handle
library
- Library to retrieve kernel from
name
- Name of kernel to retrieve
描述

pKernel中返回位于库library中名为name的内核句柄。如果未找到内核句柄,该调用将返回CUDA_ERROR_NOT_FOUND

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuKernelGetFunction, cuLibraryGetModule, cuModuleGetFunction

CUresult cuLibraryGetKernelCount ( unsigned int* count, CUlibrary lib )
返回库中的内核数量。
参数
count
- Number of kernels found within the library
lib
- Library to query
描述

返回lib中内核的数量到count参数。

CUresult cuLibraryGetManaged ( CUdeviceptr* dptr, size_t* bytes, CUlibrary library, const char* name )
返回指向托管内存的指针。
参数
dptr
- Returned pointer to the managed memory
bytes
- Returned memory size in bytes
library
- Library to retrieve managed memory from
name
- Name of managed memory to retrieve
描述

*dptr*bytes中返回请求库library中名为name的托管内存的基指针和大小。如果不存在具有请求名称name的托管内存,则调用返回CUDA_ERROR_NOT_FOUND。参数dptrbytes(不能同时为NULL)可以为NULL,此时该参数将被忽略。请注意,库library的托管内存跨设备共享,并在库加载到至少一个上下文时注册。

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload

CUresult cuLibraryGetModule ( CUmodule* pMod, CUlibrary library )
返回一个模块句柄。
参数
pMod
- Returned module handle
library
- Library to retrieve module from
描述

pMod中返回与当前上下文关联的模块句柄,该模块位于库library中。如果未找到模块句柄,则调用返回CUDA_ERROR_NOT_FOUND

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload, cuModuleGetFunction

CUresult cuLibraryGetUnifiedFunction ( void** fptr, CUlibrary library, const char* symbol )
返回指向统一函数的指针。
参数
fptr
- Returned pointer to a unified function
library
- Library to retrieve function pointer memory from
symbol
- Name of function pointer to retrieve
描述

*fptr中返回指向由symbol表示的统一函数的函数指针。如果不存在名为symbol的统一函数,该调用将返回CUDA_ERROR_NOT_FOUND。如果系统中不存在具有CU_DEVICE_ATTRIBUTE_UNIFIED_FUNCTION_POINTERS属性的设备,该调用可能会返回CUDA_ERROR_NOT_FOUND

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuLibraryUnload

CUresult cuLibraryLoadData ( CUlibrary* library, const void* code, CUjit_option* jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, CUlibraryOption* libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载一个带有指定代码和选项的库。
参数
library
- Returned library
code
- Code to load
jitOptions
- Options for JIT
jitOptionsValues
- Option values for JIT
numJitOptions
- Number of options
libraryOptions
- Options for loading
libraryOptionValues
- Option values for loading
numLibraryOptions
- Number of options for loading
描述

获取一个指针code并根据应用程序定义的库加载模式加载对应的库library

  • 如果通过"模块加载"中描述的环境变量将模块加载设置为EAGER模式,library会在调用时立即加载到所有上下文中,并在创建新上下文时自动加载,直到使用cuLibraryUnload()卸载该库为止。

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

这些环境变量在CUDA编程指南的"CUDA环境变量"章节中有详细说明。

code可能是由nvcc输出的cubin或fatbin文件,也可能是以NULL结尾的PTX代码(可以是nvcc输出或手动编写)。在进行单独编译时,fatbin文件还应包含可重定位代码。

选项通过jitOptions以数组形式传递,任何对应的参数则在jitOptionsValues中传递。总JIT选项数量通过numJitOptions提供。所有输出将通过jitOptionsValues返回。

库加载选项通过libraryOptions以数组形式传递,任何对应的参数则在libraryOptionValues中传递。总库加载选项的数量通过numLibraryOptions提供。

Note:

如果库包含托管变量且系统中没有设备支持托管变量,此调用预计将返回CUDA_ERROR_NOT_SUPPORTED

另请参阅:

cuLibraryLoadFromFile, cuLibraryUnload, cuModuleLoad, cuModuleLoadData, cuModuleLoadDataEx

CUresult cuLibraryLoadFromFile ( CUlibrary* library, const char* fileName, CUjit_option* jitOptions, void** jitOptionsValues, unsigned int  numJitOptions, CUlibraryOption* libraryOptions, void** libraryOptionValues, unsigned int  numLibraryOptions )
加载具有指定文件和选项的库。
参数
library
- Returned library
fileName
- File to load from
jitOptions
- Options for JIT
jitOptionsValues
- Option values for JIT
numJitOptions
- Number of options
libraryOptions
- Options for loading
libraryOptionValues
- Option values for loading
numLibraryOptions
- Number of options for loading
描述

获取一个指针code并根据应用程序定义的库加载模式加载对应的库library

  • 如果通过"模块加载"中描述的环境变量将模块加载设置为EAGER模式,library会在调用时立即加载到所有上下文中,并在后续创建新上下文时自动加载,直到使用cuLibraryUnload()卸载该库为止。

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

这些环境变量在CUDA编程指南的"CUDA环境变量"章节中有详细说明。

该文件应为nvcc输出的cubin文件,或由nvcc生成/手动编写的PTX文件,亦或是nvcc输出的fatbin文件。在进行单独编译时,fatbin文件还应包含可重定位代码。

选项通过jitOptions以数组形式传递,对应的参数通过jitOptionsValues传递。总选项数由numJitOptions提供。所有输出将通过jitOptionsValues返回。

库加载选项通过libraryOptions以数组形式传递,任何对应的参数则在libraryOptionValues中传递。总库加载选项的数量通过numLibraryOptions提供。

Note:

如果库中包含托管变量而系统中没有设备支持托管变量,此调用预期将返回CUDA_ERROR_NOT_SUPPORTED

另请参阅:

cuLibraryLoadData, cuLibraryUnload, cuModuleLoad, cuModuleLoadData, cuModuleLoadDataEx

CUresult cuLibraryUnload ( CUlibrary library )
卸载一个库。
参数
library
- Library to unload
描述

卸载由library指定的库

另请参阅:

cuLibraryLoadData, cuLibraryLoadFromFile, cuModuleUnload