class cv::cuda::Stream

Overview

This class encapsulates a queue of asynchronous calls. Moreā€¦

#include <cuda.hpp>

class Stream
{
public:
    // typedefs

    typedef void (*StreamCallback)(
        int status,
        void *userData
        );

    // construction

    Stream();
    Stream(const Ptr<GpuMat::Allocator>& allocator);

    // methods

    void
    enqueueHostCallback(
        StreamCallback callback,
        void* userData
        );

    operator bool_type() const;

    bool
    queryIfComplete() const;

    void
    waitEvent(const Event& event);

    void
    waitForCompletion();

    static
    Stream&
    Null();
};

Detailed Documentation

This class encapsulates a queue of asynchronous calls.

Currently, you may face problems if an operation is enqueued twice with different data. Some functions use the constant GPU memory, and next call may update the memory before the previous one has been finished. But calling different operations asynchronously is safe because each operation has its own constant buffer. Memory copy/upload/download/set operations to the buffers you hold are also safe.

The Stream class is not thread-safe. Please use different Stream objects for different CPU threads.

void thread1()
{
    cv::cuda::Stream stream1;
    cv::cuda::func1(..., stream1);
}

void thread2()
{
    cv::cuda::Stream stream2;
    cv::cuda::func2(..., stream2);
}

By default all CUDA routines are launched in Stream::Null() object, if the stream is not specified by user. In multi-threading environment the stream objects must be passed explicitly (see previous note).

Construction

Stream()

creates a new asynchronous stream

Stream(const Ptr<GpuMat::Allocator>& allocator)

creates a new asynchronous stream with custom allocator

Methods

void
enqueueHostCallback(
    StreamCallback callback,
    void* userData
    )

Adds a callback to be called on the host after all currently enqueued items in the stream have completed.

Callbacks must not make any CUDA API calls. Callbacks must not perform any synchronization that may depend on outstanding device work or other callbacks that are not mandated to run earlier. Callbacks without a mandated order (in independent streams) execute in undefined order and may be serialized.

operator bool_type() const

returns true if stream object is not default (!= 0)

bool
queryIfComplete() const

Returns true if the current stream queue is finished. Otherwise, it returns false.

void
waitEvent(const Event& event)

Makes a compute stream wait on an event.

void
waitForCompletion()

Blocks the current CPU thread until all operations in the stream are complete.

static
Stream&
Null()

return Stream object for default CUDA stream