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 "private.h"
15 #include "rtl.h"
16 
17 #include <cassert>
18 #include <climits>
19 #include <cstdio>
20 #include <string>
21 
22 DeviceTy::DeviceTy(RTLInfoTy *RTL)
23     : DeviceID(-1), RTL(RTL), RTLDeviceID(-1), IsInit(false), InitFlag(),
24       HasPendingGlobals(false), HostDataToTargetMap(), PendingCtorsDtors(),
25       ShadowPtrMap(), DataMapMtx(), PendingGlobalsMtx(), ShadowMtx() {}
26 
27 DeviceTy::~DeviceTy() {
28   if (DeviceID == -1 || !(getInfoLevel() & OMP_INFOTYPE_DUMP_TABLE))
29     return;
30 
31   ident_t loc = {0, 0, 0, 0, ";libomptarget;libomptarget;0;0;;"};
32   dumpTargetPointerMappings(&loc, *this);
33 }
34 
35 int DeviceTy::associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size) {
36   DataMapMtx.lock();
37 
38   // Check if entry exists
39   auto search = HostDataToTargetMap.find(HstPtrBeginTy{(uintptr_t)HstPtrBegin});
40   if (search != HostDataToTargetMap.end()) {
41     // Mapping already exists
42     bool isValid = search->HstPtrEnd == (uintptr_t)HstPtrBegin + Size &&
43                    search->TgtPtrBegin == (uintptr_t)TgtPtrBegin;
44     DataMapMtx.unlock();
45     if (isValid) {
46       DP("Attempt to re-associate the same device ptr+offset with the same "
47          "host ptr, nothing to do\n");
48       return OFFLOAD_SUCCESS;
49     } else {
50       REPORT("Not allowed to re-associate a different device ptr+offset with "
51              "the same host ptr\n");
52       return OFFLOAD_FAIL;
53     }
54   }
55 
56   // Mapping does not exist, allocate it with refCount=INF
57   const HostDataToTargetTy &newEntry =
58       *HostDataToTargetMap
59            .emplace(
60                /*HstPtrBase=*/(uintptr_t)HstPtrBegin,
61                /*HstPtrBegin=*/(uintptr_t)HstPtrBegin,
62                /*HstPtrEnd=*/(uintptr_t)HstPtrBegin + Size,
63                /*TgtPtrBegin=*/(uintptr_t)TgtPtrBegin,
64                /*UseHoldRefCount=*/false, /*Name=*/nullptr,
65                /*IsRefCountINF=*/true)
66            .first;
67   DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD
68      ", HstEnd=" DPxMOD ", TgtBegin=" DPxMOD ", DynRefCount=%s, "
69      "HoldRefCount=%s\n",
70      DPxPTR(newEntry.HstPtrBase), DPxPTR(newEntry.HstPtrBegin),
71      DPxPTR(newEntry.HstPtrEnd), DPxPTR(newEntry.TgtPtrBegin),
72      newEntry.dynRefCountToStr().c_str(), newEntry.holdRefCountToStr().c_str());
73   (void)newEntry;
74 
75   DataMapMtx.unlock();
76 
77   return OFFLOAD_SUCCESS;
78 }
79 
80 int DeviceTy::disassociatePtr(void *HstPtrBegin) {
81   DataMapMtx.lock();
82 
83   auto search = HostDataToTargetMap.find(HstPtrBeginTy{(uintptr_t)HstPtrBegin});
84   if (search != HostDataToTargetMap.end()) {
85     // Mapping exists
86     if (search->getHoldRefCount()) {
87       // This is based on OpenACC 3.1, sec 3.2.33 "acc_unmap_data", L3656-3657:
88       // "It is an error to call acc_unmap_data if the structured reference
89       // count for the pointer is not zero."
90       REPORT("Trying to disassociate a pointer with a non-zero hold reference "
91              "count\n");
92     } else if (search->isDynRefCountInf()) {
93       DP("Association found, removing it\n");
94       HostDataToTargetMap.erase(search);
95       DataMapMtx.unlock();
96       return OFFLOAD_SUCCESS;
97     } else {
98       REPORT("Trying to disassociate a pointer which was not mapped via "
99              "omp_target_associate_ptr\n");
100     }
101   } else {
102     REPORT("Association not found\n");
103   }
104 
105   // Mapping not found
106   DataMapMtx.unlock();
107   return OFFLOAD_FAIL;
108 }
109 
110 LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
111   uintptr_t hp = (uintptr_t)HstPtrBegin;
112   LookupResult lr;
113 
114   DP("Looking up mapping(HstPtrBegin=" DPxMOD ", Size=%" PRId64 ")...\n",
115      DPxPTR(hp), Size);
116 
117   if (HostDataToTargetMap.empty())
118     return lr;
119 
120   auto upper = HostDataToTargetMap.upper_bound(hp);
121   // check the left bin
122   if (upper != HostDataToTargetMap.begin()) {
123     lr.Entry = std::prev(upper);
124     auto &HT = *lr.Entry;
125     // Is it contained?
126     lr.Flags.IsContained = hp >= HT.HstPtrBegin && hp < HT.HstPtrEnd &&
127                            (hp + Size) <= HT.HstPtrEnd;
128     // Does it extend beyond the mapped region?
129     lr.Flags.ExtendsAfter = hp < HT.HstPtrEnd && (hp + Size) > HT.HstPtrEnd;
130   }
131 
132   // check the right bin
133   if (!(lr.Flags.IsContained || lr.Flags.ExtendsAfter) &&
134       upper != HostDataToTargetMap.end()) {
135     lr.Entry = upper;
136     auto &HT = *lr.Entry;
137     // Does it extend into an already mapped region?
138     lr.Flags.ExtendsBefore =
139         hp < HT.HstPtrBegin && (hp + Size) > HT.HstPtrBegin;
140     // Does it extend beyond the mapped region?
141     lr.Flags.ExtendsAfter = hp < HT.HstPtrEnd && (hp + Size) > HT.HstPtrEnd;
142   }
143 
144   if (lr.Flags.ExtendsBefore) {
145     DP("WARNING: Pointer is not mapped but section extends into already "
146        "mapped data\n");
147   }
148   if (lr.Flags.ExtendsAfter) {
149     DP("WARNING: Pointer is already mapped but section extends beyond mapped "
150        "region\n");
151   }
152 
153   return lr;
154 }
155 
156 TargetPointerResultTy
157 DeviceTy::getTargetPointer(void *HstPtrBegin, void *HstPtrBase, int64_t Size,
158                            map_var_info_t HstPtrName, bool HasFlagTo,
159                            bool HasFlagAlways, bool IsImplicit,
160                            bool UpdateRefCount, bool HasCloseModifier,
161                            bool HasPresentModifier, bool HasHoldModifier,
162                            AsyncInfoTy &AsyncInfo) {
163   void *TargetPointer = nullptr;
164   bool IsHostPtr = false;
165   bool IsNew = false;
166 
167   DataMapMtx.lock();
168 
169   LookupResult LR = lookupMapping(HstPtrBegin, Size);
170   auto Entry = LR.Entry;
171 
172   // Check if the pointer is contained.
173   // If a variable is mapped to the device manually by the user - which would
174   // lead to the IsContained flag to be true - then we must ensure that the
175   // device address is returned even under unified memory conditions.
176   if (LR.Flags.IsContained ||
177       ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && IsImplicit)) {
178     auto &HT = *LR.Entry;
179     const char *RefCountAction;
180     assert(HT.getTotalRefCount() > 0 && "expected existing RefCount > 0");
181     if (UpdateRefCount) {
182       // After this, RefCount > 1.
183       HT.incRefCount(HasHoldModifier);
184       RefCountAction = " (incremented)";
185     } else {
186       // It might have been allocated with the parent, but it's still new.
187       IsNew = HT.getTotalRefCount() == 1;
188       RefCountAction = " (update suppressed)";
189     }
190     const char *DynRefCountAction = HasHoldModifier ? "" : RefCountAction;
191     const char *HoldRefCountAction = HasHoldModifier ? RefCountAction : "";
192     uintptr_t Ptr = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
193     INFO(OMP_INFOTYPE_MAPPING_EXISTS, DeviceID,
194          "Mapping exists%s with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
195          ", Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s, Name=%s\n",
196          (IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), DPxPTR(Ptr),
197          Size, HT.dynRefCountToStr().c_str(), DynRefCountAction,
198          HT.holdRefCountToStr().c_str(), HoldRefCountAction,
199          (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");
200     TargetPointer = (void *)Ptr;
201   } else if ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && !IsImplicit) {
202     // Explicit extension of mapped data - not allowed.
203     MESSAGE("explicit extension not allowed: host address specified is " DPxMOD
204             " (%" PRId64
205             " bytes), but device allocation maps to host at " DPxMOD
206             " (%" PRId64 " bytes)",
207             DPxPTR(HstPtrBegin), Size, DPxPTR(Entry->HstPtrBegin),
208             Entry->HstPtrEnd - Entry->HstPtrBegin);
209     if (HasPresentModifier)
210       MESSAGE("device mapping required by 'present' map type modifier does not "
211               "exist for host address " DPxMOD " (%" PRId64 " bytes)",
212               DPxPTR(HstPtrBegin), Size);
213   } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY &&
214              !HasCloseModifier) {
215     // If unified shared memory is active, implicitly mapped variables that are
216     // not privatized use host address. Any explicitly mapped variables also use
217     // host address where correctness is not impeded. In all other cases maps
218     // are respected.
219     // In addition to the mapping rules above, the close map modifier forces the
220     // mapping of the variable to the device.
221     if (Size) {
222       DP("Return HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "
223          "memory\n",
224          DPxPTR((uintptr_t)HstPtrBegin), Size);
225       IsHostPtr = true;
226       TargetPointer = HstPtrBegin;
227     }
228   } else if (HasPresentModifier) {
229     DP("Mapping required by 'present' map type modifier does not exist for "
230        "HstPtrBegin=" DPxMOD ", Size=%" PRId64 "\n",
231        DPxPTR(HstPtrBegin), Size);
232     MESSAGE("device mapping required by 'present' map type modifier does not "
233             "exist for host address " DPxMOD " (%" PRId64 " bytes)",
234             DPxPTR(HstPtrBegin), Size);
235   } else if (Size) {
236     // If it is not contained and Size > 0, we should create a new entry for it.
237     IsNew = true;
238     uintptr_t Ptr = (uintptr_t)allocData(Size, HstPtrBegin);
239     Entry = HostDataToTargetMap
240                 .emplace((uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,
241                          (uintptr_t)HstPtrBegin + Size, Ptr, HasHoldModifier,
242                          HstPtrName)
243                 .first;
244     INFO(OMP_INFOTYPE_MAPPING_CHANGED, DeviceID,
245          "Creating new map entry with "
246          "HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", Size=%ld, "
247          "DynRefCount=%s, HoldRefCount=%s, Name=%s\n",
248          DPxPTR(HstPtrBegin), DPxPTR(Ptr), Size,
249          Entry->dynRefCountToStr().c_str(), Entry->holdRefCountToStr().c_str(),
250          (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");
251     TargetPointer = (void *)Ptr;
252   }
253 
254   // If the target pointer is valid, and we need to transfer data, issue the
255   // data transfer.
256   if (TargetPointer && !IsHostPtr && HasFlagTo && (IsNew || HasFlagAlways)) {
257     // Lock the entry before releasing the mapping table lock such that another
258     // thread that could issue data movement will get the right result.
259     Entry->lock();
260     // Release the mapping table lock right after the entry is locked.
261     DataMapMtx.unlock();
262 
263     DP("Moving %" PRId64 " bytes (hst:" DPxMOD ") -> (tgt:" DPxMOD ")\n", Size,
264        DPxPTR(HstPtrBegin), DPxPTR(TargetPointer));
265 
266     int Ret = submitData(TargetPointer, HstPtrBegin, Size, AsyncInfo);
267 
268     // Unlock the entry immediately after the data movement is issued.
269     Entry->unlock();
270 
271     if (Ret != OFFLOAD_SUCCESS) {
272       REPORT("Copying data to device failed.\n");
273       // We will also return nullptr if the data movement fails because that
274       // pointer points to a corrupted memory region so it doesn't make any
275       // sense to continue to use it.
276       TargetPointer = nullptr;
277     }
278   } else {
279     // Release the mapping table lock directly.
280     DataMapMtx.unlock();
281   }
282 
283   return {{IsNew, IsHostPtr}, Entry, TargetPointer};
284 }
285 
286 // Used by targetDataBegin, targetDataEnd, targetDataUpdate and target.
287 // Return the target pointer begin (where the data will be moved).
288 // Decrement the reference counter if called from targetDataEnd.
289 void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
290                                bool UpdateRefCount, bool UseHoldRefCount,
291                                bool &IsHostPtr, bool MustContain,
292                                bool ForceDelete) {
293   void *rc = NULL;
294   IsHostPtr = false;
295   IsLast = false;
296   DataMapMtx.lock();
297   LookupResult lr = lookupMapping(HstPtrBegin, Size);
298 
299   if (lr.Flags.IsContained ||
300       (!MustContain && (lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter))) {
301     auto &HT = *lr.Entry;
302     // We do not zero the total reference count here.  deallocTgtPtr does that
303     // atomically with removing the mapping.  Otherwise, before this thread
304     // removed the mapping in deallocTgtPtr, another thread could retrieve the
305     // mapping, increment and decrement back to zero, and then both threads
306     // would try to remove the mapping, resulting in a double free.
307     IsLast = HT.decShouldRemove(UseHoldRefCount, ForceDelete);
308     const char *RefCountAction;
309     if (!UpdateRefCount) {
310       RefCountAction = " (update suppressed)";
311     } else if (ForceDelete) {
312       HT.resetRefCount(UseHoldRefCount);
313       assert(IsLast == HT.decShouldRemove(UseHoldRefCount) &&
314              "expected correct IsLast prediction for reset");
315       if (IsLast)
316         RefCountAction = " (reset, deferred final decrement)";
317       else {
318         HT.decRefCount(UseHoldRefCount);
319         RefCountAction = " (reset)";
320       }
321     } else if (IsLast) {
322       RefCountAction = " (deferred final decrement)";
323     } else {
324       HT.decRefCount(UseHoldRefCount);
325       RefCountAction = " (decremented)";
326     }
327     const char *DynRefCountAction = UseHoldRefCount ? "" : RefCountAction;
328     const char *HoldRefCountAction = UseHoldRefCount ? RefCountAction : "";
329     uintptr_t tp = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
330     INFO(OMP_INFOTYPE_MAPPING_EXISTS, DeviceID,
331          "Mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", "
332          "Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s\n",
333          DPxPTR(HstPtrBegin), DPxPTR(tp), Size, HT.dynRefCountToStr().c_str(),
334          DynRefCountAction, HT.holdRefCountToStr().c_str(), HoldRefCountAction);
335     rc = (void *)tp;
336   } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) {
337     // If the value isn't found in the mapping and unified shared memory
338     // is on then it means we have stumbled upon a value which we need to
339     // use directly from the host.
340     DP("Get HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "
341        "memory\n",
342        DPxPTR((uintptr_t)HstPtrBegin), Size);
343     IsHostPtr = true;
344     rc = HstPtrBegin;
345   }
346 
347   DataMapMtx.unlock();
348   return rc;
349 }
350 
351 // Return the target pointer begin (where the data will be moved).
352 // Lock-free version called when loading global symbols from the fat binary.
353 void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size) {
354   uintptr_t hp = (uintptr_t)HstPtrBegin;
355   LookupResult lr = lookupMapping(HstPtrBegin, Size);
356   if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
357     auto &HT = *lr.Entry;
358     uintptr_t tp = HT.TgtPtrBegin + (hp - HT.HstPtrBegin);
359     return (void *)tp;
360   }
361 
362   return NULL;
363 }
364 
365 int DeviceTy::deallocTgtPtr(void *HstPtrBegin, int64_t Size,
366                             bool HasHoldModifier) {
367   // Check if the pointer is contained in any sub-nodes.
368   int rc;
369   DataMapMtx.lock();
370   LookupResult lr = lookupMapping(HstPtrBegin, Size);
371   if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
372     auto &HT = *lr.Entry;
373     if (HT.decRefCount(HasHoldModifier) == 0) {
374       DP("Deleting tgt data " DPxMOD " of size %" PRId64 "\n",
375          DPxPTR(HT.TgtPtrBegin), Size);
376       deleteData((void *)HT.TgtPtrBegin);
377       INFO(OMP_INFOTYPE_MAPPING_CHANGED, DeviceID,
378            "Removing map entry with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
379            ", Size=%" PRId64 ", Name=%s\n",
380            DPxPTR(HT.HstPtrBegin), DPxPTR(HT.TgtPtrBegin), Size,
381            (HT.HstPtrName) ? getNameFromMapping(HT.HstPtrName).c_str()
382                            : "unknown");
383       HostDataToTargetMap.erase(lr.Entry);
384     }
385     rc = OFFLOAD_SUCCESS;
386   } else {
387     REPORT("Section to delete (hst addr " DPxMOD ") does not exist in the"
388            " allocated memory\n",
389            DPxPTR(HstPtrBegin));
390     rc = OFFLOAD_FAIL;
391   }
392 
393   DataMapMtx.unlock();
394   return rc;
395 }
396 
397 /// Init device, should not be called directly.
398 void DeviceTy::init() {
399   // Make call to init_requires if it exists for this plugin.
400   if (RTL->init_requires)
401     RTL->init_requires(PM->RTLs.RequiresFlags);
402   int32_t Ret = RTL->init_device(RTLDeviceID);
403   if (Ret != OFFLOAD_SUCCESS)
404     return;
405 
406   IsInit = true;
407 }
408 
409 /// Thread-safe method to initialize the device only once.
410 int32_t DeviceTy::initOnce() {
411   std::call_once(InitFlag, &DeviceTy::init, this);
412 
413   // At this point, if IsInit is true, then either this thread or some other
414   // thread in the past successfully initialized the device, so we can return
415   // OFFLOAD_SUCCESS. If this thread executed init() via call_once() and it
416   // failed, return OFFLOAD_FAIL. If call_once did not invoke init(), it means
417   // that some other thread already attempted to execute init() and if IsInit
418   // is still false, return OFFLOAD_FAIL.
419   if (IsInit)
420     return OFFLOAD_SUCCESS;
421   else
422     return OFFLOAD_FAIL;
423 }
424 
425 // Load binary to device.
426 __tgt_target_table *DeviceTy::load_binary(void *Img) {
427   RTL->Mtx.lock();
428   __tgt_target_table *rc = RTL->load_binary(RTLDeviceID, Img);
429   RTL->Mtx.unlock();
430   return rc;
431 }
432 
433 void *DeviceTy::allocData(int64_t Size, void *HstPtr, int32_t Kind) {
434   return RTL->data_alloc(RTLDeviceID, Size, HstPtr, Kind);
435 }
436 
437 int32_t DeviceTy::deleteData(void *TgtPtrBegin) {
438   return RTL->data_delete(RTLDeviceID, TgtPtrBegin);
439 }
440 
441 // Submit data to device
442 int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
443                              AsyncInfoTy &AsyncInfo) {
444   if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
445     LookupResult LR = lookupMapping(HstPtrBegin, Size);
446     auto *HT = &*LR.Entry;
447 
448     INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID,
449          "Copying data from host to device, HstPtr=" DPxMOD ", TgtPtr=" DPxMOD
450          ", Size=%" PRId64 ", Name=%s\n",
451          DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBegin), Size,
452          (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str()
453                                 : "unknown");
454   }
455 
456   if (!AsyncInfo || !RTL->data_submit_async || !RTL->synchronize)
457     return RTL->data_submit(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size);
458   else
459     return RTL->data_submit_async(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size,
460                                   AsyncInfo);
461 }
462 
463 // Retrieve data from device
464 int32_t DeviceTy::retrieveData(void *HstPtrBegin, void *TgtPtrBegin,
465                                int64_t Size, AsyncInfoTy &AsyncInfo) {
466   if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
467     LookupResult LR = lookupMapping(HstPtrBegin, Size);
468     auto *HT = &*LR.Entry;
469     INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID,
470          "Copying data from device to host, TgtPtr=" DPxMOD ", HstPtr=" DPxMOD
471          ", Size=%" PRId64 ", Name=%s\n",
472          DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin), Size,
473          (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str()
474                                 : "unknown");
475   }
476 
477   if (!RTL->data_retrieve_async || !RTL->synchronize)
478     return RTL->data_retrieve(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size);
479   else
480     return RTL->data_retrieve_async(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size,
481                                     AsyncInfo);
482 }
483 
484 // Copy data from current device to destination device directly
485 int32_t DeviceTy::dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,
486                                int64_t Size, AsyncInfoTy &AsyncInfo) {
487   if (!AsyncInfo || !RTL->data_exchange_async || !RTL->synchronize) {
488     assert(RTL->data_exchange && "RTL->data_exchange is nullptr");
489     return RTL->data_exchange(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID, DstPtr,
490                               Size);
491   } else
492     return RTL->data_exchange_async(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID,
493                                     DstPtr, Size, AsyncInfo);
494 }
495 
496 // Run region on device
497 int32_t DeviceTy::runRegion(void *TgtEntryPtr, void **TgtVarsPtr,
498                             ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
499                             AsyncInfoTy &AsyncInfo) {
500   if (!RTL->run_region || !RTL->synchronize)
501     return RTL->run_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets,
502                            TgtVarsSize);
503   else
504     return RTL->run_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
505                                  TgtOffsets, TgtVarsSize, AsyncInfo);
506 }
507 
508 // Run region on device
509 bool DeviceTy::printDeviceInfo(int32_t RTLDevId) {
510   if (!RTL->print_device_info)
511     return false;
512   RTL->print_device_info(RTLDevId);
513   return true;
514 }
515 
516 // Run team region on device.
517 int32_t DeviceTy::runTeamRegion(void *TgtEntryPtr, void **TgtVarsPtr,
518                                 ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
519                                 int32_t NumTeams, int32_t ThreadLimit,
520                                 uint64_t LoopTripCount,
521                                 AsyncInfoTy &AsyncInfo) {
522   if (!RTL->run_team_region_async || !RTL->synchronize)
523     return RTL->run_team_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
524                                 TgtOffsets, TgtVarsSize, NumTeams, ThreadLimit,
525                                 LoopTripCount);
526   else
527     return RTL->run_team_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
528                                       TgtOffsets, TgtVarsSize, NumTeams,
529                                       ThreadLimit, LoopTripCount, AsyncInfo);
530 }
531 
532 // Whether data can be copied to DstDevice directly
533 bool DeviceTy::isDataExchangable(const DeviceTy &DstDevice) {
534   if (RTL != DstDevice.RTL || !RTL->is_data_exchangable)
535     return false;
536 
537   if (RTL->is_data_exchangable(RTLDeviceID, DstDevice.RTLDeviceID))
538     return (RTL->data_exchange != nullptr) ||
539            (RTL->data_exchange_async != nullptr);
540 
541   return false;
542 }
543 
544 int32_t DeviceTy::synchronize(AsyncInfoTy &AsyncInfo) {
545   if (RTL->synchronize)
546     return RTL->synchronize(RTLDeviceID, AsyncInfo);
547   return OFFLOAD_SUCCESS;
548 }
549 
550 int32_t DeviceTy::createEvent(void **Event) {
551   if (RTL->create_event)
552     return RTL->create_event(RTLDeviceID, Event);
553 
554   return OFFLOAD_SUCCESS;
555 }
556 
557 int32_t DeviceTy::recordEvent(void *Event, AsyncInfoTy &AsyncInfo) {
558   if (RTL->record_event)
559     return RTL->record_event(RTLDeviceID, Event, AsyncInfo);
560 
561   return OFFLOAD_SUCCESS;
562 }
563 
564 int32_t DeviceTy::waitEvent(void *Event, AsyncInfoTy &AsyncInfo) {
565   if (RTL->wait_event)
566     return RTL->wait_event(RTLDeviceID, Event, AsyncInfo);
567 
568   return OFFLOAD_SUCCESS;
569 }
570 
571 int32_t DeviceTy::syncEvent(void *Event) {
572   if (RTL->sync_event)
573     return RTL->sync_event(RTLDeviceID, Event);
574 
575   return OFFLOAD_SUCCESS;
576 }
577 
578 int32_t DeviceTy::destroyEvent(void *Event) {
579   if (RTL->create_event)
580     return RTL->destroy_event(RTLDeviceID, Event);
581 
582   return OFFLOAD_SUCCESS;
583 }
584 
585 /// Check whether a device has an associated RTL and initialize it if it's not
586 /// already initialized.
587 bool device_is_ready(int device_num) {
588   DP("Checking whether device %d is ready.\n", device_num);
589   // Devices.size() can only change while registering a new
590   // library, so try to acquire the lock of RTLs' mutex.
591   PM->RTLsMtx.lock();
592   size_t DevicesSize = PM->Devices.size();
593   PM->RTLsMtx.unlock();
594   if (DevicesSize <= (size_t)device_num) {
595     DP("Device ID  %d does not have a matching RTL\n", device_num);
596     return false;
597   }
598 
599   // Get device info
600   DeviceTy &Device = *PM->Devices[device_num];
601 
602   DP("Is the device %d (local ID %d) initialized? %d\n", device_num,
603      Device.RTLDeviceID, Device.IsInit);
604 
605   // Init the device if not done before
606   if (!Device.IsInit && Device.initOnce() != OFFLOAD_SUCCESS) {
607     DP("Failed to init device %d\n", device_num);
608     return false;
609   }
610 
611   DP("Device %d is ready to use.\n", device_num);
612 
613   return true;
614 }
615