DPC++ Runtime
Runtime libraries for oneAPI DPC++
_pi_context Struct Reference

PI context mapping to a CUDA context object. More...

#include <cuda/pi_cuda.hpp>

Collaboration diagram for _pi_context:

Classes

struct  deleter_data
 

Public Types

enum  kind { kind::primary, kind::user_defined, kind::primary, kind::user_defined }
 
enum  kind { kind::primary, kind::user_defined, kind::primary, kind::user_defined }
 
using native_type = CUcontext
 
using native_type = hipCtx_t
 

Public Member Functions

 _pi_context (kind k, CUcontext ctxt, _pi_device *devId, bool backend_owns=true)
 
 ~_pi_context ()
 
void invoke_extended_deleters ()
 
void set_extended_deleter (pi_context_extended_deleter function, void *user_data)
 
pi_device get_device () const noexcept
 
native_type get () const noexcept
 
bool is_primary () const noexcept
 
pi_uint32 increment_reference_count () noexcept
 
pi_uint32 decrement_reference_count () noexcept
 
pi_uint32 get_reference_count () const noexcept
 
bool backend_has_ownership () const noexcept
 
 _pi_context (pi_device ArgDevice)
 
bool checkSurfaceArgument (pi_mem_flags Flags, void *HostPtr)
 
 _pi_context (kind k, hipCtx_t ctxt, _pi_device *devId)
 
 ~_pi_context ()
 
void invoke_extended_deleters ()
 
void set_extended_deleter (pi_context_extended_deleter function, void *user_data)
 
pi_device get_device () const noexcept
 
native_type get () const noexcept
 
bool is_primary () const noexcept
 
pi_uint32 increment_reference_count () noexcept
 
pi_uint32 decrement_reference_count () noexcept
 
pi_uint32 get_reference_count () const noexcept
 
 _pi_context (ze_context_handle_t ZeContext, pi_uint32 NumDevices, const pi_device *Devs, bool OwnZeContext)
 
pi_result initialize ()
 
pi_result finalize ()
 
pi_platform getPlatform () const
 
bool isValidDevice (pi_device Device) const
 
pi_result getAvailableCommandList (pi_queue Queue, pi_command_list_ptr_t &CommandList, bool UseCopyEngine, bool AllowBatching=false, ze_command_queue_handle_t *ForcedCmdQueue=nullptr)
 
pi_result getFreeSlotInExistingOrNewPool (ze_event_pool_handle_t &, size_t &, bool HostVisible, bool ProfilingEnabled)
 
pi_result decrementUnreleasedEventsInPool (pi_event Event)
 
pi_event getEventFromCache (bool HostVisible, bool WithProfiling)
 
void addEventToCache (pi_event)
 

Public Attributes

enum _pi_context::kind kind_
 
native_type cuContext_
 
_pi_devicedeviceId_
 
std::atomic_uint32_t refCount_
 
pi_device Device
 
std::unordered_map< void *, cm_support::CmBufferSVM * > Addr2CmBufferSVM
 
std::mutex Addr2CmBufferSVMLock
 
native_type hipContext_
 
hipEvent_t evBase_
 
const ze_context_handle_t ZeContext
 
bool OwnZeContext
 
const std::vector< pi_deviceDevices
 
const pi_device SingleRootDevice = nullptr
 
ze_command_list_handle_t ZeCommandListInit
 
pi_mutex ImmediateCommandListMutex
 
pi_mutex ZeCommandListCacheMutex
 
std::unordered_map< ze_device_handle_t, std::list< ze_command_list_handle_t > > ZeComputeCommandListCache
 
std::unordered_map< ze_device_handle_t, std::list< ze_command_list_handle_t > > ZeCopyCommandListCache
 
std::unordered_map< pi_device, USMAllocContextDeviceMemAllocContexts
 
std::unordered_map< pi_device, USMAllocContextSharedMemAllocContexts
 
std::unordered_map< pi_device, USMAllocContextSharedReadOnlyMemAllocContexts
 
std::unordered_set< void * > SharedReadOnlyAllocs
 
std::unique_ptr< USMAllocContextHostMemAllocContext
 
std::unordered_map< void *, MemAllocRecordMemAllocs
 

Detailed Description

PI context mapping to a CUDA context object.

PI context mapping to a HIP context object.

There is no direct mapping between a CUDA context and a PI context, main differences described below:

CUDA context vs PI context

One of the main differences between the PI API and the CUDA driver API is that the second modifies the state of the threads by assigning CUcontext objects to threads. CUcontext objects store data associated with a given device and control access to said device from the user side. PI API context are objects that are passed to functions, and not bound to threads. The _pi_context object doesn't implement this behavior, only holds the CUDA context data. The RAII object ScopedContext implements the active context behavior.

Primary vs User-defined context

CUDA has two different types of context, the Primary context, which is usable by all threads on a given process for a given device, and the aforementioned custom contexts. CUDA documentation, and performance analysis, indicates it is recommended to use Primary context whenever possible. Primary context is used as well by the CUDA Runtime API. For PI applications to interop with CUDA Runtime API, they have to use the primary context - and make that active in the thread. The _pi_context object can be constructed with a kind parameter that allows to construct a Primary or user-defined context, so that the PI object interface is always the same.

Destructor callback

Required to implement CP023, SYCL Extended Context Destruction, the PI Context can store a number of callback functions that will be called upon destruction of the PI Context. See proposal for details.

There is no direct mapping between a HIP context and a PI context, main differences described below:

HIP context vs PI context

One of the main differences between the PI API and the HIP driver API is that the second modifies the state of the threads by assigning hipCtx_t objects to threads. hipCtx_t objects store data associated with a given device and control access to said device from the user side. PI API context are objects that are passed to functions, and not bound to threads. The _pi_context object doesn't implement this behavior, only holds the HIP context data. The RAII object ScopedContext implements the active context behavior.

Primary vs User-defined context

HIP has two different types of context, the Primary context, which is usable by all threads on a given process for a given device, and the aforementioned custom contexts. HIP documentation, and performance analysis, indicates it is recommended to use Primary context whenever possible. Primary context is used as well by the HIP Runtime API. For PI applications to interop with HIP Runtime API, they have to use the primary context - and make that active in the thread. The _pi_context object can be constructed with a kind parameter that allows to construct a Primary or user-defined context, so that the PI object interface is always the same.

Destructor callback

Required to implement CP023, SYCL Extended Context Destruction, the PI Context can store a number of callback functions that will be called upon destruction of the PI Context. See proposal for details.

Definition at line 160 of file pi_cuda.hpp.

Member Typedef Documentation

◆ native_type [1/2]

using _pi_context::native_type = hipCtx_t

Definition at line 142 of file pi_hip.hpp.

◆ native_type [2/2]

Definition at line 169 of file pi_cuda.hpp.

Member Enumeration Documentation

◆ kind [1/2]

enum _pi_context::kind
strong
Enumerator
primary 
user_defined 
primary 
user_defined 

Definition at line 144 of file pi_hip.hpp.

◆ kind [2/2]

enum _pi_context::kind
strong
Enumerator
primary 
user_defined 
primary 
user_defined 

Definition at line 171 of file pi_cuda.hpp.

Constructor & Destructor Documentation

◆ _pi_context() [1/4]

_pi_context::_pi_context ( kind  k,
CUcontext  ctxt,
_pi_device devId,
bool  backend_owns = true 
)
inline

Definition at line 176 of file pi_cuda.hpp.

◆ ~_pi_context() [1/2]

_pi_context::~_pi_context ( )
inline

Definition at line 183 of file pi_cuda.hpp.

References cuda_piDeviceRelease(), and deviceId_.

◆ _pi_context() [2/4]

_pi_context::_pi_context ( pi_device  ArgDevice)
inline

Definition at line 95 of file pi_esimd_emulator.hpp.

◆ _pi_context() [3/4]

_pi_context::_pi_context ( kind  k,
hipCtx_t  ctxt,
_pi_device devId 
)
inline

Definition at line 151 of file pi_hip.hpp.

◆ ~_pi_context() [2/2]

_pi_context::~_pi_context ( )
inline

Definition at line 157 of file pi_hip.hpp.

References deviceId_, and hip_piDeviceRelease().

◆ _pi_context() [4/4]

_pi_context::_pi_context ( ze_context_handle_t  ZeContext,
pi_uint32  NumDevices,
const pi_device Devs,
bool  OwnZeContext 
)
inline

Definition at line 626 of file pi_level_zero.hpp.

Member Function Documentation

◆ addEventToCache()

void _pi_context::addEventToCache ( pi_event  Event)

◆ backend_has_ownership()

bool _pi_context::backend_has_ownership ( ) const
inlinenoexcept

Definition at line 210 of file pi_cuda.hpp.

Referenced by cuda_piContextRelease().

◆ checkSurfaceArgument()

bool _pi_context::checkSurfaceArgument ( pi_mem_flags  Flags,
void *  HostPtr 
)

◆ decrement_reference_count() [1/2]

pi_uint32 _pi_context::decrement_reference_count ( )
inlinenoexcept

Definition at line 180 of file pi_hip.hpp.

References refCount_.

◆ decrement_reference_count() [2/2]

pi_uint32 _pi_context::decrement_reference_count ( )
inlinenoexcept

Definition at line 206 of file pi_cuda.hpp.

References refCount_.

Referenced by cuda_piContextRelease(), and hip_piContextRelease().

◆ decrementUnreleasedEventsInPool()

pi_result _pi_context::decrementUnreleasedEventsInPool ( pi_event  Event)

◆ finalize()

pi_result _pi_context::finalize ( )

◆ get() [1/2]

native_type _pi_context::get ( ) const
inlinenoexcept

Definition at line 174 of file pi_hip.hpp.

References hipContext_.

◆ get() [2/2]

native_type _pi_context::get ( ) const
inlinenoexcept

◆ get_device() [1/2]

pi_device _pi_context::get_device ( ) const
inlinenoexcept

Definition at line 172 of file pi_hip.hpp.

References deviceId_.

◆ get_device() [2/2]

◆ get_reference_count() [1/2]

pi_uint32 _pi_context::get_reference_count ( ) const
inlinenoexcept

Definition at line 182 of file pi_hip.hpp.

References refCount_.

◆ get_reference_count() [2/2]

pi_uint32 _pi_context::get_reference_count ( ) const
inlinenoexcept

◆ getAvailableCommandList()

◆ getEventFromCache()

pi_event _pi_context::getEventFromCache ( bool  HostVisible,
bool  WithProfiling 
)

Definition at line 5568 of file pi_level_zero.cpp.

Referenced by EventCreate().

◆ getFreeSlotInExistingOrNewPool()

pi_result _pi_context::getFreeSlotInExistingOrNewPool ( ze_event_pool_handle_t &  Pool,
size_t &  Index,
bool  HostVisible,
bool  ProfilingEnabled 
)

Definition at line 434 of file pi_level_zero.cpp.

References Devices, sycl::_V1::detail::for_each(), ZE_CALL, and ZeContext.

Referenced by EventCreate().

◆ getPlatform()

◆ increment_reference_count() [1/2]

pi_uint32 _pi_context::increment_reference_count ( )
inlinenoexcept

Definition at line 178 of file pi_hip.hpp.

References refCount_.

◆ increment_reference_count() [2/2]

pi_uint32 _pi_context::increment_reference_count ( )
inlinenoexcept

Definition at line 204 of file pi_cuda.hpp.

References refCount_.

Referenced by cuda_piContextRetain(), and hip_piContextRetain().

◆ initialize()

◆ invoke_extended_deleters() [1/2]

void _pi_context::invoke_extended_deleters ( )
inline

Definition at line 159 of file pi_hip.hpp.

◆ invoke_extended_deleters() [2/2]

void _pi_context::invoke_extended_deleters ( )
inline

Definition at line 185 of file pi_cuda.hpp.

Referenced by cuda_piContextRelease(), and hip_piContextRelease().

◆ is_primary() [1/2]

bool _pi_context::is_primary ( ) const
inlinenoexcept

Definition at line 176 of file pi_hip.hpp.

References kind_, and primary.

◆ is_primary() [2/2]

bool _pi_context::is_primary ( ) const
inlinenoexcept

Definition at line 202 of file pi_cuda.hpp.

References kind_, and primary.

Referenced by cuda_piContextRelease(), and hip_piContextRelease().

◆ isValidDevice()

bool _pi_context::isValidDevice ( pi_device  Device) const
inline

◆ set_extended_deleter() [1/2]

void _pi_context::set_extended_deleter ( pi_context_extended_deleter  function,
void *  user_data 
)
inline

Definition at line 166 of file pi_hip.hpp.

◆ set_extended_deleter() [2/2]

void _pi_context::set_extended_deleter ( pi_context_extended_deleter  function,
void *  user_data 
)
inline

Member Data Documentation

◆ Addr2CmBufferSVM

std::unordered_map<void *, cm_support::CmBufferSVM *> _pi_context::Addr2CmBufferSVM

Definition at line 102 of file pi_esimd_emulator.hpp.

Referenced by piContextRelease(), piextUSMFree(), and piextUSMSharedAlloc().

◆ Addr2CmBufferSVMLock

std::mutex _pi_context::Addr2CmBufferSVMLock

Definition at line 104 of file pi_esimd_emulator.hpp.

Referenced by piContextRelease(), piextUSMFree(), and piextUSMSharedAlloc().

◆ cuContext_

native_type _pi_context::cuContext_

Definition at line 172 of file pi_cuda.hpp.

Referenced by get().

◆ Device

◆ deviceId_

_pi_device * _pi_context::deviceId_

Definition at line 173 of file pi_cuda.hpp.

Referenced by cuda_piProgramGetInfo(), get_device(), hip_piProgramGetInfo(), and ~_pi_context().

◆ DeviceMemAllocContexts

std::unordered_map<pi_device, USMAllocContext> _pi_context::DeviceMemAllocContexts

Definition at line 747 of file pi_level_zero.hpp.

Referenced by initialize(), piextUSMDeviceAlloc(), and USMFreeHelper().

◆ Devices

◆ evBase_

hipEvent_t _pi_context::evBase_

Definition at line 149 of file pi_hip.hpp.

◆ hipContext_

native_type _pi_context::hipContext_

Definition at line 145 of file pi_hip.hpp.

Referenced by get().

◆ HostMemAllocContext

std::unique_ptr<USMAllocContext> _pi_context::HostMemAllocContext

Definition at line 757 of file pi_level_zero.hpp.

Referenced by initialize(), piextUSMHostAlloc(), and USMFreeHelper().

◆ ImmediateCommandListMutex

pi_mutex _pi_context::ImmediateCommandListMutex

◆ kind_

enum _pi_context::kind _pi_context::kind_

Referenced by is_primary().

◆ MemAllocs

◆ OwnZeContext

bool _pi_context::OwnZeContext

Definition at line 652 of file pi_level_zero.hpp.

Referenced by ContextReleaseHelper().

◆ refCount_

std::atomic_uint32_t _pi_context::refCount_

◆ SharedMemAllocContexts

std::unordered_map<pi_device, USMAllocContext> _pi_context::SharedMemAllocContexts

Definition at line 748 of file pi_level_zero.hpp.

Referenced by initialize(), piextUSMSharedAlloc(), and USMFreeHelper().

◆ SharedReadOnlyAllocs

std::unordered_set<void *> _pi_context::SharedReadOnlyAllocs

Definition at line 754 of file pi_level_zero.hpp.

Referenced by piextUSMSharedAlloc(), and USMFreeHelper().

◆ SharedReadOnlyMemAllocContexts

std::unordered_map<pi_device, USMAllocContext> _pi_context::SharedReadOnlyMemAllocContexts

Definition at line 749 of file pi_level_zero.hpp.

Referenced by initialize(), piextUSMSharedAlloc(), and USMFreeHelper().

◆ SingleRootDevice

const pi_device _pi_context::SingleRootDevice = nullptr

Definition at line 674 of file pi_level_zero.hpp.

Referenced by _pi_buffer::getZeHandle(), initialize(), and piMemImageCreate().

◆ ZeCommandListCacheMutex

pi_mutex _pi_context::ZeCommandListCacheMutex

◆ ZeCommandListInit

ze_command_list_handle_t _pi_context::ZeCommandListInit

◆ ZeComputeCommandListCache

std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t> > _pi_context::ZeComputeCommandListCache

◆ ZeContext

◆ ZeCopyCommandListCache

std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t> > _pi_context::ZeCopyCommandListCache

The documentation for this struct was generated from the following files: