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. The data is 289 // excepted to be mapped already, so the result will never be new. 290 TargetPointerResultTy 291 DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast, 292 bool UpdateRefCount, bool UseHoldRefCount, 293 bool &IsHostPtr, bool MustContain, bool ForceDelete) { 294 void *TargetPointer = NULL; 295 IsHostPtr = false; 296 IsLast = false; 297 DataMapMtx.lock(); 298 LookupResult lr = lookupMapping(HstPtrBegin, Size); 299 300 if (lr.Flags.IsContained || 301 (!MustContain && (lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter))) { 302 auto &HT = *lr.Entry; 303 // We do not zero the total reference count here. deallocTgtPtr does that 304 // atomically with removing the mapping. Otherwise, before this thread 305 // removed the mapping in deallocTgtPtr, another thread could retrieve the 306 // mapping, increment and decrement back to zero, and then both threads 307 // would try to remove the mapping, resulting in a double free. 308 IsLast = HT.decShouldRemove(UseHoldRefCount, ForceDelete); 309 const char *RefCountAction; 310 if (!UpdateRefCount) { 311 RefCountAction = " (update suppressed)"; 312 } else if (ForceDelete) { 313 HT.resetRefCount(UseHoldRefCount); 314 assert(IsLast == HT.decShouldRemove(UseHoldRefCount) && 315 "expected correct IsLast prediction for reset"); 316 if (IsLast) 317 RefCountAction = " (reset, deferred final decrement)"; 318 else { 319 HT.decRefCount(UseHoldRefCount); 320 RefCountAction = " (reset)"; 321 } 322 } else if (IsLast) { 323 RefCountAction = " (deferred final decrement)"; 324 } else { 325 HT.decRefCount(UseHoldRefCount); 326 RefCountAction = " (decremented)"; 327 } 328 const char *DynRefCountAction = UseHoldRefCount ? "" : RefCountAction; 329 const char *HoldRefCountAction = UseHoldRefCount ? RefCountAction : ""; 330 uintptr_t tp = HT.TgtPtrBegin + ((uintptr_t)HstPtrBegin - HT.HstPtrBegin); 331 INFO(OMP_INFOTYPE_MAPPING_EXISTS, DeviceID, 332 "Mapping exists with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD ", " 333 "Size=%" PRId64 ", DynRefCount=%s%s, HoldRefCount=%s%s\n", 334 DPxPTR(HstPtrBegin), DPxPTR(tp), Size, HT.dynRefCountToStr().c_str(), 335 DynRefCountAction, HT.holdRefCountToStr().c_str(), HoldRefCountAction); 336 TargetPointer = (void *)tp; 337 } else if (PM->RTLs.RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) { 338 // If the value isn't found in the mapping and unified shared memory 339 // is on then it means we have stumbled upon a value which we need to 340 // use directly from the host. 341 DP("Get HstPtrBegin " DPxMOD " Size=%" PRId64 " for unified shared " 342 "memory\n", 343 DPxPTR((uintptr_t)HstPtrBegin), Size); 344 IsHostPtr = true; 345 TargetPointer = HstPtrBegin; 346 } 347 348 DataMapMtx.unlock(); 349 return {{false, IsHostPtr}, lr.Entry, TargetPointer}; 350 } 351 352 // Return the target pointer begin (where the data will be moved). 353 // Lock-free version called when loading global symbols from the fat binary. 354 void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size) { 355 uintptr_t hp = (uintptr_t)HstPtrBegin; 356 LookupResult lr = lookupMapping(HstPtrBegin, Size); 357 if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) { 358 auto &HT = *lr.Entry; 359 uintptr_t tp = HT.TgtPtrBegin + (hp - HT.HstPtrBegin); 360 return (void *)tp; 361 } 362 363 return NULL; 364 } 365 366 int DeviceTy::deallocTgtPtr(void *HstPtrBegin, int64_t Size, 367 bool HasHoldModifier) { 368 // Check if the pointer is contained in any sub-nodes. 369 int rc; 370 DataMapMtx.lock(); 371 LookupResult lr = lookupMapping(HstPtrBegin, Size); 372 if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) { 373 auto &HT = *lr.Entry; 374 if (HT.decRefCount(HasHoldModifier) == 0) { 375 DP("Deleting tgt data " DPxMOD " of size %" PRId64 "\n", 376 DPxPTR(HT.TgtPtrBegin), Size); 377 deleteData((void *)HT.TgtPtrBegin); 378 INFO(OMP_INFOTYPE_MAPPING_CHANGED, DeviceID, 379 "Removing map entry with HstPtrBegin=" DPxMOD ", TgtPtrBegin=" DPxMOD 380 ", Size=%" PRId64 ", Name=%s\n", 381 DPxPTR(HT.HstPtrBegin), DPxPTR(HT.TgtPtrBegin), Size, 382 (HT.HstPtrName) ? getNameFromMapping(HT.HstPtrName).c_str() 383 : "unknown"); 384 HostDataToTargetMap.erase(lr.Entry); 385 } 386 rc = OFFLOAD_SUCCESS; 387 } else { 388 REPORT("Section to delete (hst addr " DPxMOD ") does not exist in the" 389 " allocated memory\n", 390 DPxPTR(HstPtrBegin)); 391 rc = OFFLOAD_FAIL; 392 } 393 394 DataMapMtx.unlock(); 395 return rc; 396 } 397 398 /// Init device, should not be called directly. 399 void DeviceTy::init() { 400 // Make call to init_requires if it exists for this plugin. 401 if (RTL->init_requires) 402 RTL->init_requires(PM->RTLs.RequiresFlags); 403 int32_t Ret = RTL->init_device(RTLDeviceID); 404 if (Ret != OFFLOAD_SUCCESS) 405 return; 406 407 IsInit = true; 408 } 409 410 /// Thread-safe method to initialize the device only once. 411 int32_t DeviceTy::initOnce() { 412 std::call_once(InitFlag, &DeviceTy::init, this); 413 414 // At this point, if IsInit is true, then either this thread or some other 415 // thread in the past successfully initialized the device, so we can return 416 // OFFLOAD_SUCCESS. If this thread executed init() via call_once() and it 417 // failed, return OFFLOAD_FAIL. If call_once did not invoke init(), it means 418 // that some other thread already attempted to execute init() and if IsInit 419 // is still false, return OFFLOAD_FAIL. 420 if (IsInit) 421 return OFFLOAD_SUCCESS; 422 else 423 return OFFLOAD_FAIL; 424 } 425 426 // Load binary to device. 427 __tgt_target_table *DeviceTy::load_binary(void *Img) { 428 RTL->Mtx.lock(); 429 __tgt_target_table *rc = RTL->load_binary(RTLDeviceID, Img); 430 RTL->Mtx.unlock(); 431 return rc; 432 } 433 434 void *DeviceTy::allocData(int64_t Size, void *HstPtr, int32_t Kind) { 435 return RTL->data_alloc(RTLDeviceID, Size, HstPtr, Kind); 436 } 437 438 int32_t DeviceTy::deleteData(void *TgtPtrBegin) { 439 return RTL->data_delete(RTLDeviceID, TgtPtrBegin); 440 } 441 442 // Submit data to device 443 int32_t DeviceTy::submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size, 444 AsyncInfoTy &AsyncInfo) { 445 if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) { 446 LookupResult LR = lookupMapping(HstPtrBegin, Size); 447 auto *HT = &*LR.Entry; 448 449 INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID, 450 "Copying data from host to device, HstPtr=" DPxMOD ", TgtPtr=" DPxMOD 451 ", Size=%" PRId64 ", Name=%s\n", 452 DPxPTR(HstPtrBegin), DPxPTR(TgtPtrBegin), Size, 453 (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str() 454 : "unknown"); 455 } 456 457 if (!AsyncInfo || !RTL->data_submit_async || !RTL->synchronize) 458 return RTL->data_submit(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size); 459 else 460 return RTL->data_submit_async(RTLDeviceID, TgtPtrBegin, HstPtrBegin, Size, 461 AsyncInfo); 462 } 463 464 // Retrieve data from device 465 int32_t DeviceTy::retrieveData(void *HstPtrBegin, void *TgtPtrBegin, 466 int64_t Size, AsyncInfoTy &AsyncInfo) { 467 if (getInfoLevel() & OMP_INFOTYPE_DATA_TRANSFER) { 468 LookupResult LR = lookupMapping(HstPtrBegin, Size); 469 auto *HT = &*LR.Entry; 470 INFO(OMP_INFOTYPE_DATA_TRANSFER, DeviceID, 471 "Copying data from device to host, TgtPtr=" DPxMOD ", HstPtr=" DPxMOD 472 ", Size=%" PRId64 ", Name=%s\n", 473 DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin), Size, 474 (HT && HT->HstPtrName) ? getNameFromMapping(HT->HstPtrName).c_str() 475 : "unknown"); 476 } 477 478 if (!RTL->data_retrieve_async || !RTL->synchronize) 479 return RTL->data_retrieve(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size); 480 else 481 return RTL->data_retrieve_async(RTLDeviceID, HstPtrBegin, TgtPtrBegin, Size, 482 AsyncInfo); 483 } 484 485 // Copy data from current device to destination device directly 486 int32_t DeviceTy::dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr, 487 int64_t Size, AsyncInfoTy &AsyncInfo) { 488 if (!AsyncInfo || !RTL->data_exchange_async || !RTL->synchronize) { 489 assert(RTL->data_exchange && "RTL->data_exchange is nullptr"); 490 return RTL->data_exchange(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID, DstPtr, 491 Size); 492 } else 493 return RTL->data_exchange_async(RTLDeviceID, SrcPtr, DstDev.RTLDeviceID, 494 DstPtr, Size, AsyncInfo); 495 } 496 497 // Run region on device 498 int32_t DeviceTy::runRegion(void *TgtEntryPtr, void **TgtVarsPtr, 499 ptrdiff_t *TgtOffsets, int32_t TgtVarsSize, 500 AsyncInfoTy &AsyncInfo) { 501 if (!RTL->run_region || !RTL->synchronize) 502 return RTL->run_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets, 503 TgtVarsSize); 504 else 505 return RTL->run_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, 506 TgtOffsets, TgtVarsSize, AsyncInfo); 507 } 508 509 // Run region on device 510 bool DeviceTy::printDeviceInfo(int32_t RTLDevId) { 511 if (!RTL->print_device_info) 512 return false; 513 RTL->print_device_info(RTLDevId); 514 return true; 515 } 516 517 // Run team region on device. 518 int32_t DeviceTy::runTeamRegion(void *TgtEntryPtr, void **TgtVarsPtr, 519 ptrdiff_t *TgtOffsets, int32_t TgtVarsSize, 520 int32_t NumTeams, int32_t ThreadLimit, 521 uint64_t LoopTripCount, 522 AsyncInfoTy &AsyncInfo) { 523 if (!RTL->run_team_region_async || !RTL->synchronize) 524 return RTL->run_team_region(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, 525 TgtOffsets, TgtVarsSize, NumTeams, ThreadLimit, 526 LoopTripCount); 527 else 528 return RTL->run_team_region_async(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, 529 TgtOffsets, TgtVarsSize, NumTeams, 530 ThreadLimit, LoopTripCount, AsyncInfo); 531 } 532 533 // Whether data can be copied to DstDevice directly 534 bool DeviceTy::isDataExchangable(const DeviceTy &DstDevice) { 535 if (RTL != DstDevice.RTL || !RTL->is_data_exchangable) 536 return false; 537 538 if (RTL->is_data_exchangable(RTLDeviceID, DstDevice.RTLDeviceID)) 539 return (RTL->data_exchange != nullptr) || 540 (RTL->data_exchange_async != nullptr); 541 542 return false; 543 } 544 545 int32_t DeviceTy::synchronize(AsyncInfoTy &AsyncInfo) { 546 if (RTL->synchronize) 547 return RTL->synchronize(RTLDeviceID, AsyncInfo); 548 return OFFLOAD_SUCCESS; 549 } 550 551 int32_t DeviceTy::createEvent(void **Event) { 552 if (RTL->create_event) 553 return RTL->create_event(RTLDeviceID, Event); 554 555 return OFFLOAD_SUCCESS; 556 } 557 558 int32_t DeviceTy::recordEvent(void *Event, AsyncInfoTy &AsyncInfo) { 559 if (RTL->record_event) 560 return RTL->record_event(RTLDeviceID, Event, AsyncInfo); 561 562 return OFFLOAD_SUCCESS; 563 } 564 565 int32_t DeviceTy::waitEvent(void *Event, AsyncInfoTy &AsyncInfo) { 566 if (RTL->wait_event) 567 return RTL->wait_event(RTLDeviceID, Event, AsyncInfo); 568 569 return OFFLOAD_SUCCESS; 570 } 571 572 int32_t DeviceTy::syncEvent(void *Event) { 573 if (RTL->sync_event) 574 return RTL->sync_event(RTLDeviceID, Event); 575 576 return OFFLOAD_SUCCESS; 577 } 578 579 int32_t DeviceTy::destroyEvent(void *Event) { 580 if (RTL->create_event) 581 return RTL->destroy_event(RTLDeviceID, Event); 582 583 return OFFLOAD_SUCCESS; 584 } 585 586 /// Check whether a device has an associated RTL and initialize it if it's not 587 /// already initialized. 588 bool device_is_ready(int device_num) { 589 DP("Checking whether device %d is ready.\n", device_num); 590 // Devices.size() can only change while registering a new 591 // library, so try to acquire the lock of RTLs' mutex. 592 PM->RTLsMtx.lock(); 593 size_t DevicesSize = PM->Devices.size(); 594 PM->RTLsMtx.unlock(); 595 if (DevicesSize <= (size_t)device_num) { 596 DP("Device ID %d does not have a matching RTL\n", device_num); 597 return false; 598 } 599 600 // Get device info 601 DeviceTy &Device = *PM->Devices[device_num]; 602 603 DP("Is the device %d (local ID %d) initialized? %d\n", device_num, 604 Device.RTLDeviceID, Device.IsInit); 605 606 // Init the device if not done before 607 if (!Device.IsInit && Device.initOnce() != OFFLOAD_SUCCESS) { 608 DP("Failed to init device %d\n", device_num); 609 return false; 610 } 611 612 DP("Device %d is ready to use.\n", device_num); 613 614 return true; 615 } 616