6.18. 流管理

本节介绍低级CUDA驱动程序应用程序编程接口的流管理功能。

Functions

CUresult cuStreamAddCallback ( CUstream hStream, CUstreamCallback callback, void* userData, unsigned int  flags )
Add a callback to a compute stream.
CUresult cuStreamAttachMemAsync ( CUstream hStream, CUdeviceptr dptr, size_t length, unsigned int  flags )
Attach memory to a stream asynchronously.
CUresult cuStreamBeginCapture ( CUstream hStream, CUstreamCaptureMode mode )
Begins graph capture on a stream.
CUresult cuStreamBeginCaptureToGraph ( CUstream hStream, CUgraph hGraph, const CUgraphNode* dependencies, const CUgraphEdgeData* dependencyData, size_t numDependencies, CUstreamCaptureMode mode )
Begins graph capture on a stream to an existing graph.
CUresult cuStreamCopyAttributes ( CUstream dst, CUstream src )
Copies attributes from source stream to destination stream.
CUresult cuStreamCreate ( CUstream* phStream, unsigned int  Flags )
Create a stream.
CUresult cuStreamCreateWithPriority ( CUstream* phStream, unsigned int  flags, int  priority )
Create a stream with the given priority.
CUresult cuStreamDestroy ( CUstream hStream )
Destroys a stream.
CUresult cuStreamEndCapture ( CUstream hStream, CUgraph* phGraph )
Ends capture on a stream, returning the captured graph.
CUresult cuStreamGetAttribute ( CUstream hStream, CUstreamAttrID attr, CUstreamAttrValue* value_out )
Queries stream attribute.
CUresult cuStreamGetCaptureInfo ( CUstream hStream, CUstreamCaptureStatus* captureStatus_out, cuuint64_t* id_out, CUgraph* graph_out, const CUgraphNode** dependencies_out, size_t* numDependencies_out )
Query a stream's capture state.
CUresult cuStreamGetCaptureInfo_v3 ( CUstream hStream, CUstreamCaptureStatus* captureStatus_out, cuuint64_t* id_out, CUgraph* graph_out, const CUgraphNode** dependencies_out, const CUgraphEdgeData** edgeData_out, size_t* numDependencies_out )
Query a stream's capture state (12.3+).
CUresult cuStreamGetCtx ( CUstream hStream, CUcontext* pctx )
Query the context associated with a stream.
CUresult cuStreamGetCtx_v2 ( CUstream hStream, CUcontext* pCtx, CUgreenCtx* pGreenCtx )
Query the contexts associated with a stream.
CUresult cuStreamGetDevice ( CUstream hStream, CUdevice* device )
Returns the device handle of the stream.
CUresult cuStreamGetFlags ( CUstream hStream, unsigned int* flags )
Query the flags of a given stream.
CUresult cuStreamGetId ( CUstream hStream, unsigned long long* streamId )
Returns the unique Id associated with the stream handle supplied.
CUresult cuStreamGetPriority ( CUstream hStream, int* priority )
Query the priority of a given stream.
CUresult cuStreamIsCapturing ( CUstream hStream, CUstreamCaptureStatus* captureStatus )
Returns a stream's capture status.
CUresult cuStreamQuery ( CUstream hStream )
Determine status of a compute stream.
CUresult cuStreamSetAttribute ( CUstream hStream, CUstreamAttrID attr, const CUstreamAttrValue* value )
Sets stream attribute.
CUresult cuStreamSynchronize ( CUstream hStream )
Wait until a stream's tasks are completed.
CUresult cuStreamUpdateCaptureDependencies ( CUstream hStream, CUgraphNode* dependencies, size_t numDependencies, unsigned int  flags )
Update the set of dependencies in a capturing stream (11.3+).
CUresult cuStreamUpdateCaptureDependencies_v2 ( CUstream hStream, CUgraphNode* dependencies, const CUgraphEdgeData* dependencyData, size_t numDependencies, unsigned int  flags )
Update the set of dependencies in a capturing stream (12.3+).
CUresult cuStreamWaitEvent ( CUstream hStream, CUevent hEvent, unsigned int  Flags )
Make a compute stream wait on an event.
CUresult cuThreadExchangeStreamCaptureMode ( CUstreamCaptureMode* mode )
Swaps the stream capture interaction mode for a thread.

Functions

CUresult cuStreamAddCallback ( CUstream hStream, CUstreamCallback callback, void* userData, unsigned int  flags )
向计算流添加回调函数。
参数
hStream
- Stream to add callback to
callback
- The function to call once preceding stream operations are complete
userData
- User specified data to be passed to the callback function
flags
- Reserved for future use, must be 0
描述

Note:

此函数计划最终弃用并移除。如果您不需要在设备错误时执行回调,请考虑使用cuLaunchHostFunc。此外,与cuLaunchHostFunc不同,此函数不支持与cuStreamBeginCapturecuStreamEndCapture一起使用。

在所有当前在流中排队的项目完成后,在主控端添加一个回调函数。对于每个cuStreamAddCallback调用,回调函数将精确执行一次。该回调函数会阻塞流中的后续工作,直到它完成。

回调可能会传递CUDA_SUCCESS或错误代码。如果发生设备错误,所有后续执行的回调都将收到相应的CUresult

回调函数不得进行任何CUDA API调用。尝试使用CUDA API将导致CUDA_ERROR_NOT_PERMITTED。回调函数不得执行任何可能依赖于未完成设备工作或其他未被强制要求先运行的回调函数的同步操作。没有强制顺序要求的回调函数(在独立流中)会以未定义的顺序执行,并可能被串行化。

就统一内存管理而言,回调执行提供了以下保证:

  • 回调流在回调期间被视为空闲状态。因此,例如,回调可以始终使用附加到回调流的内存。

  • 回调函数的开始执行效果等同于在回调之前立即同步同一流中记录的事件。因此,它会同步在回调之前已被"连接"的流。

  • 在任何流中添加设备工作不会使该流变为活动状态,直到所有先前的主机函数和流回调执行完毕。因此,例如,即使工作已添加到另一个流中,如果该工作通过事件排序在回调之后,回调仍可能使用全局附加内存。

  • 回调的完成不会使流变为活动状态,除非如上所述。如果没有后续的设备工作,回调流将保持空闲状态,并且在连续的回调之间如果没有设备工作,也将保持空闲。因此,例如,可以通过在流结束时从回调发出信号来完成流同步。

Note:
  • 此函数使用标准的默认流语义。

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

另请参阅:

cuStreamCreate, cuStreamQuery, cuStreamSynchronize, cuStreamWaitEvent, cuStreamDestroy, cuMemAllocManaged, cuStreamAttachMemAsync, cuLaunchHostFunc, cudaStreamAddCallback

CUresult cuStreamAttachMemAsync ( CUstream hStream, CUdeviceptr dptr, size_t length, unsigned int  flags )
异步地将内存附加到流。
参数
hStream
- Stream in which to enqueue the attach operation
dptr
- Pointer to memory (must be a pointer to managed memory or to a valid host-accessible region of system-allocated pageable memory)
length
- Length of memory
flags
- Must be one of CUmemAttach_flags
描述

hStream中排队一个操作,用于指定从dptr开始的length字节内存的流关联。此函数是一个流顺序操作,意味着它依赖于流中先前的工作,并且只有在流中先前的工作完成后才会生效。任何先前的关联都会被自动替换。

dptr 必须指向以下类型的内存之一:

  • 使用__managed__关键字声明的托管内存或通过cuMemAllocManaged分配的内存。

  • 系统分配的页式内存中一个主机可访问的有效区域。只有当与流关联的设备报告设备属性CU_DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS为非零值时,才能指定此类内存。

对于托管分配,length必须为零或整个分配的大小。两者都表示正在更改整个分配的流关联。目前,无法更改托管分配部分区域的流关联。

对于可分页的主机内存分配,length必须为非零值。

流关联通过flags指定,该标志必须是CUmemAttach_flags之一。如果指定了CU_MEM_ATTACH_GLOBAL标志,则任何设备上的任何流都可以访问该内存。如果指定了CU_MEM_ATTACH_HOST标志,程序保证不会从设备属性CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS值为零的设备上的任何流访问该设备上的内存。如果指定了CU_MEM_ATTACH_SINGLE标志且hStream关联的设备其设备属性CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS值为零,程序保证仅通过hStream访问该设备上的内存。单独附加到NULL流是非法的,因为NULL流是虚拟全局流而非特定流。这种情况下将返回错误。

当内存与单个流关联时,只要hStream中的所有操作已完成,统一内存系统将允许CPU访问此内存区域,而不管其他流是否处于活动状态。实际上,这将活动GPU对托管内存区域的独占所有权限制为按流活动,而非整个GPU活动。

从未关联的流访问设备内存将产生未定义的结果。统一内存系统不会执行错误检查,以确保在其他流中启动的内核不会访问此区域。

程序有责任通过事件、同步或其他方式对cuStreamAttachMemAsync的调用进行排序,以确保始终合法访问内存。对于所有跟随流关联更改的内核,数据可见性和一致性将被适当更改。

如果在数据与hStream关联时销毁该流,关联关系将被解除,分配的内存将恢复为cuMemAllocManaged中指定的默认可见性。对于__managed__变量,默认关联始终是CU_MEM_ATTACH_GLOBAL。请注意,销毁流是一个异步操作,因此,在流中的所有工作完成之前,不会发生向默认关联的更改。

Note:
  • 此函数使用标准的默认流语义。

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

另请参阅:

cuStreamCreate, cuStreamQuery, cuStreamSynchronize, cuStreamWaitEvent, cuStreamDestroy, cuMemAllocManaged, cudaStreamAttachMemAsync

CUresult cuStreamBeginCapture ( CUstream hStream, CUstreamCaptureMode mode )
在流上开始图形捕获。
参数
hStream
- Stream in which to initiate capture
mode
- Controls the interaction of this capture sequence with other API calls that are potentially unsafe. For more details see cuThreadExchangeStreamCaptureMode.
描述

hStream上开始图形捕获。当流处于捕获模式时,所有推入该流的操作将不会被执行,而是会被捕获到一个图形中,该图形将通过cuStreamEndCapture返回。如果stream是CU_STREAM_LEGACY,则不能启动捕获。捕获必须在启动它的同一流上结束,并且只有在流尚未处于捕获模式时才能启动。可以通过cuStreamIsCapturing查询捕获模式。可以通过cuStreamGetCaptureInfo查询表示捕获序列的唯一ID。

如果mode不是CU_STREAM_CAPTURE_MODE_RELAXED,则必须从同一线程在此流上调用cuStreamEndCapture

Note:

使用此API捕获的内核不得使用纹理和表面引用。通过任何纹理或表面引用进行读写都是未定义行为。此限制不适用于纹理和表面对象。

Note:

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

另请参阅:

cuStreamCreate, cuStreamIsCapturing, cuStreamEndCapture, cuThreadExchangeStreamCaptureMode

CUresult cuStreamBeginCaptureToGraph ( CUstream hStream, CUgraph hGraph, const CUgraphNode* dependencies, const CUgraphEdgeData* dependencyData, size_t numDependencies, CUstreamCaptureMode mode )
在现有图上开始流式图的捕获。
参数
hStream
- Stream in which to initiate capture.
hGraph
- Graph to capture into.
dependencies
- Dependencies of the first node captured in the stream. Can be NULL if numDependencies is 0.
dependencyData
- Optional array of data associated with each dependency.
numDependencies
- Number of dependencies.
mode
- Controls the interaction of this capture sequence with other API calls that are potentially unsafe. For more details see cuThreadExchangeStreamCaptureMode.
描述

hStream上开始图形捕获,将新节点放入现有图形中。当流处于捕获模式时,所有推入流的操作都不会被执行,而是会被捕获到hGraph中。在用户调用cuStreamEndCapture之前,该图形将无法实例化。

如果stream是CU_STREAM_LEGACY,则可能无法启动捕获。捕获必须在启动它的同一流上结束,并且只有在流尚未处于捕获模式时才能启动。可以通过cuStreamIsCapturing查询捕获模式。可以通过cuStreamGetCaptureInfo查询表示捕获序列的唯一ID。

如果mode不是CU_STREAM_CAPTURE_MODE_RELAXED,则必须从同一线程在此流上调用cuStreamEndCapture

Note:

使用此API捕获的内核不得使用纹理和表面引用。通过任何纹理或表面引用进行读写都是未定义行为。此限制不适用于纹理和表面对象。

Note:

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

另请参阅:

cuStreamBeginCapture, cuStreamCreate, cuStreamIsCapturing, cuStreamEndCapture, cuThreadExchangeStreamCaptureMode, cuGraphAddNode,

CUresult cuStreamCopyAttributes ( CUstream dst, CUstream src )
将属性从源流复制到目标流。
参数
dst
Destination stream
src
Source stream For list of attributes see CUstreamAttrID
描述

将属性从源流src复制到目标流dst。两个流必须具有相同的上下文。

Note:

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

另请参阅:

CUaccessPolicyWindow

CUresult cuStreamCreate ( CUstream* phStream, unsigned int  Flags )
创建一个流。
参数
phStream
- Returned newly created stream
Flags
- Parameters for stream creation
描述

创建一个流并在phStream中返回一个句柄。Flags参数决定了流的行为。

Flags的有效值为:

  • CU_STREAM_DEFAULT: 默认流创建标志。

  • CU_STREAM_NON_BLOCKING: 指定在创建的流中运行的工作可以与流0(NULL流)中的工作并发运行,并且创建的流不应与流0执行任何隐式同步。

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreateWithPriority, cuGreenCtxStreamCreate, cuStreamGetPriority, cuStreamGetFlags, cuStreamGetDevicecuStreamWaitEvent, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cudaStreamCreate, cudaStreamCreateWithFlags

CUresult cuStreamCreateWithPriority ( CUstream* phStream, unsigned int  flags, int  priority )
创建一个具有指定优先级的流。
参数
phStream
- Returned newly created stream
flags
- Flags for stream creation. See cuStreamCreate for a list of valid flags
priority
- Stream priority. Lower numbers represent higher priorities. See cuCtxGetStreamPriorityRange for more information about meaningful stream priorities that can be passed.
描述

创建一个具有指定优先级的流,并在phStream中返回一个句柄。这会影响流中工作的调度优先级。优先级提供了一种提示,以便在可能的情况下优先运行优先级更高的工作,但不会抢占已经在运行的工作,也不会对执行顺序提供任何其他功能保证。

priority遵循数值越小优先级越高的约定。'0'表示默认优先级。可通过cuCtxGetStreamPriorityRange查询有效数值优先级的范围。若指定优先级超出cuCtxGetStreamPriorityRange返回的数值范围,将自动调整为该范围内的最小值或最大值。

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

  • 仅支持计算能力3.5或更高版本的GPU上的流优先级。

  • 在当前实现中,只有优先级流中启动的计算内核会受到流优先级的影响。流优先级对主机到设备和设备到主机的内存操作没有影响。

另请参阅:

cuStreamDestroy, cuStreamCreate, cuGreenCtxStreamCreate, cuStreamGetPriority, cuCtxGetStreamPriorityRange, cuStreamGetFlags, cuStreamGetDevicecuStreamWaitEvent, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cudaStreamCreateWithPriority

CUresult cuStreamDestroy ( CUstream hStream )
销毁一个流。
参数
hStream
- Stream to destroy
描述

销毁由hStream指定的流。

如果在调用cuStreamDestroy()时设备仍在流hStream中执行工作,该函数将立即返回,并且与hStream关联的资源将在设备完成hStream中的所有工作后自动释放。

Note:

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

另请参阅:

cuStreamCreate, cuStreamWaitEvent, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cudaStreamDestroy

CUresult cuStreamEndCapture ( CUstream hStream, CUgraph* phGraph )
结束在流上的捕获,返回捕获的图。
参数
hStream
- Stream to query
phGraph
- The captured graph
描述

hStream上结束捕获,并通过phGraph返回捕获的图。必须已通过调用cuStreamBeginCapturehStream上启动捕获。如果由于违反流捕获规则而导致捕获无效,则将返回NULL图。

如果传递给cuStreamBeginCapturemode参数不是CU_STREAM_CAPTURE_MODE_RELAXED,则此调用必须与cuStreamBeginCapture来自同一线程。

Note:

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

另请参阅:

cuStreamCreate, cuStreamBeginCapture, cuStreamIsCapturing, cuGraphDestroy

CUresult cuStreamGetAttribute ( CUstream hStream, CUstreamAttrID attr, CUstreamAttrValue* value_out )
查询流属性。
参数
hStream
attr
value_out
描述

hStream查询属性attr,并将其存储在value_out对应的成员中。

Note:

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

另请参阅:

CUaccessPolicyWindow

CUresult cuStreamGetCaptureInfo ( CUstream hStream, CUstreamCaptureStatus* captureStatus_out, cuuint64_t* id_out, CUgraph* graph_out, const CUgraphNode** dependencies_out, size_t* numDependencies_out )
查询流的捕获状态。
参数
hStream
- The stream to query
captureStatus_out
- Location to return the capture status of the stream; required
id_out
- Optional location to return an id for the capture sequence, which is unique over the lifetime of the process
graph_out
- Optional location to return the graph being captured into. All operations other than destroy and node removal are permitted on the graph while the capture sequence is in progress. This API does not transfer ownership of the graph, which is transferred or destroyed at cuStreamEndCapture. Note that the graph handle may be invalidated before end of capture for certain errors. Nodes that are or become unreachable from the original stream at cuStreamEndCapture due to direct actions on the graph do not trigger CUDA_ERROR_STREAM_CAPTURE_UNJOINED.
dependencies_out
- Optional location to store a pointer to an array of nodes. The next node to be captured in the stream will depend on this set of nodes, absent operations such as event wait which modify this set. The array pointer is valid until the next API call which operates on the stream or until the capture is terminated. The node handles may be copied out and are valid until they or the graph is destroyed. The driver-owned array may also be passed directly to APIs that operate on the graph (not the stream) without copying.
numDependencies_out
- Optional location to store the size of the array returned in dependencies_out.
描述

查询与流捕获相关的流状态。

如果在非使用CU_STREAM_NON_BLOCKING创建的流正在捕获时调用CU_STREAM_LEGACY("空流"),则返回CUDA_ERROR_STREAM_CAPTURE_IMPLICIT

只有在以下两个条件同时满足时,才会返回有效数据(捕获状态除外):

Note:
  • 图形对象不是线程安全的。更多信息

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

另请参阅:

cuStreamGetCaptureInfo_v3cuStreamBeginCapture, cuStreamIsCapturing, cuStreamUpdateCaptureDependencies

CUresult cuStreamGetCaptureInfo_v3 ( CUstream hStream, CUstreamCaptureStatus* captureStatus_out, cuuint64_t* id_out, CUgraph* graph_out, const CUgraphNode** dependencies_out, const CUgraphEdgeData** edgeData_out, size_t* numDependencies_out )
查询流的捕获状态(12.3+版本)。
参数
hStream
- The stream to query
captureStatus_out
- Location to return the capture status of the stream; required
id_out
- Optional location to return an id for the capture sequence, which is unique over the lifetime of the process
graph_out
- Optional location to return the graph being captured into. All operations other than destroy and node removal are permitted on the graph while the capture sequence is in progress. This API does not transfer ownership of the graph, which is transferred or destroyed at cuStreamEndCapture. Note that the graph handle may be invalidated before end of capture for certain errors. Nodes that are or become unreachable from the original stream at cuStreamEndCapture due to direct actions on the graph do not trigger CUDA_ERROR_STREAM_CAPTURE_UNJOINED.
dependencies_out
- Optional location to store a pointer to an array of nodes. The next node to be captured in the stream will depend on this set of nodes, absent operations such as event wait which modify this set. The array pointer is valid until the next API call which operates on the stream or until the capture is terminated. The node handles may be copied out and are valid until they or the graph is destroyed. The driver-owned array may also be passed directly to APIs that operate on the graph (not the stream) without copying.
edgeData_out
- Optional location to store a pointer to an array of graph edge data. This array parallels dependencies_out; the next node to be added has an edge to dependencies_out[i] with annotation edgeData_out[i] for each i. The array pointer is valid until the next API call which operates on the stream or until the capture is terminated.
numDependencies_out
- Optional location to store the size of the array returned in dependencies_out.
描述

查询与流捕获相关的流状态。

如果在非使用CU_STREAM_NON_BLOCKING创建的流正在捕获时调用CU_STREAM_LEGACY("空流"),则返回CUDA_ERROR_STREAM_CAPTURE_IMPLICIT

只有在以下两个条件同时满足时,才会返回有效数据(捕获状态除外):

如果edgeData_out非空,则dependencies_out也必须非空。如果dependencies_out非空而edgeData_out为空,但当前流依赖关系中存在一个或多个非零边数据时,该调用将返回CUDA_ERROR_LOSSY_QUERY

Note:
  • 图形对象不是线程安全的。更多信息

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

另请参阅:

cuStreamGetCaptureInfocuStreamBeginCapture, cuStreamIsCapturing, cuStreamUpdateCaptureDependencies

CUresult cuStreamGetCtx ( CUstream hStream, CUcontext* pctx )
查询与流关联的上下文。
参数
hStream
- Handle to the stream to be queried
pctx
- Returned context associated with the stream
描述

返回该流所关联的CUDA上下文。

请注意,该API有一个更新版本cuStreamGetCtx_v2。它将在CUDA 13.0中取代当前版本。建议在此之前使用cuStreamGetCtx_v2,因为当前版本对于通过APIcuGreenCtxStreamCreate创建的流将返回CUDA_ERROR_NOT_SUPPORTED错误。

流句柄 hStream 可以指向以下任意一种情况:

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreateWithPriority, cuStreamGetPriority, cuStreamGetFlags, cuStreamGetDevicecuStreamWaitEvent, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cudaStreamCreate, cuStreamGetCtx_v2, cudaStreamCreateWithFlags

CUresult cuStreamGetCtx_v2 ( CUstream hStream, CUcontext* pCtx, CUgreenCtx* pGreenCtx )
查询与流相关联的上下文。
参数
hStream
- Handle to the stream to be queried
pCtx
- Returned regular context associated with the stream
pGreenCtx
- Returned green context if the stream is associated with a green context or NULL if not
描述

返回该流所关联的上下文。

如果流与绿色上下文相关联,API会在pGreenCtx中返回绿色上下文,并在pCtx中返回关联设备的主上下文。

如果流与常规上下文相关联,API将在pCtx中返回常规上下文,在pGreenCtx中返回NULL。

流句柄 hStream 可以指向以下任意一种情况:

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreatecuStreamCreateWithPriority, cuGreenCtxStreamCreate, cuStreamGetPriority, cuStreamGetFlags, cuStreamGetDevicecuStreamWaitEvent, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cudaStreamCreate, cudaStreamCreateWithFlags,

CUresult cuStreamGetDevice ( CUstream hStream, CUdevice* device )
返回流的设备句柄。
参数
hStream
- Handle to the stream to be queried
device
- Returns the device to which a stream belongs
描述

返回*device中流的设备句柄

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreate, cuGreenCtxStreamCreate, cuStreamGetFlags

CUresult cuStreamGetFlags ( CUstream hStream, unsigned int* flags )
查询给定流的标志位。
参数
hStream
- Handle to the stream to be queried
flags
- Pointer to an unsigned integer in which the stream's flags are returned The value returned in flags is a logical 'OR' of all flags that were used while creating this stream. See cuStreamCreate for the list of valid flags
描述

查询使用cuStreamCreatecuStreamCreateWithPrioritycuGreenCtxStreamCreate创建的流标志,并将标志返回到flags中。

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreate, cuGreenCtxStreamCreate, cuStreamGetPriority, cudaStreamGetFlagscuStreamGetDevice

CUresult cuStreamGetId ( CUstream hStream, unsigned long long* streamId )
返回与提供的流句柄相关联的唯一ID。
参数
hStream
- Handle to the stream to be queried
streamId
- Pointer to store the Id of the stream
描述

返回与给定流句柄关联的唯一ID到streamId中。该ID在程序生命周期内是唯一的。

流句柄 hStream 可以指向以下任意一种情况:

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreate, cuStreamGetPriority, cudaStreamGetId

CUresult cuStreamGetPriority ( CUstream hStream, int* priority )
查询给定流的优先级。
参数
hStream
- Handle to the stream to be queried
priority
- Pointer to a signed integer in which the stream's priority is returned
描述

查询使用cuStreamCreatecuStreamCreateWithPrioritycuGreenCtxStreamCreate创建的流优先级,并将优先级返回至priority。请注意,如果流创建时使用的优先级超出了cuCtxGetStreamPriorityRange返回的数值范围,此函数将返回截断后的优先级值。关于优先级截断的详细信息,请参阅cuStreamCreateWithPriority

Note:

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

另请参阅:

cuStreamDestroy, cuStreamCreate, cuStreamCreateWithPriority, cuGreenCtxStreamCreate, cuCtxGetStreamPriorityRange, cuStreamGetFlags, cuStreamGetDevicecudaStreamGetPriority

CUresult cuStreamIsCapturing ( CUstream hStream, CUstreamCaptureStatus* captureStatus )
返回流的捕获状态。
参数
hStream
- Stream to query
captureStatus
- Returns the stream's capture status
描述

通过captureStatus返回hStream的捕获状态。成功调用后,*captureStatus将包含以下值之一:

请注意,如果在同一上下文中有一个阻塞流正在捕获时,对CU_STREAM_LEGACY("空流")调用此函数,它将返回CUDA_ERROR_STREAM_CAPTURE_IMPLICIT,并且调用后*captureStatus的状态未指定。阻塞流的捕获操作不会被置为无效。

当一个阻塞流正在进行捕获时,传统流将处于不可用状态,直到阻塞流捕获终止。 传统流不支持流捕获功能,但尝试使用会产生对捕获流的隐式依赖。

Note:

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

另请参阅:

cuStreamCreate, cuStreamBeginCapture, cuStreamEndCapture

CUresult cuStreamQuery ( CUstream hStream )
确定计算流的状态。
参数
hStream
- Stream to query status of
描述

如果由hStream指定的流中的所有操作已完成,则返回CUDA_SUCCESS;如果未完成,则返回CUDA_ERROR_NOT_READY

就统一内存管理而言,返回值CUDA_SUCCESS等同于调用了cuStreamSynchronize()函数。

Note:
  • 此函数使用标准的默认流语义。

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

另请参阅:

cuStreamCreate, cuStreamWaitEvent, cuStreamDestroy, cuStreamSynchronize, cuStreamAddCallback, cudaStreamQuery

CUresult cuStreamSetAttribute ( CUstream hStream, CUstreamAttrID attr, const CUstreamAttrValue* value )
设置流属性。
参数
hStream
attr
value
描述

value对应的属性设置到hStreamattr属性上。更新后的属性将应用于后续提交到该流的工作,但不会影响之前已提交的工作。

Note:

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

另请参阅:

CUaccessPolicyWindow

CUresult cuStreamSynchronize ( CUstream hStream )
等待直到流中的所有任务完成。
参数
hStream
- Stream to wait for
描述

等待设备完成由hStream指定的流中的所有操作。如果上下文是使用CU_CTX_SCHED_BLOCKING_SYNC标志创建的,CPU线程将阻塞,直到流完成其所有任务。

Note:
  • 此函数使用标准的默认流语义。

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

另请参阅:

cuStreamCreate, cuStreamDestroy, cuStreamWaitEvent, cuStreamQuery, cuStreamAddCallback, cudaStreamSynchronize

CUresult cuStreamUpdateCaptureDependencies ( CUstream hStream, CUgraphNode* dependencies, size_t numDependencies, unsigned int  flags )
更新捕获流中的依赖项集合(11.3+版本)。
参数
hStream
- The stream to update
dependencies
- The set of dependencies to add
numDependencies
- The size of the dependencies array
flags
- See above
描述

修改捕获流的依赖集。依赖集是指流中下一个被捕获节点所依赖的节点集合。

有效标志包括CU_STREAM_ADD_CAPTURE_DEPENDENCIESCU_STREAM_SET_CAPTURE_DEPENDENCIES。这些标志控制传递给API的集合是添加到现有集合还是替换它。标志值为0时默认为CU_STREAM_ADD_CAPTURE_DEPENDENCIES

通过此API从依赖集中移除的节点,如果在cuStreamEndCapture时无法从流中访问,则不会导致CUDA_ERROR_STREAM_CAPTURE_UNJOINED错误。

如果流未处于捕获状态,则返回CUDA_ERROR_ILLEGAL_STATE

该API是CUDA 11.3新增的。需要与CUDA 11.0各小版本保持兼容的开发者不应使用此API或提供回退方案。

另请参阅:

cuStreamBeginCapture, cuStreamGetCaptureInfo,

CUresult cuStreamUpdateCaptureDependencies_v2 ( CUstream hStream, CUgraphNode* dependencies, const CUgraphEdgeData* dependencyData, size_t numDependencies, unsigned int  flags )
更新捕获流中的依赖项集合(12.3+版本)。
参数
hStream
- The stream to update
dependencies
- The set of dependencies to add
dependencyData
- Optional array of data associated with each dependency.
numDependencies
- The size of the dependencies array
flags
- See above
描述

修改捕获流的依赖集。依赖集是指流中下一个捕获节点所依赖的节点集合,以及这些依赖关系的边数据。

有效标志包括CU_STREAM_ADD_CAPTURE_DEPENDENCIESCU_STREAM_SET_CAPTURE_DEPENDENCIES。这些标志控制传递给API的集合是添加到现有集合还是替换它。标志值为0时默认为CU_STREAM_ADD_CAPTURE_DEPENDENCIES

通过此API从依赖集中移除的节点,如果在cuStreamEndCapture时无法从流中访问,则不会导致CUDA_ERROR_STREAM_CAPTURE_UNJOINED错误。

如果流未处于捕获状态,则返回CUDA_ERROR_ILLEGAL_STATE

另请参阅:

cuStreamBeginCapture, cuStreamGetCaptureInfo,

CUresult cuStreamWaitEvent ( CUstream hStream, CUevent hEvent, unsigned int  Flags )
让计算流等待某个事件。
参数
hStream
- Stream to wait
hEvent
- Event to wait on (may not be NULL)
Flags
- See CUevent_capture_flags
描述

使所有后续提交到hStream的工作等待hEvent中捕获的所有工作完成。关于事件捕获内容的详细信息,请参阅cuEventRecord()。在适用情况下,同步操作将在设备上高效执行。 hEvent可以来自与hStream不同的上下文或设备。

标志包括:

Note:
  • 此函数使用标准的默认流语义。

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

另请参阅:

cuStreamCreate, cuEventRecord, cuStreamQuery, cuStreamSynchronize, cuStreamAddCallback, cuStreamDestroy, cudaStreamWaitEvent

CUresult cuThreadExchangeStreamCaptureMode ( CUstreamCaptureMode* mode )
交换线程的流捕获交互模式。
参数
mode
- Pointer to mode value to swap with the current mode
描述

将调用线程的流捕获交互模式设置为*mode中包含的值,并用线程之前的模式覆盖*mode。为了确保跨函数或模块边界的行为确定性,建议调用者以压入-弹出的方式使用此API:

CUstreamCaptureMode mode = desiredMode;
           cuThreadExchangeStreamCaptureMode(&mode);
           ...
           cuThreadExchangeStreamCaptureMode(&mode); // restore previous mode

在流捕获期间(参见cuStreamBeginCapture),某些操作(例如调用cudaMalloc)可能不安全。对于cudaMalloc的情况,该操作不会异步排队到流中,也不会被流捕获观察到。因此,如果通过cuStreamBeginCapture捕获的操作序列依赖于每次启动图时重放分配操作,那么捕获的图将是无效的。

因此,流捕获对在cuStreamBeginCapture-cuStreamEndCapture序列内部或并发执行的API调用施加了限制。这一行为可以通过此API以及传递给cuStreamBeginCapture的标志进行控制。

线程的模式为以下之一:

  • CU_STREAM_CAPTURE_MODE_GLOBAL: 这是默认模式。如果本地线程有一个正在进行的捕获序列,该序列未在cuStreamBeginCapture时以CU_STREAM_CAPTURE_MODE_RELAXED模式启动,或者如果任何其他线程有一个使用CU_STREAM_CAPTURE_MODE_GLOBAL启动的并发捕获序列,则该线程将被禁止进行可能不安全的API调用。

  • CU_STREAM_CAPTURE_MODE_THREAD_LOCAL: 如果当前线程存在一个未以CU_STREAM_CAPTURE_MODE_RELAXED方式启动的捕获序列,则该线程将被禁止调用可能不安全的API接口。其他线程中的并发捕获序列不受影响。

  • CU_STREAM_CAPTURE_MODE_RELAXED: 不禁止本地线程调用可能存在风险的API。但需注意,线程仍被禁止调用必然与流捕获冲突的API,例如尝试对捕获序列内最后记录的事件执行cuEventQuery操作。

Note:

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

另请参阅:

cuStreamBeginCapture