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