DPC++ Runtime
Runtime libraries for oneAPI DPC++
pi_esimd_emulator.hpp
Go to the documentation of this file.
1 //===---------- pi_esimd_emulator.hpp - CM Emulation Plugin ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
14 
15 #pragma once
16 
18 #include <sycl/detail/pi.h>
19 
20 #include <atomic>
21 #include <cassert>
22 #include <mutex>
23 #include <unordered_map>
24 
25 #include <malloc.h>
26 
27 // This version should be incremented for any change made to this file or its
28 // corresponding .cpp file.
29 #define _PI_ESIMD_PLUGIN_VERSION 1
30 
31 #define _PI_ESIMD_PLUGIN_VERSION_STRING \
32  _PI_PLUGIN_VERSION_STRING(_PI_ESIMD_PLUGIN_VERSION)
33 
34 namespace cm_support {
35 #include <cm_rt.h>
36 } // namespace cm_support
37 
38 template <class To, class From> To pi_cast(From Value) {
39  // TODO: see if more sanity checks are possible.
40  assert(sizeof(From) == sizeof(To));
41  return (To)(Value);
42 }
43 
44 template <> uint32_t pi_cast(uint64_t Value) {
45  // Cast value and check that we don't lose any information.
46  uint32_t CastedValue = (uint32_t)(Value);
47  assert((uint64_t)CastedValue == Value);
48  return CastedValue;
49 }
50 
51 // TODO: Currently die is defined in each plugin. Probably some
52 // common header file with utilities should be created.
53 [[noreturn]] void die(const char *Message) {
54  std::cerr << "die: " << Message << std::endl;
55  std::terminate();
56 }
57 
58 // Base class to store common data
59 struct _pi_object {
60  _pi_object() : RefCount{1} {}
61 
62  std::atomic<pi_uint32> RefCount;
63 
64  virtual ~_pi_object() = default;
65 };
66 struct _pi_platform {
67  _pi_platform() = default;
68 
69  // Single-entry Cache pi_devices for reuse
70  std::unique_ptr<_pi_device> PiDeviceCache;
71  std::mutex PiDeviceCacheMutex;
72  bool DeviceCachePopulated = false;
73 
74  // Check the device cache and load it if necessary.
76 
77  // Keep Version information.
78  std::string CmEmuVersion;
79 };
80 
81 struct _pi_device : _pi_object {
82  _pi_device(pi_platform ArgPlt, cm_support::CmDevice *ArgCmDev,
83  std::string ArgVersionStr)
84  : Platform{ArgPlt}, CmDevicePtr{ArgCmDev}, VersionStr{ArgVersionStr} {}
85 
87  // TODO: Check if serialization is required when ESIMD_EMULATOR
88  // plug-in calls CM runtime functions
89  cm_support::CmDevice *CmDevicePtr = nullptr;
90 
91  std::string VersionStr;
92 };
93 
94 struct _pi_context : _pi_object {
95  _pi_context(pi_device ArgDevice) : Device{ArgDevice} {}
96 
97  // One-to-one mapping between Context and Device
99 
100  // Map SVM memory starting address to corresponding
101  // CmBufferSVM object. CmBufferSVM object is needed to release memory.
102  std::unordered_map<void *, cm_support::CmBufferSVM *> Addr2CmBufferSVM;
103  // A lock guarding access to Addr2CmBufferSVM
105 
106  bool checkSurfaceArgument(pi_mem_flags Flags, void *HostPtr);
107 };
108 
109 struct _pi_queue : _pi_object {
110  _pi_queue(pi_context ContextArg, cm_support::CmQueue *CmQueueArg)
111  : Context{ContextArg}, CmQueuePtr{CmQueueArg} {}
112 
113  // Keeps the PI context to which this queue belongs.
114  pi_context Context = nullptr;
115  cm_support::CmQueue *CmQueuePtr = nullptr;
116 };
117 
119  // 'UP' means 'User-Provided' in CM Lib - corresponding to
120  // Buffer/Image created with PI_MEM_FLAGS_HOST_PTR_USE option in
121  // SYCL
122  enum SurfaceType {
128  };
130 
131  union {
132  cm_support::CmBuffer *RegularBufPtr = nullptr;
133  cm_support::CmBufferUP *UPBufPtr;
134  cm_support::CmSurface2D *RegularImgPtr;
135  cm_support::CmSurface2DUP *UPImgPtr;
136  };
137 };
138 
139 struct _pi_mem : _pi_object {
141 
142  // To be used for piEnqueueMemBufferMap
143  char *MapHostPtr = nullptr;
144 
145  std::mutex SurfaceLock;
146 
147  // Surface index
148 
149  unsigned int SurfaceIndex;
150  // Supplementary data to keep track of the mappings of this memory
151  // created with piEnqueueMemBufferMap
152  struct Mapping {
153  // The offset in the buffer giving the start of the mapped region.
154  size_t Offset = 0;
155  // The size of the mapped region.
156  size_t Size = 0;
157  };
158 
159  // The key is the host pointer representing an active mapping.
160  // The value is the information needed to maintain/undo the mapping.
161  // TODO : std::unordered_map is imported from L0.
162  // Use std::stack for strict LIFO behavior checking?
163  std::unordered_map<void *, Mapping> Mappings;
164  // Supporing multi-threaded mapping/unmapping calls
165  std::mutex MappingsMutex;
166 
168 
169  // Destructor for invoking buffer/image destory calls in CM Runtime
170  ~_pi_mem();
171 
172 protected:
173  _pi_mem(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg,
174  unsigned int SurfaceIdxArg)
175  : Context{ctxt}, MapHostPtr{HostPtr}, SurfaceIndex{SurfaceIdxArg},
176  SurfacePtr{SurfacePtrArg} {}
177 };
178 
179 struct _pi_buffer final : _pi_mem {
180  // Buffer/Sub-buffer constructor
181  _pi_buffer(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg,
182  unsigned int SurfaceIdxArg, size_t SizeArg)
183  : _pi_mem(ctxt, HostPtr, SurfacePtrArg, SurfaceIdxArg), Size{SizeArg} {}
184 
185  size_t Size;
186 };
187 
188 struct _pi_image final : _pi_mem {
189  // Image constructor
190  _pi_image(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg,
191  unsigned int SurfaceIdxArg, size_t WidthArg, size_t HeightArg,
192  size_t BPPArg)
193  : _pi_mem(ctxt, HostPtr, SurfacePtrArg, SurfaceIdxArg), Width{WidthArg},
194  Height{HeightArg}, BytesPerPixel{BPPArg} {}
195 
196  size_t Width;
197  size_t Height;
199 };
200 
201 struct _pi_event : _pi_object {
203 
204  cm_support::CmEvent *CmEventPtr = nullptr;
205  cm_support::CmQueue *OwnerQueue = nullptr;
206  pi_context Context = nullptr;
207  bool IsDummyEvent = false;
208 };
209 
210 struct _pi_program : _pi_object {
212 
213  // Keep the context of the program.
215 };
216 
217 struct _pi_kernel : _pi_object {
219 };
220 
_pi_queue::_pi_queue
_pi_queue(pi_context ContextArg, cm_support::CmQueue *CmQueueArg)
Definition: pi_esimd_emulator.hpp:110
_pi_object::RefCount
std::atomic< pi_uint32 > RefCount
Definition: pi_esimd_emulator.hpp:62
cm_surface_ptr_t::TypeUserProvidedImage
@ TypeUserProvidedImage
Definition: pi_esimd_emulator.hpp:127
die
void die(const char *Message)
Definition: pi_esimd_emulator.hpp:53
_pi_mem
PI Mem mapping to CUDA memory allocations, both data and texture/surface.
Definition: pi_cuda.hpp:222
_pi_context::_pi_context
_pi_context(pi_device ArgDevice)
Definition: pi_esimd_emulator.hpp:95
_pi_mem::Mapping
Definition: pi_esimd_emulator.hpp:152
_pi_device::CmDevicePtr
cm_support::CmDevice * CmDevicePtr
Definition: pi_esimd_emulator.hpp:89
esimd_emulator_device_interface.hpp
pi.h
_pi_image::Width
size_t Width
Definition: pi_esimd_emulator.hpp:196
_pi_mem::Mapping::Offset
size_t Offset
Definition: pi_esimd_emulator.hpp:154
_pi_mem::Mappings
std::unordered_map< void *, Mapping > Mappings
Definition: pi_esimd_emulator.hpp:163
_pi_buffer
Definition: pi_esimd_emulator.hpp:179
_pi_platform::PiDeviceCacheMutex
std::mutex PiDeviceCacheMutex
Definition: pi_esimd_emulator.hpp:71
_pi_image
Definition: pi_esimd_emulator.hpp:188
cm_surface_ptr_t::TypeNone
@ TypeNone
Definition: pi_esimd_emulator.hpp:123
cm_surface_ptr_t
Definition: pi_esimd_emulator.hpp:118
_pi_result
_pi_result
Definition: pi.h:110
_pi_event::CmEventPtr
cm_support::CmEvent * CmEventPtr
Definition: pi_esimd_emulator.hpp:204
_pi_object::~_pi_object
virtual ~_pi_object()=default
_pi_device::_pi_device
_pi_device(pi_platform ArgPlt, cm_support::CmDevice *ArgCmDev, std::string ArgVersionStr)
Definition: pi_esimd_emulator.hpp:82
cm_support
Definition: pi_esimd_emulator.hpp:34
_pi_device::VersionStr
std::string VersionStr
Definition: pi_esimd_emulator.hpp:91
_pi_platform
A PI platform stores all known PI devices, in the CUDA plugin this is just a vector of available devi...
Definition: pi_cuda.hpp:73
_pi_object
Definition: pi_esimd_emulator.hpp:59
_pi_kernel
Implementation of a PI Kernel for CUDA.
Definition: pi_cuda.hpp:774
pi_cast
To pi_cast(From Value)
Definition: pi_esimd_emulator.hpp:38
_pi_platform::_pi_platform
_pi_platform()=default
cm_surface_ptr_t::RegularBufPtr
cm_support::CmBuffer * RegularBufPtr
Definition: pi_esimd_emulator.hpp:132
_pi_mem::Context
pi_context Context
Definition: pi_esimd_emulator.hpp:140
_pi_mem::~_pi_mem
~_pi_mem()
Definition: pi_cuda.hpp:363
cm_surface_ptr_t::RegularImgPtr
cm_support::CmSurface2D * RegularImgPtr
Definition: pi_esimd_emulator.hpp:134
_pi_queue
PI queue mapping on to CUstream objects.
Definition: pi_cuda.hpp:393
_pi_context::Addr2CmBufferSVMLock
std::mutex Addr2CmBufferSVMLock
Definition: pi_esimd_emulator.hpp:104
_pi_mem::MapHostPtr
char * MapHostPtr
Definition: pi_esimd_emulator.hpp:143
cm_surface_ptr_t::tag
SurfaceType tag
Definition: pi_esimd_emulator.hpp:129
_pi_mem::SurfacePtr
cm_surface_ptr_t SurfacePtr
Definition: pi_esimd_emulator.hpp:167
_pi_platform::PiDeviceCache
std::unique_ptr< _pi_device > PiDeviceCache
Definition: pi_esimd_emulator.hpp:70
pi_mem_flags
pi_bitfield pi_mem_flags
Definition: pi.h:531
_pi_queue::Context
pi_context Context
Definition: pi_esimd_emulator.hpp:114
_pi_mem::Mapping::Size
size_t Size
Definition: pi_esimd_emulator.hpp:156
_pi_platform::DeviceCachePopulated
bool DeviceCachePopulated
Definition: pi_esimd_emulator.hpp:72
_pi_device::Platform
pi_platform Platform
Definition: pi_esimd_emulator.hpp:86
_pi_program::Context
pi_context Context
Definition: pi_esimd_emulator.hpp:214
_pi_mem::_pi_mem
_pi_mem(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg, unsigned int SurfaceIdxArg)
Definition: pi_esimd_emulator.hpp:173
_pi_platform::CmEmuVersion
std::string CmEmuVersion
Definition: pi_esimd_emulator.hpp:78
std::cerr
__SYCL_EXTERN_STREAM_ATTRS ostream cerr
Linked to standard error (unbuffered)
_pi_image::BytesPerPixel
size_t BytesPerPixel
Definition: pi_esimd_emulator.hpp:198
_pi_program
Implementation of PI Program on CUDA Module object.
Definition: pi_cuda.hpp:719
cm_surface_ptr_t::TypeRegularImage
@ TypeRegularImage
Definition: pi_esimd_emulator.hpp:126
_pi_image::Height
size_t Height
Definition: pi_esimd_emulator.hpp:197
_pi_kernel::_pi_kernel
_pi_kernel()
Definition: pi_esimd_emulator.hpp:218
cm_surface_ptr_t::UPBufPtr
cm_support::CmBufferUP * UPBufPtr
Definition: pi_esimd_emulator.hpp:133
iostream_proxy.hpp
_pi_context::Addr2CmBufferSVM
std::unordered_map< void *, cm_support::CmBufferSVM * > Addr2CmBufferSVM
Definition: pi_esimd_emulator.hpp:102
_pi_event::_pi_event
_pi_event()
Definition: pi_esimd_emulator.hpp:202
_pi_program::_pi_program
_pi_program()
Definition: pi_esimd_emulator.hpp:211
_pi_queue::CmQueuePtr
cm_support::CmQueue * CmQueuePtr
Definition: pi_esimd_emulator.hpp:115
_pi_event
PI Event mapping to CUevent.
Definition: pi_cuda.hpp:591
_pi_mem::MappingsMutex
std::mutex MappingsMutex
Definition: pi_esimd_emulator.hpp:165
cm_surface_ptr_t::TypeRegularBuffer
@ TypeRegularBuffer
Definition: pi_esimd_emulator.hpp:124
cm_surface_ptr_t::UPImgPtr
cm_support::CmSurface2DUP * UPImgPtr
Definition: pi_esimd_emulator.hpp:135
_pi_mem::SurfaceIndex
unsigned int SurfaceIndex
Definition: pi_esimd_emulator.hpp:149
cm_surface_ptr_t::TypeUserProvidedBuffer
@ TypeUserProvidedBuffer
Definition: pi_esimd_emulator.hpp:125
cm_surface_ptr_t::SurfaceType
SurfaceType
Definition: pi_esimd_emulator.hpp:122
_pi_object::_pi_object
_pi_object()
Definition: pi_esimd_emulator.hpp:60
_pi_buffer::_pi_buffer
_pi_buffer(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg, unsigned int SurfaceIdxArg, size_t SizeArg)
Definition: pi_esimd_emulator.hpp:181
_pi_platform::populateDeviceCacheIfNeeded
pi_result populateDeviceCacheIfNeeded()
Definition: pi_esimd_emulator.cpp:525
_pi_event::IsDummyEvent
bool IsDummyEvent
Definition: pi_esimd_emulator.hpp:207
_pi_image::_pi_image
_pi_image(pi_context ctxt, char *HostPtr, cm_surface_ptr_t SurfacePtrArg, unsigned int SurfaceIdxArg, size_t WidthArg, size_t HeightArg, size_t BPPArg)
Definition: pi_esimd_emulator.hpp:190
_pi_mem::SurfaceLock
std::mutex SurfaceLock
Definition: pi_esimd_emulator.hpp:145
_pi_context::Device
pi_device Device
Definition: pi_esimd_emulator.hpp:98
_pi_event::OwnerQueue
cm_support::CmQueue * OwnerQueue
Definition: pi_esimd_emulator.hpp:205
_pi_context
PI context mapping to a CUDA context object.
Definition: pi_cuda.hpp:160
_pi_event::Context
pi_context Context
Definition: pi_esimd_emulator.hpp:206
_pi_device
PI device mapping to a CUdevice.
Definition: pi_cuda.hpp:83
_pi_context::checkSurfaceArgument
bool checkSurfaceArgument(pi_mem_flags Flags, void *HostPtr)
Definition: pi_esimd_emulator.cpp:907
_pi_buffer::Size
size_t Size
Definition: pi_esimd_emulator.hpp:185