1 //===--------- device.cpp - Target independent OpenMP target RTL ----------===//
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 //
9 // Functionality for managing devices that are handled by RTL plugins.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "device.h"
14 #include "MemoryManager.h"
15 #include "private.h"
16 #include "rtl.h"
17
18 #include <cassert>
19 #include <climits>
20 #include <cstdio>
21 #include <string>
22
DeviceTy(const DeviceTy & D)23 DeviceTy::DeviceTy(const DeviceTy &D)
24 : DeviceID(D.DeviceID), RTL(D.RTL), RTLDeviceID(D.RTLDeviceID),
25 IsInit(D.IsInit), InitFlag(), HasPendingGlobals(D.HasPendingGlobals),
26 HostDataToTargetMap(D.HostDataToTargetMap),
27 PendingCtorsDtors(D.PendingCtorsDtors), ShadowPtrMap(D.ShadowPtrMap),
28 DataMapMtx(), PendingGlobalsMtx(), ShadowMtx(),
29 LoopTripCnt(D.LoopTripCnt), MemoryManager(nullptr) {}
30
operator =(const DeviceTy & D)31 DeviceTy &DeviceTy::operator=(const DeviceTy &D) {
32 DeviceID = D.DeviceID;
33 RTL = D.RTL;
34 RTLDeviceID = D.RTLDeviceID;
35 IsInit = D.IsInit;
36 HasPendingGlobals = D.HasPendingGlobals;
37 HostDataToTargetMap = D.HostDataToTargetMap;
38 PendingCtorsDtors = D.PendingCtorsDtors;
39 ShadowPtrMap = D.ShadowPtrMap;
40 LoopTripCnt = D.LoopTripCnt;
41
42 return *this;
43 }
44
DeviceTy(RTLInfoTy * RTL)45 DeviceTy::DeviceTy(RTLInfoTy *RTL)
46 : DeviceID(-1), RTL(RTL), RTLDeviceID(-1), IsInit(false), InitFlag(),
47 HasPendingGlobals(false), HostDataToTargetMap(), PendingCtorsDtors(),
48 ShadowPtrMap(), DataMapMtx(), PendingGlobalsMtx(), ShadowMtx(),
49 MemoryManager(nullptr) {}
50
~DeviceTy()51 DeviceTy::~DeviceTy() {
52 if (DeviceID == -1 || getInfoLevel() < 1)
53 return;
54
55 dumpTargetPointerMappings(*this);
56 }
57
associatePtr(void * HstPtrBegin,void * TgtPtrBegin,int64_t Size)58 int DeviceTy::associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size) {
59 DataMapMtx.lock();
60
61 // Check if entry exists
62 auto search = HostDataToTargetMap.find(HstPtrBeginTy{(uintptr_t)HstPtrBegin});
63 if (search != HostDataToTargetMap.end()) {
64 // Mapping already exists
65 bool isValid = search->HstPtrEnd == (uintptr_t)HstPtrBegin + Size &&
66 search->TgtPtrBegin == (uintptr_t)TgtPtrBegin;
67 DataMapMtx.unlock();
68 if (isValid) {
69 DP("Attempt to re-associate the same device ptr+offset with the same "
70 "host ptr, nothing to do\n");
71 return OFFLOAD_SUCCESS;
72 } else {
73 REPORT("Not allowed to re-associate a different device ptr+offset with "
74 "the same host ptr\n");
75 return OFFLOAD_FAIL;
76 }
77 }
78
79 // Mapping does not exist, allocate it with refCount=INF
80 HostDataToTargetTy newEntry((uintptr_t)HstPtrBegin /*HstPtrBase*/,
81 (uintptr_t)HstPtrBegin /*HstPtrBegin*/,
82 (uintptr_t)HstPtrBegin + Size /*HstPtrEnd*/,
83 (uintptr_t)TgtPtrBegin /*TgtPtrBegin*/, nullptr,
84 true /*IsRefCountINF*/);
85
86 DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD ", HstEnd="
87 DPxMOD ", TgtBegin=" DPxMOD "\n", DPxPTR(newEntry.HstPtrBase),
88 DPxPTR(newEntry.HstPtrBegin), DPxPTR(newEntry.HstPtrEnd),
89 DPxPTR(newEntry.TgtPtrBegin));
90 HostDataToTargetMap.insert(newEntry);
91
92 DataMapMtx.unlock();
93
94 return OFFLOAD_SUCCESS;
95 }
96
disassociatePtr(void * HstPtrBegin)97 int DeviceTy::disassociatePtr(void *HstPtrBegin) {
98 DataMapMtx.lock();
99
100 auto search = HostDataToTargetMap.find(HstPtrBeginTy{(uintptr_t)HstPtrBegin});
101 if (search != HostDataToTargetMap.end()) {
102 // Mapping exists
103 if (search->isRefCountInf()) {
104 DP("Association found, removing it\n");
105 HostDataToTargetMap.erase(search);
106 DataMapMtx.unlock();
107 return OFFLOAD_SUCCESS;
108 } else {
109 REPORT("Trying to disassociate a pointer which was not mapped via "
110 "omp_target_associate_ptr\n");
111 }
112 }
113
114 // Mapping not found
115 DataMapMtx.unlock();
116 REPORT("Association not found\n");
117 return OFFLOAD_FAIL;
118 }
119
120 // Get ref count of map entry containing HstPtrBegin
getMapEntryRefCnt(void * HstPtrBegin)121 uint64_t DeviceTy::getMapEntryRefCnt(void *HstPtrBegin) {
122 uintptr_t hp = (uintptr_t)HstPtrBegin;
123 uint64_t RefCnt = 0;
124
125 DataMapMtx.lock();
126 if (!HostDataToTargetMap.empty()) {
127 auto upper = HostDataToTargetMap.upper_bound(hp);
128 if (upper != HostDataToTargetMap.begin()) {
129 upper--;
130 if (hp >= upper->HstPtrBegin && hp < upper->HstPtrEnd) {
131 DP("DeviceTy::getMapEntry: requested entry found\n");
132 RefCnt = upper->getRefCount();
133 }
134 }
135 }
136 DataMapMtx.unlock();
137
138 if (RefCnt == 0) {
139 DP("DeviceTy::getMapEntry: requested entry not found\n");
140 }
141
142 return RefCnt;
143 }
144
lookupMapping(void * HstPtrBegin,int64_t Size)145 LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
146 uintptr_t hp = (uintptr_t)HstPtrBegin;
147 LookupResult lr;
148
149 DP("Looking up mapping(HstPtrBegin=" DPxMOD ", Size=%" PRId64 ")...\n",
150 DPxPTR(hp), Size);
151
152 if (HostDataToTargetMap.empty())
153 return lr;
154
155 auto upper = HostDataToTargetMap.upper_bound(hp);
156 // check the left bin
157 if (upper != HostDataToTargetMap.begin()) {
158 lr.Entry = std::prev(upper);
159 auto &HT = *lr.Entry;
160 // Is it contained?
161 lr.Flags.IsContained = hp >= HT.HstPtrBegin && hp < HT.HstPtrEnd &&
162 (hp+Size) <= HT.HstPtrEnd;
163 // Does it extend beyond the mapped region?
164 lr.Flags.ExtendsAfter = hp < HT.HstPtrEnd && (hp + Size) > HT.HstPtrEnd;
165 }
166
167 // check the right bin
168 if (!(lr.Flags.IsContained || lr.Flags.ExtendsAfter) &&
169 upper != HostDataToTargetMap.end()) {
170 lr.Entry = upper;
171 auto &HT = *lr.Entry;
172 // Does it extend into an already mapped region?
173 lr.Flags.ExtendsBefore = hp < HT.HstPtrBegin && (hp+Size) > HT.HstPtrBegin;
174 // Does it extend beyond the mapped region?
175 lr.Flags.ExtendsAfter = hp < HT.HstPtrEnd && (hp+Size) > HT.HstPtrEnd;
176 }
177
178 if (lr.Flags.ExtendsBefore) {
179 DP("WARNING: Pointer is not mapped but section extends into already "
180 "mapped data\n");
181 }
182 if (lr.Flags.ExtendsAfter) {
183 DP("WARNING: Pointer is already mapped but section extends beyond mapped "
184 "region\n");
185 }
186
187 return lr;
188 }
189
190 // Used by targetDataBegin
191 // Return the target pointer begin (where the data will be moved).
192 // Allocate memory if this is the first occurrence of this mapping.
193 // Increment the reference counter.
194 // If NULL is returned, then either data allocation failed or the user tried
195 // to do an illegal mapping.
getOrAllocTgtPtr(void * HstPtrBegin,void * HstPtrBase,int64_t Size,map_var_info_t HstPtrName,bool & IsNew,bool & IsHostPtr,bool IsImplicit,bool UpdateRefCount,bool HasCloseModifier,bool HasPresentModifier)196 void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
197 int64_t Size, map_var_info_t HstPtrName,
198 bool &IsNew, bool &IsHostPtr, bool IsImplicit,
199 bool UpdateRefCount, bool HasCloseModifier,
200 bool HasPresentModifier) {
201 void *rc = NULL;
202 IsHostPtr = false;
203 IsNew = false;
204 DataMapMtx.lock();
205 LookupResult lr = lookupMapping(HstPtrBegin, Size);
206
207 // Check if the pointer is contained.
208 // If a variable is mapped to the device manually by the user - which would
209 // lead to the IsContained flag to be true - then we must ensure that the
210 // device address is returned even under unified memory conditions.
211 if (lr.Flags.IsContained ||
212 ((lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) && IsImplicit)) {
213 auto &HT = *lr.Entry;
214 IsNew = false;
215
216 if (UpdateRefCount)
217 HT.incRefCount();
218
219 uintptr_t tp = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
220 INFO(DeviceID,
221 "Mapping exists%s with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
222 ", "
223 "Size=%" PRId64 ",%s RefCount=%s, Name=%s\n",
224 (IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), DPxPTR(tp),
225 Size, (UpdateRefCount ? " updated" : ""),
226 HT.isRefCountInf() ? "INF" : std::to_string(HT.getRefCount()).c_str(),
227 (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "(null)");
228 rc = (void *)tp;
229 } else if ((lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) && !IsImplicit) {
230 // Explicit extension of mapped data - not allowed.
231 MESSAGE("explicit extension not allowed: host address specified is " DPxMOD
232 " (%" PRId64 " bytes), but device allocation maps to host at "
233 DPxMOD " (%" PRId64 " bytes)",
234 DPxPTR(HstPtrBegin), Size, DPxPTR(lr.Entry->HstPtrBegin),
235 lr.Entry->HstPtrEnd - lr.Entry->HstPtrBegin);
236 if (HasPresentModifier)
237 MESSAGE("device mapping required by 'present' map type modifier does not "
238 "exist for host address " DPxMOD " (%" PRId64 " bytes)",
239 DPxPTR(HstPtrBegin), Size);
240 } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY &&
241 !HasCloseModifier) {
242 // If unified shared memory is active, implicitly mapped variables that are
243 // not privatized use host address. Any explicitly mapped variables also use
244 // host address where correctness is not impeded. In all other cases maps
245 // are respected.
246 // In addition to the mapping rules above, the close map modifier forces the
247 // mapping of the variable to the device.
248 if (Size) {
249 DP("Return HstPtrBegin " DPxMOD " Size=%" PRId64 " RefCount=%s\n",
250 DPxPTR((uintptr_t)HstPtrBegin), Size,
251 (UpdateRefCount ? " updated" : ""));
252 IsHostPtr = true;
253 rc = HstPtrBegin;
254 }
255 } else if (HasPresentModifier) {
256 DP("Mapping required by 'present' map type modifier does not exist for "
257 "HstPtrBegin=" DPxMOD ", Size=%" PRId64 "\n",
258 DPxPTR(HstPtrBegin), Size);
259 MESSAGE("device mapping required by 'present' map type modifier does not "
260 "exist for host address " DPxMOD " (%" PRId64 " bytes)",
261 DPxPTR(HstPtrBegin), Size);
262 } else if (Size) {
263 // If it is not contained and Size > 0, we should create a new entry for it.
264 IsNew = true;
265 uintptr_t tp = (uintptr_t)allocData(Size, HstPtrBegin);
266 DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD ", "
267 "HstEnd=" DPxMOD ", TgtBegin=" DPxMOD "\n",
268 DPxPTR(HstPtrBase), DPxPTR(HstPtrBegin),
269 DPxPTR((uintptr_t)HstPtrBegin + Size), DPxPTR(tp));
270 HostDataToTargetMap.emplace(
271 HostDataToTargetTy((uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,
272 (uintptr_t)HstPtrBegin + Size, tp, HstPtrName));
273 rc = (void *)tp;
274 }
275
276 DataMapMtx.unlock();
277 return rc;
278 }
279
280 // Used by targetDataBegin, targetDataEnd, targetDataUpdate and target.
281 // Return the target pointer begin (where the data will be moved).
282 // Decrement the reference counter if called from targetDataEnd.
getTgtPtrBegin(void * HstPtrBegin,int64_t Size,bool & IsLast,bool UpdateRefCount,bool & IsHostPtr,bool MustContain)283 void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
284 bool UpdateRefCount, bool &IsHostPtr,
285 bool MustContain) {
286 void *rc = NULL;
287 IsHostPtr = false;
288 IsLast = false;
289 DataMapMtx.lock();
290 LookupResult lr = lookupMapping(HstPtrBegin, Size);
291
292 if (lr.Flags.IsContained ||
293 (!MustContain && (lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter))) {
294 auto &HT = *lr.Entry;
295 IsLast = HT.getRefCount() == 1;
296
297 if (!IsLast && UpdateRefCount)
298 HT.decRefCount();
299
300 uintptr_t tp = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
301 DP("Mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", "
302 "Size=%" PRId64 ",%s RefCount=%s\n", DPxPTR(HstPtrBegin), DPxPTR(tp),
303 Size, (UpdateRefCount ? " updated" : ""),
304 HT.isRefCountInf() ? "INF" : std::to_string(HT.getRefCount()).c_str());
305 rc = (void *)tp;
306 } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) {
307 // If the value isn't found in the mapping and unified shared memory
308 // is on then it means we have stumbled upon a value which we need to
309 // use directly from the host.
310 DP("Get HstPtrBegin " DPxMOD " Size=%" PRId64 " RefCount=%s\n",
311 DPxPTR((uintptr_t)HstPtrBegin), Size, (UpdateRefCount ? " updated" : ""));
312 IsHostPtr = true;
313 rc = HstPtrBegin;
314 }
315
316 DataMapMtx.unlock();
317 return rc;
318 }
319
320 // Return the target pointer begin (where the data will be moved).
321 // Lock-free version called when loading global symbols from the fat binary.
getTgtPtrBegin(void * HstPtrBegin,int64_t Size)322 void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size) {
323 uintptr_t hp = (uintptr_t)HstPtrBegin;
324 LookupResult lr = lookupMapping(HstPtrBegin, Size);
325 if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
326 auto &HT = *lr.Entry;
327 uintptr_t tp = HT.TgtPtrBegin + (hp - HT.HstPtrBegin);
328 return (void *)tp;
329 }
330
331 return NULL;
332 }
333
deallocTgtPtr(void * HstPtrBegin,int64_t Size,bool ForceDelete,bool HasCloseModifier)334 int DeviceTy::deallocTgtPtr(void *HstPtrBegin, int64_t Size, bool ForceDelete,
335 bool HasCloseModifier) {
336 if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY &&
337 !HasCloseModifier)
338 return OFFLOAD_SUCCESS;
339 // Check if the pointer is contained in any sub-nodes.
340 int rc;
341 DataMapMtx.lock();
342 LookupResult lr = lookupMapping(HstPtrBegin, Size);
343 if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
344 auto &HT = *lr.Entry;
345 if (ForceDelete)
346 HT.resetRefCount();
347 if (HT.decRefCount() == 0) {
348 DP("Deleting tgt data " DPxMOD " of size %" PRId64 "\n",
349 DPxPTR(HT.TgtPtrBegin), Size);
350 deleteData((void *)HT.TgtPtrBegin);
351 DP("Removing%s mapping with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
352 ", Size=%" PRId64 "\n", (ForceDelete ? " (forced)" : ""),
353 DPxPTR(HT.HstPtrBegin), DPxPTR(HT.TgtPtrBegin), Size);
354 HostDataToTargetMap.erase(lr.Entry);
355 }
356 rc = OFFLOAD_SUCCESS;
357 } else {
358 REPORT("Section to delete (hst addr " DPxMOD ") does not exist in the"
359 " allocated memory\n",
360 DPxPTR(HstPtrBegin));
361 rc = OFFLOAD_FAIL;
362 }
363
364 DataMapMtx.unlock();
365 return rc;
366 }
367
368 /// Init device, should not be called directly.
init()369 void DeviceTy::init() {
370 // Make call to init_requires if it exists for this plugin.
371 if (RTL->init_requires)
372 RTL->init_requires(PM->RTLs.RequiresFlags);
373 int32_t Ret = RTL->init_device(RTLDeviceID);
374 if (Ret != OFFLOAD_SUCCESS)
375 return;
376
377 // The memory manager will only be disabled when users provide a threshold via
378 // the environment variable \p LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD and set
379 // it to 0.
380 if (const char *Env = std::getenv("LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD")) {
381 size_t Threshold = std::stoul(Env);
382 if (Threshold)
383 MemoryManager = std::make_unique<MemoryManagerTy>(*this, Threshold);
384 } else
385 MemoryManager = std::make_unique<MemoryManagerTy>(*this);
386
387 IsInit = true;
388 }
389
390 /// Thread-safe method to initialize the device only once.
initOnce()391 int32_t DeviceTy::initOnce() {
392 std::call_once(InitFlag, &DeviceTy::init, this);
393
394 // At this point, if IsInit is true, then either this thread or some other
395 // thread in the past successfully initialized the device, so we can return
396 // OFFLOAD_SUCCESS. If this thread executed init() via call_once() and it
397 // failed, return OFFLOAD_FAIL. If call_once did not invoke init(), it means
398 // that some other thread already attempted to execute init() and if IsInit
399 // is still false, return OFFLOAD_FAIL.
400 if (IsInit)
401 return OFFLOAD_SUCCESS;
402 else
403 return OFFLOAD_FAIL;
404 }
405
406 // Load binary to device.
load_binary(void * Img)407 __tgt_target_table *DeviceTy::load_binary(void *Img) {
408 RTL->Mtx.lock();
409 __tgt_target_table *rc = RTL->load_binary(RTLDeviceID, Img);
410 RTL->Mtx.unlock();
411 return rc;
412 }
413
allocData(int64_t Size,void * HstPtr)414 void *DeviceTy::allocData(int64_t Size, void *HstPtr) {
415 // If memory manager is enabled, we will allocate data via memory manager.
416 if (MemoryManager)
417 return MemoryManager->allocate(Size, HstPtr);
418
419 return RTL->data_alloc(RTLDeviceID, Size, HstPtr);
420 }
421
deleteData(void * TgtPtrBegin)422 int32_t DeviceTy::deleteData(void *TgtPtrBegin) {
423 // If memory manager is enabled, we will deallocate data via memory manager.
424 if (MemoryManager)
425 return MemoryManager->free(TgtPtrBegin);
426
427 return RTL->data_delete(RTLDeviceID, TgtPtrBegin);
428 }
429
430 // Submit data to device
submitData(void * TgtPtrBegin,void * HstPtrBegin,int64_t Size,__tgt_async_info * AsyncInfoPtr)431 int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
432 __tgt_async_info *AsyncInfoPtr) {
433 if (!AsyncInfoPtr || !RTL->data_submit_async || !RTL->synchronize)
434 return RTL->data_submit(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size);
435 else
436 return RTL->data_submit_async(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size,
437 AsyncInfoPtr);
438 }
439
440 // Retrieve data from device
retrieveData(void * HstPtrBegin,void * TgtPtrBegin,int64_t Size,__tgt_async_info * AsyncInfoPtr)441 int32_t DeviceTy::retrieveData(void *HstPtrBegin, void *TgtPtrBegin,
442 int64_t Size, __tgt_async_info *AsyncInfoPtr) {
443 if (!AsyncInfoPtr || !RTL->data_retrieve_async || !RTL->synchronize)
444 return RTL->data_retrieve(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size);
445 else
446 return RTL->data_retrieve_async(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size,
447 AsyncInfoPtr);
448 }
449
450 // Copy data from current device to destination device directly
dataExchange(void * SrcPtr,DeviceTy & DstDev,void * DstPtr,int64_t Size,__tgt_async_info * AsyncInfo)451 int32_t DeviceTy::dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,
452 int64_t Size, __tgt_async_info *AsyncInfo) {
453 if (!AsyncInfo || !RTL->data_exchange_async || !RTL->synchronize) {
454 assert(RTL->data_exchange && "RTL->data_exchange is nullptr");
455 return RTL->data_exchange(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID, DstPtr,
456 Size);
457 } else
458 return RTL->data_exchange_async(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID,
459 DstPtr, Size, AsyncInfo);
460 }
461
462 // Run region on device
runRegion(void * TgtEntryPtr,void ** TgtVarsPtr,ptrdiff_t * TgtOffsets,int32_t TgtVarsSize,__tgt_async_info * AsyncInfoPtr)463 int32_t DeviceTy::runRegion(void *TgtEntryPtr, void **TgtVarsPtr,
464 ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
465 __tgt_async_info *AsyncInfoPtr) {
466 if (!AsyncInfoPtr || !RTL->run_region || !RTL->synchronize)
467 return RTL->run_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets,
468 TgtVarsSize);
469 else
470 return RTL->run_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
471 TgtOffsets, TgtVarsSize, AsyncInfoPtr);
472 }
473
474 // Run team region on device.
runTeamRegion(void * TgtEntryPtr,void ** TgtVarsPtr,ptrdiff_t * TgtOffsets,int32_t TgtVarsSize,int32_t NumTeams,int32_t ThreadLimit,uint64_t LoopTripCount,__tgt_async_info * AsyncInfoPtr)475 int32_t DeviceTy::runTeamRegion(void *TgtEntryPtr, void **TgtVarsPtr,
476 ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
477 int32_t NumTeams, int32_t ThreadLimit,
478 uint64_t LoopTripCount,
479 __tgt_async_info *AsyncInfoPtr) {
480 if (!AsyncInfoPtr || !RTL->run_team_region_async || !RTL->synchronize)
481 return RTL->run_team_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
482 TgtOffsets, TgtVarsSize, NumTeams, ThreadLimit,
483 LoopTripCount);
484 else
485 return RTL->run_team_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
486 TgtOffsets, TgtVarsSize, NumTeams,
487 ThreadLimit, LoopTripCount, AsyncInfoPtr);
488 }
489
490 // Whether data can be copied to DstDevice directly
isDataExchangable(const DeviceTy & DstDevice)491 bool DeviceTy::isDataExchangable(const DeviceTy &DstDevice) {
492 if (RTL != DstDevice.RTL || !RTL->is_data_exchangable)
493 return false;
494
495 if (RTL->is_data_exchangable(RTLDeviceID, DstDevice.RTLDeviceID))
496 return (RTL->data_exchange != nullptr) ||
497 (RTL->data_exchange_async != nullptr);
498
499 return false;
500 }
501
synchronize(__tgt_async_info * AsyncInfoPtr)502 int32_t DeviceTy::synchronize(__tgt_async_info *AsyncInfoPtr) {
503 if (RTL->synchronize)
504 return RTL->synchronize(RTLDeviceID, AsyncInfoPtr);
505 return OFFLOAD_SUCCESS;
506 }
507
508 /// Check whether a device has an associated RTL and initialize it if it's not
509 /// already initialized.
device_is_ready(int device_num)510 bool device_is_ready(int device_num) {
511 DP("Checking whether device %d is ready.\n", device_num);
512 // Devices.size() can only change while registering a new
513 // library, so try to acquire the lock of RTLs' mutex.
514 PM->RTLsMtx.lock();
515 size_t DevicesSize = PM->Devices.size();
516 PM->RTLsMtx.unlock();
517 if (DevicesSize <= (size_t)device_num) {
518 DP("Device ID %d does not have a matching RTL\n", device_num);
519 return false;
520 }
521
522 // Get device info
523 DeviceTy &Device = PM->Devices[device_num];
524
525 DP("Is the device %d (local ID %d) initialized? %d\n", device_num,
526 Device.RTLDeviceID, Device.IsInit);
527
528 // Init the device if not done before
529 if (!Device.IsInit && Device.initOnce() != OFFLOAD_SUCCESS) {
530 DP("Failed to init device %d\n", device_num);
531 return false;
532 }
533
534 DP("Device %d is ready to use.\n", device_num);
535
536 return true;
537 }
538