What is cuEventRecord guaranteed to do if it gets the default-stream's handle?

95 views Asked by At

Suppose I call cuEventRecord(0, my_event_handle).

cuEventRecord() requires the stream and the event to belong to the same context. Now, one can interpret the 0 as "the default stream in the appropriate context" - the requirements are satisfied and this should work; but one can also interpret it as "the default stream in the current context" - in which case if the current context is not the event's context - this should fail. Or it may all just be undefined/inconsistent behavior.

My question: Is cuEventRecord() guaranteed to prefer one interpretation over the other?

1

There are 1 answers

0
einpoklum On

My personal tinkering suggests that the CUDA driver expects the current context to be the event and the stream's context. Perhaps it even expects that for any stream, not just the default one. Try this program:

#include <cuda/api.hpp>

#include <iostream>
#include <stdexcept>

int main()
{
    cuda::initialize_driver();
    auto pc_0 = cuda::device::primary_context::detail_::obtain_and_increase_refcount(0);
    auto pc_1 = cuda::device::primary_context::detail_::obtain_and_increase_refcount(1);
    cuda::context::current::detail_::push(pc_1);
    CUevent eh = cuda::event::detail_::create_raw_in_current_context(0);
    cuda::context::current::pop();
    // At this point, the context stack is empty
    cuda::context::current::detail_::push(pc_0);
    CUstream default_stream_handle = nullptr;
    cuda::event::detail_::enqueue_in_current_context(default_stream_handle, eh);
}

with this commit of my cuda-api-wrappers library to see for yourself; if you replace pc_0 with pc_1 - it all works.