Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/cpp/cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,17 +854,37 @@ namespace pycuda
};

inline
boost::shared_ptr<context> device::make_context(unsigned int flags)
boost::shared_ptr<context> device::make_context(unsigned int flags)
{
context::prepare_context_switch();

CUcontext ctx;
CUDAPP_CALL_GUARDED_THREADED(cuCtxCreate, (&ctx, flags, m_device));
boost::shared_ptr<context> result(new context(ctx));
context_stack::get().push(result);
return result;
}
#if CUDAPP_CUDA_VERSION >= 13000
// CUDA 13+: avoid cuCtxCreate (now takes CUctxCreateParams*). Use primary context.
unsigned int pc_flags = 0;
int pc_active = 0;

// NOTE: argument order is (dev, &flags, &active)
CUDAPP_CALL_GUARDED(cuDevicePrimaryCtxGetState, (m_device, &pc_flags, &pc_active));

// If the primary context isn’t active yet, set flags if requested
if (!pc_active && flags != 0 && pc_flags != flags)
CUDAPP_CALL_GUARDED(cuDevicePrimaryCtxSetFlags, (m_device, flags));

// Retain + make current
CUDAPP_CALL_GUARDED_THREADED(cuDevicePrimaryCtxRetain, (&ctx, m_device));
CUDAPP_CALL_GUARDED(cuCtxSetCurrent, (ctx));

boost::shared_ptr<context> result(new primary_context(ctx, m_device));
#else
// Older CUDA: original 3-arg cuCtxCreate still works
CUDAPP_CALL_GUARDED_THREADED(cuCtxCreate, (&ctx, flags, m_device));
boost::shared_ptr<context> result(new context(ctx));
#endif

context_stack::get().push(result);
return result;
}

#if CUDAPP_CUDA_VERSION >= 7000
inline boost::shared_ptr<context> device::retain_primary_context()
Expand Down