13029616dSJonas Hahnfeld //===--------- device.cpp - Target independent OpenMP target RTL ----------===//
23029616dSJonas Hahnfeld //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63029616dSJonas Hahnfeld //
73029616dSJonas Hahnfeld //===----------------------------------------------------------------------===//
83029616dSJonas Hahnfeld //
93029616dSJonas Hahnfeld // Functionality for managing devices that are handled by RTL plugins.
103029616dSJonas Hahnfeld //
113029616dSJonas Hahnfeld //===----------------------------------------------------------------------===//
123029616dSJonas Hahnfeld
133029616dSJonas Hahnfeld #include "device.h"
14307bbd3cSJohannes Doerfert #include "omptarget.h"
153029616dSJonas Hahnfeld #include "private.h"
163029616dSJonas Hahnfeld #include "rtl.h"
173029616dSJonas Hahnfeld
183029616dSJonas Hahnfeld #include <cassert>
193029616dSJonas Hahnfeld #include <climits>
204e34f061SJohannes Doerfert #include <cstdint>
21c3e6054bSJoseph Huber #include <cstdio>
223029616dSJonas Hahnfeld #include <string>
23b3161268SJohannes Doerfert #include <thread>
243029616dSJonas Hahnfeld
addEventIfNecessary(DeviceTy & Device,AsyncInfoTy & AsyncInfo) const25307bbd3cSJohannes Doerfert int HostDataToTargetTy::addEventIfNecessary(DeviceTy &Device,
26307bbd3cSJohannes Doerfert AsyncInfoTy &AsyncInfo) const {
271e447d03SJohannes Doerfert // First, check if the user disabled atomic map transfer/malloc/dealloc.
281e447d03SJohannes Doerfert if (!PM->UseEventsForAtomicTransfers)
291e447d03SJohannes Doerfert return OFFLOAD_SUCCESS;
301e447d03SJohannes Doerfert
311e447d03SJohannes Doerfert void *Event = getEvent();
321e447d03SJohannes Doerfert bool NeedNewEvent = Event == nullptr;
331e447d03SJohannes Doerfert if (NeedNewEvent && Device.createEvent(&Event) != OFFLOAD_SUCCESS) {
341e447d03SJohannes Doerfert REPORT("Failed to create event\n");
351e447d03SJohannes Doerfert return OFFLOAD_FAIL;
361e447d03SJohannes Doerfert }
371e447d03SJohannes Doerfert
381e447d03SJohannes Doerfert // We cannot assume the event should not be nullptr because we don't
391e447d03SJohannes Doerfert // know if the target support event. But if a target doesn't,
401e447d03SJohannes Doerfert // recordEvent should always return success.
411e447d03SJohannes Doerfert if (Device.recordEvent(Event, AsyncInfo) != OFFLOAD_SUCCESS) {
421e447d03SJohannes Doerfert REPORT("Failed to set dependence on event " DPxMOD "\n", DPxPTR(Event));
431e447d03SJohannes Doerfert return OFFLOAD_FAIL;
441e447d03SJohannes Doerfert }
451e447d03SJohannes Doerfert
461e447d03SJohannes Doerfert if (NeedNewEvent)
471e447d03SJohannes Doerfert setEvent(Event);
481e447d03SJohannes Doerfert
491e447d03SJohannes Doerfert return OFFLOAD_SUCCESS;
501e447d03SJohannes Doerfert }
511e447d03SJohannes Doerfert
DeviceTy(RTLInfoTy * RTL)5202896967SShilei Tian DeviceTy::DeviceTy(RTLInfoTy *RTL)
5302896967SShilei Tian : DeviceID(-1), RTL(RTL), RTLDeviceID(-1), IsInit(false), InitFlag(),
544e34f061SJohannes Doerfert HasPendingGlobals(false), PendingCtorsDtors(), ShadowPtrMap(),
554e34f061SJohannes Doerfert PendingGlobalsMtx(), ShadowMtx() {}
5602896967SShilei Tian
~DeviceTy()57c3e6054bSJoseph Huber DeviceTy::~DeviceTy() {
58fe5d51a4SJoseph Huber if (DeviceID == -1 || !(getInfoLevel() & OMP_INFOTYPE_DUMP_TABLE))
59c3e6054bSJoseph Huber return;
60c3e6054bSJoseph Huber
61*d27d0a67SJoseph Huber ident_t Loc = {0, 0, 0, 0, ";libomptarget;libomptarget;0;0;;"};
62*d27d0a67SJoseph Huber dumpTargetPointerMappings(&Loc, *this);
63c3e6054bSJoseph Huber }
6402896967SShilei Tian
associatePtr(void * HstPtrBegin,void * TgtPtrBegin,int64_t Size)653029616dSJonas Hahnfeld int DeviceTy::associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size) {
664e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
673029616dSJonas Hahnfeld
683029616dSJonas Hahnfeld // Check if entry exists
694e34f061SJohannes Doerfert auto It = HDTTMap->find(HstPtrBegin);
704e34f061SJohannes Doerfert if (It != HDTTMap->end()) {
714e34f061SJohannes Doerfert HostDataToTargetTy &HDTT = *It->HDTT;
723029616dSJonas Hahnfeld // Mapping already exists
73*d27d0a67SJoseph Huber bool IsValid = HDTT.HstPtrEnd == (uintptr_t)HstPtrBegin + Size &&
744e34f061SJohannes Doerfert HDTT.TgtPtrBegin == (uintptr_t)TgtPtrBegin;
75*d27d0a67SJoseph Huber if (IsValid) {
763029616dSJonas Hahnfeld DP("Attempt to re-associate the same device ptr+offset with the same "
773029616dSJonas Hahnfeld "host ptr, nothing to do\n");
783029616dSJonas Hahnfeld return OFFLOAD_SUCCESS;
79*d27d0a67SJoseph Huber }
807a5a74eaSJoseph Huber REPORT("Not allowed to re-associate a different device ptr+offset with "
817a5a74eaSJoseph Huber "the same host ptr\n");
823029616dSJonas Hahnfeld return OFFLOAD_FAIL;
833029616dSJonas Hahnfeld }
843029616dSJonas Hahnfeld
85e244145aSGeorge Rokos // Mapping does not exist, allocate it with refCount=INF
86*d27d0a67SJoseph Huber const HostDataToTargetTy &NewEntry =
874e34f061SJohannes Doerfert *HDTTMap
884e34f061SJohannes Doerfert ->emplace(new HostDataToTargetTy(
8948421ac4SJoel E. Denny /*HstPtrBase=*/(uintptr_t)HstPtrBegin,
9048421ac4SJoel E. Denny /*HstPtrBegin=*/(uintptr_t)HstPtrBegin,
9148421ac4SJoel E. Denny /*HstPtrEnd=*/(uintptr_t)HstPtrBegin + Size,
92ec1ebcd3SJoel E. Denny /*TgtPtrBegin=*/(uintptr_t)TgtPtrBegin,
93ec1ebcd3SJoel E. Denny /*UseHoldRefCount=*/false, /*Name=*/nullptr,
944e34f061SJohannes Doerfert /*IsRefCountINF=*/true))
954e34f061SJohannes Doerfert .first->HDTT;
969cd1e222SJohannes Doerfert DP("Creating new map entry: HstBase=" DPxMOD ", HstBegin=" DPxMOD
97ec1ebcd3SJoel E. Denny ", HstEnd=" DPxMOD ", TgtBegin=" DPxMOD ", DynRefCount=%s, "
98ec1ebcd3SJoel E. Denny "HoldRefCount=%s\n",
99*d27d0a67SJoseph Huber DPxPTR(NewEntry.HstPtrBase), DPxPTR(NewEntry.HstPtrBegin),
100*d27d0a67SJoseph Huber DPxPTR(NewEntry.HstPtrEnd), DPxPTR(NewEntry.TgtPtrBegin),
101*d27d0a67SJoseph Huber NewEntry.dynRefCountToStr().c_str(), NewEntry.holdRefCountToStr().c_str());
102*d27d0a67SJoseph Huber (void)NewEntry;
1033029616dSJonas Hahnfeld
1043029616dSJonas Hahnfeld return OFFLOAD_SUCCESS;
1053029616dSJonas Hahnfeld }
1063029616dSJonas Hahnfeld
disassociatePtr(void * HstPtrBegin)1073029616dSJonas Hahnfeld int DeviceTy::disassociatePtr(void *HstPtrBegin) {
1084e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
1093029616dSJonas Hahnfeld
1104e34f061SJohannes Doerfert auto It = HDTTMap->find(HstPtrBegin);
1114e34f061SJohannes Doerfert if (It != HDTTMap->end()) {
1124e34f061SJohannes Doerfert HostDataToTargetTy &HDTT = *It->HDTT;
1133029616dSJonas Hahnfeld // Mapping exists
1144e34f061SJohannes Doerfert if (HDTT.getHoldRefCount()) {
115ec1ebcd3SJoel E. Denny // This is based on OpenACC 3.1, sec 3.2.33 "acc_unmap_data", L3656-3657:
116ec1ebcd3SJoel E. Denny // "It is an error to call acc_unmap_data if the structured reference
117ec1ebcd3SJoel E. Denny // count for the pointer is not zero."
118ec1ebcd3SJoel E. Denny REPORT("Trying to disassociate a pointer with a non-zero hold reference "
119ec1ebcd3SJoel E. Denny "count\n");
1204e34f061SJohannes Doerfert } else if (HDTT.isDynRefCountInf()) {
1213029616dSJonas Hahnfeld DP("Association found, removing it\n");
1224e34f061SJohannes Doerfert void *Event = HDTT.getEvent();
1234e34f061SJohannes Doerfert delete &HDTT;
1249584c6faSShilei Tian if (Event)
1259584c6faSShilei Tian destroyEvent(Event);
1264e34f061SJohannes Doerfert HDTTMap->erase(It);
1273029616dSJonas Hahnfeld return OFFLOAD_SUCCESS;
1283029616dSJonas Hahnfeld } else {
1297a5a74eaSJoseph Huber REPORT("Trying to disassociate a pointer which was not mapped via "
1303029616dSJonas Hahnfeld "omp_target_associate_ptr\n");
1313029616dSJonas Hahnfeld }
132ec1ebcd3SJoel E. Denny } else {
133ec1ebcd3SJoel E. Denny REPORT("Association not found\n");
1343029616dSJonas Hahnfeld }
1353029616dSJonas Hahnfeld
1363029616dSJonas Hahnfeld // Mapping not found
1373029616dSJonas Hahnfeld return OFFLOAD_FAIL;
1383029616dSJonas Hahnfeld }
1393029616dSJonas Hahnfeld
lookupMapping(HDTTMapAccessorTy & HDTTMap,void * HstPtrBegin,int64_t Size)1404e34f061SJohannes Doerfert LookupResult DeviceTy::lookupMapping(HDTTMapAccessorTy &HDTTMap,
1414e34f061SJohannes Doerfert void *HstPtrBegin, int64_t Size) {
1424e34f061SJohannes Doerfert
143*d27d0a67SJoseph Huber uintptr_t HP = (uintptr_t)HstPtrBegin;
144*d27d0a67SJoseph Huber LookupResult LR;
1453029616dSJonas Hahnfeld
14640470eb2SGeorge Rokos DP("Looking up mapping(HstPtrBegin=" DPxMOD ", Size=%" PRId64 ")...\n",
147*d27d0a67SJoseph Huber DPxPTR(HP), Size);
1486e5f64c4SYe Luo
1494e34f061SJohannes Doerfert if (HDTTMap->empty())
150*d27d0a67SJoseph Huber return LR;
1516e5f64c4SYe Luo
152*d27d0a67SJoseph Huber auto Upper = HDTTMap->upper_bound(HP);
153c1a6fe19SYe Luo
154c1a6fe19SYe Luo if (Size == 0) {
155c1a6fe19SYe Luo // specification v5.1 Pointer Initialization for Device Data Environments
156c1a6fe19SYe Luo // upper_bound satisfies
157c1a6fe19SYe Luo // std::prev(upper)->HDTT.HstPtrBegin <= hp < upper->HDTT.HstPtrBegin
158*d27d0a67SJoseph Huber if (Upper != HDTTMap->begin()) {
159*d27d0a67SJoseph Huber LR.Entry = std::prev(Upper)->HDTT;
160*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
161c1a6fe19SYe Luo // the left side of extended address range is satisified.
162c1a6fe19SYe Luo // hp >= HT.HstPtrBegin || hp >= HT.HstPtrBase
163*d27d0a67SJoseph Huber LR.Flags.IsContained = HP < HT.HstPtrEnd || HP < HT.HstPtrBase;
164c1a6fe19SYe Luo }
165c1a6fe19SYe Luo
166*d27d0a67SJoseph Huber if (!LR.Flags.IsContained && Upper != HDTTMap->end()) {
167*d27d0a67SJoseph Huber LR.Entry = Upper->HDTT;
168*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
169c1a6fe19SYe Luo // the right side of extended address range is satisified.
170c1a6fe19SYe Luo // hp < HT.HstPtrEnd || hp < HT.HstPtrBase
171*d27d0a67SJoseph Huber LR.Flags.IsContained = HP >= HT.HstPtrBase;
172c1a6fe19SYe Luo }
173c1a6fe19SYe Luo } else {
1746e5f64c4SYe Luo // check the left bin
175*d27d0a67SJoseph Huber if (Upper != HDTTMap->begin()) {
176*d27d0a67SJoseph Huber LR.Entry = std::prev(Upper)->HDTT;
177*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
1783029616dSJonas Hahnfeld // Is it contained?
179*d27d0a67SJoseph Huber LR.Flags.IsContained = HP >= HT.HstPtrBegin && HP < HT.HstPtrEnd &&
180*d27d0a67SJoseph Huber (HP + Size) <= HT.HstPtrEnd;
1816e5f64c4SYe Luo // Does it extend beyond the mapped region?
182*d27d0a67SJoseph Huber LR.Flags.ExtendsAfter = HP < HT.HstPtrEnd && (HP + Size) > HT.HstPtrEnd;
1836e5f64c4SYe Luo }
1846e5f64c4SYe Luo
1856e5f64c4SYe Luo // check the right bin
186*d27d0a67SJoseph Huber if (!(LR.Flags.IsContained || LR.Flags.ExtendsAfter) &&
187*d27d0a67SJoseph Huber Upper != HDTTMap->end()) {
188*d27d0a67SJoseph Huber LR.Entry = Upper->HDTT;
189*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
1903029616dSJonas Hahnfeld // Does it extend into an already mapped region?
191*d27d0a67SJoseph Huber LR.Flags.ExtendsBefore =
192*d27d0a67SJoseph Huber HP < HT.HstPtrBegin && (HP + Size) > HT.HstPtrBegin;
1933029616dSJonas Hahnfeld // Does it extend beyond the mapped region?
194*d27d0a67SJoseph Huber LR.Flags.ExtendsAfter = HP < HT.HstPtrEnd && (HP + Size) > HT.HstPtrEnd;
1953029616dSJonas Hahnfeld }
1963029616dSJonas Hahnfeld
197*d27d0a67SJoseph Huber if (LR.Flags.ExtendsBefore) {
1983029616dSJonas Hahnfeld DP("WARNING: Pointer is not mapped but section extends into already "
1993029616dSJonas Hahnfeld "mapped data\n");
2003029616dSJonas Hahnfeld }
201*d27d0a67SJoseph Huber if (LR.Flags.ExtendsAfter) {
2023029616dSJonas Hahnfeld DP("WARNING: Pointer is already mapped but section extends beyond mapped "
2033029616dSJonas Hahnfeld "region\n");
2043029616dSJonas Hahnfeld }
205c1a6fe19SYe Luo }
2063029616dSJonas Hahnfeld
207*d27d0a67SJoseph Huber return LR;
2083029616dSJonas Hahnfeld }
2093029616dSJonas Hahnfeld
getTargetPointer(void * HstPtrBegin,void * HstPtrBase,int64_t Size,map_var_info_t HstPtrName,bool HasFlagTo,bool HasFlagAlways,bool IsImplicit,bool UpdateRefCount,bool HasCloseModifier,bool HasPresentModifier,bool HasHoldModifier,AsyncInfoTy & AsyncInfo)210307bbd3cSJohannes Doerfert TargetPointerResultTy DeviceTy::getTargetPointer(
211307bbd3cSJohannes Doerfert void *HstPtrBegin, void *HstPtrBase, int64_t Size,
212307bbd3cSJohannes Doerfert map_var_info_t HstPtrName, bool HasFlagTo, bool HasFlagAlways,
213307bbd3cSJohannes Doerfert bool IsImplicit, bool UpdateRefCount, bool HasCloseModifier,
214307bbd3cSJohannes Doerfert bool HasPresentModifier, bool HasHoldModifier, AsyncInfoTy &AsyncInfo) {
2154e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
2164e34f061SJohannes Doerfert
21718ce3d3fSShilei Tian void *TargetPointer = nullptr;
218369216abSShilei Tian bool IsHostPtr = false;
21918ce3d3fSShilei Tian bool IsNew = false;
22018ce3d3fSShilei Tian
2214e34f061SJohannes Doerfert LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
2224e34f061SJohannes Doerfert auto *Entry = LR.Entry;
2233029616dSJonas Hahnfeld
2243029616dSJonas Hahnfeld // Check if the pointer is contained.
225a1d20506SGheorghe-Teodor Bercea // If a variable is mapped to the device manually by the user - which would
226a1d20506SGheorghe-Teodor Bercea // lead to the IsContained flag to be true - then we must ensure that the
227a1d20506SGheorghe-Teodor Bercea // device address is returned even under unified memory conditions.
228369216abSShilei Tian if (LR.Flags.IsContained ||
229369216abSShilei Tian ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && IsImplicit)) {
230369216abSShilei Tian auto &HT = *LR.Entry;
231ec1ebcd3SJoel E. Denny const char *RefCountAction;
232ec1ebcd3SJoel E. Denny if (UpdateRefCount) {
233b3161268SJohannes Doerfert // After this, reference count >= 1. If the reference count was 0 but the
234b3161268SJohannes Doerfert // entry was still there we can reuse the data on the device and avoid a
235b3161268SJohannes Doerfert // new submission.
236ec1ebcd3SJoel E. Denny HT.incRefCount(HasHoldModifier);
237ec1ebcd3SJoel E. Denny RefCountAction = " (incremented)";
238ec1ebcd3SJoel E. Denny } else {
239d99f65deSJoel E. Denny // It might have been allocated with the parent, but it's still new.
240ec1ebcd3SJoel E. Denny IsNew = HT.getTotalRefCount() == 1;
241ec1ebcd3SJoel E. Denny RefCountAction = " (update suppressed)";
242ec1ebcd3SJoel E. Denny }
243ec1ebcd3SJoel E. Denny const char *DynRefCountAction = HasHoldModifier ? "" : RefCountAction;
244ec1ebcd3SJoel E. Denny const char *HoldRefCountAction = HasHoldModifier ? RefCountAction : "";
245369216abSShilei Tian uintptr_t Ptr = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
246119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_MAPPING_EXISTS, DeviceID,
247c3e6054bSJoseph Huber "Mapping exists%s with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
248ec1ebcd3SJoel E. Denny ", Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s, Name=%s\n",
249369216abSShilei Tian (IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), DPxPTR(Ptr),
250ec1ebcd3SJoel E. Denny Size, HT.dynRefCountToStr().c_str(), DynRefCountAction,
251ec1ebcd3SJoel E. Denny HT.holdRefCountToStr().c_str(), HoldRefCountAction,
252fe5d51a4SJoseph Huber (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");
253369216abSShilei Tian TargetPointer = (void *)Ptr;
254369216abSShilei Tian } else if ((LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) && !IsImplicit) {
2553029616dSJonas Hahnfeld // Explicit extension of mapped data - not allowed.
25641b1aefeSJoel E. Denny MESSAGE("explicit extension not allowed: host address specified is " DPxMOD
2579cd1e222SJohannes Doerfert " (%" PRId64
2589cd1e222SJohannes Doerfert " bytes), but device allocation maps to host at " DPxMOD
2599cd1e222SJohannes Doerfert " (%" PRId64 " bytes)",
260369216abSShilei Tian DPxPTR(HstPtrBegin), Size, DPxPTR(Entry->HstPtrBegin),
261369216abSShilei Tian Entry->HstPtrEnd - Entry->HstPtrBegin);
26241b1aefeSJoel E. Denny if (HasPresentModifier)
26341b1aefeSJoel E. Denny MESSAGE("device mapping required by 'present' map type modifier does not "
26441b1aefeSJoel E. Denny "exist for host address " DPxMOD " (%" PRId64 " bytes)",
26541b1aefeSJoel E. Denny DPxPTR(HstPtrBegin), Size);
266a95b25b2SAtmn Patel } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY &&
26763cef621SAlexey Bataev !HasCloseModifier) {
268708752b2SJoel E. Denny // If unified shared memory is active, implicitly mapped variables that are
269708752b2SJoel E. Denny // not privatized use host address. Any explicitly mapped variables also use
270708752b2SJoel E. Denny // host address where correctness is not impeded. In all other cases maps
271708752b2SJoel E. Denny // are respected.
272708752b2SJoel E. Denny // In addition to the mapping rules above, the close map modifier forces the
273708752b2SJoel E. Denny // mapping of the variable to the device.
274708752b2SJoel E. Denny if (Size) {
27548421ac4SJoel E. Denny DP("Return HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "
27648421ac4SJoel E. Denny "memory\n",
27748421ac4SJoel E. Denny DPxPTR((uintptr_t)HstPtrBegin), Size);
278a1d20506SGheorghe-Teodor Bercea IsHostPtr = true;
279369216abSShilei Tian TargetPointer = HstPtrBegin;
280708752b2SJoel E. Denny }
281708752b2SJoel E. Denny } else if (HasPresentModifier) {
282708752b2SJoel E. Denny DP("Mapping required by 'present' map type modifier does not exist for "
28340470eb2SGeorge Rokos "HstPtrBegin=" DPxMOD ", Size=%" PRId64 "\n",
284708752b2SJoel E. Denny DPxPTR(HstPtrBegin), Size);
285708752b2SJoel E. Denny MESSAGE("device mapping required by 'present' map type modifier does not "
28640470eb2SGeorge Rokos "exist for host address " DPxMOD " (%" PRId64 " bytes)",
287708752b2SJoel E. Denny DPxPTR(HstPtrBegin), Size);
288708752b2SJoel E. Denny } else if (Size) {
289708752b2SJoel E. Denny // If it is not contained and Size > 0, we should create a new entry for it.
2903029616dSJonas Hahnfeld IsNew = true;
291369216abSShilei Tian uintptr_t Ptr = (uintptr_t)allocData(Size, HstPtrBegin);
2924e34f061SJohannes Doerfert Entry = HDTTMap
2934e34f061SJohannes Doerfert ->emplace(new HostDataToTargetTy(
2944e34f061SJohannes Doerfert (uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,
295ec1ebcd3SJoel E. Denny (uintptr_t)HstPtrBegin + Size, Ptr, HasHoldModifier,
2964e34f061SJohannes Doerfert HstPtrName))
2974e34f061SJohannes Doerfert .first->HDTT;
29883d4b2e2SJoseph Huber INFO(OMP_INFOTYPE_MAPPING_CHANGED, DeviceID,
299c1a6fe19SYe Luo "Creating new map entry with HstPtrBase=" DPxMOD
300c1a6fe19SYe Luo ", HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", Size=%ld, "
301ec1ebcd3SJoel E. Denny "DynRefCount=%s, HoldRefCount=%s, Name=%s\n",
302c1a6fe19SYe Luo DPxPTR(HstPtrBase), DPxPTR(HstPtrBegin), DPxPTR(Ptr), Size,
303ec1ebcd3SJoel E. Denny Entry->dynRefCountToStr().c_str(), Entry->holdRefCountToStr().c_str(),
30483d4b2e2SJoseph Huber (HstPtrName) ? getNameFromMapping(HstPtrName).c_str() : "unknown");
305369216abSShilei Tian TargetPointer = (void *)Ptr;
3063029616dSJonas Hahnfeld }
3073029616dSJonas Hahnfeld
30818ce3d3fSShilei Tian // If the target pointer is valid, and we need to transfer data, issue the
30918ce3d3fSShilei Tian // data transfer.
310d11bab0bSJoel E. Denny if (TargetPointer && !IsHostPtr && HasFlagTo && (IsNew || HasFlagAlways)) {
31118ce3d3fSShilei Tian // Lock the entry before releasing the mapping table lock such that another
31218ce3d3fSShilei Tian // thread that could issue data movement will get the right result.
313307bbd3cSJohannes Doerfert std::lock_guard<decltype(*Entry)> LG(*Entry);
31418ce3d3fSShilei Tian // Release the mapping table lock right after the entry is locked.
3154e34f061SJohannes Doerfert HDTTMap.destroy();
31618ce3d3fSShilei Tian
31718ce3d3fSShilei Tian DP("Moving %" PRId64 " bytes (hst:" DPxMOD ") -> (tgt:" DPxMOD ")\n", Size,
31818ce3d3fSShilei Tian DPxPTR(HstPtrBegin), DPxPTR(TargetPointer));
31918ce3d3fSShilei Tian
32018ce3d3fSShilei Tian int Ret = submitData(TargetPointer, HstPtrBegin, Size, AsyncInfo);
32118ce3d3fSShilei Tian if (Ret != OFFLOAD_SUCCESS) {
32218ce3d3fSShilei Tian REPORT("Copying data to device failed.\n");
32318ce3d3fSShilei Tian // We will also return nullptr if the data movement fails because that
32418ce3d3fSShilei Tian // pointer points to a corrupted memory region so it doesn't make any
32518ce3d3fSShilei Tian // sense to continue to use it.
32618ce3d3fSShilei Tian TargetPointer = nullptr;
327307bbd3cSJohannes Doerfert } else if (Entry->addEventIfNecessary(*this, AsyncInfo) != OFFLOAD_SUCCESS)
3289584c6faSShilei Tian return {{false /* IsNewEntry */, false /* IsHostPointer */},
3294e34f061SJohannes Doerfert nullptr /* Entry */,
3309584c6faSShilei Tian nullptr /* TargetPointer */};
33118ce3d3fSShilei Tian } else {
33218ce3d3fSShilei Tian // Release the mapping table lock directly.
3334e34f061SJohannes Doerfert HDTTMap.destroy();
3349584c6faSShilei Tian // If not a host pointer and no present modifier, we need to wait for the
3359584c6faSShilei Tian // event if it exists.
336aab62aabSShilei Tian // Note: Entry might be nullptr because of zero length array section.
3374e34f061SJohannes Doerfert if (Entry && !IsHostPtr && !HasPresentModifier) {
338307bbd3cSJohannes Doerfert std::lock_guard<decltype(*Entry)> LG(*Entry);
3399584c6faSShilei Tian void *Event = Entry->getEvent();
3409584c6faSShilei Tian if (Event) {
3419584c6faSShilei Tian int Ret = waitEvent(Event, AsyncInfo);
3429584c6faSShilei Tian if (Ret != OFFLOAD_SUCCESS) {
3439584c6faSShilei Tian // If it fails to wait for the event, we need to return nullptr in
3449584c6faSShilei Tian // case of any data race.
3459584c6faSShilei Tian REPORT("Failed to wait for event " DPxMOD ".\n", DPxPTR(Event));
3469584c6faSShilei Tian return {{false /* IsNewEntry */, false /* IsHostPointer */},
3474e34f061SJohannes Doerfert nullptr /* Entry */,
3489584c6faSShilei Tian nullptr /* TargetPointer */};
3499584c6faSShilei Tian }
3509584c6faSShilei Tian }
3519584c6faSShilei Tian }
35218ce3d3fSShilei Tian }
35318ce3d3fSShilei Tian
354369216abSShilei Tian return {{IsNew, IsHostPtr}, Entry, TargetPointer};
3553029616dSJonas Hahnfeld }
3563029616dSJonas Hahnfeld
3577036fe8aScchen // Used by targetDataBegin, targetDataEnd, targetDataUpdate and target.
3583029616dSJonas Hahnfeld // Return the target pointer begin (where the data will be moved).
3598425bde8SJoseph Huber // Decrement the reference counter if called from targetDataEnd.
360b0789a1bSJohannes Doerfert TargetPointerResultTy
getTgtPtrBegin(void * HstPtrBegin,int64_t Size,bool & IsLast,bool UpdateRefCount,bool UseHoldRefCount,bool & IsHostPtr,bool MustContain,bool ForceDelete)361b0789a1bSJohannes Doerfert DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
362ec1ebcd3SJoel E. Denny bool UpdateRefCount, bool UseHoldRefCount,
363b0789a1bSJohannes Doerfert bool &IsHostPtr, bool MustContain, bool ForceDelete) {
3644e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
3654e34f061SJohannes Doerfert
366b0789a1bSJohannes Doerfert void *TargetPointer = NULL;
367b0789a1bSJohannes Doerfert bool IsNew = false;
368a1d20506SGheorghe-Teodor Bercea IsHostPtr = false;
369a1d20506SGheorghe-Teodor Bercea IsLast = false;
370*d27d0a67SJoseph Huber LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
3713029616dSJonas Hahnfeld
372*d27d0a67SJoseph Huber if (LR.Flags.IsContained ||
373*d27d0a67SJoseph Huber (!MustContain && (LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter))) {
374*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
375ec1ebcd3SJoel E. Denny IsLast = HT.decShouldRemove(UseHoldRefCount, ForceDelete);
376b3161268SJohannes Doerfert
377b3161268SJohannes Doerfert if (ForceDelete) {
378ec1ebcd3SJoel E. Denny HT.resetRefCount(UseHoldRefCount);
379ec1ebcd3SJoel E. Denny assert(IsLast == HT.decShouldRemove(UseHoldRefCount) &&
3809fa5e328SJoel E. Denny "expected correct IsLast prediction for reset");
38148421ac4SJoel E. Denny }
382b3161268SJohannes Doerfert
383b3161268SJohannes Doerfert const char *RefCountAction;
384b3161268SJohannes Doerfert if (!UpdateRefCount) {
385b3161268SJohannes Doerfert RefCountAction = " (update suppressed)";
386ec1ebcd3SJoel E. Denny } else if (IsLast) {
387b3161268SJohannes Doerfert // Mark the entry as to be deleted by this thread. Another thread might
388b3161268SJohannes Doerfert // reuse the entry and take "ownership" for the deletion while this thread
389b3161268SJohannes Doerfert // is waiting for data transfers. That is fine and the current thread will
390b3161268SJohannes Doerfert // simply skip the deletion step then.
391b3161268SJohannes Doerfert HT.setDeleteThreadId();
392b3161268SJohannes Doerfert HT.decRefCount(UseHoldRefCount);
393b3161268SJohannes Doerfert assert(HT.getTotalRefCount() == 0 &&
394b3161268SJohannes Doerfert "Expected zero reference count when deletion is scheduled");
395b3161268SJohannes Doerfert if (ForceDelete)
396b3161268SJohannes Doerfert RefCountAction = " (reset, delayed deletion)";
397b3161268SJohannes Doerfert else
398b3161268SJohannes Doerfert RefCountAction = " (decremented, delayed deletion)";
399ec1ebcd3SJoel E. Denny } else {
400ec1ebcd3SJoel E. Denny HT.decRefCount(UseHoldRefCount);
401ec1ebcd3SJoel E. Denny RefCountAction = " (decremented)";
402ec1ebcd3SJoel E. Denny }
403ec1ebcd3SJoel E. Denny const char *DynRefCountAction = UseHoldRefCount ? "" : RefCountAction;
404ec1ebcd3SJoel E. Denny const char *HoldRefCountAction = UseHoldRefCount ? RefCountAction : "";
405*d27d0a67SJoseph Huber uintptr_t TP = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin);
40648421ac4SJoel E. Denny INFO(OMP_INFOTYPE_MAPPING_EXISTS, DeviceID,
40748421ac4SJoel E. Denny "Mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", "
408ec1ebcd3SJoel E. Denny "Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s\n",
409*d27d0a67SJoseph Huber DPxPTR(HstPtrBegin), DPxPTR(TP), Size, HT.dynRefCountToStr().c_str(),
410ec1ebcd3SJoel E. Denny DynRefCountAction, HT.holdRefCountToStr().c_str(), HoldRefCountAction);
411*d27d0a67SJoseph Huber TargetPointer = (void *)TP;
412a95b25b2SAtmn Patel } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) {
413a1d20506SGheorghe-Teodor Bercea // If the value isn't found in the mapping and unified shared memory
414a1d20506SGheorghe-Teodor Bercea // is on then it means we have stumbled upon a value which we need to
415a1d20506SGheorghe-Teodor Bercea // use directly from the host.
41648421ac4SJoel E. Denny DP("Get HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared "
41748421ac4SJoel E. Denny "memory\n",
41848421ac4SJoel E. Denny DPxPTR((uintptr_t)HstPtrBegin), Size);
419a1d20506SGheorghe-Teodor Bercea IsHostPtr = true;
420b0789a1bSJohannes Doerfert TargetPointer = HstPtrBegin;
4213029616dSJonas Hahnfeld }
4223029616dSJonas Hahnfeld
423*d27d0a67SJoseph Huber return {{IsNew, IsHostPtr}, LR.Entry, TargetPointer};
4243029616dSJonas Hahnfeld }
4253029616dSJonas Hahnfeld
4263029616dSJonas Hahnfeld // Return the target pointer begin (where the data will be moved).
getTgtPtrBegin(HDTTMapAccessorTy & HDTTMap,void * HstPtrBegin,int64_t Size)4274e34f061SJohannes Doerfert void *DeviceTy::getTgtPtrBegin(HDTTMapAccessorTy &HDTTMap, void *HstPtrBegin,
4284e34f061SJohannes Doerfert int64_t Size) {
429*d27d0a67SJoseph Huber uintptr_t HP = (uintptr_t)HstPtrBegin;
430*d27d0a67SJoseph Huber LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
431*d27d0a67SJoseph Huber if (LR.Flags.IsContained || LR.Flags.ExtendsBefore || LR.Flags.ExtendsAfter) {
432*d27d0a67SJoseph Huber auto &HT = *LR.Entry;
433*d27d0a67SJoseph Huber uintptr_t TP = HT.TgtPtrBegin + (HP - HT.HstPtrBegin);
434*d27d0a67SJoseph Huber return (void *)TP;
4353029616dSJonas Hahnfeld }
4363029616dSJonas Hahnfeld
4373029616dSJonas Hahnfeld return NULL;
4383029616dSJonas Hahnfeld }
4393029616dSJonas Hahnfeld
deallocTgtPtr(HDTTMapAccessorTy & HDTTMap,LookupResult LR,int64_t Size)440b3161268SJohannes Doerfert int DeviceTy::deallocTgtPtr(HDTTMapAccessorTy &HDTTMap, LookupResult LR,
441b3161268SJohannes Doerfert int64_t Size) {
4423029616dSJonas Hahnfeld // Check if the pointer is contained in any sub-nodes.
443b3161268SJohannes Doerfert if (!(LR.Flags.IsContained || LR.Flags.ExtendsBefore ||
444b3161268SJohannes Doerfert LR.Flags.ExtendsAfter)) {
445b3161268SJohannes Doerfert REPORT("Section to delete (hst addr " DPxMOD ") does not exist in the"
446b3161268SJohannes Doerfert " allocated memory\n",
447b3161268SJohannes Doerfert DPxPTR(LR.Entry->HstPtrBegin));
448b3161268SJohannes Doerfert return OFFLOAD_FAIL;
449b3161268SJohannes Doerfert }
450b3161268SJohannes Doerfert
451b3161268SJohannes Doerfert auto &HT = *LR.Entry;
452b3161268SJohannes Doerfert // Verify this thread is still in charge of deleting the entry.
453b3161268SJohannes Doerfert assert(HT.getTotalRefCount() == 0 &&
454b3161268SJohannes Doerfert HT.getDeleteThreadId() == std::this_thread::get_id() &&
455b3161268SJohannes Doerfert "Trying to delete entry that is in use or owned by another thread.");
456b3161268SJohannes Doerfert
45740470eb2SGeorge Rokos DP("Deleting tgt data " DPxMOD " of size %" PRId64 "\n",
4583029616dSJonas Hahnfeld DPxPTR(HT.TgtPtrBegin), Size);
4593ce69d4dSShilei Tian deleteData((void *)HT.TgtPtrBegin);
46083d4b2e2SJoseph Huber INFO(OMP_INFOTYPE_MAPPING_CHANGED, DeviceID,
4619fa5e328SJoel E. Denny "Removing map entry with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD
4629fa5e328SJoel E. Denny ", Size=%" PRId64 ", Name=%s\n",
4639fa5e328SJoel E. Denny DPxPTR(HT.HstPtrBegin), DPxPTR(HT.TgtPtrBegin), Size,
464b3161268SJohannes Doerfert (HT.HstPtrName) ? getNameFromMapping(HT.HstPtrName).c_str() : "unknown");
465b3161268SJohannes Doerfert void *Event = LR.Entry->getEvent();
466b3161268SJohannes Doerfert HDTTMap->erase(LR.Entry);
467b3161268SJohannes Doerfert delete LR.Entry;
468b3161268SJohannes Doerfert
469b3161268SJohannes Doerfert int Ret = OFFLOAD_SUCCESS;
4709584c6faSShilei Tian if (Event && destroyEvent(Event) != OFFLOAD_SUCCESS) {
4719584c6faSShilei Tian REPORT("Failed to destroy event " DPxMOD "\n", DPxPTR(Event));
4729584c6faSShilei Tian Ret = OFFLOAD_FAIL;
4733029616dSJonas Hahnfeld }
4743029616dSJonas Hahnfeld
4759584c6faSShilei Tian return Ret;
4763029616dSJonas Hahnfeld }
4773029616dSJonas Hahnfeld
4783029616dSJonas Hahnfeld /// Init device, should not be called directly.
init()4793029616dSJonas Hahnfeld void DeviceTy::init() {
480c5fe030cSGheorghe-Teodor Bercea // Make call to init_requires if it exists for this plugin.
481c5fe030cSGheorghe-Teodor Bercea if (RTL->init_requires)
482a95b25b2SAtmn Patel RTL->init_requires(PM->RTLs.RequiresFlags);
48302896967SShilei Tian int32_t Ret = RTL->init_device(RTLDeviceID);
48402896967SShilei Tian if (Ret != OFFLOAD_SUCCESS)
48502896967SShilei Tian return;
48602896967SShilei Tian
4873029616dSJonas Hahnfeld IsInit = true;
4883029616dSJonas Hahnfeld }
4893029616dSJonas Hahnfeld
4903029616dSJonas Hahnfeld /// Thread-safe method to initialize the device only once.
initOnce()4913029616dSJonas Hahnfeld int32_t DeviceTy::initOnce() {
4923029616dSJonas Hahnfeld std::call_once(InitFlag, &DeviceTy::init, this);
4933029616dSJonas Hahnfeld
4943029616dSJonas Hahnfeld // At this point, if IsInit is true, then either this thread or some other
4953029616dSJonas Hahnfeld // thread in the past successfully initialized the device, so we can return
4963029616dSJonas Hahnfeld // OFFLOAD_SUCCESS. If this thread executed init() via call_once() and it
4973029616dSJonas Hahnfeld // failed, return OFFLOAD_FAIL. If call_once did not invoke init(), it means
4983029616dSJonas Hahnfeld // that some other thread already attempted to execute init() and if IsInit
4993029616dSJonas Hahnfeld // is still false, return OFFLOAD_FAIL.
5003029616dSJonas Hahnfeld if (IsInit)
5013029616dSJonas Hahnfeld return OFFLOAD_SUCCESS;
5023029616dSJonas Hahnfeld return OFFLOAD_FAIL;
5033029616dSJonas Hahnfeld }
5043029616dSJonas Hahnfeld
deinit()50510aa83ffSJohannes Doerfert void DeviceTy::deinit() {
50610aa83ffSJohannes Doerfert if (RTL->deinit_device)
50710aa83ffSJohannes Doerfert RTL->deinit_device(RTLDeviceID);
50810aa83ffSJohannes Doerfert }
50910aa83ffSJohannes Doerfert
5103029616dSJonas Hahnfeld // Load binary to device.
loadBinary(void * Img)511*d27d0a67SJoseph Huber __tgt_target_table *DeviceTy::loadBinary(void *Img) {
512307bbd3cSJohannes Doerfert std::lock_guard<decltype(RTL->Mtx)> LG(RTL->Mtx);
513*d27d0a67SJoseph Huber return RTL->load_binary(RTLDeviceID, Img);
5143029616dSJonas Hahnfeld }
5153029616dSJonas Hahnfeld
allocData(int64_t Size,void * HstPtr,int32_t Kind)5162468fdd9SGeorge Rokos void *DeviceTy::allocData(int64_t Size, void *HstPtr, int32_t Kind) {
5172468fdd9SGeorge Rokos return RTL->data_alloc(RTLDeviceID, Size, HstPtr, Kind);
51893231666SYe Luo }
51993231666SYe Luo
deleteData(void * TgtPtrBegin)5203ce69d4dSShilei Tian int32_t DeviceTy::deleteData(void *TgtPtrBegin) {
52193231666SYe Luo return RTL->data_delete(RTLDeviceID, TgtPtrBegin);
52293231666SYe Luo }
52393231666SYe Luo
52432ed2927SShilei Tian // Submit data to device
submitData(void * TgtPtrBegin,void * HstPtrBegin,int64_t Size,AsyncInfoTy & AsyncInfo)5253ce69d4dSShilei Tian int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,
526758b8499SJohannes Doerfert AsyncInfoTy &AsyncInfo) {
527df965513SJoseph Huber if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
5284e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
5294e34f061SJohannes Doerfert LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
530df965513SJoseph Huber auto *HT = &*LR.Entry;
531df965513SJoseph Huber
532df965513SJoseph Huber INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID,
533df965513SJoseph Huber "Copying data from host to device, HstPtr=" DPxMOD ", TgtPtr=" DPxMOD
534df965513SJoseph Huber ", Size=%" PRId64 ", Name=%s\n",
535df965513SJoseph Huber DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBegin), Size,
536df965513SJoseph Huber (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str()
537df965513SJoseph Huber : "unknown");
538df965513SJoseph Huber }
539df965513SJoseph Huber
540758b8499SJohannes Doerfert if (!AsyncInfo || !RTL->data_submit_async || !RTL->synchronize)
54103ff643dSShilei Tian return RTL->data_submit(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size);
54203ff643dSShilei Tian return RTL->data_submit_async(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size,
543758b8499SJohannes Doerfert AsyncInfo);
5443029616dSJonas Hahnfeld }
5453029616dSJonas Hahnfeld
54632ed2927SShilei Tian // Retrieve data from device
retrieveData(void * HstPtrBegin,void * TgtPtrBegin,int64_t Size,AsyncInfoTy & AsyncInfo)5470f101656SShilei Tian int32_t DeviceTy::retrieveData(void *HstPtrBegin, void *TgtPtrBegin,
548758b8499SJohannes Doerfert int64_t Size, AsyncInfoTy &AsyncInfo) {
549df965513SJoseph Huber if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) {
5504e34f061SJohannes Doerfert HDTTMapAccessorTy HDTTMap = HostDataToTargetMap.getExclusiveAccessor();
5514e34f061SJohannes Doerfert LookupResult LR = lookupMapping(HDTTMap, HstPtrBegin, Size);
552df965513SJoseph Huber auto *HT = &*LR.Entry;
553df965513SJoseph Huber INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID,
554df965513SJoseph Huber "Copying data from device to host, TgtPtr=" DPxMOD ", HstPtr=" DPxMOD
555df965513SJoseph Huber ", Size=%" PRId64 ", Name=%s\n",
556df965513SJoseph Huber DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin), Size,
557df965513SJoseph Huber (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str()
558df965513SJoseph Huber : "unknown");
559df965513SJoseph Huber }
560df965513SJoseph Huber
561758b8499SJohannes Doerfert if (!RTL->data_retrieve_async || !RTL->synchronize)
56203ff643dSShilei Tian return RTL->data_retrieve(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size);
56303ff643dSShilei Tian return RTL->data_retrieve_async(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size,
564758b8499SJohannes Doerfert AsyncInfo);
5653029616dSJonas Hahnfeld }
5663029616dSJonas Hahnfeld
567a014fbbcSShilei Tian // Copy data from current device to destination device directly
dataExchange(void * SrcPtr,DeviceTy & DstDev,void * DstPtr,int64_t Size,AsyncInfoTy & AsyncInfo)56883c3d079SShilei Tian int32_t DeviceTy::dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,
569758b8499SJohannes Doerfert int64_t Size, AsyncInfoTy &AsyncInfo) {
57083c3d079SShilei Tian if (!AsyncInfo || !RTL->data_exchange_async || !RTL->synchronize) {
571a014fbbcSShilei Tian assert(RTL->data_exchange && "RTL->data_exchange is nullptr");
572a014fbbcSShilei Tian return RTL->data_exchange(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID, DstPtr,
573a014fbbcSShilei Tian Size);
574*d27d0a67SJoseph Huber }
575a014fbbcSShilei Tian return RTL->data_exchange_async(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID,
57683c3d079SShilei Tian DstPtr, Size, AsyncInfo);
577a014fbbcSShilei Tian }
578a014fbbcSShilei Tian
5793029616dSJonas Hahnfeld // Run region on device
runRegion(void * TgtEntryPtr,void ** TgtVarsPtr,ptrdiff_t * TgtOffsets,int32_t TgtVarsSize,AsyncInfoTy & AsyncInfo)5803ce69d4dSShilei Tian int32_t DeviceTy::runRegion(void *TgtEntryPtr, void **TgtVarsPtr,
58132ed2927SShilei Tian ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
582758b8499SJohannes Doerfert AsyncInfoTy &AsyncInfo) {
583758b8499SJohannes Doerfert if (!RTL->run_region || !RTL->synchronize)
5843029616dSJonas Hahnfeld return RTL->run_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets,
58503ff643dSShilei Tian TgtVarsSize);
586*d27d0a67SJoseph Huber return RTL->run_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets,
587*d27d0a67SJoseph Huber TgtVarsSize, AsyncInfo);
5883029616dSJonas Hahnfeld }
5893029616dSJonas Hahnfeld
590d2f85d09SJose M Monsalve Diaz // Run region on device
printDeviceInfo(int32_t RTLDevId)591d2f85d09SJose M Monsalve Diaz bool DeviceTy::printDeviceInfo(int32_t RTLDevId) {
592d2f85d09SJose M Monsalve Diaz if (!RTL->print_device_info)
593d2f85d09SJose M Monsalve Diaz return false;
594d2f85d09SJose M Monsalve Diaz RTL->print_device_info(RTLDevId);
595d2f85d09SJose M Monsalve Diaz return true;
596d2f85d09SJose M Monsalve Diaz }
597d2f85d09SJose M Monsalve Diaz
5983029616dSJonas Hahnfeld // 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,AsyncInfoTy & AsyncInfo)5993ce69d4dSShilei Tian int32_t DeviceTy::runTeamRegion(void *TgtEntryPtr, void **TgtVarsPtr,
60032ed2927SShilei Tian ptrdiff_t *TgtOffsets, int32_t TgtVarsSize,
60132ed2927SShilei Tian int32_t NumTeams, int32_t ThreadLimit,
60232ed2927SShilei Tian uint64_t LoopTripCount,
603758b8499SJohannes Doerfert AsyncInfoTy &AsyncInfo) {
604758b8499SJohannes Doerfert if (!RTL->run_team_region_async || !RTL->synchronize)
60503ff643dSShilei Tian return RTL->run_team_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
60603ff643dSShilei Tian TgtOffsets, TgtVarsSize, NumTeams, ThreadLimit,
60703ff643dSShilei Tian LoopTripCount);
60803ff643dSShilei Tian return RTL->run_team_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr,
60903ff643dSShilei Tian TgtOffsets, TgtVarsSize, NumTeams,
610758b8499SJohannes Doerfert ThreadLimit, LoopTripCount, AsyncInfo);
6113029616dSJonas Hahnfeld }
612a3b147abSJonas Hahnfeld
613a014fbbcSShilei Tian // Whether data can be copied to DstDevice directly
isDataExchangable(const DeviceTy & DstDevice)614a014fbbcSShilei Tian bool DeviceTy::isDataExchangable(const DeviceTy &DstDevice) {
615a014fbbcSShilei Tian if (RTL != DstDevice.RTL || !RTL->is_data_exchangable)
616a014fbbcSShilei Tian return false;
617a014fbbcSShilei Tian
618a014fbbcSShilei Tian if (RTL->is_data_exchangable(RTLDeviceID, DstDevice.RTLDeviceID))
619a014fbbcSShilei Tian return (RTL->data_exchange != nullptr) ||
620a014fbbcSShilei Tian (RTL->data_exchange_async != nullptr);
621a014fbbcSShilei Tian
622a014fbbcSShilei Tian return false;
623a014fbbcSShilei Tian }
624a014fbbcSShilei Tian
synchronize(AsyncInfoTy & AsyncInfo)625758b8499SJohannes Doerfert int32_t DeviceTy::synchronize(AsyncInfoTy &AsyncInfo) {
62693231666SYe Luo if (RTL->synchronize)
627758b8499SJohannes Doerfert return RTL->synchronize(RTLDeviceID, AsyncInfo);
62893231666SYe Luo return OFFLOAD_SUCCESS;
62993231666SYe Luo }
63093231666SYe Luo
createEvent(void ** Event)63129df4ab3SShilei Tian int32_t DeviceTy::createEvent(void **Event) {
63229df4ab3SShilei Tian if (RTL->create_event)
63329df4ab3SShilei Tian return RTL->create_event(RTLDeviceID, Event);
63429df4ab3SShilei Tian
63529df4ab3SShilei Tian return OFFLOAD_SUCCESS;
63629df4ab3SShilei Tian }
63729df4ab3SShilei Tian
recordEvent(void * Event,AsyncInfoTy & AsyncInfo)63829df4ab3SShilei Tian int32_t DeviceTy::recordEvent(void *Event, AsyncInfoTy &AsyncInfo) {
63929df4ab3SShilei Tian if (RTL->record_event)
64029df4ab3SShilei Tian return RTL->record_event(RTLDeviceID, Event, AsyncInfo);
64129df4ab3SShilei Tian
64229df4ab3SShilei Tian return OFFLOAD_SUCCESS;
64329df4ab3SShilei Tian }
64429df4ab3SShilei Tian
waitEvent(void * Event,AsyncInfoTy & AsyncInfo)64529df4ab3SShilei Tian int32_t DeviceTy::waitEvent(void *Event, AsyncInfoTy &AsyncInfo) {
64629df4ab3SShilei Tian if (RTL->wait_event)
64729df4ab3SShilei Tian return RTL->wait_event(RTLDeviceID, Event, AsyncInfo);
64829df4ab3SShilei Tian
64929df4ab3SShilei Tian return OFFLOAD_SUCCESS;
65029df4ab3SShilei Tian }
65129df4ab3SShilei Tian
syncEvent(void * Event)65229df4ab3SShilei Tian int32_t DeviceTy::syncEvent(void *Event) {
65329df4ab3SShilei Tian if (RTL->sync_event)
65429df4ab3SShilei Tian return RTL->sync_event(RTLDeviceID, Event);
65529df4ab3SShilei Tian
65629df4ab3SShilei Tian return OFFLOAD_SUCCESS;
65729df4ab3SShilei Tian }
65829df4ab3SShilei Tian
destroyEvent(void * Event)65929df4ab3SShilei Tian int32_t DeviceTy::destroyEvent(void *Event) {
66029df4ab3SShilei Tian if (RTL->create_event)
66129df4ab3SShilei Tian return RTL->destroy_event(RTLDeviceID, Event);
66229df4ab3SShilei Tian
66329df4ab3SShilei Tian return OFFLOAD_SUCCESS;
66429df4ab3SShilei Tian }
66529df4ab3SShilei Tian
666a3b147abSJonas Hahnfeld /// Check whether a device has an associated RTL and initialize it if it's not
667a3b147abSJonas Hahnfeld /// already initialized.
deviceIsReady(int DeviceNum)668*d27d0a67SJoseph Huber bool deviceIsReady(int DeviceNum) {
669*d27d0a67SJoseph Huber DP("Checking whether device %d is ready.\n", DeviceNum);
670a3b147abSJonas Hahnfeld // Devices.size() can only change while registering a new
671a3b147abSJonas Hahnfeld // library, so try to acquire the lock of RTLs' mutex.
672307bbd3cSJohannes Doerfert size_t DevicesSize;
673307bbd3cSJohannes Doerfert {
674307bbd3cSJohannes Doerfert std::lock_guard<decltype(PM->RTLsMtx)> LG(PM->RTLsMtx);
675307bbd3cSJohannes Doerfert DevicesSize = PM->Devices.size();
676307bbd3cSJohannes Doerfert }
677*d27d0a67SJoseph Huber if (DevicesSize <= (size_t)DeviceNum) {
678*d27d0a67SJoseph Huber DP("Device ID %d does not have a matching RTL\n", DeviceNum);
679a3b147abSJonas Hahnfeld return false;
680a3b147abSJonas Hahnfeld }
681a3b147abSJonas Hahnfeld
682a3b147abSJonas Hahnfeld // Get device info
683*d27d0a67SJoseph Huber DeviceTy &Device = *PM->Devices[DeviceNum];
684a3b147abSJonas Hahnfeld
685*d27d0a67SJoseph Huber DP("Is the device %d (local ID %d) initialized? %d\n", DeviceNum,
686a3b147abSJonas Hahnfeld Device.RTLDeviceID, Device.IsInit);
687a3b147abSJonas Hahnfeld
688a3b147abSJonas Hahnfeld // Init the device if not done before
689a3b147abSJonas Hahnfeld if (!Device.IsInit && Device.initOnce() != OFFLOAD_SUCCESS) {
690*d27d0a67SJoseph Huber DP("Failed to init device %d\n", DeviceNum);
691a3b147abSJonas Hahnfeld return false;
692a3b147abSJonas Hahnfeld }
693a3b147abSJonas Hahnfeld
694*d27d0a67SJoseph Huber DP("Device %d is ready to use.\n", DeviceNum);
695a3b147abSJonas Hahnfeld
696a3b147abSJonas Hahnfeld return true;
697a3b147abSJonas Hahnfeld }
698