13de4cd12SGeorge Rokos //===----RTLs/cuda/src/rtl.cpp - Target RTLs Implementation ------- C++ -*-===// 23de4cd12SGeorge Rokos // 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 63de4cd12SGeorge Rokos // 73de4cd12SGeorge Rokos //===----------------------------------------------------------------------===// 83de4cd12SGeorge Rokos // 93de4cd12SGeorge Rokos // RTL for CUDA machine 103de4cd12SGeorge Rokos // 113de4cd12SGeorge Rokos //===----------------------------------------------------------------------===// 123de4cd12SGeorge Rokos 1310aa83ffSJohannes Doerfert #include <algorithm> 143de4cd12SGeorge Rokos #include <cassert> 153de4cd12SGeorge Rokos #include <cstddef> 163de4cd12SGeorge Rokos #include <cuda.h> 173de4cd12SGeorge Rokos #include <list> 18a5153dbcSJohannes Doerfert #include <memory> 19feed674dSShilei Tian #include <mutex> 203de4cd12SGeorge Rokos #include <string> 21a15f8589SJoseph Huber #include <unordered_map> 223de4cd12SGeorge Rokos #include <vector> 233de4cd12SGeorge Rokos 24ae95ceebSJoseph Huber #include "Debug.h" 250c554a47SJon Chesterfield #include "DeviceEnvironment.h" 2610aa83ffSJohannes Doerfert #include "omptarget.h" 271546d319SGeorge Rokos #include "omptargetplugin.h" 283de4cd12SGeorge Rokos 293de4cd12SGeorge Rokos #define TARGET_NAME CUDA 30ae95ceebSJoseph Huber #define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL" 3178769ec4SMichael Kruse 320871d6d5SShilei Tian #include "MemoryManager.h" 330871d6d5SShilei Tian 34ca999f71SShilei Tian #include "llvm/Frontend/OpenMP/OMPConstants.h" 35ca999f71SShilei Tian 3678769ec4SMichael Kruse // Utility for retrieving and printing CUDA error string. 37ae95ceebSJoseph Huber #ifdef OMPTARGET_DEBUG 3878769ec4SMichael Kruse #define CUDA_ERR_STRING(err) \ 3978769ec4SMichael Kruse do { \ 40ae95ceebSJoseph Huber if (getDebugLevel() > 0) { \ 4103111e5eSYe Luo const char *errStr = nullptr; \ 4203111e5eSYe Luo CUresult errStr_status = cuGetErrorString(err, &errStr); \ 4303111e5eSYe Luo if (errStr_status == CUDA_ERROR_INVALID_VALUE) \ 442ce16810SJoseph Huber REPORT("Unrecognized CUDA error code: %d\n", err); \ 4503111e5eSYe Luo else if (errStr_status == CUDA_SUCCESS) \ 462ce16810SJoseph Huber REPORT("CUDA error is: %s\n", errStr); \ 4703111e5eSYe Luo else { \ 482ce16810SJoseph Huber REPORT("Unresolved CUDA error code: %d\n", err); \ 492ce16810SJoseph Huber REPORT("Unsuccessful cuGetErrorString return status: %d\n", \ 5003111e5eSYe Luo errStr_status); \ 5103111e5eSYe Luo } \ 522ce16810SJoseph Huber } else { \ 532ce16810SJoseph Huber const char *errStr = nullptr; \ 542ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 552ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 562ce16810SJoseph Huber REPORT("%s \n", errStr); \ 5778769ec4SMichael Kruse } \ 5878769ec4SMichael Kruse } while (false) 59b305d26bSSergey Dmitriev #else // OMPTARGET_DEBUG 602ce16810SJoseph Huber #define CUDA_ERR_STRING(err) \ 612ce16810SJoseph Huber do { \ 622ce16810SJoseph Huber const char *errStr = nullptr; \ 632ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 642ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 652ce16810SJoseph Huber REPORT("%s \n", errStr); \ 662ce16810SJoseph Huber } while (false) 67b305d26bSSergey Dmitriev #endif // OMPTARGET_DEBUG 683de4cd12SGeorge Rokos 69d2f85d09SJose M Monsalve Diaz #define BOOL2TEXT(b) ((b) ? "Yes" : "No") 70d2f85d09SJose M Monsalve Diaz 71a81c68aeSShilei Tian #include "elf_common.h" 723de4cd12SGeorge Rokos 733de4cd12SGeorge Rokos /// Keep entries table per device. 743de4cd12SGeorge Rokos struct FuncOrGblEntryTy { 753de4cd12SGeorge Rokos __tgt_target_table Table; 763de4cd12SGeorge Rokos std::vector<__tgt_offload_entry> Entries; 773de4cd12SGeorge Rokos }; 783de4cd12SGeorge Rokos 79dc34b1c9SRon Lieberman /// Use a single entity to encode a kernel and a set of flags. 803de4cd12SGeorge Rokos struct KernelTy { 813de4cd12SGeorge Rokos CUfunction Func; 823de4cd12SGeorge Rokos 833de4cd12SGeorge Rokos // execution mode of kernel 84ca999f71SShilei Tian llvm::omp::OMPTgtExecModeFlags ExecutionMode; 853de4cd12SGeorge Rokos 86aa27cfc1SJohannes Doerfert /// Maximal number of threads per block for this kernel. 87aa27cfc1SJohannes Doerfert int MaxThreadsPerBlock = 0; 88aa27cfc1SJohannes Doerfert 89ca999f71SShilei Tian KernelTy(CUfunction _Func, llvm::omp::OMPTgtExecModeFlags _ExecutionMode) 903de4cd12SGeorge Rokos : Func(_Func), ExecutionMode(_ExecutionMode) {} 913de4cd12SGeorge Rokos }; 923de4cd12SGeorge Rokos 93feed674dSShilei Tian namespace { 94feed674dSShilei Tian bool checkResult(CUresult Err, const char *ErrMsg) { 95feed674dSShilei Tian if (Err == CUDA_SUCCESS) 96feed674dSShilei Tian return true; 97feed674dSShilei Tian 982ce16810SJoseph Huber REPORT("%s", ErrMsg); 99feed674dSShilei Tian CUDA_ERR_STRING(Err); 100feed674dSShilei Tian return false; 101feed674dSShilei Tian } 1024031bb98SShilei Tian 103a014fbbcSShilei Tian int memcpyDtoD(const void *SrcPtr, void *DstPtr, int64_t Size, 104a014fbbcSShilei Tian CUstream Stream) { 10595f0d1edSJon Chesterfield CUresult Err = 10695f0d1edSJon Chesterfield cuMemcpyDtoDAsync((CUdeviceptr)DstPtr, (CUdeviceptr)SrcPtr, Size, Stream); 107a014fbbcSShilei Tian 108a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10929338459SJoseph Huber DP("Error when copying data from device to device. Pointers: src " 110a014fbbcSShilei Tian "= " DPxMOD ", dst = " DPxMOD ", size = %" PRId64 "\n", 111a014fbbcSShilei Tian DPxPTR(SrcPtr), DPxPTR(DstPtr), Size); 112a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 113a014fbbcSShilei Tian return OFFLOAD_FAIL; 114a014fbbcSShilei Tian } 115a014fbbcSShilei Tian 116a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 117a014fbbcSShilei Tian } 118a014fbbcSShilei Tian 11929df4ab3SShilei Tian int recordEvent(void *EventPtr, __tgt_async_info *AsyncInfo) { 12029df4ab3SShilei Tian CUstream Stream = reinterpret_cast<CUstream>(AsyncInfo->Queue); 12129df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 12229df4ab3SShilei Tian 12329df4ab3SShilei Tian CUresult Err = cuEventRecord(Event, Stream); 12429df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 12529df4ab3SShilei Tian DP("Error when recording event. stream = " DPxMOD ", event = " DPxMOD "\n", 12629df4ab3SShilei Tian DPxPTR(Stream), DPxPTR(Event)); 12729df4ab3SShilei Tian CUDA_ERR_STRING(Err); 12829df4ab3SShilei Tian return OFFLOAD_FAIL; 12929df4ab3SShilei Tian } 13029df4ab3SShilei Tian 13129df4ab3SShilei Tian return OFFLOAD_SUCCESS; 13229df4ab3SShilei Tian } 13329df4ab3SShilei Tian 13429df4ab3SShilei Tian int syncEvent(void *EventPtr) { 13529df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 13629df4ab3SShilei Tian 13729df4ab3SShilei Tian CUresult Err = cuEventSynchronize(Event); 13829df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 13929df4ab3SShilei Tian DP("Error when syncing event = " DPxMOD "\n", DPxPTR(Event)); 14029df4ab3SShilei Tian CUDA_ERR_STRING(Err); 14129df4ab3SShilei Tian return OFFLOAD_FAIL; 14229df4ab3SShilei Tian } 14329df4ab3SShilei Tian 14429df4ab3SShilei Tian return OFFLOAD_SUCCESS; 14529df4ab3SShilei Tian } 14629df4ab3SShilei Tian 147*1660288bSJohannes Doerfert namespace { 148*1660288bSJohannes Doerfert 1494031bb98SShilei Tian // Structure contains per-device data 1504031bb98SShilei Tian struct DeviceDataTy { 1515272d29eSJohannes Doerfert /// List that contains all the kernels. 1525272d29eSJohannes Doerfert std::list<KernelTy> KernelsList; 1535272d29eSJohannes Doerfert 1544031bb98SShilei Tian std::list<FuncOrGblEntryTy> FuncGblEntries; 1555272d29eSJohannes Doerfert 1564031bb98SShilei Tian CUcontext Context = nullptr; 1574031bb98SShilei Tian // Device properties 1584031bb98SShilei Tian int ThreadsPerBlock = 0; 1594031bb98SShilei Tian int BlocksPerGrid = 0; 1604031bb98SShilei Tian int WarpSize = 0; 1614031bb98SShilei Tian // OpenMP properties 1624031bb98SShilei Tian int NumTeams = 0; 1634031bb98SShilei Tian int NumThreads = 0; 1644031bb98SShilei Tian }; 165feed674dSShilei Tian 166a697a0a4SShilei Tian /// Resource allocator where \p T is the resource type. 167a697a0a4SShilei Tian /// Functions \p create and \p destroy return OFFLOAD_SUCCESS and OFFLOAD_FAIL 168a697a0a4SShilei Tian /// accordingly. The implementation should not raise any exception. 169*1660288bSJohannes Doerfert template <typename T> struct AllocatorTy { 170*1660288bSJohannes Doerfert AllocatorTy(CUcontext C) noexcept : Context(C) {} 171*1660288bSJohannes Doerfert using ElementTy = T; 172*1660288bSJohannes Doerfert 173*1660288bSJohannes Doerfert virtual ~AllocatorTy() {} 174*1660288bSJohannes Doerfert 175a697a0a4SShilei Tian /// Create a resource and assign to R. 176*1660288bSJohannes Doerfert virtual int create(T &R) noexcept = 0; 177a697a0a4SShilei Tian /// Destroy the resource. 178*1660288bSJohannes Doerfert virtual int destroy(T) noexcept = 0; 179*1660288bSJohannes Doerfert 180*1660288bSJohannes Doerfert protected: 181*1660288bSJohannes Doerfert CUcontext Context; 182a697a0a4SShilei Tian }; 183feed674dSShilei Tian 184a697a0a4SShilei Tian /// Allocator for CUstream. 185*1660288bSJohannes Doerfert struct StreamAllocatorTy final : public AllocatorTy<CUstream> { 186*1660288bSJohannes Doerfert StreamAllocatorTy(CUcontext C) noexcept : AllocatorTy<CUstream>(C) {} 187a697a0a4SShilei Tian 188a697a0a4SShilei Tian /// See AllocatorTy<T>::create. 189*1660288bSJohannes Doerfert int create(CUstream &Stream) noexcept override { 190a697a0a4SShilei Tian if (!checkResult(cuCtxSetCurrent(Context), 191a697a0a4SShilei Tian "Error returned from cuCtxSetCurrent\n")) 192a697a0a4SShilei Tian return OFFLOAD_FAIL; 193a697a0a4SShilei Tian 194a697a0a4SShilei Tian if (!checkResult(cuStreamCreate(&Stream, CU_STREAM_NON_BLOCKING), 195a697a0a4SShilei Tian "Error returned from cuStreamCreate\n")) 196a697a0a4SShilei Tian return OFFLOAD_FAIL; 197a697a0a4SShilei Tian 198a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 199feed674dSShilei Tian } 200feed674dSShilei Tian 201a697a0a4SShilei Tian /// See AllocatorTy<T>::destroy. 202*1660288bSJohannes Doerfert int destroy(CUstream Stream) noexcept override { 203a697a0a4SShilei Tian if (!checkResult(cuCtxSetCurrent(Context), 204a697a0a4SShilei Tian "Error returned from cuCtxSetCurrent\n")) 205a697a0a4SShilei Tian return OFFLOAD_FAIL; 206a697a0a4SShilei Tian if (!checkResult(cuStreamDestroy(Stream), 207a697a0a4SShilei Tian "Error returned from cuStreamDestroy\n")) 208a697a0a4SShilei Tian return OFFLOAD_FAIL; 2094031bb98SShilei Tian 210a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 211feed674dSShilei Tian } 212a697a0a4SShilei Tian }; 213a697a0a4SShilei Tian 214943d1d83SShilei Tian /// Allocator for CUevent. 215*1660288bSJohannes Doerfert struct EventAllocatorTy final : public AllocatorTy<CUevent> { 216*1660288bSJohannes Doerfert EventAllocatorTy(CUcontext C) noexcept : AllocatorTy<CUevent>(C) {} 217*1660288bSJohannes Doerfert 218943d1d83SShilei Tian /// See AllocatorTy<T>::create. 219*1660288bSJohannes Doerfert int create(CUevent &Event) noexcept override { 220943d1d83SShilei Tian if (!checkResult(cuEventCreate(&Event, CU_EVENT_DEFAULT), 221943d1d83SShilei Tian "Error returned from cuEventCreate\n")) 222943d1d83SShilei Tian return OFFLOAD_FAIL; 223943d1d83SShilei Tian 224943d1d83SShilei Tian return OFFLOAD_SUCCESS; 225943d1d83SShilei Tian } 226943d1d83SShilei Tian 227943d1d83SShilei Tian /// See AllocatorTy<T>::destroy. 228*1660288bSJohannes Doerfert int destroy(CUevent Event) noexcept override { 229943d1d83SShilei Tian if (!checkResult(cuEventDestroy(Event), 230943d1d83SShilei Tian "Error returned from cuEventDestroy\n")) 231943d1d83SShilei Tian return OFFLOAD_FAIL; 232943d1d83SShilei Tian 233943d1d83SShilei Tian return OFFLOAD_SUCCESS; 234943d1d83SShilei Tian } 235943d1d83SShilei Tian }; 236943d1d83SShilei Tian 237a697a0a4SShilei Tian /// A generic pool of resources where \p T is the resource type. 238a697a0a4SShilei Tian /// \p T should be copyable as the object is stored in \p std::vector . 239*1660288bSJohannes Doerfert template <typename AllocTy> class ResourcePoolTy { 240*1660288bSJohannes Doerfert using ElementTy = typename AllocTy::ElementTy; 241a697a0a4SShilei Tian /// Index of the next available resource. 242a697a0a4SShilei Tian size_t Next = 0; 243a697a0a4SShilei Tian /// Mutex to guard the pool. 244a697a0a4SShilei Tian std::mutex Mutex; 245a697a0a4SShilei Tian /// Pool of resources. 246*1660288bSJohannes Doerfert std::vector<ElementTy> Resources; 247a697a0a4SShilei Tian /// A reference to the corresponding allocator. 248*1660288bSJohannes Doerfert AllocTy Allocator; 249a697a0a4SShilei Tian 250a697a0a4SShilei Tian /// If `Resources` is used up, we will fill in more resources. It assumes that 251a697a0a4SShilei Tian /// the new size `Size` should be always larger than the current size. 252a697a0a4SShilei Tian bool resize(size_t Size) { 253a697a0a4SShilei Tian auto CurSize = Resources.size(); 254a697a0a4SShilei Tian assert(Size > CurSize && "Unexpected smaller size"); 255a697a0a4SShilei Tian Resources.reserve(Size); 256a697a0a4SShilei Tian for (auto I = CurSize; I < Size; ++I) { 257*1660288bSJohannes Doerfert ElementTy NewItem; 258a697a0a4SShilei Tian int Ret = Allocator.create(NewItem); 259a697a0a4SShilei Tian if (Ret != OFFLOAD_SUCCESS) 260a697a0a4SShilei Tian return false; 261a697a0a4SShilei Tian Resources.push_back(NewItem); 262a697a0a4SShilei Tian } 263a697a0a4SShilei Tian return true; 264feed674dSShilei Tian } 265feed674dSShilei Tian 266feed674dSShilei Tian public: 267*1660288bSJohannes Doerfert ResourcePoolTy(AllocTy &&A, size_t Size = 0) noexcept 268a697a0a4SShilei Tian : Allocator(std::move(A)) { 269357c8031SShilei Tian if (Size) 270a697a0a4SShilei Tian (void)resize(Size); 271feed674dSShilei Tian } 272feed674dSShilei Tian 273357c8031SShilei Tian ~ResourcePoolTy() noexcept { clear(); } 274feed674dSShilei Tian 275a697a0a4SShilei Tian /// Get a resource from pool. `Next` always points to the next available 276a697a0a4SShilei Tian /// resource. That means, `[0, next-1]` have been assigned, and `[id,]` are 277a697a0a4SShilei Tian /// still available. If there is no resource left, we will ask for more. Each 278a697a0a4SShilei Tian /// time a resource is assigned, the id will increase one. 279a697a0a4SShilei Tian /// xxxxxs+++++++++ 280a697a0a4SShilei Tian /// ^ 281a697a0a4SShilei Tian /// Next 282a697a0a4SShilei Tian /// After assignment, the pool becomes the following and s is assigned. 283a697a0a4SShilei Tian /// xxxxxs+++++++++ 284a697a0a4SShilei Tian /// ^ 285a697a0a4SShilei Tian /// Next 286*1660288bSJohannes Doerfert int acquire(ElementTy &R) noexcept { 287a697a0a4SShilei Tian std::lock_guard<std::mutex> LG(Mutex); 288357c8031SShilei Tian if (Next == Resources.size()) { 289357c8031SShilei Tian auto NewSize = Resources.size() ? Resources.size() * 2 : 1; 290357c8031SShilei Tian if (!resize(NewSize)) 291a697a0a4SShilei Tian return OFFLOAD_FAIL; 292357c8031SShilei Tian } 293357c8031SShilei Tian 294357c8031SShilei Tian assert(Next < Resources.size()); 295a697a0a4SShilei Tian 296a697a0a4SShilei Tian R = Resources[Next++]; 297a697a0a4SShilei Tian 298a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 299feed674dSShilei Tian } 300feed674dSShilei Tian 301a697a0a4SShilei Tian /// Return the resource back to the pool. When we return a resource, we need 302a697a0a4SShilei Tian /// to first decrease `Next`, and then copy the resource back. It is worth 303a697a0a4SShilei Tian /// noting that, the order of resources return might be different from that 304a697a0a4SShilei Tian /// they're assigned, that saying, at some point, there might be two identical 305a697a0a4SShilei Tian /// resources. 306a697a0a4SShilei Tian /// xxax+a+++++ 307a697a0a4SShilei Tian /// ^ 308a697a0a4SShilei Tian /// Next 309a697a0a4SShilei Tian /// However, it doesn't matter, because they're always on the two sides of 310a697a0a4SShilei Tian /// `Next`. The left one will in the end be overwritten by another resource. 311a697a0a4SShilei Tian /// Therefore, after several execution, the order of pool might be different 312a697a0a4SShilei Tian /// from its initial state. 313*1660288bSJohannes Doerfert void release(ElementTy R) noexcept { 314a697a0a4SShilei Tian std::lock_guard<std::mutex> LG(Mutex); 315a697a0a4SShilei Tian Resources[--Next] = R; 316feed674dSShilei Tian } 317357c8031SShilei Tian 318357c8031SShilei Tian /// Released all stored resources and clear the pool. 319357c8031SShilei Tian /// Note: This function is not thread safe. Be sure to guard it if necessary. 320357c8031SShilei Tian void clear() noexcept { 321357c8031SShilei Tian for (auto &R : Resources) 322357c8031SShilei Tian (void)Allocator.destroy(R); 323357c8031SShilei Tian Resources.clear(); 324357c8031SShilei Tian } 325feed674dSShilei Tian }; 326feed674dSShilei Tian 327*1660288bSJohannes Doerfert } // namespace 328*1660288bSJohannes Doerfert 3294031bb98SShilei Tian class DeviceRTLTy { 3303de4cd12SGeorge Rokos int NumberOfDevices; 3314031bb98SShilei Tian // OpenMP environment properties 3323de4cd12SGeorge Rokos int EnvNumTeams; 3333de4cd12SGeorge Rokos int EnvTeamLimit; 334422adaa8SJoseph Huber int EnvTeamThreadLimit; 3354031bb98SShilei Tian // OpenMP requires flags 336c5fe030cSGheorghe-Teodor Bercea int64_t RequiresFlags; 337f1c821faSJoseph Huber // Amount of dynamic shared memory to use at launch. 338f1c821faSJoseph Huber uint64_t DynamicMemorySize; 339*1660288bSJohannes Doerfert 340*1660288bSJohannes Doerfert /// Number of initial streams for each device. 341a697a0a4SShilei Tian int NumInitialStreams = 32; 342c5fe030cSGheorghe-Teodor Bercea 343*1660288bSJohannes Doerfert /// Number of initial events for each device. 344*1660288bSJohannes Doerfert int NumInitialEvents = 8; 345*1660288bSJohannes Doerfert 346aca33b0bSShilei Tian static constexpr const int32_t HardThreadLimit = 1024; 347aca33b0bSShilei Tian static constexpr const int32_t DefaultNumTeams = 128; 348aca33b0bSShilei Tian static constexpr const int32_t DefaultNumThreads = 128; 3493de4cd12SGeorge Rokos 350*1660288bSJohannes Doerfert using StreamPoolTy = ResourcePoolTy<StreamAllocatorTy>; 351a697a0a4SShilei Tian std::vector<std::unique_ptr<StreamPoolTy>> StreamPool; 352a697a0a4SShilei Tian 353*1660288bSJohannes Doerfert using EventPoolTy = ResourcePoolTy<EventAllocatorTy>; 354*1660288bSJohannes Doerfert std::vector<std::unique_ptr<EventPoolTy>> EventPool; 355943d1d83SShilei Tian 3564031bb98SShilei Tian std::vector<DeviceDataTy> DeviceData; 3574031bb98SShilei Tian std::vector<CUmodule> Modules; 358feed674dSShilei Tian 35910aa83ffSJohannes Doerfert /// Vector of flags indicating the initalization status of all associated 36010aa83ffSJohannes Doerfert /// devices. 36110aa83ffSJohannes Doerfert std::vector<bool> InitializedFlags; 36210aa83ffSJohannes Doerfert 3630871d6d5SShilei Tian /// A class responsible for interacting with device native runtime library to 3640871d6d5SShilei Tian /// allocate and free memory. 3650871d6d5SShilei Tian class CUDADeviceAllocatorTy : public DeviceAllocatorTy { 3660871d6d5SShilei Tian const int DeviceId; 3670871d6d5SShilei Tian const std::vector<DeviceDataTy> &DeviceData; 368a15f8589SJoseph Huber std::unordered_map<void *, TargetAllocTy> HostPinnedAllocs; 3690871d6d5SShilei Tian 3700871d6d5SShilei Tian public: 3710871d6d5SShilei Tian CUDADeviceAllocatorTy(int DeviceId, std::vector<DeviceDataTy> &DeviceData) 3720871d6d5SShilei Tian : DeviceId(DeviceId), DeviceData(DeviceData) {} 3730871d6d5SShilei Tian 374a15f8589SJoseph Huber void *allocate(size_t Size, void *, TargetAllocTy Kind) override { 3750871d6d5SShilei Tian if (Size == 0) 3760871d6d5SShilei Tian return nullptr; 3770871d6d5SShilei Tian 3780871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 3790871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 3800871d6d5SShilei Tian return nullptr; 3810871d6d5SShilei Tian 382a15f8589SJoseph Huber void *MemAlloc = nullptr; 383a15f8589SJoseph Huber switch (Kind) { 384a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 385a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 3860871d6d5SShilei Tian CUdeviceptr DevicePtr; 38795f0d1edSJon Chesterfield Err = cuMemAlloc(&DevicePtr, Size); 388a15f8589SJoseph Huber MemAlloc = (void *)DevicePtr; 38995f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemAlloc\n")) 3900871d6d5SShilei Tian return nullptr; 391a15f8589SJoseph Huber break; 392a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 393a15f8589SJoseph Huber void *HostPtr; 394a15f8589SJoseph Huber Err = cuMemAllocHost(&HostPtr, Size); 395a15f8589SJoseph Huber MemAlloc = HostPtr; 396a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocHost\n")) 397a15f8589SJoseph Huber return nullptr; 398a15f8589SJoseph Huber HostPinnedAllocs[MemAlloc] = Kind; 399a15f8589SJoseph Huber break; 400a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 401a15f8589SJoseph Huber CUdeviceptr SharedPtr; 402a15f8589SJoseph Huber Err = cuMemAllocManaged(&SharedPtr, Size, CU_MEM_ATTACH_GLOBAL); 403a15f8589SJoseph Huber MemAlloc = (void *)SharedPtr; 404a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocManaged\n")) 405a15f8589SJoseph Huber return nullptr; 406a15f8589SJoseph Huber break; 407a15f8589SJoseph Huber } 4080871d6d5SShilei Tian 409a15f8589SJoseph Huber return MemAlloc; 4100871d6d5SShilei Tian } 4110871d6d5SShilei Tian 4120871d6d5SShilei Tian int free(void *TgtPtr) override { 4130871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 4140871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 4150871d6d5SShilei Tian return OFFLOAD_FAIL; 4160871d6d5SShilei Tian 417a15f8589SJoseph Huber // Host pinned memory must be freed differently. 418a15f8589SJoseph Huber TargetAllocTy Kind = 419a15f8589SJoseph Huber (HostPinnedAllocs.find(TgtPtr) == HostPinnedAllocs.end()) 420a15f8589SJoseph Huber ? TARGET_ALLOC_DEFAULT 421a15f8589SJoseph Huber : TARGET_ALLOC_HOST; 422a15f8589SJoseph Huber switch (Kind) { 423a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 424a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 425a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 42695f0d1edSJon Chesterfield Err = cuMemFree((CUdeviceptr)TgtPtr); 42795f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemFree\n")) 4280871d6d5SShilei Tian return OFFLOAD_FAIL; 429a15f8589SJoseph Huber break; 430a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 431a15f8589SJoseph Huber Err = cuMemFreeHost(TgtPtr); 432a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemFreeHost\n")) 433a15f8589SJoseph Huber return OFFLOAD_FAIL; 434a15f8589SJoseph Huber break; 435a15f8589SJoseph Huber } 4360871d6d5SShilei Tian 4370871d6d5SShilei Tian return OFFLOAD_SUCCESS; 4380871d6d5SShilei Tian } 4390871d6d5SShilei Tian }; 4400871d6d5SShilei Tian 4410871d6d5SShilei Tian /// A vector of device allocators 4420871d6d5SShilei Tian std::vector<CUDADeviceAllocatorTy> DeviceAllocators; 4430871d6d5SShilei Tian 4440871d6d5SShilei Tian /// A vector of memory managers. Since the memory manager is non-copyable and 4450871d6d5SShilei Tian // non-removable, we wrap them into std::unique_ptr. 4460871d6d5SShilei Tian std::vector<std::unique_ptr<MemoryManagerTy>> MemoryManagers; 4470871d6d5SShilei Tian 4480871d6d5SShilei Tian /// Whether use memory manager 4490871d6d5SShilei Tian bool UseMemoryManager = true; 4500871d6d5SShilei Tian 4513de4cd12SGeorge Rokos // Record entry point associated with device 4524031bb98SShilei Tian void addOffloadEntry(const int DeviceId, const __tgt_offload_entry entry) { 4534031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4543de4cd12SGeorge Rokos E.Entries.push_back(entry); 4553de4cd12SGeorge Rokos } 4563de4cd12SGeorge Rokos 457ae209397SJoseph Huber // Return a pointer to the entry associated with the pointer 458ae209397SJoseph Huber const __tgt_offload_entry *getOffloadEntry(const int DeviceId, 459ae209397SJoseph Huber const void *Addr) const { 4604031bb98SShilei Tian for (const __tgt_offload_entry &Itr : 4614031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.back().Entries) 4624031bb98SShilei Tian if (Itr.addr == Addr) 463ae209397SJoseph Huber return &Itr; 4643de4cd12SGeorge Rokos 465ae209397SJoseph Huber return nullptr; 4663de4cd12SGeorge Rokos } 4673de4cd12SGeorge Rokos 4683de4cd12SGeorge Rokos // Return the pointer to the target entries table 4694031bb98SShilei Tian __tgt_target_table *getOffloadEntriesTable(const int DeviceId) { 4704031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4713de4cd12SGeorge Rokos 4724031bb98SShilei Tian if (E.Entries.empty()) 4734031bb98SShilei Tian return nullptr; 4743de4cd12SGeorge Rokos 4753de4cd12SGeorge Rokos // Update table info according to the entries and return the pointer 4764031bb98SShilei Tian E.Table.EntriesBegin = E.Entries.data(); 4774031bb98SShilei Tian E.Table.EntriesEnd = E.Entries.data() + E.Entries.size(); 4783de4cd12SGeorge Rokos 4793de4cd12SGeorge Rokos return &E.Table; 4803de4cd12SGeorge Rokos } 4813de4cd12SGeorge Rokos 4823de4cd12SGeorge Rokos // Clear entries table for a device 4834031bb98SShilei Tian void clearOffloadEntriesTable(const int DeviceId) { 4844031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.emplace_back(); 4854031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4863de4cd12SGeorge Rokos E.Entries.clear(); 4874031bb98SShilei Tian E.Table.EntriesBegin = E.Table.EntriesEnd = nullptr; 4883de4cd12SGeorge Rokos } 4893de4cd12SGeorge Rokos 490f44e41afSSri Hari Krishna Narayanan public: 4915449fbb5SJohannes Doerfert CUstream getStream(const int DeviceId, __tgt_async_info *AsyncInfo) const { 4925449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 4934031bb98SShilei Tian 494a697a0a4SShilei Tian if (!AsyncInfo->Queue) { 495a697a0a4SShilei Tian CUstream S; 496a697a0a4SShilei Tian if (StreamPool[DeviceId]->acquire(S) != OFFLOAD_SUCCESS) 497a697a0a4SShilei Tian return nullptr; 498a697a0a4SShilei Tian 499a697a0a4SShilei Tian AsyncInfo->Queue = S; 500a697a0a4SShilei Tian } 5014031bb98SShilei Tian 5025449fbb5SJohannes Doerfert return reinterpret_cast<CUstream>(AsyncInfo->Queue); 503b305d26bSSergey Dmitriev } 5044031bb98SShilei Tian 5054031bb98SShilei Tian // This class should not be copied 5064031bb98SShilei Tian DeviceRTLTy(const DeviceRTLTy &) = delete; 5074031bb98SShilei Tian DeviceRTLTy(DeviceRTLTy &&) = delete; 5084031bb98SShilei Tian 5094031bb98SShilei Tian DeviceRTLTy() 5104031bb98SShilei Tian : NumberOfDevices(0), EnvNumTeams(-1), EnvTeamLimit(-1), 511f1c821faSJoseph Huber EnvTeamThreadLimit(-1), RequiresFlags(OMP_REQ_UNDEFINED), 512*1660288bSJohannes Doerfert DynamicMemorySize(0) { 513b305d26bSSergey Dmitriev 5143de4cd12SGeorge Rokos DP("Start initializing CUDA\n"); 5153de4cd12SGeorge Rokos 5164031bb98SShilei Tian CUresult Err = cuInit(0); 5177baff00eSJon Chesterfield if (Err == CUDA_ERROR_INVALID_HANDLE) { 5187baff00eSJon Chesterfield // Can't call cuGetErrorString if dlsym failed 5197baff00eSJon Chesterfield DP("Failed to load CUDA shared library\n"); 5207baff00eSJon Chesterfield return; 5217baff00eSJon Chesterfield } 5224031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuInit\n")) { 5233de4cd12SGeorge Rokos return; 5243de4cd12SGeorge Rokos } 5253de4cd12SGeorge Rokos 5264031bb98SShilei Tian Err = cuDeviceGetCount(&NumberOfDevices); 5274031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGetCount\n")) 5283de4cd12SGeorge Rokos return; 5293de4cd12SGeorge Rokos 5303de4cd12SGeorge Rokos if (NumberOfDevices == 0) { 5313de4cd12SGeorge Rokos DP("There are no devices supporting CUDA.\n"); 5323de4cd12SGeorge Rokos return; 5333de4cd12SGeorge Rokos } 5343de4cd12SGeorge Rokos 5354031bb98SShilei Tian DeviceData.resize(NumberOfDevices); 536a697a0a4SShilei Tian StreamPool.resize(NumberOfDevices); 537*1660288bSJohannes Doerfert EventPool.resize(NumberOfDevices); 5383de4cd12SGeorge Rokos 5393de4cd12SGeorge Rokos // Get environment variables regarding teams 5404031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_TEAM_LIMIT")) { 5413de4cd12SGeorge Rokos // OMP_TEAM_LIMIT has been set 5424031bb98SShilei Tian EnvTeamLimit = std::stoi(EnvStr); 5433de4cd12SGeorge Rokos DP("Parsed OMP_TEAM_LIMIT=%d\n", EnvTeamLimit); 5443de4cd12SGeorge Rokos } 545422adaa8SJoseph Huber if (const char *EnvStr = getenv("OMP_TEAMS_THREAD_LIMIT")) { 546422adaa8SJoseph Huber // OMP_TEAMS_THREAD_LIMIT has been set 547422adaa8SJoseph Huber EnvTeamThreadLimit = std::stoi(EnvStr); 548422adaa8SJoseph Huber DP("Parsed OMP_TEAMS_THREAD_LIMIT=%d\n", EnvTeamThreadLimit); 549422adaa8SJoseph Huber } 5504031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_NUM_TEAMS")) { 5513de4cd12SGeorge Rokos // OMP_NUM_TEAMS has been set 5524031bb98SShilei Tian EnvNumTeams = std::stoi(EnvStr); 5533de4cd12SGeorge Rokos DP("Parsed OMP_NUM_TEAMS=%d\n", EnvNumTeams); 5543de4cd12SGeorge Rokos } 555f1c821faSJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_SHARED_MEMORY_SIZE")) { 556f1c821faSJoseph Huber // LIBOMPTARGET_SHARED_MEMORY_SIZE has been set 557f1c821faSJoseph Huber DynamicMemorySize = std::stoi(EnvStr); 55849e976c9SShilei Tian DP("Parsed LIBOMPTARGET_SHARED_MEMORY_SIZE = %" PRIu64 "\n", 55949e976c9SShilei Tian DynamicMemorySize); 560f1c821faSJoseph Huber } 561a697a0a4SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_NUM_INITIAL_STREAMS")) { 562a697a0a4SShilei Tian // LIBOMPTARGET_NUM_INITIAL_STREAMS has been set 563a697a0a4SShilei Tian NumInitialStreams = std::stoi(EnvStr); 564a697a0a4SShilei Tian DP("Parsed LIBOMPTARGET_NUM_INITIAL_STREAMS=%d\n", NumInitialStreams); 565a697a0a4SShilei Tian } 5660871d6d5SShilei Tian 5670871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 5680871d6d5SShilei Tian DeviceAllocators.emplace_back(I, DeviceData); 5690871d6d5SShilei Tian 5700871d6d5SShilei Tian // Get the size threshold from environment variable 5710871d6d5SShilei Tian std::pair<size_t, bool> Res = MemoryManagerTy::getSizeThresholdFromEnv(); 5720871d6d5SShilei Tian UseMemoryManager = Res.second; 5730871d6d5SShilei Tian size_t MemoryManagerThreshold = Res.first; 5740871d6d5SShilei Tian 5750871d6d5SShilei Tian if (UseMemoryManager) 5760871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 5770871d6d5SShilei Tian MemoryManagers.emplace_back(std::make_unique<MemoryManagerTy>( 5780871d6d5SShilei Tian DeviceAllocators[I], MemoryManagerThreshold)); 57910aa83ffSJohannes Doerfert 58010aa83ffSJohannes Doerfert // We lazily initialize all devices later. 58110aa83ffSJohannes Doerfert InitializedFlags.assign(NumberOfDevices, false); 5823de4cd12SGeorge Rokos } 5833de4cd12SGeorge Rokos 5844031bb98SShilei Tian ~DeviceRTLTy() { 58510aa83ffSJohannes Doerfert for (int DeviceId = 0; DeviceId < NumberOfDevices; ++DeviceId) 58610aa83ffSJohannes Doerfert deinitDevice(DeviceId); 5873de4cd12SGeorge Rokos } 5883de4cd12SGeorge Rokos 5894031bb98SShilei Tian // Check whether a given DeviceId is valid 5904031bb98SShilei Tian bool isValidDeviceId(const int DeviceId) const { 5914031bb98SShilei Tian return DeviceId >= 0 && DeviceId < NumberOfDevices; 59232ed2927SShilei Tian } 59332ed2927SShilei Tian 594cb038927SShilei Tian int getNumOfDevices() const { return NumberOfDevices; } 5954031bb98SShilei Tian 5964031bb98SShilei Tian void setRequiresFlag(const int64_t Flags) { this->RequiresFlags = Flags; } 5974031bb98SShilei Tian 5984031bb98SShilei Tian int initDevice(const int DeviceId) { 5994031bb98SShilei Tian CUdevice Device; 6004031bb98SShilei Tian 6014031bb98SShilei Tian DP("Getting device %d\n", DeviceId); 6024031bb98SShilei Tian CUresult Err = cuDeviceGet(&Device, DeviceId); 6034031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGet\n")) 60432ed2927SShilei Tian return OFFLOAD_FAIL; 6053de4cd12SGeorge Rokos 60610aa83ffSJohannes Doerfert assert(InitializedFlags[DeviceId] == false && "Reinitializing device!"); 60710aa83ffSJohannes Doerfert InitializedFlags[DeviceId] = true; 60810aa83ffSJohannes Doerfert 609c5348aecSYe Luo // Query the current flags of the primary context and set its flags if 610c5348aecSYe Luo // it is inactive 611c5348aecSYe Luo unsigned int FormerPrimaryCtxFlags = 0; 612c5348aecSYe Luo int FormerPrimaryCtxIsActive = 0; 613c5348aecSYe Luo Err = cuDevicePrimaryCtxGetState(Device, &FormerPrimaryCtxFlags, 614c5348aecSYe Luo &FormerPrimaryCtxIsActive); 615c5348aecSYe Luo if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxGetState\n")) 616c5348aecSYe Luo return OFFLOAD_FAIL; 617c5348aecSYe Luo 618c5348aecSYe Luo if (FormerPrimaryCtxIsActive) { 619c5348aecSYe Luo DP("The primary context is active, no change to its flags\n"); 620c5348aecSYe Luo if ((FormerPrimaryCtxFlags & CU_CTX_SCHED_MASK) != 621c5348aecSYe Luo CU_CTX_SCHED_BLOCKING_SYNC) 622c5348aecSYe Luo DP("Warning the current flags are not CU_CTX_SCHED_BLOCKING_SYNC\n"); 623c5348aecSYe Luo } else { 624c5348aecSYe Luo DP("The primary context is inactive, set its flags to " 625c5348aecSYe Luo "CU_CTX_SCHED_BLOCKING_SYNC\n"); 62695f0d1edSJon Chesterfield Err = cuDevicePrimaryCtxSetFlags(Device, CU_CTX_SCHED_BLOCKING_SYNC); 62795f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxSetFlags\n")) 628c5348aecSYe Luo return OFFLOAD_FAIL; 629c5348aecSYe Luo } 630c5348aecSYe Luo 631c5348aecSYe Luo // Retain the per device primary context and save it to use whenever this 632c5348aecSYe Luo // device is selected. 633c5348aecSYe Luo Err = cuDevicePrimaryCtxRetain(&DeviceData[DeviceId].Context, Device); 634c5348aecSYe Luo if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxRetain\n")) 6353de4cd12SGeorge Rokos return OFFLOAD_FAIL; 6363de4cd12SGeorge Rokos 6374031bb98SShilei Tian Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 6384031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 6394031bb98SShilei Tian return OFFLOAD_FAIL; 640a5153dbcSJohannes Doerfert 641*1660288bSJohannes Doerfert // Initialize the stream pool. 642a697a0a4SShilei Tian if (!StreamPool[DeviceId]) 643a697a0a4SShilei Tian StreamPool[DeviceId] = std::make_unique<StreamPoolTy>( 644*1660288bSJohannes Doerfert StreamAllocatorTy(DeviceData[DeviceId].Context), NumInitialStreams); 645*1660288bSJohannes Doerfert 646*1660288bSJohannes Doerfert // Initialize the event pool. 647*1660288bSJohannes Doerfert if (!EventPool[DeviceId]) 648*1660288bSJohannes Doerfert EventPool[DeviceId] = std::make_unique<EventPoolTy>( 649*1660288bSJohannes Doerfert EventAllocatorTy(DeviceData[DeviceId].Context), NumInitialEvents); 650a5153dbcSJohannes Doerfert 651bb51d398SJonas Hahnfeld // Query attributes to determine number of threads/block and blocks/grid. 6524031bb98SShilei Tian int MaxGridDimX; 6534031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxGridDimX, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, 6544031bb98SShilei Tian Device); 6554031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6564031bb98SShilei Tian DP("Error getting max grid dimension, use default value %d\n", 6574031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 6584031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = DeviceRTLTy::DefaultNumTeams; 659aca33b0bSShilei Tian } else { 6604031bb98SShilei Tian DP("Using %d CUDA blocks per grid\n", MaxGridDimX); 6614031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = MaxGridDimX; 6623de4cd12SGeorge Rokos } 6633de4cd12SGeorge Rokos 664bb51d398SJonas Hahnfeld // We are only exploiting threads along the x axis. 6654031bb98SShilei Tian int MaxBlockDimX; 6664031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxBlockDimX, 6674031bb98SShilei Tian CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, Device); 6684031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6694031bb98SShilei Tian DP("Error getting max block dimension, use default value %d\n", 6704031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 6714031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::DefaultNumThreads; 672422adaa8SJoseph Huber } else { 6734031bb98SShilei Tian DP("Using %d CUDA threads per block\n", MaxBlockDimX); 6744031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = MaxBlockDimX; 675422adaa8SJoseph Huber 676422adaa8SJoseph Huber if (EnvTeamThreadLimit > 0 && 677422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock > EnvTeamThreadLimit) { 678422adaa8SJoseph Huber DP("Max CUDA threads per block %d exceeds the thread limit %d set by " 679422adaa8SJoseph Huber "OMP_TEAMS_THREAD_LIMIT, capping at the limit\n", 680422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, EnvTeamThreadLimit); 681422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock = EnvTeamThreadLimit; 682422adaa8SJoseph Huber } 683422adaa8SJoseph Huber if (DeviceData[DeviceId].ThreadsPerBlock > DeviceRTLTy::HardThreadLimit) { 6844031bb98SShilei Tian DP("Max CUDA threads per block %d exceeds the hard thread limit %d, " 6854031bb98SShilei Tian "capping at the hard limit\n", 686422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceRTLTy::HardThreadLimit); 6874031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::HardThreadLimit; 6883de4cd12SGeorge Rokos } 689422adaa8SJoseph Huber } 6903de4cd12SGeorge Rokos 6914031bb98SShilei Tian // Get and set warp size 6924031bb98SShilei Tian int WarpSize; 6934031bb98SShilei Tian Err = 6944031bb98SShilei Tian cuDeviceGetAttribute(&WarpSize, CU_DEVICE_ATTRIBUTE_WARP_SIZE, Device); 6954031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6964031bb98SShilei Tian DP("Error getting warp size, assume default value 32\n"); 6974031bb98SShilei Tian DeviceData[DeviceId].WarpSize = 32; 698bb51d398SJonas Hahnfeld } else { 6994031bb98SShilei Tian DP("Using warp size %d\n", WarpSize); 7004031bb98SShilei Tian DeviceData[DeviceId].WarpSize = WarpSize; 7013de4cd12SGeorge Rokos } 7023de4cd12SGeorge Rokos 7033de4cd12SGeorge Rokos // Adjust teams to the env variables 7044031bb98SShilei Tian if (EnvTeamLimit > 0 && DeviceData[DeviceId].BlocksPerGrid > EnvTeamLimit) { 7053de4cd12SGeorge Rokos DP("Capping max CUDA blocks per grid to OMP_TEAM_LIMIT=%d\n", 7064031bb98SShilei Tian EnvTeamLimit); 7074031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = EnvTeamLimit; 7083de4cd12SGeorge Rokos } 7093de4cd12SGeorge Rokos 7103817ba13SJoseph Huber size_t StackLimit; 7113817ba13SJoseph Huber size_t HeapLimit; 7123817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_STACK_SIZE")) { 7133817ba13SJoseph Huber StackLimit = std::stol(EnvStr); 7143817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_STACK_SIZE, StackLimit) != CUDA_SUCCESS) 7153817ba13SJoseph Huber return OFFLOAD_FAIL; 7163817ba13SJoseph Huber } else { 7173817ba13SJoseph Huber if (cuCtxGetLimit(&StackLimit, CU_LIMIT_STACK_SIZE) != CUDA_SUCCESS) 7183817ba13SJoseph Huber return OFFLOAD_FAIL; 7193817ba13SJoseph Huber } 7203817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_HEAP_SIZE")) { 7213817ba13SJoseph Huber HeapLimit = std::stol(EnvStr); 7223817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_MALLOC_HEAP_SIZE, HeapLimit) != CUDA_SUCCESS) 7233817ba13SJoseph Huber return OFFLOAD_FAIL; 7243817ba13SJoseph Huber } else { 7253817ba13SJoseph Huber if (cuCtxGetLimit(&HeapLimit, CU_LIMIT_MALLOC_HEAP_SIZE) != CUDA_SUCCESS) 7263817ba13SJoseph Huber return OFFLOAD_FAIL; 7273817ba13SJoseph Huber } 7283817ba13SJoseph Huber 729119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 730ae209397SJoseph Huber "Device supports up to %d CUDA blocks and %d threads with a " 731ae209397SJoseph Huber "warp size of %d\n", 732ae209397SJoseph Huber DeviceData[DeviceId].BlocksPerGrid, 733ae209397SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceData[DeviceId].WarpSize); 7343817ba13SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 7353817ba13SJoseph Huber "Device heap size is %d Bytes, device stack size is %d Bytes per " 7363817ba13SJoseph Huber "thread\n", 7373817ba13SJoseph Huber (int)HeapLimit, (int)StackLimit); 7383de4cd12SGeorge Rokos 7393de4cd12SGeorge Rokos // Set default number of teams 7404031bb98SShilei Tian if (EnvNumTeams > 0) { 7413de4cd12SGeorge Rokos DP("Default number of teams set according to environment %d\n", 7424031bb98SShilei Tian EnvNumTeams); 7434031bb98SShilei Tian DeviceData[DeviceId].NumTeams = EnvNumTeams; 7443de4cd12SGeorge Rokos } else { 7454031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceRTLTy::DefaultNumTeams; 7463de4cd12SGeorge Rokos DP("Default number of teams set according to library's default %d\n", 7474031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 7483de4cd12SGeorge Rokos } 7494031bb98SShilei Tian 7504031bb98SShilei Tian if (DeviceData[DeviceId].NumTeams > DeviceData[DeviceId].BlocksPerGrid) { 7513de4cd12SGeorge Rokos DP("Default number of teams exceeds device limit, capping at %d\n", 7524031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 7534031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceData[DeviceId].BlocksPerGrid; 7543de4cd12SGeorge Rokos } 7553de4cd12SGeorge Rokos 7563de4cd12SGeorge Rokos // Set default number of threads 7574031bb98SShilei Tian DeviceData[DeviceId].NumThreads = DeviceRTLTy::DefaultNumThreads; 7583de4cd12SGeorge Rokos DP("Default number of threads set according to library's default %d\n", 7594031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 7604031bb98SShilei Tian if (DeviceData[DeviceId].NumThreads > 7614031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock) { 7623de4cd12SGeorge Rokos DP("Default number of threads exceeds device limit, capping at %d\n", 7634031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 764c060c634SShilei Tian DeviceData[DeviceId].NumThreads = DeviceData[DeviceId].ThreadsPerBlock; 7653de4cd12SGeorge Rokos } 7663de4cd12SGeorge Rokos 7673de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 7683de4cd12SGeorge Rokos } 7693de4cd12SGeorge Rokos 77010aa83ffSJohannes Doerfert int deinitDevice(const int DeviceId) { 77110aa83ffSJohannes Doerfert auto IsInitialized = InitializedFlags[DeviceId]; 77210aa83ffSJohannes Doerfert if (!IsInitialized) 77310aa83ffSJohannes Doerfert return OFFLOAD_SUCCESS; 77410aa83ffSJohannes Doerfert InitializedFlags[DeviceId] = false; 77510aa83ffSJohannes Doerfert 77610aa83ffSJohannes Doerfert if (UseMemoryManager) 77710aa83ffSJohannes Doerfert MemoryManagers[DeviceId].release(); 77810aa83ffSJohannes Doerfert 77910aa83ffSJohannes Doerfert // Close module 78010aa83ffSJohannes Doerfert if (CUmodule &M = Modules[DeviceId]) 78110aa83ffSJohannes Doerfert checkResult(cuModuleUnload(M), "Error returned from cuModuleUnload\n"); 78210aa83ffSJohannes Doerfert 78310aa83ffSJohannes Doerfert StreamPool[DeviceId].reset(); 784*1660288bSJohannes Doerfert EventPool[DeviceId].reset(); 78510aa83ffSJohannes Doerfert 78610aa83ffSJohannes Doerfert // Destroy context 78710aa83ffSJohannes Doerfert DeviceDataTy &D = DeviceData[DeviceId]; 78810aa83ffSJohannes Doerfert if (D.Context) { 78910aa83ffSJohannes Doerfert if (checkResult(cuCtxSetCurrent(D.Context), 79010aa83ffSJohannes Doerfert "Error returned from cuCtxSetCurrent\n")) { 79110aa83ffSJohannes Doerfert CUdevice Device; 79210aa83ffSJohannes Doerfert if (checkResult(cuCtxGetDevice(&Device), 79310aa83ffSJohannes Doerfert "Error returned from cuCtxGetDevice\n")) 79410aa83ffSJohannes Doerfert checkResult(cuDevicePrimaryCtxRelease(Device), 79510aa83ffSJohannes Doerfert "Error returned from cuDevicePrimaryCtxRelease\n"); 79610aa83ffSJohannes Doerfert } 79710aa83ffSJohannes Doerfert } 79810aa83ffSJohannes Doerfert return OFFLOAD_SUCCESS; 79910aa83ffSJohannes Doerfert } 80010aa83ffSJohannes Doerfert 8014031bb98SShilei Tian __tgt_target_table *loadBinary(const int DeviceId, 8024031bb98SShilei Tian const __tgt_device_image *Image) { 8034031bb98SShilei Tian // Set the context we are using 8044031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 8054031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 8064031bb98SShilei Tian return nullptr; 8073de4cd12SGeorge Rokos 8083de4cd12SGeorge Rokos // Clear the offload table as we are going to create a new one. 8094031bb98SShilei Tian clearOffloadEntriesTable(DeviceId); 8103de4cd12SGeorge Rokos 8113de4cd12SGeorge Rokos // Create the module and extract the function pointers. 8124031bb98SShilei Tian CUmodule Module; 8134031bb98SShilei Tian DP("Load data from image " DPxMOD "\n", DPxPTR(Image->ImageStart)); 8144031bb98SShilei Tian Err = cuModuleLoadDataEx(&Module, Image->ImageStart, 0, nullptr, nullptr); 8154031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuModuleLoadDataEx\n")) 8164031bb98SShilei Tian return nullptr; 8173de4cd12SGeorge Rokos 8183de4cd12SGeorge Rokos DP("CUDA module successfully loaded!\n"); 8194031bb98SShilei Tian 8204031bb98SShilei Tian Modules.push_back(Module); 8213de4cd12SGeorge Rokos 8223de4cd12SGeorge Rokos // Find the symbols in the module by name. 8234031bb98SShilei Tian const __tgt_offload_entry *HostBegin = Image->EntriesBegin; 8244031bb98SShilei Tian const __tgt_offload_entry *HostEnd = Image->EntriesEnd; 8253de4cd12SGeorge Rokos 8265272d29eSJohannes Doerfert std::list<KernelTy> &KernelsList = DeviceData[DeviceId].KernelsList; 8274031bb98SShilei Tian for (const __tgt_offload_entry *E = HostBegin; E != HostEnd; ++E) { 8284031bb98SShilei Tian if (!E->addr) { 8294031bb98SShilei Tian // We return nullptr when something like this happens, the host should 8304031bb98SShilei Tian // have always something in the address to uniquely identify the target 8314031bb98SShilei Tian // region. 8324031bb98SShilei Tian DP("Invalid binary: host entry '<null>' (size = %zd)...\n", E->size); 8334031bb98SShilei Tian return nullptr; 8343de4cd12SGeorge Rokos } 8353de4cd12SGeorge Rokos 8364031bb98SShilei Tian if (E->size) { 8374031bb98SShilei Tian __tgt_offload_entry Entry = *E; 8384031bb98SShilei Tian CUdeviceptr CUPtr; 8394031bb98SShilei Tian size_t CUSize; 84095f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&CUPtr, &CUSize, Module, E->name); 8414031bb98SShilei Tian // We keep this style here because we need the name 8424031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8432ce16810SJoseph Huber REPORT("Loading global '%s' Failed\n", E->name); 8444031bb98SShilei Tian CUDA_ERR_STRING(Err); 8454031bb98SShilei Tian return nullptr; 8463de4cd12SGeorge Rokos } 8473de4cd12SGeorge Rokos 8484031bb98SShilei Tian if (CUSize != E->size) { 8494031bb98SShilei Tian DP("Loading global '%s' - size mismatch (%zd != %zd)\n", E->name, 8504031bb98SShilei Tian CUSize, E->size); 8514031bb98SShilei Tian return nullptr; 8523de4cd12SGeorge Rokos } 8533de4cd12SGeorge Rokos 8543de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to global %s (" DPxMOD ")\n", 8554031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(CUPtr)); 8564031bb98SShilei Tian 8574031bb98SShilei Tian Entry.addr = (void *)(CUPtr); 8583de4cd12SGeorge Rokos 859aace6d28SGheorghe-Teodor Bercea // Note: In the current implementation declare target variables 860aace6d28SGheorghe-Teodor Bercea // can either be link or to. This means that once unified 861aace6d28SGheorghe-Teodor Bercea // memory is activated via the requires directive, the variable 862aace6d28SGheorghe-Teodor Bercea // can be used directly from the host in both cases. 863aace6d28SGheorghe-Teodor Bercea // TODO: when variables types other than to or link are added, 8644c6a098aSKazuaki Ishizaki // the below condition should be changed to explicitly 865aace6d28SGheorghe-Teodor Bercea // check for to and link variables types: 8664031bb98SShilei Tian // (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && (e->flags & 8674031bb98SShilei Tian // OMP_DECLARE_TARGET_LINK || e->flags == OMP_DECLARE_TARGET_TO)) 8684031bb98SShilei Tian if (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) { 869aace6d28SGheorghe-Teodor Bercea // If unified memory is present any target link or to variables 870c5fe030cSGheorghe-Teodor Bercea // can access host addresses directly. There is no longer a 871c5fe030cSGheorghe-Teodor Bercea // need for device copies. 87295f0d1edSJon Chesterfield cuMemcpyHtoD(CUPtr, E->addr, sizeof(void *)); 8734031bb98SShilei Tian DP("Copy linked variable host address (" DPxMOD 8744031bb98SShilei Tian ") to device address (" DPxMOD ")\n", 8754031bb98SShilei Tian DPxPTR(*((void **)E->addr)), DPxPTR(CUPtr)); 876c5fe030cSGheorghe-Teodor Bercea } 877c5fe030cSGheorghe-Teodor Bercea 8784031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 8793de4cd12SGeorge Rokos 8803de4cd12SGeorge Rokos continue; 8813de4cd12SGeorge Rokos } 8823de4cd12SGeorge Rokos 8834031bb98SShilei Tian CUfunction Func; 8844031bb98SShilei Tian Err = cuModuleGetFunction(&Func, Module, E->name); 8854031bb98SShilei Tian // We keep this style here because we need the name 8864031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8872ce16810SJoseph Huber REPORT("Loading '%s' Failed\n", E->name); 8884031bb98SShilei Tian CUDA_ERR_STRING(Err); 8894031bb98SShilei Tian return nullptr; 8903de4cd12SGeorge Rokos } 8913de4cd12SGeorge Rokos 8923de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to %s (" DPxMOD ")\n", 8934031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(Func)); 8943de4cd12SGeorge Rokos 8953de4cd12SGeorge Rokos // default value GENERIC (in case symbol is missing from cubin file) 896ca999f71SShilei Tian llvm::omp::OMPTgtExecModeFlags ExecModeVal; 8974031bb98SShilei Tian std::string ExecModeNameStr(E->name); 8983de4cd12SGeorge Rokos ExecModeNameStr += "_exec_mode"; 8993de4cd12SGeorge Rokos const char *ExecModeName = ExecModeNameStr.c_str(); 9003de4cd12SGeorge Rokos 9013de4cd12SGeorge Rokos CUdeviceptr ExecModePtr; 9024031bb98SShilei Tian size_t CUSize; 90395f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&ExecModePtr, &CUSize, Module, ExecModeName); 9044031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 905ca999f71SShilei Tian if (CUSize != sizeof(llvm::omp::OMPTgtExecModeFlags)) { 9063de4cd12SGeorge Rokos DP("Loading global exec_mode '%s' - size mismatch (%zd != %zd)\n", 907ca999f71SShilei Tian ExecModeName, CUSize, sizeof(llvm::omp::OMPTgtExecModeFlags)); 9084031bb98SShilei Tian return nullptr; 9093de4cd12SGeorge Rokos } 9103de4cd12SGeorge Rokos 91195f0d1edSJon Chesterfield Err = cuMemcpyDtoH(&ExecModeVal, ExecModePtr, CUSize); 9124031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 9132ce16810SJoseph Huber REPORT("Error when copying data from device to host. Pointers: " 9143de4cd12SGeorge Rokos "host = " DPxMOD ", device = " DPxMOD ", size = %zd\n", 9154031bb98SShilei Tian DPxPTR(&ExecModeVal), DPxPTR(ExecModePtr), CUSize); 9164031bb98SShilei Tian CUDA_ERR_STRING(Err); 9174031bb98SShilei Tian return nullptr; 9183de4cd12SGeorge Rokos } 9193de4cd12SGeorge Rokos } else { 920edb8acdcSJoseph Huber DP("Loading global exec_mode '%s' - symbol missing, using default " 9214031bb98SShilei Tian "value GENERIC (1)\n", 9224031bb98SShilei Tian ExecModeName); 9233de4cd12SGeorge Rokos } 9243de4cd12SGeorge Rokos 9254031bb98SShilei Tian KernelsList.emplace_back(Func, ExecModeVal); 9263de4cd12SGeorge Rokos 9274031bb98SShilei Tian __tgt_offload_entry Entry = *E; 9284031bb98SShilei Tian Entry.addr = &KernelsList.back(); 9294031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 9303de4cd12SGeorge Rokos } 9313de4cd12SGeorge Rokos 932e1c7a46dSGuansong Zhang // send device environment data to the device 933e1c7a46dSGuansong Zhang { 934ec02c34bSJoseph Huber // TODO: The device ID used here is not the real device ID used by OpenMP. 9350c554a47SJon Chesterfield DeviceEnvironmentTy DeviceEnv{0, static_cast<uint32_t>(NumberOfDevices), 9360c554a47SJon Chesterfield static_cast<uint32_t>(DeviceId), 9370c554a47SJon Chesterfield static_cast<uint32_t>(DynamicMemorySize)}; 938e1c7a46dSGuansong Zhang 9394031bb98SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_DEVICE_RTL_DEBUG")) 9400c554a47SJon Chesterfield DeviceEnv.DebugKind = std::stoi(EnvStr); 941e1c7a46dSGuansong Zhang 9424031bb98SShilei Tian const char *DeviceEnvName = "omptarget_device_environment"; 9434031bb98SShilei Tian CUdeviceptr DeviceEnvPtr; 9444031bb98SShilei Tian size_t CUSize; 945e1c7a46dSGuansong Zhang 94695f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&DeviceEnvPtr, &CUSize, Module, DeviceEnvName); 9474031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 9484031bb98SShilei Tian if (CUSize != sizeof(DeviceEnv)) { 9492ce16810SJoseph Huber REPORT( 9502ce16810SJoseph Huber "Global device_environment '%s' - size mismatch (%zu != %zu)\n", 9514031bb98SShilei Tian DeviceEnvName, CUSize, sizeof(int32_t)); 9524031bb98SShilei Tian CUDA_ERR_STRING(Err); 9534031bb98SShilei Tian return nullptr; 954e1c7a46dSGuansong Zhang } 955e1c7a46dSGuansong Zhang 95695f0d1edSJon Chesterfield Err = cuMemcpyHtoD(DeviceEnvPtr, &DeviceEnv, CUSize); 9574031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 9582ce16810SJoseph Huber REPORT("Error when copying data from host to device. Pointers: " 959e1c7a46dSGuansong Zhang "host = " DPxMOD ", device = " DPxMOD ", size = %zu\n", 9604031bb98SShilei Tian DPxPTR(&DeviceEnv), DPxPTR(DeviceEnvPtr), CUSize); 9614031bb98SShilei Tian CUDA_ERR_STRING(Err); 9624031bb98SShilei Tian return nullptr; 963e1c7a46dSGuansong Zhang } 964e1c7a46dSGuansong Zhang 9654031bb98SShilei Tian DP("Sending global device environment data %zu bytes\n", CUSize); 966e1c7a46dSGuansong Zhang } else { 9674031bb98SShilei Tian DP("Finding global device environment '%s' - symbol missing.\n", 9684031bb98SShilei Tian DeviceEnvName); 9694031bb98SShilei Tian DP("Continue, considering this is a device RTL which does not accept " 9704031bb98SShilei Tian "environment setting.\n"); 971e1c7a46dSGuansong Zhang } 972e1c7a46dSGuansong Zhang } 973e1c7a46dSGuansong Zhang 9744031bb98SShilei Tian return getOffloadEntriesTable(DeviceId); 9753de4cd12SGeorge Rokos } 9763de4cd12SGeorge Rokos 977a15f8589SJoseph Huber void *dataAlloc(const int DeviceId, const int64_t Size, 978a15f8589SJoseph Huber const TargetAllocTy Kind) { 979a15f8589SJoseph Huber switch (Kind) { 980a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 981a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 9820871d6d5SShilei Tian if (UseMemoryManager) 9830871d6d5SShilei Tian return MemoryManagers[DeviceId]->allocate(Size, nullptr); 984a15f8589SJoseph Huber else 985a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 986a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 987a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 988a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 989a15f8589SJoseph Huber } 9904031bb98SShilei Tian 991a15f8589SJoseph Huber REPORT("Invalid target data allocation kind or requested allocator not " 992a15f8589SJoseph Huber "implemented yet\n"); 993a15f8589SJoseph Huber 994a15f8589SJoseph Huber return nullptr; 9953de4cd12SGeorge Rokos } 9963de4cd12SGeorge Rokos 9974031bb98SShilei Tian int dataSubmit(const int DeviceId, const void *TgtPtr, const void *HstPtr, 9985449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 9995449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 10003de4cd12SGeorge Rokos 10014031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 10024031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 10033de4cd12SGeorge Rokos return OFFLOAD_FAIL; 10043de4cd12SGeorge Rokos 10055449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 10063de4cd12SGeorge Rokos 100795f0d1edSJon Chesterfield Err = cuMemcpyHtoDAsync((CUdeviceptr)TgtPtr, HstPtr, Size, Stream); 10084031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 100929338459SJoseph Huber DP("Error when copying data from host to device. Pointers: host " 10102ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 10114031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 10124031bb98SShilei Tian CUDA_ERR_STRING(Err); 10133de4cd12SGeorge Rokos return OFFLOAD_FAIL; 10143de4cd12SGeorge Rokos } 10153de4cd12SGeorge Rokos 10163de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 10173de4cd12SGeorge Rokos } 10183de4cd12SGeorge Rokos 10194031bb98SShilei Tian int dataRetrieve(const int DeviceId, void *HstPtr, const void *TgtPtr, 10205449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 10215449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 10224031bb98SShilei Tian 10234031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 10244031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 102503ff643dSShilei Tian return OFFLOAD_FAIL; 102603ff643dSShilei Tian 10275449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 102803ff643dSShilei Tian 102995f0d1edSJon Chesterfield Err = cuMemcpyDtoHAsync(HstPtr, (CUdeviceptr)TgtPtr, Size, Stream); 10304031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 103129338459SJoseph Huber DP("Error when copying data from device to host. Pointers: host " 10322ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 10334031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 10344031bb98SShilei Tian CUDA_ERR_STRING(Err); 10353de4cd12SGeorge Rokos return OFFLOAD_FAIL; 10363de4cd12SGeorge Rokos } 10373de4cd12SGeorge Rokos 10384031bb98SShilei Tian return OFFLOAD_SUCCESS; 10394031bb98SShilei Tian } 10404031bb98SShilei Tian 1041a014fbbcSShilei Tian int dataExchange(int SrcDevId, const void *SrcPtr, int DstDevId, void *DstPtr, 10425449fbb5SJohannes Doerfert int64_t Size, __tgt_async_info *AsyncInfo) const { 10435449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 1044a014fbbcSShilei Tian 1045a014fbbcSShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[SrcDevId].Context); 1046a014fbbcSShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 1047a014fbbcSShilei Tian return OFFLOAD_FAIL; 1048a014fbbcSShilei Tian 10495449fbb5SJohannes Doerfert CUstream Stream = getStream(SrcDevId, AsyncInfo); 1050a014fbbcSShilei Tian 1051a014fbbcSShilei Tian // If they are two devices, we try peer to peer copy first 1052a014fbbcSShilei Tian if (SrcDevId != DstDevId) { 1053a014fbbcSShilei Tian int CanAccessPeer = 0; 1054a014fbbcSShilei Tian Err = cuDeviceCanAccessPeer(&CanAccessPeer, SrcDevId, DstDevId); 1055a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10562ce16810SJoseph Huber REPORT("Error returned from cuDeviceCanAccessPeer. src = %" PRId32 1057a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 1058a014fbbcSShilei Tian SrcDevId, DstDevId); 1059a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1060a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1061a014fbbcSShilei Tian } 1062a014fbbcSShilei Tian 1063a014fbbcSShilei Tian if (!CanAccessPeer) { 1064a014fbbcSShilei Tian DP("P2P memcpy not supported so fall back to D2D memcpy"); 1065a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1066a014fbbcSShilei Tian } 1067a014fbbcSShilei Tian 1068a014fbbcSShilei Tian Err = cuCtxEnablePeerAccess(DeviceData[DstDevId].Context, 0); 1069a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10702ce16810SJoseph Huber REPORT("Error returned from cuCtxEnablePeerAccess. src = %" PRId32 1071a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 1072a014fbbcSShilei Tian SrcDevId, DstDevId); 1073a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1074a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1075a014fbbcSShilei Tian } 1076a014fbbcSShilei Tian 1077a014fbbcSShilei Tian Err = cuMemcpyPeerAsync((CUdeviceptr)DstPtr, DeviceData[DstDevId].Context, 1078a014fbbcSShilei Tian (CUdeviceptr)SrcPtr, DeviceData[SrcDevId].Context, 1079a014fbbcSShilei Tian Size, Stream); 1080a014fbbcSShilei Tian if (Err == CUDA_SUCCESS) 1081a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 1082a014fbbcSShilei Tian 108329338459SJoseph Huber DP("Error returned from cuMemcpyPeerAsync. src_ptr = " DPxMOD 108429338459SJoseph Huber ", src_id =%" PRId32 ", dst_ptr = " DPxMOD ", dst_id =%" PRId32 "\n", 108545bb073dSYe Luo DPxPTR(SrcPtr), SrcDevId, DPxPTR(DstPtr), DstDevId); 1086a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1087a014fbbcSShilei Tian } 1088a014fbbcSShilei Tian 1089a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1090a014fbbcSShilei Tian } 1091a014fbbcSShilei Tian 10920871d6d5SShilei Tian int dataDelete(const int DeviceId, void *TgtPtr) { 10930871d6d5SShilei Tian if (UseMemoryManager) 10940871d6d5SShilei Tian return MemoryManagers[DeviceId]->free(TgtPtr); 10954031bb98SShilei Tian 10960871d6d5SShilei Tian return DeviceAllocators[DeviceId].free(TgtPtr); 10974031bb98SShilei Tian } 10984031bb98SShilei Tian 1099aa27cfc1SJohannes Doerfert int runTargetTeamRegion(const int DeviceId, void *TgtEntryPtr, void **TgtArgs, 1100aa27cfc1SJohannes Doerfert ptrdiff_t *TgtOffsets, const int ArgNum, 1101aa27cfc1SJohannes Doerfert const int TeamNum, const int ThreadLimit, 11024031bb98SShilei Tian const unsigned int LoopTripCount, 11034031bb98SShilei Tian __tgt_async_info *AsyncInfo) const { 11044031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 11054031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 11064031bb98SShilei Tian return OFFLOAD_FAIL; 11074031bb98SShilei Tian 11083de4cd12SGeorge Rokos // All args are references. 11094031bb98SShilei Tian std::vector<void *> Args(ArgNum); 11104031bb98SShilei Tian std::vector<void *> Ptrs(ArgNum); 11113de4cd12SGeorge Rokos 11124031bb98SShilei Tian for (int I = 0; I < ArgNum; ++I) { 11134031bb98SShilei Tian Ptrs[I] = (void *)((intptr_t)TgtArgs[I] + TgtOffsets[I]); 11144031bb98SShilei Tian Args[I] = &Ptrs[I]; 11151546d319SGeorge Rokos } 11163de4cd12SGeorge Rokos 1117aa27cfc1SJohannes Doerfert KernelTy *KernelInfo = reinterpret_cast<KernelTy *>(TgtEntryPtr); 11183de4cd12SGeorge Rokos 1119ca999f71SShilei Tian const bool IsSPMDGenericMode = 1120ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_GENERIC_SPMD; 1121ca999f71SShilei Tian const bool IsSPMDMode = 1122ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_SPMD; 1123ca999f71SShilei Tian const bool IsGenericMode = 1124ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_GENERIC; 1125ca999f71SShilei Tian 1126aa27cfc1SJohannes Doerfert int CudaThreadsPerBlock; 11274031bb98SShilei Tian if (ThreadLimit > 0) { 11284031bb98SShilei Tian DP("Setting CUDA threads per block to requested %d\n", ThreadLimit); 11294031bb98SShilei Tian CudaThreadsPerBlock = ThreadLimit; 11303de4cd12SGeorge Rokos // Add master warp if necessary 1131ca999f71SShilei Tian if (IsGenericMode) { 11324031bb98SShilei Tian DP("Adding master warp: +%d threads\n", DeviceData[DeviceId].WarpSize); 11334031bb98SShilei Tian CudaThreadsPerBlock += DeviceData[DeviceId].WarpSize; 11343de4cd12SGeorge Rokos } 1135c13df8e5SGeorge Rokos } else { 1136c13df8e5SGeorge Rokos DP("Setting CUDA threads per block to default %d\n", 11374031bb98SShilei Tian DeviceData[DeviceId].NumThreads); 11384031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].NumThreads; 1139c13df8e5SGeorge Rokos } 11403de4cd12SGeorge Rokos 11414031bb98SShilei Tian if (CudaThreadsPerBlock > DeviceData[DeviceId].ThreadsPerBlock) { 11423de4cd12SGeorge Rokos DP("Threads per block capped at device limit %d\n", 11434031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 11444031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].ThreadsPerBlock; 11453de4cd12SGeorge Rokos } 11463de4cd12SGeorge Rokos 1147aa27cfc1SJohannes Doerfert if (!KernelInfo->MaxThreadsPerBlock) { 1148aa27cfc1SJohannes Doerfert Err = cuFuncGetAttribute(&KernelInfo->MaxThreadsPerBlock, 11494031bb98SShilei Tian CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, 11504031bb98SShilei Tian KernelInfo->Func); 1151aa27cfc1SJohannes Doerfert if (!checkResult(Err, "Error returned from cuFuncGetAttribute\n")) 1152aa27cfc1SJohannes Doerfert return OFFLOAD_FAIL; 1153aa27cfc1SJohannes Doerfert } 1154aa27cfc1SJohannes Doerfert 1155aa27cfc1SJohannes Doerfert if (KernelInfo->MaxThreadsPerBlock < CudaThreadsPerBlock) { 1156aa27cfc1SJohannes Doerfert DP("Threads per block capped at kernel limit %d\n", 1157aa27cfc1SJohannes Doerfert KernelInfo->MaxThreadsPerBlock); 1158aa27cfc1SJohannes Doerfert CudaThreadsPerBlock = KernelInfo->MaxThreadsPerBlock; 11593de4cd12SGeorge Rokos } 11603de4cd12SGeorge Rokos 11614031bb98SShilei Tian unsigned int CudaBlocksPerGrid; 11624031bb98SShilei Tian if (TeamNum <= 0) { 11634031bb98SShilei Tian if (LoopTripCount > 0 && EnvNumTeams < 0) { 1164ca999f71SShilei Tian if (IsSPMDGenericMode) { 1165ca999f71SShilei Tian // If we reach this point, then we are executing a kernel that was 1166ca999f71SShilei Tian // transformed from Generic-mode to SPMD-mode. This kernel has 1167ca999f71SShilei Tian // SPMD-mode execution, but needs its blocks to be scheduled 1168ca999f71SShilei Tian // differently because the current loop trip count only applies to the 1169ca999f71SShilei Tian // `teams distribute` region and will create var too few blocks using 1170ca999f71SShilei Tian // the regular SPMD-mode method. 1171ca999f71SShilei Tian CudaBlocksPerGrid = LoopTripCount; 1172ca999f71SShilei Tian } else if (IsSPMDMode) { 11734031bb98SShilei Tian // We have a combined construct, i.e. `target teams distribute 11744031bb98SShilei Tian // parallel for [simd]`. We launch so many teams so that each thread 11754031bb98SShilei Tian // will execute one iteration of the loop. round up to the nearest 11764031bb98SShilei Tian // integer 11774031bb98SShilei Tian CudaBlocksPerGrid = ((LoopTripCount - 1) / CudaThreadsPerBlock) + 1; 1178ca999f71SShilei Tian } else if (IsGenericMode) { 1179c13df8e5SGeorge Rokos // If we reach this point, then we have a non-combined construct, i.e. 1180c13df8e5SGeorge Rokos // `teams distribute` with a nested `parallel for` and each team is 1181c13df8e5SGeorge Rokos // assigned one iteration of the `distribute` loop. E.g.: 1182c13df8e5SGeorge Rokos // 1183c13df8e5SGeorge Rokos // #pragma omp target teams distribute 1184c13df8e5SGeorge Rokos // for(...loop_tripcount...) { 1185c13df8e5SGeorge Rokos // #pragma omp parallel for 1186c13df8e5SGeorge Rokos // for(...) {} 1187c13df8e5SGeorge Rokos // } 1188c13df8e5SGeorge Rokos // 1189c13df8e5SGeorge Rokos // Threads within a team will execute the iterations of the `parallel` 1190c13df8e5SGeorge Rokos // loop. 11914031bb98SShilei Tian CudaBlocksPerGrid = LoopTripCount; 1192a158d366SJoseph Huber } else { 1193ca999f71SShilei Tian REPORT("Unknown execution mode: %d\n", 1194ca999f71SShilei Tian static_cast<int8_t>(KernelInfo->ExecutionMode)); 1195a158d366SJoseph Huber return OFFLOAD_FAIL; 1196c13df8e5SGeorge Rokos } 119745bb073dSYe Luo DP("Using %d teams due to loop trip count %" PRIu32 11984031bb98SShilei Tian " and number of threads per block %d\n", 11994031bb98SShilei Tian CudaBlocksPerGrid, LoopTripCount, CudaThreadsPerBlock); 12003de4cd12SGeorge Rokos } else { 12014031bb98SShilei Tian DP("Using default number of teams %d\n", DeviceData[DeviceId].NumTeams); 12024031bb98SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].NumTeams; 12033de4cd12SGeorge Rokos } 12043de4cd12SGeorge Rokos } else { 12054031bb98SShilei Tian DP("Using requested number of teams %d\n", TeamNum); 12064031bb98SShilei Tian CudaBlocksPerGrid = TeamNum; 12073de4cd12SGeorge Rokos } 12083de4cd12SGeorge Rokos 1209f6685f77SShilei Tian if (CudaBlocksPerGrid > DeviceData[DeviceId].BlocksPerGrid) { 1210f6685f77SShilei Tian DP("Capping number of teams to team limit %d\n", 1211f6685f77SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 1212f6685f77SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].BlocksPerGrid; 1213f6685f77SShilei Tian } 1214f6685f77SShilei Tian 1215119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 1216ca999f71SShilei Tian "Launching kernel %s with %d blocks and %d threads in %s mode\n", 1217ae209397SJoseph Huber (getOffloadEntry(DeviceId, TgtEntryPtr)) 1218ae209397SJoseph Huber ? getOffloadEntry(DeviceId, TgtEntryPtr)->name 1219ae209397SJoseph Huber : "(null)", 1220ae209397SJoseph Huber CudaBlocksPerGrid, CudaThreadsPerBlock, 1221ca999f71SShilei Tian (!IsSPMDMode ? (IsGenericMode ? "Generic" : "SPMD-Generic") : "SPMD")); 12223de4cd12SGeorge Rokos 12234031bb98SShilei Tian CUstream Stream = getStream(DeviceId, AsyncInfo); 12244031bb98SShilei Tian Err = cuLaunchKernel(KernelInfo->Func, CudaBlocksPerGrid, /* gridDimY */ 1, 12254031bb98SShilei Tian /* gridDimZ */ 1, CudaThreadsPerBlock, 12264031bb98SShilei Tian /* blockDimY */ 1, /* blockDimZ */ 1, 1227f1c821faSJoseph Huber DynamicMemorySize, Stream, &Args[0], nullptr); 12284031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuLaunchKernel\n")) 12294031bb98SShilei Tian return OFFLOAD_FAIL; 12304031bb98SShilei Tian 12314031bb98SShilei Tian DP("Launch of entry point at " DPxMOD " successful!\n", 12324031bb98SShilei Tian DPxPTR(TgtEntryPtr)); 12334031bb98SShilei Tian 12344031bb98SShilei Tian return OFFLOAD_SUCCESS; 12354031bb98SShilei Tian } 12364031bb98SShilei Tian 12375449fbb5SJohannes Doerfert int synchronize(const int DeviceId, __tgt_async_info *AsyncInfo) const { 12385449fbb5SJohannes Doerfert CUstream Stream = reinterpret_cast<CUstream>(AsyncInfo->Queue); 12394031bb98SShilei Tian CUresult Err = cuStreamSynchronize(Stream); 12403de4cd12SGeorge Rokos 12414031bb98SShilei Tian // Once the stream is synchronized, return it to stream pool and reset 12425449fbb5SJohannes Doerfert // AsyncInfo. This is to make sure the synchronization only works for its 12434031bb98SShilei Tian // own tasks. 1244a697a0a4SShilei Tian StreamPool[DeviceId]->release(reinterpret_cast<CUstream>(AsyncInfo->Queue)); 12455449fbb5SJohannes Doerfert AsyncInfo->Queue = nullptr; 12463de4cd12SGeorge Rokos 124716064e71SJoseph Huber if (Err != CUDA_SUCCESS) { 124829338459SJoseph Huber DP("Error when synchronizing stream. stream = " DPxMOD 124916064e71SJoseph Huber ", async info ptr = " DPxMOD "\n", 125016064e71SJoseph Huber DPxPTR(Stream), DPxPTR(AsyncInfo)); 125116064e71SJoseph Huber CUDA_ERR_STRING(Err); 125216064e71SJoseph Huber } 125316064e71SJoseph Huber return (Err == CUDA_SUCCESS) ? OFFLOAD_SUCCESS : OFFLOAD_FAIL; 12543de4cd12SGeorge Rokos } 1255d2f85d09SJose M Monsalve Diaz 1256d2f85d09SJose M Monsalve Diaz void printDeviceInfo(int32_t device_id) { 1257d2f85d09SJose M Monsalve Diaz char TmpChar[1000]; 1258d2f85d09SJose M Monsalve Diaz std::string TmpStr; 1259d2f85d09SJose M Monsalve Diaz size_t TmpSt; 1260d2f85d09SJose M Monsalve Diaz int TmpInt, TmpInt2, TmpInt3; 1261d2f85d09SJose M Monsalve Diaz 1262d2f85d09SJose M Monsalve Diaz CUdevice Device; 1263d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGet(&Device, device_id), 1264d2f85d09SJose M Monsalve Diaz "Error returned from cuCtxGetDevice\n"); 1265d2f85d09SJose M Monsalve Diaz 1266d2f85d09SJose M Monsalve Diaz cuDriverGetVersion(&TmpInt); 1267d2f85d09SJose M Monsalve Diaz printf(" CUDA Driver Version: \t\t%d \n", TmpInt); 1268d2f85d09SJose M Monsalve Diaz printf(" CUDA Device Number: \t\t%d \n", device_id); 1269d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetName(TmpChar, 1000, Device), 1270d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetName\n"); 1271d2f85d09SJose M Monsalve Diaz printf(" Device Name: \t\t\t%s \n", TmpChar); 1272d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceTotalMem(&TmpSt, Device), 1273d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceTotalMem\n"); 1274d2f85d09SJose M Monsalve Diaz printf(" Global Memory Size: \t\t%zu bytes \n", TmpSt); 1275d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1276d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, Device), 1277d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1278d2f85d09SJose M Monsalve Diaz printf(" Number of Multiprocessors: \t\t%d \n", TmpInt); 1279d2f85d09SJose M Monsalve Diaz checkResult( 1280d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_GPU_OVERLAP, Device), 1281d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1282d2f85d09SJose M Monsalve Diaz printf(" Concurrent Copy and Execution: \t%s \n", BOOL2TEXT(TmpInt)); 1283d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1284d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_TOTAL_CONSTANT_MEMORY, Device), 1285d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1286d2f85d09SJose M Monsalve Diaz printf(" Total Constant Memory: \t\t%d bytes\n", TmpInt); 1287d2f85d09SJose M Monsalve Diaz checkResult( 1288d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1289d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK, Device), 1290d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1291d2f85d09SJose M Monsalve Diaz printf(" Max Shared Memory per Block: \t%d bytes \n", TmpInt); 129228939b6aSShilei Tian checkResult( 129328939b6aSShilei Tian cuDeviceGetAttribute( 1294d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK, Device), 1295d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1296d2f85d09SJose M Monsalve Diaz printf(" Registers per Block: \t\t%d \n", TmpInt); 1297d2f85d09SJose M Monsalve Diaz checkResult( 1298d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_WARP_SIZE, Device), 1299d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1300d2f85d09SJose M Monsalve Diaz printf(" Warp Size: \t\t\t\t%d Threads \n", TmpInt); 1301d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1302d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK, Device), 1303d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1304d2f85d09SJose M Monsalve Diaz printf(" Maximum Threads per Block: \t\t%d \n", TmpInt); 130528939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 130628939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, Device), 1307d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 130828939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 130928939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y, Device), 1310d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 131128939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 131228939b6aSShilei Tian &TmpInt3, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z, Device), 1313d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1314d2f85d09SJose M Monsalve Diaz printf(" Maximum Block Dimensions: \t\t%d, %d, %d \n", TmpInt, TmpInt2, 1315d2f85d09SJose M Monsalve Diaz TmpInt3); 131628939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 131728939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, Device), 1318d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 131928939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 132028939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Y, Device), 1321d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 132228939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 132328939b6aSShilei Tian &TmpInt3, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Z, Device), 1324d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1325d2f85d09SJose M Monsalve Diaz printf(" Maximum Grid Dimensions: \t\t%d x %d x %d \n", TmpInt, TmpInt2, 1326d2f85d09SJose M Monsalve Diaz TmpInt3); 1327d2f85d09SJose M Monsalve Diaz checkResult( 1328d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_MAX_PITCH, Device), 1329d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1330d2f85d09SJose M Monsalve Diaz printf(" Maximum Memory Pitch: \t\t%d bytes \n", TmpInt); 1331d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1332d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT, Device), 1333d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1334d2f85d09SJose M Monsalve Diaz printf(" Texture Alignment: \t\t\t%d bytes \n", TmpInt); 1335d2f85d09SJose M Monsalve Diaz checkResult( 1336d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_CLOCK_RATE, Device), 1337d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1338d2f85d09SJose M Monsalve Diaz printf(" Clock Rate: \t\t\t%d kHz\n", TmpInt); 1339d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1340d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT, Device), 1341d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1342d2f85d09SJose M Monsalve Diaz printf(" Execution Timeout: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1343d2f85d09SJose M Monsalve Diaz checkResult( 1344d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_INTEGRATED, Device), 1345d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1346d2f85d09SJose M Monsalve Diaz printf(" Integrated Device: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1347d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1348d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CAN_MAP_HOST_MEMORY, Device), 1349d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1350d2f85d09SJose M Monsalve Diaz printf(" Can Map Host Memory: \t\t%s \n", BOOL2TEXT(TmpInt)); 1351d2f85d09SJose M Monsalve Diaz checkResult( 1352d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_MODE, Device), 1353d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1354d2f85d09SJose M Monsalve Diaz if (TmpInt == CU_COMPUTEMODE_DEFAULT) 1355d2f85d09SJose M Monsalve Diaz TmpStr = "DEFAULT"; 1356d2f85d09SJose M Monsalve Diaz else if (TmpInt == CU_COMPUTEMODE_PROHIBITED) 1357d2f85d09SJose M Monsalve Diaz TmpStr = "PROHIBITED"; 1358d2f85d09SJose M Monsalve Diaz else if (TmpInt == CU_COMPUTEMODE_EXCLUSIVE_PROCESS) 1359d2f85d09SJose M Monsalve Diaz TmpStr = "EXCLUSIVE PROCESS"; 1360d2f85d09SJose M Monsalve Diaz else 1361d2f85d09SJose M Monsalve Diaz TmpStr = "unknown"; 1362d2f85d09SJose M Monsalve Diaz printf(" Compute Mode: \t\t\t%s \n", TmpStr.c_str()); 1363d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1364d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CONCURRENT_KERNELS, Device), 1365d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1366d2f85d09SJose M Monsalve Diaz printf(" Concurrent Kernels: \t\t%s \n", BOOL2TEXT(TmpInt)); 1367d2f85d09SJose M Monsalve Diaz checkResult( 1368d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_ECC_ENABLED, Device), 1369d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1370d2f85d09SJose M Monsalve Diaz printf(" ECC Enabled: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1371d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1372d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE, Device), 1373d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1374d2f85d09SJose M Monsalve Diaz printf(" Memory Clock Rate: \t\t\t%d kHz\n", TmpInt); 137528939b6aSShilei Tian checkResult( 137628939b6aSShilei Tian cuDeviceGetAttribute( 1377d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH, Device), 1378d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1379d2f85d09SJose M Monsalve Diaz printf(" Memory Bus Width: \t\t\t%d bits\n", TmpInt); 138028939b6aSShilei Tian checkResult(cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_L2_CACHE_SIZE, 138128939b6aSShilei Tian Device), 1382d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1383d2f85d09SJose M Monsalve Diaz printf(" L2 Cache Size: \t\t\t%d bytes \n", TmpInt); 138428939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 138528939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR, 138628939b6aSShilei Tian Device), 1387d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1388d2f85d09SJose M Monsalve Diaz printf(" Max Threads Per SMP: \t\t%d \n", TmpInt); 1389d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1390d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_ASYNC_ENGINE_COUNT, Device), 1391d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1392d2f85d09SJose M Monsalve Diaz printf(" Async Engines: \t\t\t%s (%d) \n", BOOL2TEXT(TmpInt), TmpInt); 1393d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1394d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, Device), 1395d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1396d2f85d09SJose M Monsalve Diaz printf(" Unified Addressing: \t\t%s \n", BOOL2TEXT(TmpInt)); 139728939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 139828939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MANAGED_MEMORY, Device), 1399d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1400d2f85d09SJose M Monsalve Diaz printf(" Managed Memory: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1401d2f85d09SJose M Monsalve Diaz checkResult( 1402d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1403d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, Device), 1404d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1405d2f85d09SJose M Monsalve Diaz printf(" Concurrent Managed Memory: \t\t%s \n", BOOL2TEXT(TmpInt)); 1406d2f85d09SJose M Monsalve Diaz checkResult( 1407d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1408d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_PREEMPTION_SUPPORTED, Device), 1409d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1410d2f85d09SJose M Monsalve Diaz printf(" Preemption Supported: \t\t%s \n", BOOL2TEXT(TmpInt)); 1411d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1412d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_COOPERATIVE_LAUNCH, Device), 1413d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1414d2f85d09SJose M Monsalve Diaz printf(" Cooperative Launch: \t\t%s \n", BOOL2TEXT(TmpInt)); 141528939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 141628939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MULTI_GPU_BOARD, Device), 1417d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1418d2f85d09SJose M Monsalve Diaz printf(" Multi-Device Boars: \t\t%s \n", BOOL2TEXT(TmpInt)); 141928939b6aSShilei Tian checkResult( 142028939b6aSShilei Tian cuDeviceGetAttribute( 142128939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, Device), 1422d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 142328939b6aSShilei Tian checkResult( 142428939b6aSShilei Tian cuDeviceGetAttribute( 142528939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, Device), 1426d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1427d2f85d09SJose M Monsalve Diaz printf(" Compute Capabilities: \t\t%d%d \n", TmpInt, TmpInt2); 1428d2f85d09SJose M Monsalve Diaz } 142929df4ab3SShilei Tian 1430*1660288bSJohannes Doerfert int createEvent(int DeviceId, void **P) { 1431943d1d83SShilei Tian CUevent Event = nullptr; 1432*1660288bSJohannes Doerfert if (EventPool[DeviceId]->acquire(Event) != OFFLOAD_SUCCESS) 1433943d1d83SShilei Tian return OFFLOAD_FAIL; 1434943d1d83SShilei Tian *P = Event; 1435943d1d83SShilei Tian return OFFLOAD_SUCCESS; 1436943d1d83SShilei Tian } 1437943d1d83SShilei Tian 1438*1660288bSJohannes Doerfert int destroyEvent(int DeviceId, void *EventPtr) { 1439*1660288bSJohannes Doerfert EventPool[DeviceId]->release(reinterpret_cast<CUevent>(EventPtr)); 1440943d1d83SShilei Tian return OFFLOAD_SUCCESS; 1441943d1d83SShilei Tian } 1442943d1d83SShilei Tian 144329df4ab3SShilei Tian int waitEvent(const int DeviceId, __tgt_async_info *AsyncInfo, 144429df4ab3SShilei Tian void *EventPtr) const { 144529df4ab3SShilei Tian CUstream Stream = getStream(DeviceId, AsyncInfo); 144629df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 144729df4ab3SShilei Tian 144829df4ab3SShilei Tian // We don't use CU_EVENT_WAIT_DEFAULT here as it is only available from 144929df4ab3SShilei Tian // specific CUDA version, and defined as 0x0. In previous version, per CUDA 145029df4ab3SShilei Tian // API document, that argument has to be 0x0. 145129df4ab3SShilei Tian CUresult Err = cuStreamWaitEvent(Stream, Event, 0); 145229df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 145329df4ab3SShilei Tian DP("Error when waiting event. stream = " DPxMOD ", event = " DPxMOD "\n", 145429df4ab3SShilei Tian DPxPTR(Stream), DPxPTR(Event)); 145529df4ab3SShilei Tian CUDA_ERR_STRING(Err); 145629df4ab3SShilei Tian return OFFLOAD_FAIL; 145729df4ab3SShilei Tian } 145829df4ab3SShilei Tian 145929df4ab3SShilei Tian return OFFLOAD_SUCCESS; 146029df4ab3SShilei Tian } 1461f44e41afSSri Hari Krishna Narayanan 1462f44e41afSSri Hari Krishna Narayanan int releaseAsyncInfo(int DeviceId, __tgt_async_info *AsyncInfo) const { 1463f44e41afSSri Hari Krishna Narayanan if (AsyncInfo->Queue) { 1464f44e41afSSri Hari Krishna Narayanan StreamPool[DeviceId]->release( 1465f44e41afSSri Hari Krishna Narayanan reinterpret_cast<CUstream>(AsyncInfo->Queue)); 1466f44e41afSSri Hari Krishna Narayanan AsyncInfo->Queue = nullptr; 1467f44e41afSSri Hari Krishna Narayanan } 1468f44e41afSSri Hari Krishna Narayanan 1469f44e41afSSri Hari Krishna Narayanan return OFFLOAD_SUCCESS; 1470f44e41afSSri Hari Krishna Narayanan } 1471f44e41afSSri Hari Krishna Narayanan 1472f44e41afSSri Hari Krishna Narayanan int initAsyncInfo(int DeviceId, __tgt_async_info **AsyncInfo) const { 1473f44e41afSSri Hari Krishna Narayanan CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 1474f44e41afSSri Hari Krishna Narayanan if (!checkResult(Err, "error returned from cuCtxSetCurrent")) 1475f44e41afSSri Hari Krishna Narayanan return OFFLOAD_FAIL; 1476f44e41afSSri Hari Krishna Narayanan 1477f44e41afSSri Hari Krishna Narayanan *AsyncInfo = new __tgt_async_info; 1478f44e41afSSri Hari Krishna Narayanan getStream(DeviceId, *AsyncInfo); 1479f44e41afSSri Hari Krishna Narayanan return OFFLOAD_SUCCESS; 1480f44e41afSSri Hari Krishna Narayanan } 1481f44e41afSSri Hari Krishna Narayanan 1482f44e41afSSri Hari Krishna Narayanan int initDeviceInfo(int DeviceId, __tgt_device_info *DeviceInfo, 1483f44e41afSSri Hari Krishna Narayanan const char **ErrStr) const { 1484f44e41afSSri Hari Krishna Narayanan assert(DeviceInfo && "DeviceInfo is nullptr"); 1485f44e41afSSri Hari Krishna Narayanan 1486f44e41afSSri Hari Krishna Narayanan if (!DeviceInfo->Context) 1487f44e41afSSri Hari Krishna Narayanan DeviceInfo->Context = DeviceData[DeviceId].Context; 1488f44e41afSSri Hari Krishna Narayanan if (!DeviceInfo->Device) { 1489f44e41afSSri Hari Krishna Narayanan CUdevice Dev; 1490f44e41afSSri Hari Krishna Narayanan CUresult Err = cuDeviceGet(&Dev, DeviceId); 1491f44e41afSSri Hari Krishna Narayanan if (Err == CUDA_SUCCESS) { 1492f44e41afSSri Hari Krishna Narayanan DeviceInfo->Device = reinterpret_cast<void *>(Dev); 1493f44e41afSSri Hari Krishna Narayanan } else { 1494f44e41afSSri Hari Krishna Narayanan cuGetErrorString(Err, ErrStr); 1495f44e41afSSri Hari Krishna Narayanan return OFFLOAD_FAIL; 1496f44e41afSSri Hari Krishna Narayanan } 1497f44e41afSSri Hari Krishna Narayanan } 1498f44e41afSSri Hari Krishna Narayanan return OFFLOAD_SUCCESS; 1499f44e41afSSri Hari Krishna Narayanan } 15004031bb98SShilei Tian }; 15014031bb98SShilei Tian 15024031bb98SShilei Tian DeviceRTLTy DeviceRTL; 15034031bb98SShilei Tian } // namespace 15044031bb98SShilei Tian 15054031bb98SShilei Tian // Exposed library API function 15064031bb98SShilei Tian #ifdef __cplusplus 15074031bb98SShilei Tian extern "C" { 15084031bb98SShilei Tian #endif 15094031bb98SShilei Tian 15104031bb98SShilei Tian int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *image) { 15114031bb98SShilei Tian return elf_check_machine(image, /* EM_CUDA */ 190); 15124031bb98SShilei Tian } 15134031bb98SShilei Tian 15144031bb98SShilei Tian int32_t __tgt_rtl_number_of_devices() { return DeviceRTL.getNumOfDevices(); } 15154031bb98SShilei Tian 15164031bb98SShilei Tian int64_t __tgt_rtl_init_requires(int64_t RequiresFlags) { 151740470eb2SGeorge Rokos DP("Init requires flags to %" PRId64 "\n", RequiresFlags); 15184031bb98SShilei Tian DeviceRTL.setRequiresFlag(RequiresFlags); 15194031bb98SShilei Tian return RequiresFlags; 15204031bb98SShilei Tian } 15214031bb98SShilei Tian 1522a014fbbcSShilei Tian int32_t __tgt_rtl_is_data_exchangable(int32_t src_dev_id, int dst_dev_id) { 1523a014fbbcSShilei Tian if (DeviceRTL.isValidDeviceId(src_dev_id) && 1524a014fbbcSShilei Tian DeviceRTL.isValidDeviceId(dst_dev_id)) 1525a014fbbcSShilei Tian return 1; 1526a014fbbcSShilei Tian 1527a014fbbcSShilei Tian return 0; 1528a014fbbcSShilei Tian } 1529a014fbbcSShilei Tian 15304031bb98SShilei Tian int32_t __tgt_rtl_init_device(int32_t device_id) { 15314031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15324031bb98SShilei Tian 15334031bb98SShilei Tian return DeviceRTL.initDevice(device_id); 15344031bb98SShilei Tian } 15354031bb98SShilei Tian 153610aa83ffSJohannes Doerfert int32_t __tgt_rtl_deinit_device(int32_t device_id) { 153710aa83ffSJohannes Doerfert assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 153810aa83ffSJohannes Doerfert 153910aa83ffSJohannes Doerfert return DeviceRTL.deinitDevice(device_id); 154010aa83ffSJohannes Doerfert } 154110aa83ffSJohannes Doerfert 15424031bb98SShilei Tian __tgt_target_table *__tgt_rtl_load_binary(int32_t device_id, 15434031bb98SShilei Tian __tgt_device_image *image) { 15444031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15454031bb98SShilei Tian 15464031bb98SShilei Tian return DeviceRTL.loadBinary(device_id, image); 15474031bb98SShilei Tian } 15484031bb98SShilei Tian 15492468fdd9SGeorge Rokos void *__tgt_rtl_data_alloc(int32_t device_id, int64_t size, void *, 15502468fdd9SGeorge Rokos int32_t kind) { 15514031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15524031bb98SShilei Tian 1553a15f8589SJoseph Huber return DeviceRTL.dataAlloc(device_id, size, (TargetAllocTy)kind); 15544031bb98SShilei Tian } 15554031bb98SShilei Tian 15564031bb98SShilei Tian int32_t __tgt_rtl_data_submit(int32_t device_id, void *tgt_ptr, void *hst_ptr, 15574031bb98SShilei Tian int64_t size) { 15584031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15594031bb98SShilei Tian 15605449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 15614031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_submit_async(device_id, tgt_ptr, hst_ptr, 15625449fbb5SJohannes Doerfert size, &AsyncInfo); 15634031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 15644031bb98SShilei Tian return OFFLOAD_FAIL; 15654031bb98SShilei Tian 15665449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 15674031bb98SShilei Tian } 15684031bb98SShilei Tian 15694031bb98SShilei Tian int32_t __tgt_rtl_data_submit_async(int32_t device_id, void *tgt_ptr, 15704031bb98SShilei Tian void *hst_ptr, int64_t size, 15714031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 15724031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15734031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 15744031bb98SShilei Tian 15754031bb98SShilei Tian return DeviceRTL.dataSubmit(device_id, tgt_ptr, hst_ptr, size, 15764031bb98SShilei Tian async_info_ptr); 15774031bb98SShilei Tian } 15784031bb98SShilei Tian 15794031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve(int32_t device_id, void *hst_ptr, void *tgt_ptr, 15804031bb98SShilei Tian int64_t size) { 15814031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15824031bb98SShilei Tian 15835449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 15844031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_retrieve_async(device_id, hst_ptr, tgt_ptr, 15855449fbb5SJohannes Doerfert size, &AsyncInfo); 15864031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 15874031bb98SShilei Tian return OFFLOAD_FAIL; 15884031bb98SShilei Tian 15895449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 15904031bb98SShilei Tian } 15914031bb98SShilei Tian 15924031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve_async(int32_t device_id, void *hst_ptr, 15934031bb98SShilei Tian void *tgt_ptr, int64_t size, 15944031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 15954031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15964031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 15974031bb98SShilei Tian 15984031bb98SShilei Tian return DeviceRTL.dataRetrieve(device_id, hst_ptr, tgt_ptr, size, 15994031bb98SShilei Tian async_info_ptr); 16004031bb98SShilei Tian } 16014031bb98SShilei Tian 1602a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange_async(int32_t src_dev_id, void *src_ptr, 1603a014fbbcSShilei Tian int dst_dev_id, void *dst_ptr, 1604a014fbbcSShilei Tian int64_t size, 16055449fbb5SJohannes Doerfert __tgt_async_info *AsyncInfo) { 1606a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1607a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 16085449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 1609a014fbbcSShilei Tian 1610a014fbbcSShilei Tian return DeviceRTL.dataExchange(src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, 16115449fbb5SJohannes Doerfert AsyncInfo); 1612a014fbbcSShilei Tian } 1613a014fbbcSShilei Tian 1614a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange(int32_t src_dev_id, void *src_ptr, 1615a014fbbcSShilei Tian int32_t dst_dev_id, void *dst_ptr, 1616a014fbbcSShilei Tian int64_t size) { 1617a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1618a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 1619a014fbbcSShilei Tian 16205449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 1621a014fbbcSShilei Tian const int32_t rc = __tgt_rtl_data_exchange_async( 16225449fbb5SJohannes Doerfert src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, &AsyncInfo); 1623a014fbbcSShilei Tian if (rc != OFFLOAD_SUCCESS) 1624a014fbbcSShilei Tian return OFFLOAD_FAIL; 1625a014fbbcSShilei Tian 16265449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(src_dev_id, &AsyncInfo); 1627a014fbbcSShilei Tian } 1628a014fbbcSShilei Tian 16294031bb98SShilei Tian int32_t __tgt_rtl_data_delete(int32_t device_id, void *tgt_ptr) { 16304031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16314031bb98SShilei Tian 16324031bb98SShilei Tian return DeviceRTL.dataDelete(device_id, tgt_ptr); 16334031bb98SShilei Tian } 16344031bb98SShilei Tian 16354031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region(int32_t device_id, void *tgt_entry_ptr, 16364031bb98SShilei Tian void **tgt_args, 16374031bb98SShilei Tian ptrdiff_t *tgt_offsets, 16384031bb98SShilei Tian int32_t arg_num, int32_t team_num, 16394031bb98SShilei Tian int32_t thread_limit, 16404031bb98SShilei Tian uint64_t loop_tripcount) { 16414031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16424031bb98SShilei Tian 16435449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 16444031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_team_region_async( 16454031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 16465449fbb5SJohannes Doerfert thread_limit, loop_tripcount, &AsyncInfo); 16474031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 16484031bb98SShilei Tian return OFFLOAD_FAIL; 16494031bb98SShilei Tian 16505449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 16514031bb98SShilei Tian } 16524031bb98SShilei Tian 16534031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region_async( 16544031bb98SShilei Tian int32_t device_id, void *tgt_entry_ptr, void **tgt_args, 16554031bb98SShilei Tian ptrdiff_t *tgt_offsets, int32_t arg_num, int32_t team_num, 16564031bb98SShilei Tian int32_t thread_limit, uint64_t loop_tripcount, 16574031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 16584031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16594031bb98SShilei Tian 16604031bb98SShilei Tian return DeviceRTL.runTargetTeamRegion( 16614031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 16624031bb98SShilei Tian thread_limit, loop_tripcount, async_info_ptr); 16634031bb98SShilei Tian } 16643de4cd12SGeorge Rokos 16653de4cd12SGeorge Rokos int32_t __tgt_rtl_run_target_region(int32_t device_id, void *tgt_entry_ptr, 166632ed2927SShilei Tian void **tgt_args, ptrdiff_t *tgt_offsets, 166703ff643dSShilei Tian int32_t arg_num) { 16684031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16694031bb98SShilei Tian 16705449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 16714031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_region_async( 16725449fbb5SJohannes Doerfert device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, &AsyncInfo); 167303ff643dSShilei Tian if (rc != OFFLOAD_SUCCESS) 167403ff643dSShilei Tian return OFFLOAD_FAIL; 167503ff643dSShilei Tian 16765449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 167703ff643dSShilei Tian } 167803ff643dSShilei Tian 167903ff643dSShilei Tian int32_t __tgt_rtl_run_target_region_async(int32_t device_id, 168003ff643dSShilei Tian void *tgt_entry_ptr, void **tgt_args, 168103ff643dSShilei Tian ptrdiff_t *tgt_offsets, 168232ed2927SShilei Tian int32_t arg_num, 16834031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 16844031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16854031bb98SShilei Tian 168603ff643dSShilei Tian return __tgt_rtl_run_target_team_region_async( 16874031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, 16884031bb98SShilei Tian /* team num*/ 1, /* thread_limit */ 1, /* loop_tripcount */ 0, 16894031bb98SShilei Tian async_info_ptr); 169032ed2927SShilei Tian } 169132ed2927SShilei Tian 16924031bb98SShilei Tian int32_t __tgt_rtl_synchronize(int32_t device_id, 16934031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 16944031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16954031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 16964031bb98SShilei Tian assert(async_info_ptr->Queue && "async_info_ptr->Queue is nullptr"); 169732ed2927SShilei Tian 16984031bb98SShilei Tian return DeviceRTL.synchronize(device_id, async_info_ptr); 16993de4cd12SGeorge Rokos } 17003de4cd12SGeorge Rokos 17012b6f2008SJoseph Huber void __tgt_rtl_set_info_flag(uint32_t NewInfoLevel) { 170259b68490SJoseph Huber std::atomic<uint32_t> &InfoLevel = getInfoLevelInternal(); 17032b6f2008SJoseph Huber InfoLevel.store(NewInfoLevel); 17042b6f2008SJoseph Huber } 17052b6f2008SJoseph Huber 1706d2f85d09SJose M Monsalve Diaz void __tgt_rtl_print_device_info(int32_t device_id) { 1707d2f85d09SJose M Monsalve Diaz assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 1708d2f85d09SJose M Monsalve Diaz DeviceRTL.printDeviceInfo(device_id); 1709d2f85d09SJose M Monsalve Diaz } 1710d2f85d09SJose M Monsalve Diaz 171129df4ab3SShilei Tian int32_t __tgt_rtl_create_event(int32_t device_id, void **event) { 171229df4ab3SShilei Tian assert(event && "event is nullptr"); 1713*1660288bSJohannes Doerfert return DeviceRTL.createEvent(device_id, event); 171429df4ab3SShilei Tian } 171529df4ab3SShilei Tian 171629df4ab3SShilei Tian int32_t __tgt_rtl_record_event(int32_t device_id, void *event_ptr, 171729df4ab3SShilei Tian __tgt_async_info *async_info_ptr) { 171829df4ab3SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 171929df4ab3SShilei Tian assert(async_info_ptr->Queue && "async_info_ptr->Queue is nullptr"); 172029df4ab3SShilei Tian assert(event_ptr && "event_ptr is nullptr"); 172129df4ab3SShilei Tian 172229df4ab3SShilei Tian return recordEvent(event_ptr, async_info_ptr); 172329df4ab3SShilei Tian } 172429df4ab3SShilei Tian 172529df4ab3SShilei Tian int32_t __tgt_rtl_wait_event(int32_t device_id, void *event_ptr, 172629df4ab3SShilei Tian __tgt_async_info *async_info_ptr) { 172729df4ab3SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 172829df4ab3SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 172929df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 173029df4ab3SShilei Tian 173129df4ab3SShilei Tian return DeviceRTL.waitEvent(device_id, async_info_ptr, event_ptr); 173229df4ab3SShilei Tian } 173329df4ab3SShilei Tian 173429df4ab3SShilei Tian int32_t __tgt_rtl_sync_event(int32_t device_id, void *event_ptr) { 173529df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 173629df4ab3SShilei Tian 173729df4ab3SShilei Tian return syncEvent(event_ptr); 173829df4ab3SShilei Tian } 173929df4ab3SShilei Tian 174029df4ab3SShilei Tian int32_t __tgt_rtl_destroy_event(int32_t device_id, void *event_ptr) { 174129df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 174229df4ab3SShilei Tian 1743*1660288bSJohannes Doerfert return DeviceRTL.destroyEvent(device_id, event_ptr); 174429df4ab3SShilei Tian } 174529df4ab3SShilei Tian 1746f44e41afSSri Hari Krishna Narayanan int32_t __tgt_rtl_release_async_info(int32_t device_id, 1747f44e41afSSri Hari Krishna Narayanan __tgt_async_info *async_info) { 1748f44e41afSSri Hari Krishna Narayanan assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 1749f44e41afSSri Hari Krishna Narayanan assert(async_info && "async_info is nullptr"); 1750f44e41afSSri Hari Krishna Narayanan 1751f44e41afSSri Hari Krishna Narayanan return DeviceRTL.releaseAsyncInfo(device_id, async_info); 1752f44e41afSSri Hari Krishna Narayanan } 1753f44e41afSSri Hari Krishna Narayanan 1754f44e41afSSri Hari Krishna Narayanan int32_t __tgt_rtl_init_async_info(int32_t device_id, 1755f44e41afSSri Hari Krishna Narayanan __tgt_async_info **async_info) { 1756f44e41afSSri Hari Krishna Narayanan assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 1757f44e41afSSri Hari Krishna Narayanan assert(async_info && "async_info is nullptr"); 1758f44e41afSSri Hari Krishna Narayanan 1759f44e41afSSri Hari Krishna Narayanan return DeviceRTL.initAsyncInfo(device_id, async_info); 1760f44e41afSSri Hari Krishna Narayanan } 1761f44e41afSSri Hari Krishna Narayanan 1762f44e41afSSri Hari Krishna Narayanan int32_t __tgt_rtl_init_device_info(int32_t device_id, 1763f44e41afSSri Hari Krishna Narayanan __tgt_device_info *device_info_ptr, 1764f44e41afSSri Hari Krishna Narayanan const char **err_str) { 1765f44e41afSSri Hari Krishna Narayanan assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 1766f44e41afSSri Hari Krishna Narayanan assert(device_info_ptr && "device_info_ptr is nullptr"); 1767f44e41afSSri Hari Krishna Narayanan 1768f44e41afSSri Hari Krishna Narayanan return DeviceRTL.initDeviceInfo(device_id, device_info_ptr, err_str); 1769f44e41afSSri Hari Krishna Narayanan } 1770f44e41afSSri Hari Krishna Narayanan 17713de4cd12SGeorge Rokos #ifdef __cplusplus 17723de4cd12SGeorge Rokos } 17733de4cd12SGeorge Rokos #endif 1774