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 133de4cd12SGeorge Rokos #include <cassert> 143de4cd12SGeorge Rokos #include <cstddef> 153de4cd12SGeorge Rokos #include <cuda.h> 163de4cd12SGeorge Rokos #include <list> 17a5153dbcSJohannes Doerfert #include <memory> 18feed674dSShilei Tian #include <mutex> 193de4cd12SGeorge Rokos #include <string> 20a15f8589SJoseph Huber #include <unordered_map> 213de4cd12SGeorge Rokos #include <vector> 223de4cd12SGeorge Rokos 23ae95ceebSJoseph Huber #include "Debug.h" 241546d319SGeorge Rokos #include "omptargetplugin.h" 253de4cd12SGeorge Rokos 263de4cd12SGeorge Rokos #define TARGET_NAME CUDA 27ae95ceebSJoseph Huber #define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL" 2878769ec4SMichael Kruse 290871d6d5SShilei Tian #include "MemoryManager.h" 300871d6d5SShilei Tian 3178769ec4SMichael Kruse // Utility for retrieving and printing CUDA error string. 32ae95ceebSJoseph Huber #ifdef OMPTARGET_DEBUG 3378769ec4SMichael Kruse #define CUDA_ERR_STRING(err) \ 3478769ec4SMichael Kruse do { \ 35ae95ceebSJoseph Huber if (getDebugLevel() > 0) { \ 3603111e5eSYe Luo const char *errStr = nullptr; \ 3703111e5eSYe Luo CUresult errStr_status = cuGetErrorString(err, &errStr); \ 3803111e5eSYe Luo if (errStr_status == CUDA_ERROR_INVALID_VALUE) \ 392ce16810SJoseph Huber REPORT("Unrecognized CUDA error code: %d\n", err); \ 4003111e5eSYe Luo else if (errStr_status == CUDA_SUCCESS) \ 412ce16810SJoseph Huber REPORT("CUDA error is: %s\n", errStr); \ 4203111e5eSYe Luo else { \ 432ce16810SJoseph Huber REPORT("Unresolved CUDA error code: %d\n", err); \ 442ce16810SJoseph Huber REPORT("Unsuccessful cuGetErrorString return status: %d\n", \ 4503111e5eSYe Luo errStr_status); \ 4603111e5eSYe Luo } \ 472ce16810SJoseph Huber } else { \ 482ce16810SJoseph Huber const char *errStr = nullptr; \ 492ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 502ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 512ce16810SJoseph Huber REPORT("%s \n", errStr); \ 5278769ec4SMichael Kruse } \ 5378769ec4SMichael Kruse } while (false) 54b305d26bSSergey Dmitriev #else // OMPTARGET_DEBUG 552ce16810SJoseph Huber #define CUDA_ERR_STRING(err) \ 562ce16810SJoseph Huber do { \ 572ce16810SJoseph Huber const char *errStr = nullptr; \ 582ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 592ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 602ce16810SJoseph Huber REPORT("%s \n", errStr); \ 612ce16810SJoseph Huber } while (false) 62b305d26bSSergey Dmitriev #endif // OMPTARGET_DEBUG 633de4cd12SGeorge Rokos 64a81c68aeSShilei Tian #include "elf_common.h" 653de4cd12SGeorge Rokos 663de4cd12SGeorge Rokos /// Keep entries table per device. 673de4cd12SGeorge Rokos struct FuncOrGblEntryTy { 683de4cd12SGeorge Rokos __tgt_target_table Table; 693de4cd12SGeorge Rokos std::vector<__tgt_offload_entry> Entries; 703de4cd12SGeorge Rokos }; 713de4cd12SGeorge Rokos 723de4cd12SGeorge Rokos enum ExecutionModeType { 73c13df8e5SGeorge Rokos SPMD, // constructors, destructors, 74c13df8e5SGeorge Rokos // combined constructs (`teams distribute parallel for [simd]`) 75c13df8e5SGeorge Rokos GENERIC, // everything else 767d576392SJoseph Huber SPMD_GENERIC, // Generic kernel with SPMD execution 773de4cd12SGeorge Rokos NONE 783de4cd12SGeorge Rokos }; 793de4cd12SGeorge Rokos 80dc34b1c9SRon Lieberman /// Use a single entity to encode a kernel and a set of flags. 813de4cd12SGeorge Rokos struct KernelTy { 823de4cd12SGeorge Rokos CUfunction Func; 833de4cd12SGeorge Rokos 843de4cd12SGeorge Rokos // execution mode of kernel 853de4cd12SGeorge Rokos // 0 - SPMD mode (without master warp) 863de4cd12SGeorge Rokos // 1 - Generic mode (with master warp) 877d576392SJoseph Huber // 2 - SPMD mode execution with Generic mode semantics. 883de4cd12SGeorge Rokos int8_t ExecutionMode; 893de4cd12SGeorge Rokos 90aa27cfc1SJohannes Doerfert /// Maximal number of threads per block for this kernel. 91aa27cfc1SJohannes Doerfert int MaxThreadsPerBlock = 0; 92aa27cfc1SJohannes Doerfert 933de4cd12SGeorge Rokos KernelTy(CUfunction _Func, int8_t _ExecutionMode) 943de4cd12SGeorge Rokos : Func(_Func), ExecutionMode(_ExecutionMode) {} 953de4cd12SGeorge Rokos }; 963de4cd12SGeorge Rokos 974c6a098aSKazuaki Ishizaki /// Device environment data 98feed674dSShilei Tian /// Manually sync with the deviceRTL side for now, move to a dedicated header 99feed674dSShilei Tian /// file later. 100e1c7a46dSGuansong Zhang struct omptarget_device_environmentTy { 101e1c7a46dSGuansong Zhang int32_t debug_level; 102e1c7a46dSGuansong Zhang }; 103e1c7a46dSGuansong Zhang 104feed674dSShilei Tian namespace { 105feed674dSShilei Tian bool checkResult(CUresult Err, const char *ErrMsg) { 106feed674dSShilei Tian if (Err == CUDA_SUCCESS) 107feed674dSShilei Tian return true; 108feed674dSShilei Tian 1092ce16810SJoseph Huber REPORT("%s", ErrMsg); 110feed674dSShilei Tian CUDA_ERR_STRING(Err); 111feed674dSShilei Tian return false; 112feed674dSShilei Tian } 1134031bb98SShilei Tian 114a014fbbcSShilei Tian int memcpyDtoD(const void *SrcPtr, void *DstPtr, int64_t Size, 115a014fbbcSShilei Tian CUstream Stream) { 11695f0d1edSJon Chesterfield CUresult Err = 11795f0d1edSJon Chesterfield cuMemcpyDtoDAsync((CUdeviceptr)DstPtr, (CUdeviceptr)SrcPtr, Size, Stream); 118a014fbbcSShilei Tian 119a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 12029338459SJoseph Huber DP("Error when copying data from device to device. Pointers: src " 121a014fbbcSShilei Tian "= " DPxMOD ", dst = " DPxMOD ", size = %" PRId64 "\n", 122a014fbbcSShilei Tian DPxPTR(SrcPtr), DPxPTR(DstPtr), Size); 123a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 124a014fbbcSShilei Tian return OFFLOAD_FAIL; 125a014fbbcSShilei Tian } 126a014fbbcSShilei Tian 127a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 128a014fbbcSShilei Tian } 129a014fbbcSShilei Tian 1304031bb98SShilei Tian // Structure contains per-device data 1314031bb98SShilei Tian struct DeviceDataTy { 1325272d29eSJohannes Doerfert /// List that contains all the kernels. 1335272d29eSJohannes Doerfert std::list<KernelTy> KernelsList; 1345272d29eSJohannes Doerfert 1354031bb98SShilei Tian std::list<FuncOrGblEntryTy> FuncGblEntries; 1365272d29eSJohannes Doerfert 1374031bb98SShilei Tian CUcontext Context = nullptr; 1384031bb98SShilei Tian // Device properties 1394031bb98SShilei Tian int ThreadsPerBlock = 0; 1404031bb98SShilei Tian int BlocksPerGrid = 0; 1414031bb98SShilei Tian int WarpSize = 0; 1424031bb98SShilei Tian // OpenMP properties 1434031bb98SShilei Tian int NumTeams = 0; 1444031bb98SShilei Tian int NumThreads = 0; 1454031bb98SShilei Tian }; 146feed674dSShilei Tian 147feed674dSShilei Tian class StreamManagerTy { 148feed674dSShilei Tian int NumberOfDevices; 1494031bb98SShilei Tian // The initial size of stream pool 1504031bb98SShilei Tian int EnvNumInitialStreams; 151feed674dSShilei Tian // Per-device stream mutex 152feed674dSShilei Tian std::vector<std::unique_ptr<std::mutex>> StreamMtx; 153feed674dSShilei Tian // Per-device stream Id indicates the next available stream in the pool 154feed674dSShilei Tian std::vector<int> NextStreamId; 155feed674dSShilei Tian // Per-device stream pool 156feed674dSShilei Tian std::vector<std::vector<CUstream>> StreamPool; 1574031bb98SShilei Tian // Reference to per-device data 1584031bb98SShilei Tian std::vector<DeviceDataTy> &DeviceData; 159feed674dSShilei Tian 160feed674dSShilei Tian // If there is no CUstream left in the pool, we will resize the pool to 161feed674dSShilei Tian // allocate more CUstream. This function should be called with device mutex, 162feed674dSShilei Tian // and we do not resize to smaller one. 163feed674dSShilei Tian void resizeStreamPool(const int DeviceId, const size_t NewSize) { 164feed674dSShilei Tian std::vector<CUstream> &Pool = StreamPool[DeviceId]; 165feed674dSShilei Tian const size_t CurrentSize = Pool.size(); 166feed674dSShilei Tian assert(NewSize > CurrentSize && "new size is not larger than current size"); 167feed674dSShilei Tian 1684031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 1694031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) { 170feed674dSShilei Tian // We will return if cannot switch to the right context in case of 171feed674dSShilei Tian // creating bunch of streams that are not corresponding to the right 172feed674dSShilei Tian // device. The offloading will fail later because selected CUstream is 173feed674dSShilei Tian // nullptr. 174feed674dSShilei Tian return; 175feed674dSShilei Tian } 176feed674dSShilei Tian 1774031bb98SShilei Tian Pool.resize(NewSize, nullptr); 1784031bb98SShilei Tian 179feed674dSShilei Tian for (size_t I = CurrentSize; I < NewSize; ++I) { 1804031bb98SShilei Tian checkResult(cuStreamCreate(&Pool[I], CU_STREAM_NON_BLOCKING), 1814031bb98SShilei Tian "Error returned from cuStreamCreate\n"); 182feed674dSShilei Tian } 183feed674dSShilei Tian } 184feed674dSShilei Tian 185feed674dSShilei Tian public: 1864031bb98SShilei Tian StreamManagerTy(const int NumberOfDevices, 1874031bb98SShilei Tian std::vector<DeviceDataTy> &DeviceData) 1884031bb98SShilei Tian : NumberOfDevices(NumberOfDevices), EnvNumInitialStreams(32), 1894031bb98SShilei Tian DeviceData(DeviceData) { 190feed674dSShilei Tian StreamPool.resize(NumberOfDevices); 191feed674dSShilei Tian NextStreamId.resize(NumberOfDevices); 192feed674dSShilei Tian StreamMtx.resize(NumberOfDevices); 193feed674dSShilei Tian 1944031bb98SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_NUM_INITIAL_STREAMS")) 1954031bb98SShilei Tian EnvNumInitialStreams = std::stoi(EnvStr); 196feed674dSShilei Tian 197feed674dSShilei Tian // Initialize the next stream id 198feed674dSShilei Tian std::fill(NextStreamId.begin(), NextStreamId.end(), 0); 199feed674dSShilei Tian 200feed674dSShilei Tian // Initialize stream mutex 201feed674dSShilei Tian for (std::unique_ptr<std::mutex> &Ptr : StreamMtx) 202feed674dSShilei Tian Ptr = std::make_unique<std::mutex>(); 203feed674dSShilei Tian } 204feed674dSShilei Tian 205feed674dSShilei Tian ~StreamManagerTy() { 206feed674dSShilei Tian // Destroy streams 207feed674dSShilei Tian for (int I = 0; I < NumberOfDevices; ++I) { 2084031bb98SShilei Tian checkResult(cuCtxSetCurrent(DeviceData[I].Context), 2094031bb98SShilei Tian "Error returned from cuCtxSetCurrent\n"); 210feed674dSShilei Tian 211feed674dSShilei Tian for (CUstream &S : StreamPool[I]) { 2124031bb98SShilei Tian if (S) 21395f0d1edSJon Chesterfield checkResult(cuStreamDestroy(S), 21495f0d1edSJon Chesterfield "Error returned from cuStreamDestroy\n"); 215feed674dSShilei Tian } 216feed674dSShilei Tian } 217feed674dSShilei Tian } 218feed674dSShilei Tian 219feed674dSShilei Tian // Get a CUstream from pool. Per-device next stream id always points to the 220feed674dSShilei Tian // next available CUstream. That means, CUstreams [0, id-1] have been 221feed674dSShilei Tian // assigned, and [id,] are still available. If there is no CUstream left, we 222feed674dSShilei Tian // will ask more CUstreams from CUDA RT. Each time a CUstream is assigned, 223feed674dSShilei Tian // the id will increase one. 224feed674dSShilei Tian // xxxxxs+++++++++ 225feed674dSShilei Tian // ^ 226feed674dSShilei Tian // id 227feed674dSShilei Tian // After assignment, the pool becomes the following and s is assigned. 228feed674dSShilei Tian // xxxxxs+++++++++ 229feed674dSShilei Tian // ^ 230feed674dSShilei Tian // id 231feed674dSShilei Tian CUstream getStream(const int DeviceId) { 232feed674dSShilei Tian const std::lock_guard<std::mutex> Lock(*StreamMtx[DeviceId]); 233feed674dSShilei Tian int &Id = NextStreamId[DeviceId]; 234feed674dSShilei Tian // No CUstream left in the pool, we need to request from CUDA RT 235ad94fce8SJohannes Doerfert if (Id == static_cast<int>(StreamPool[DeviceId].size())) { 236feed674dSShilei Tian // By default we double the stream pool every time 237feed674dSShilei Tian resizeStreamPool(DeviceId, Id * 2); 238feed674dSShilei Tian } 239feed674dSShilei Tian return StreamPool[DeviceId][Id++]; 240feed674dSShilei Tian } 241feed674dSShilei Tian 242feed674dSShilei Tian // Return a CUstream back to pool. As mentioned above, per-device next 243feed674dSShilei Tian // stream is always points to the next available CUstream, so when we return 244feed674dSShilei Tian // a CUstream, we need to first decrease the id, and then copy the CUstream 245feed674dSShilei Tian // back. 246feed674dSShilei Tian // It is worth noting that, the order of streams return might be different 247feed674dSShilei Tian // from that they're assigned, that saying, at some point, there might be 248feed674dSShilei Tian // two identical CUstreams. 249feed674dSShilei Tian // xxax+a+++++ 250feed674dSShilei Tian // ^ 251feed674dSShilei Tian // id 252feed674dSShilei Tian // However, it doesn't matter, because they're always on the two sides of 253feed674dSShilei Tian // id. The left one will in the end be overwritten by another CUstream. 254feed674dSShilei Tian // Therefore, after several execution, the order of pool might be different 255feed674dSShilei Tian // from its initial state. 256feed674dSShilei Tian void returnStream(const int DeviceId, CUstream Stream) { 257feed674dSShilei Tian const std::lock_guard<std::mutex> Lock(*StreamMtx[DeviceId]); 258feed674dSShilei Tian int &Id = NextStreamId[DeviceId]; 259feed674dSShilei Tian assert(Id > 0 && "Wrong stream ID"); 260feed674dSShilei Tian StreamPool[DeviceId][--Id] = Stream; 261feed674dSShilei Tian } 262feed674dSShilei Tian 2634031bb98SShilei Tian bool initializeDeviceStreamPool(const int DeviceId) { 2644031bb98SShilei Tian assert(StreamPool[DeviceId].empty() && "stream pool has been initialized"); 2654031bb98SShilei Tian 2664031bb98SShilei Tian resizeStreamPool(DeviceId, EnvNumInitialStreams); 2674031bb98SShilei Tian 2684031bb98SShilei Tian // Check the size of stream pool 269ad94fce8SJohannes Doerfert if (static_cast<int>(StreamPool[DeviceId].size()) != EnvNumInitialStreams) 2704031bb98SShilei Tian return false; 2714031bb98SShilei Tian 2724031bb98SShilei Tian // Check whether each stream is valid 2734031bb98SShilei Tian for (CUstream &S : StreamPool[DeviceId]) 2744031bb98SShilei Tian if (!S) 2754031bb98SShilei Tian return false; 2764031bb98SShilei Tian 2774031bb98SShilei Tian return true; 278feed674dSShilei Tian } 279feed674dSShilei Tian }; 280feed674dSShilei Tian 2814031bb98SShilei Tian class DeviceRTLTy { 2823de4cd12SGeorge Rokos int NumberOfDevices; 2834031bb98SShilei Tian // OpenMP environment properties 2843de4cd12SGeorge Rokos int EnvNumTeams; 2853de4cd12SGeorge Rokos int EnvTeamLimit; 286422adaa8SJoseph Huber int EnvTeamThreadLimit; 2874031bb98SShilei Tian // OpenMP requires flags 288c5fe030cSGheorghe-Teodor Bercea int64_t RequiresFlags; 289c5fe030cSGheorghe-Teodor Bercea 2904031bb98SShilei Tian static constexpr const int HardTeamLimit = 1U << 16U; // 64k 2914031bb98SShilei Tian static constexpr const int HardThreadLimit = 1024; 2924031bb98SShilei Tian static constexpr const int DefaultNumTeams = 128; 2934031bb98SShilei Tian static constexpr const int DefaultNumThreads = 128; 2943de4cd12SGeorge Rokos 2954031bb98SShilei Tian std::unique_ptr<StreamManagerTy> StreamManager; 2964031bb98SShilei Tian std::vector<DeviceDataTy> DeviceData; 2974031bb98SShilei Tian std::vector<CUmodule> Modules; 298feed674dSShilei Tian 2990871d6d5SShilei Tian /// A class responsible for interacting with device native runtime library to 3000871d6d5SShilei Tian /// allocate and free memory. 3010871d6d5SShilei Tian class CUDADeviceAllocatorTy : public DeviceAllocatorTy { 3020871d6d5SShilei Tian const int DeviceId; 3030871d6d5SShilei Tian const std::vector<DeviceDataTy> &DeviceData; 304a15f8589SJoseph Huber std::unordered_map<void *, TargetAllocTy> HostPinnedAllocs; 3050871d6d5SShilei Tian 3060871d6d5SShilei Tian public: 3070871d6d5SShilei Tian CUDADeviceAllocatorTy(int DeviceId, std::vector<DeviceDataTy> &DeviceData) 3080871d6d5SShilei Tian : DeviceId(DeviceId), DeviceData(DeviceData) {} 3090871d6d5SShilei Tian 310a15f8589SJoseph Huber void *allocate(size_t Size, void *, TargetAllocTy Kind) override { 3110871d6d5SShilei Tian if (Size == 0) 3120871d6d5SShilei Tian return nullptr; 3130871d6d5SShilei Tian 3140871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 3150871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 3160871d6d5SShilei Tian return nullptr; 3170871d6d5SShilei Tian 318a15f8589SJoseph Huber void *MemAlloc = nullptr; 319a15f8589SJoseph Huber switch (Kind) { 320a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 321a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 3220871d6d5SShilei Tian CUdeviceptr DevicePtr; 32395f0d1edSJon Chesterfield Err = cuMemAlloc(&DevicePtr, Size); 324a15f8589SJoseph Huber MemAlloc = (void *)DevicePtr; 32595f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemAlloc\n")) 3260871d6d5SShilei Tian return nullptr; 327a15f8589SJoseph Huber break; 328a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 329a15f8589SJoseph Huber void *HostPtr; 330a15f8589SJoseph Huber Err = cuMemAllocHost(&HostPtr, Size); 331a15f8589SJoseph Huber MemAlloc = HostPtr; 332a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocHost\n")) 333a15f8589SJoseph Huber return nullptr; 334a15f8589SJoseph Huber HostPinnedAllocs[MemAlloc] = Kind; 335a15f8589SJoseph Huber break; 336a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 337a15f8589SJoseph Huber CUdeviceptr SharedPtr; 338a15f8589SJoseph Huber Err = cuMemAllocManaged(&SharedPtr, Size, CU_MEM_ATTACH_GLOBAL); 339a15f8589SJoseph Huber MemAlloc = (void *)SharedPtr; 340a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocManaged\n")) 341a15f8589SJoseph Huber return nullptr; 342a15f8589SJoseph Huber break; 343a15f8589SJoseph Huber } 3440871d6d5SShilei Tian 345a15f8589SJoseph Huber return MemAlloc; 3460871d6d5SShilei Tian } 3470871d6d5SShilei Tian 3480871d6d5SShilei Tian int free(void *TgtPtr) override { 3490871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 3500871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 3510871d6d5SShilei Tian return OFFLOAD_FAIL; 3520871d6d5SShilei Tian 353a15f8589SJoseph Huber // Host pinned memory must be freed differently. 354a15f8589SJoseph Huber TargetAllocTy Kind = 355a15f8589SJoseph Huber (HostPinnedAllocs.find(TgtPtr) == HostPinnedAllocs.end()) 356a15f8589SJoseph Huber ? TARGET_ALLOC_DEFAULT 357a15f8589SJoseph Huber : TARGET_ALLOC_HOST; 358a15f8589SJoseph Huber switch (Kind) { 359a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 360a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 361a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 36295f0d1edSJon Chesterfield Err = cuMemFree((CUdeviceptr)TgtPtr); 36395f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemFree\n")) 3640871d6d5SShilei Tian return OFFLOAD_FAIL; 365a15f8589SJoseph Huber break; 366a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 367a15f8589SJoseph Huber Err = cuMemFreeHost(TgtPtr); 368a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemFreeHost\n")) 369a15f8589SJoseph Huber return OFFLOAD_FAIL; 370a15f8589SJoseph Huber break; 371a15f8589SJoseph Huber } 3720871d6d5SShilei Tian 3730871d6d5SShilei Tian return OFFLOAD_SUCCESS; 3740871d6d5SShilei Tian } 3750871d6d5SShilei Tian }; 3760871d6d5SShilei Tian 3770871d6d5SShilei Tian /// A vector of device allocators 3780871d6d5SShilei Tian std::vector<CUDADeviceAllocatorTy> DeviceAllocators; 3790871d6d5SShilei Tian 3800871d6d5SShilei Tian /// A vector of memory managers. Since the memory manager is non-copyable and 3810871d6d5SShilei Tian // non-removable, we wrap them into std::unique_ptr. 3820871d6d5SShilei Tian std::vector<std::unique_ptr<MemoryManagerTy>> MemoryManagers; 3830871d6d5SShilei Tian 3840871d6d5SShilei Tian /// Whether use memory manager 3850871d6d5SShilei Tian bool UseMemoryManager = true; 3860871d6d5SShilei Tian 3873de4cd12SGeorge Rokos // Record entry point associated with device 3884031bb98SShilei Tian void addOffloadEntry(const int DeviceId, const __tgt_offload_entry entry) { 3894031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 3903de4cd12SGeorge Rokos E.Entries.push_back(entry); 3913de4cd12SGeorge Rokos } 3923de4cd12SGeorge Rokos 393ae209397SJoseph Huber // Return a pointer to the entry associated with the pointer 394ae209397SJoseph Huber const __tgt_offload_entry *getOffloadEntry(const int DeviceId, 395ae209397SJoseph Huber const void *Addr) const { 3964031bb98SShilei Tian for (const __tgt_offload_entry &Itr : 3974031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.back().Entries) 3984031bb98SShilei Tian if (Itr.addr == Addr) 399ae209397SJoseph Huber return &Itr; 4003de4cd12SGeorge Rokos 401ae209397SJoseph Huber return nullptr; 4023de4cd12SGeorge Rokos } 4033de4cd12SGeorge Rokos 4043de4cd12SGeorge Rokos // Return the pointer to the target entries table 4054031bb98SShilei Tian __tgt_target_table *getOffloadEntriesTable(const int DeviceId) { 4064031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4073de4cd12SGeorge Rokos 4084031bb98SShilei Tian if (E.Entries.empty()) 4094031bb98SShilei Tian return nullptr; 4103de4cd12SGeorge Rokos 4113de4cd12SGeorge Rokos // Update table info according to the entries and return the pointer 4124031bb98SShilei Tian E.Table.EntriesBegin = E.Entries.data(); 4134031bb98SShilei Tian E.Table.EntriesEnd = E.Entries.data() + E.Entries.size(); 4143de4cd12SGeorge Rokos 4153de4cd12SGeorge Rokos return &E.Table; 4163de4cd12SGeorge Rokos } 4173de4cd12SGeorge Rokos 4183de4cd12SGeorge Rokos // Clear entries table for a device 4194031bb98SShilei Tian void clearOffloadEntriesTable(const int DeviceId) { 4204031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.emplace_back(); 4214031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4223de4cd12SGeorge Rokos E.Entries.clear(); 4234031bb98SShilei Tian E.Table.EntriesBegin = E.Table.EntriesEnd = nullptr; 4243de4cd12SGeorge Rokos } 4253de4cd12SGeorge Rokos 4265449fbb5SJohannes Doerfert CUstream getStream(const int DeviceId, __tgt_async_info *AsyncInfo) const { 4275449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 4284031bb98SShilei Tian 4295449fbb5SJohannes Doerfert if (!AsyncInfo->Queue) 4305449fbb5SJohannes Doerfert AsyncInfo->Queue = StreamManager->getStream(DeviceId); 4314031bb98SShilei Tian 4325449fbb5SJohannes Doerfert return reinterpret_cast<CUstream>(AsyncInfo->Queue); 433b305d26bSSergey Dmitriev } 4344031bb98SShilei Tian 4354031bb98SShilei Tian public: 4364031bb98SShilei Tian // This class should not be copied 4374031bb98SShilei Tian DeviceRTLTy(const DeviceRTLTy &) = delete; 4384031bb98SShilei Tian DeviceRTLTy(DeviceRTLTy &&) = delete; 4394031bb98SShilei Tian 4404031bb98SShilei Tian DeviceRTLTy() 4414031bb98SShilei Tian : NumberOfDevices(0), EnvNumTeams(-1), EnvTeamLimit(-1), 442422adaa8SJoseph Huber EnvTeamThreadLimit(-1), RequiresFlags(OMP_REQ_UNDEFINED) { 443b305d26bSSergey Dmitriev 4443de4cd12SGeorge Rokos DP("Start initializing CUDA\n"); 4453de4cd12SGeorge Rokos 4464031bb98SShilei Tian CUresult Err = cuInit(0); 4477baff00eSJon Chesterfield if (Err == CUDA_ERROR_INVALID_HANDLE) { 4487baff00eSJon Chesterfield // Can't call cuGetErrorString if dlsym failed 4497baff00eSJon Chesterfield DP("Failed to load CUDA shared library\n"); 4507baff00eSJon Chesterfield return; 4517baff00eSJon Chesterfield } 4524031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuInit\n")) { 4533de4cd12SGeorge Rokos return; 4543de4cd12SGeorge Rokos } 4553de4cd12SGeorge Rokos 4564031bb98SShilei Tian Err = cuDeviceGetCount(&NumberOfDevices); 4574031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGetCount\n")) 4583de4cd12SGeorge Rokos return; 4593de4cd12SGeorge Rokos 4603de4cd12SGeorge Rokos if (NumberOfDevices == 0) { 4613de4cd12SGeorge Rokos DP("There are no devices supporting CUDA.\n"); 4623de4cd12SGeorge Rokos return; 4633de4cd12SGeorge Rokos } 4643de4cd12SGeorge Rokos 4654031bb98SShilei Tian DeviceData.resize(NumberOfDevices); 4663de4cd12SGeorge Rokos 4673de4cd12SGeorge Rokos // Get environment variables regarding teams 4684031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_TEAM_LIMIT")) { 4693de4cd12SGeorge Rokos // OMP_TEAM_LIMIT has been set 4704031bb98SShilei Tian EnvTeamLimit = std::stoi(EnvStr); 4713de4cd12SGeorge Rokos DP("Parsed OMP_TEAM_LIMIT=%d\n", EnvTeamLimit); 4723de4cd12SGeorge Rokos } 473422adaa8SJoseph Huber if (const char *EnvStr = getenv("OMP_TEAMS_THREAD_LIMIT")) { 474422adaa8SJoseph Huber // OMP_TEAMS_THREAD_LIMIT has been set 475422adaa8SJoseph Huber EnvTeamThreadLimit = std::stoi(EnvStr); 476422adaa8SJoseph Huber DP("Parsed OMP_TEAMS_THREAD_LIMIT=%d\n", EnvTeamThreadLimit); 477422adaa8SJoseph Huber } 4784031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_NUM_TEAMS")) { 4793de4cd12SGeorge Rokos // OMP_NUM_TEAMS has been set 4804031bb98SShilei Tian EnvNumTeams = std::stoi(EnvStr); 4813de4cd12SGeorge Rokos DP("Parsed OMP_NUM_TEAMS=%d\n", EnvNumTeams); 4823de4cd12SGeorge Rokos } 483c5fe030cSGheorghe-Teodor Bercea 484feed674dSShilei Tian StreamManager = 4854031bb98SShilei Tian std::make_unique<StreamManagerTy>(NumberOfDevices, DeviceData); 4860871d6d5SShilei Tian 4870871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 4880871d6d5SShilei Tian DeviceAllocators.emplace_back(I, DeviceData); 4890871d6d5SShilei Tian 4900871d6d5SShilei Tian // Get the size threshold from environment variable 4910871d6d5SShilei Tian std::pair<size_t, bool> Res = MemoryManagerTy::getSizeThresholdFromEnv(); 4920871d6d5SShilei Tian UseMemoryManager = Res.second; 4930871d6d5SShilei Tian size_t MemoryManagerThreshold = Res.first; 4940871d6d5SShilei Tian 4950871d6d5SShilei Tian if (UseMemoryManager) 4960871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 4970871d6d5SShilei Tian MemoryManagers.emplace_back(std::make_unique<MemoryManagerTy>( 4980871d6d5SShilei Tian DeviceAllocators[I], MemoryManagerThreshold)); 4993de4cd12SGeorge Rokos } 5003de4cd12SGeorge Rokos 5014031bb98SShilei Tian ~DeviceRTLTy() { 5020871d6d5SShilei Tian // We first destruct memory managers in case that its dependent data are 5030871d6d5SShilei Tian // destroyed before it. 5040871d6d5SShilei Tian for (auto &M : MemoryManagers) 5050871d6d5SShilei Tian M.release(); 5060871d6d5SShilei Tian 507feed674dSShilei Tian StreamManager = nullptr; 508feed674dSShilei Tian 5094031bb98SShilei Tian for (CUmodule &M : Modules) 5104031bb98SShilei Tian // Close module 5114031bb98SShilei Tian if (M) 5124031bb98SShilei Tian checkResult(cuModuleUnload(M), "Error returned from cuModuleUnload\n"); 5134031bb98SShilei Tian 5144031bb98SShilei Tian for (DeviceDataTy &D : DeviceData) { 5154031bb98SShilei Tian // Destroy context 516c5348aecSYe Luo if (D.Context) { 517c5348aecSYe Luo checkResult(cuCtxSetCurrent(D.Context), 518c5348aecSYe Luo "Error returned from cuCtxSetCurrent\n"); 519c5348aecSYe Luo CUdevice Device; 520c5348aecSYe Luo checkResult(cuCtxGetDevice(&Device), 521c5348aecSYe Luo "Error returned from cuCtxGetDevice\n"); 52295f0d1edSJon Chesterfield checkResult(cuDevicePrimaryCtxRelease(Device), 52395f0d1edSJon Chesterfield "Error returned from cuDevicePrimaryCtxRelease\n"); 524c5348aecSYe Luo } 5253de4cd12SGeorge Rokos } 5263de4cd12SGeorge Rokos } 5273de4cd12SGeorge Rokos 5284031bb98SShilei Tian // Check whether a given DeviceId is valid 5294031bb98SShilei Tian bool isValidDeviceId(const int DeviceId) const { 5304031bb98SShilei Tian return DeviceId >= 0 && DeviceId < NumberOfDevices; 53132ed2927SShilei Tian } 53232ed2927SShilei Tian 533cb038927SShilei Tian int getNumOfDevices() const { return NumberOfDevices; } 5344031bb98SShilei Tian 5354031bb98SShilei Tian void setRequiresFlag(const int64_t Flags) { this->RequiresFlags = Flags; } 5364031bb98SShilei Tian 5374031bb98SShilei Tian int initDevice(const int DeviceId) { 5384031bb98SShilei Tian CUdevice Device; 5394031bb98SShilei Tian 5404031bb98SShilei Tian DP("Getting device %d\n", DeviceId); 5414031bb98SShilei Tian CUresult Err = cuDeviceGet(&Device, DeviceId); 5424031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGet\n")) 54332ed2927SShilei Tian return OFFLOAD_FAIL; 5443de4cd12SGeorge Rokos 545c5348aecSYe Luo // Query the current flags of the primary context and set its flags if 546c5348aecSYe Luo // it is inactive 547c5348aecSYe Luo unsigned int FormerPrimaryCtxFlags = 0; 548c5348aecSYe Luo int FormerPrimaryCtxIsActive = 0; 549c5348aecSYe Luo Err = cuDevicePrimaryCtxGetState(Device, &FormerPrimaryCtxFlags, 550c5348aecSYe Luo &FormerPrimaryCtxIsActive); 551c5348aecSYe Luo if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxGetState\n")) 552c5348aecSYe Luo return OFFLOAD_FAIL; 553c5348aecSYe Luo 554c5348aecSYe Luo if (FormerPrimaryCtxIsActive) { 555c5348aecSYe Luo DP("The primary context is active, no change to its flags\n"); 556c5348aecSYe Luo if ((FormerPrimaryCtxFlags & CU_CTX_SCHED_MASK) != 557c5348aecSYe Luo CU_CTX_SCHED_BLOCKING_SYNC) 558c5348aecSYe Luo DP("Warning the current flags are not CU_CTX_SCHED_BLOCKING_SYNC\n"); 559c5348aecSYe Luo } else { 560c5348aecSYe Luo DP("The primary context is inactive, set its flags to " 561c5348aecSYe Luo "CU_CTX_SCHED_BLOCKING_SYNC\n"); 56295f0d1edSJon Chesterfield Err = cuDevicePrimaryCtxSetFlags(Device, CU_CTX_SCHED_BLOCKING_SYNC); 56395f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxSetFlags\n")) 564c5348aecSYe Luo return OFFLOAD_FAIL; 565c5348aecSYe Luo } 566c5348aecSYe Luo 567c5348aecSYe Luo // Retain the per device primary context and save it to use whenever this 568c5348aecSYe Luo // device is selected. 569c5348aecSYe Luo Err = cuDevicePrimaryCtxRetain(&DeviceData[DeviceId].Context, Device); 570c5348aecSYe Luo if (!checkResult(Err, "Error returned from cuDevicePrimaryCtxRetain\n")) 5713de4cd12SGeorge Rokos return OFFLOAD_FAIL; 5723de4cd12SGeorge Rokos 5734031bb98SShilei Tian Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 5744031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 5754031bb98SShilei Tian return OFFLOAD_FAIL; 576a5153dbcSJohannes Doerfert 577feed674dSShilei Tian // Initialize stream pool 5784031bb98SShilei Tian if (!StreamManager->initializeDeviceStreamPool(DeviceId)) 5794031bb98SShilei Tian return OFFLOAD_FAIL; 580a5153dbcSJohannes Doerfert 581bb51d398SJonas Hahnfeld // Query attributes to determine number of threads/block and blocks/grid. 5824031bb98SShilei Tian int MaxGridDimX; 5834031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxGridDimX, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, 5844031bb98SShilei Tian Device); 5854031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 5864031bb98SShilei Tian DP("Error getting max grid dimension, use default value %d\n", 5874031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 5884031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = DeviceRTLTy::DefaultNumTeams; 5894031bb98SShilei Tian } else if (MaxGridDimX <= DeviceRTLTy::HardTeamLimit) { 5904031bb98SShilei Tian DP("Using %d CUDA blocks per grid\n", MaxGridDimX); 5914031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = MaxGridDimX; 5923de4cd12SGeorge Rokos } else { 5933de4cd12SGeorge Rokos DP("Max CUDA blocks per grid %d exceeds the hard team limit %d, capping " 594bb51d398SJonas Hahnfeld "at the hard limit\n", 5954031bb98SShilei Tian MaxGridDimX, DeviceRTLTy::HardTeamLimit); 5964031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = DeviceRTLTy::HardTeamLimit; 5973de4cd12SGeorge Rokos } 5983de4cd12SGeorge Rokos 599bb51d398SJonas Hahnfeld // We are only exploiting threads along the x axis. 6004031bb98SShilei Tian int MaxBlockDimX; 6014031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxBlockDimX, 6024031bb98SShilei Tian CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, Device); 6034031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6044031bb98SShilei Tian DP("Error getting max block dimension, use default value %d\n", 6054031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 6064031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::DefaultNumThreads; 607422adaa8SJoseph Huber } else { 6084031bb98SShilei Tian DP("Using %d CUDA threads per block\n", MaxBlockDimX); 6094031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = MaxBlockDimX; 610422adaa8SJoseph Huber 611422adaa8SJoseph Huber if (EnvTeamThreadLimit > 0 && 612422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock > EnvTeamThreadLimit) { 613422adaa8SJoseph Huber DP("Max CUDA threads per block %d exceeds the thread limit %d set by " 614422adaa8SJoseph Huber "OMP_TEAMS_THREAD_LIMIT, capping at the limit\n", 615422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, EnvTeamThreadLimit); 616422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock = EnvTeamThreadLimit; 617422adaa8SJoseph Huber } 618422adaa8SJoseph Huber if (DeviceData[DeviceId].ThreadsPerBlock > DeviceRTLTy::HardThreadLimit) { 6194031bb98SShilei Tian DP("Max CUDA threads per block %d exceeds the hard thread limit %d, " 6204031bb98SShilei Tian "capping at the hard limit\n", 621422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceRTLTy::HardThreadLimit); 6224031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::HardThreadLimit; 6233de4cd12SGeorge Rokos } 624422adaa8SJoseph Huber } 6253de4cd12SGeorge Rokos 6264031bb98SShilei Tian // Get and set warp size 6274031bb98SShilei Tian int WarpSize; 6284031bb98SShilei Tian Err = 6294031bb98SShilei Tian cuDeviceGetAttribute(&WarpSize, CU_DEVICE_ATTRIBUTE_WARP_SIZE, Device); 6304031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6314031bb98SShilei Tian DP("Error getting warp size, assume default value 32\n"); 6324031bb98SShilei Tian DeviceData[DeviceId].WarpSize = 32; 633bb51d398SJonas Hahnfeld } else { 6344031bb98SShilei Tian DP("Using warp size %d\n", WarpSize); 6354031bb98SShilei Tian DeviceData[DeviceId].WarpSize = WarpSize; 6363de4cd12SGeorge Rokos } 6373de4cd12SGeorge Rokos 6383de4cd12SGeorge Rokos // Adjust teams to the env variables 6394031bb98SShilei Tian if (EnvTeamLimit > 0 && DeviceData[DeviceId].BlocksPerGrid > EnvTeamLimit) { 6403de4cd12SGeorge Rokos DP("Capping max CUDA blocks per grid to OMP_TEAM_LIMIT=%d\n", 6414031bb98SShilei Tian EnvTeamLimit); 6424031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = EnvTeamLimit; 6433de4cd12SGeorge Rokos } 6443de4cd12SGeorge Rokos 645*3817ba13SJoseph Huber size_t StackLimit; 646*3817ba13SJoseph Huber size_t HeapLimit; 647*3817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_STACK_SIZE")) { 648*3817ba13SJoseph Huber StackLimit = std::stol(EnvStr); 649*3817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_STACK_SIZE, StackLimit) != CUDA_SUCCESS) 650*3817ba13SJoseph Huber return OFFLOAD_FAIL; 651*3817ba13SJoseph Huber } else { 652*3817ba13SJoseph Huber if (cuCtxGetLimit(&StackLimit, CU_LIMIT_STACK_SIZE) != CUDA_SUCCESS) 653*3817ba13SJoseph Huber return OFFLOAD_FAIL; 654*3817ba13SJoseph Huber } 655*3817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_HEAP_SIZE")) { 656*3817ba13SJoseph Huber HeapLimit = std::stol(EnvStr); 657*3817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_MALLOC_HEAP_SIZE, HeapLimit) != CUDA_SUCCESS) 658*3817ba13SJoseph Huber return OFFLOAD_FAIL; 659*3817ba13SJoseph Huber } else { 660*3817ba13SJoseph Huber if (cuCtxGetLimit(&HeapLimit, CU_LIMIT_MALLOC_HEAP_SIZE) != CUDA_SUCCESS) 661*3817ba13SJoseph Huber return OFFLOAD_FAIL; 662*3817ba13SJoseph Huber } 663*3817ba13SJoseph Huber 664119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 665ae209397SJoseph Huber "Device supports up to %d CUDA blocks and %d threads with a " 666ae209397SJoseph Huber "warp size of %d\n", 667ae209397SJoseph Huber DeviceData[DeviceId].BlocksPerGrid, 668ae209397SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceData[DeviceId].WarpSize); 669*3817ba13SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 670*3817ba13SJoseph Huber "Device heap size is %d Bytes, device stack size is %d Bytes per " 671*3817ba13SJoseph Huber "thread\n", 672*3817ba13SJoseph Huber (int)HeapLimit, (int)StackLimit); 6733de4cd12SGeorge Rokos 6743de4cd12SGeorge Rokos // Set default number of teams 6754031bb98SShilei Tian if (EnvNumTeams > 0) { 6763de4cd12SGeorge Rokos DP("Default number of teams set according to environment %d\n", 6774031bb98SShilei Tian EnvNumTeams); 6784031bb98SShilei Tian DeviceData[DeviceId].NumTeams = EnvNumTeams; 6793de4cd12SGeorge Rokos } else { 6804031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceRTLTy::DefaultNumTeams; 6813de4cd12SGeorge Rokos DP("Default number of teams set according to library's default %d\n", 6824031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 6833de4cd12SGeorge Rokos } 6844031bb98SShilei Tian 6854031bb98SShilei Tian if (DeviceData[DeviceId].NumTeams > DeviceData[DeviceId].BlocksPerGrid) { 6863de4cd12SGeorge Rokos DP("Default number of teams exceeds device limit, capping at %d\n", 6874031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 6884031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceData[DeviceId].BlocksPerGrid; 6893de4cd12SGeorge Rokos } 6903de4cd12SGeorge Rokos 6913de4cd12SGeorge Rokos // Set default number of threads 6924031bb98SShilei Tian DeviceData[DeviceId].NumThreads = DeviceRTLTy::DefaultNumThreads; 6933de4cd12SGeorge Rokos DP("Default number of threads set according to library's default %d\n", 6944031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 6954031bb98SShilei Tian if (DeviceData[DeviceId].NumThreads > 6964031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock) { 6973de4cd12SGeorge Rokos DP("Default number of threads exceeds device limit, capping at %d\n", 6984031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 6994031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceData[DeviceId].ThreadsPerBlock; 7003de4cd12SGeorge Rokos } 7013de4cd12SGeorge Rokos 7023de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 7033de4cd12SGeorge Rokos } 7043de4cd12SGeorge Rokos 7054031bb98SShilei Tian __tgt_target_table *loadBinary(const int DeviceId, 7064031bb98SShilei Tian const __tgt_device_image *Image) { 7074031bb98SShilei Tian // Set the context we are using 7084031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 7094031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 7104031bb98SShilei Tian return nullptr; 7113de4cd12SGeorge Rokos 7123de4cd12SGeorge Rokos // Clear the offload table as we are going to create a new one. 7134031bb98SShilei Tian clearOffloadEntriesTable(DeviceId); 7143de4cd12SGeorge Rokos 7153de4cd12SGeorge Rokos // Create the module and extract the function pointers. 7164031bb98SShilei Tian CUmodule Module; 7174031bb98SShilei Tian DP("Load data from image " DPxMOD "\n", DPxPTR(Image->ImageStart)); 7184031bb98SShilei Tian Err = cuModuleLoadDataEx(&Module, Image->ImageStart, 0, nullptr, nullptr); 7194031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuModuleLoadDataEx\n")) 7204031bb98SShilei Tian return nullptr; 7213de4cd12SGeorge Rokos 7223de4cd12SGeorge Rokos DP("CUDA module successfully loaded!\n"); 7234031bb98SShilei Tian 7244031bb98SShilei Tian Modules.push_back(Module); 7253de4cd12SGeorge Rokos 7263de4cd12SGeorge Rokos // Find the symbols in the module by name. 7274031bb98SShilei Tian const __tgt_offload_entry *HostBegin = Image->EntriesBegin; 7284031bb98SShilei Tian const __tgt_offload_entry *HostEnd = Image->EntriesEnd; 7293de4cd12SGeorge Rokos 7305272d29eSJohannes Doerfert std::list<KernelTy> &KernelsList = DeviceData[DeviceId].KernelsList; 7314031bb98SShilei Tian for (const __tgt_offload_entry *E = HostBegin; E != HostEnd; ++E) { 7324031bb98SShilei Tian if (!E->addr) { 7334031bb98SShilei Tian // We return nullptr when something like this happens, the host should 7344031bb98SShilei Tian // have always something in the address to uniquely identify the target 7354031bb98SShilei Tian // region. 7364031bb98SShilei Tian DP("Invalid binary: host entry '<null>' (size = %zd)...\n", E->size); 7374031bb98SShilei Tian return nullptr; 7383de4cd12SGeorge Rokos } 7393de4cd12SGeorge Rokos 7404031bb98SShilei Tian if (E->size) { 7414031bb98SShilei Tian __tgt_offload_entry Entry = *E; 7424031bb98SShilei Tian CUdeviceptr CUPtr; 7434031bb98SShilei Tian size_t CUSize; 74495f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&CUPtr, &CUSize, Module, E->name); 7454031bb98SShilei Tian // We keep this style here because we need the name 7464031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 7472ce16810SJoseph Huber REPORT("Loading global '%s' Failed\n", E->name); 7484031bb98SShilei Tian CUDA_ERR_STRING(Err); 7494031bb98SShilei Tian return nullptr; 7503de4cd12SGeorge Rokos } 7513de4cd12SGeorge Rokos 7524031bb98SShilei Tian if (CUSize != E->size) { 7534031bb98SShilei Tian DP("Loading global '%s' - size mismatch (%zd != %zd)\n", E->name, 7544031bb98SShilei Tian CUSize, E->size); 7554031bb98SShilei Tian return nullptr; 7563de4cd12SGeorge Rokos } 7573de4cd12SGeorge Rokos 7583de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to global %s (" DPxMOD ")\n", 7594031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(CUPtr)); 7604031bb98SShilei Tian 7614031bb98SShilei Tian Entry.addr = (void *)(CUPtr); 7623de4cd12SGeorge Rokos 763aace6d28SGheorghe-Teodor Bercea // Note: In the current implementation declare target variables 764aace6d28SGheorghe-Teodor Bercea // can either be link or to. This means that once unified 765aace6d28SGheorghe-Teodor Bercea // memory is activated via the requires directive, the variable 766aace6d28SGheorghe-Teodor Bercea // can be used directly from the host in both cases. 767aace6d28SGheorghe-Teodor Bercea // TODO: when variables types other than to or link are added, 7684c6a098aSKazuaki Ishizaki // the below condition should be changed to explicitly 769aace6d28SGheorghe-Teodor Bercea // check for to and link variables types: 7704031bb98SShilei Tian // (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && (e->flags & 7714031bb98SShilei Tian // OMP_DECLARE_TARGET_LINK || e->flags == OMP_DECLARE_TARGET_TO)) 7724031bb98SShilei Tian if (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) { 773aace6d28SGheorghe-Teodor Bercea // If unified memory is present any target link or to variables 774c5fe030cSGheorghe-Teodor Bercea // can access host addresses directly. There is no longer a 775c5fe030cSGheorghe-Teodor Bercea // need for device copies. 77695f0d1edSJon Chesterfield cuMemcpyHtoD(CUPtr, E->addr, sizeof(void *)); 7774031bb98SShilei Tian DP("Copy linked variable host address (" DPxMOD 7784031bb98SShilei Tian ") to device address (" DPxMOD ")\n", 7794031bb98SShilei Tian DPxPTR(*((void **)E->addr)), DPxPTR(CUPtr)); 780c5fe030cSGheorghe-Teodor Bercea } 781c5fe030cSGheorghe-Teodor Bercea 7824031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 7833de4cd12SGeorge Rokos 7843de4cd12SGeorge Rokos continue; 7853de4cd12SGeorge Rokos } 7863de4cd12SGeorge Rokos 7874031bb98SShilei Tian CUfunction Func; 7884031bb98SShilei Tian Err = cuModuleGetFunction(&Func, Module, E->name); 7894031bb98SShilei Tian // We keep this style here because we need the name 7904031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 7912ce16810SJoseph Huber REPORT("Loading '%s' Failed\n", E->name); 7924031bb98SShilei Tian CUDA_ERR_STRING(Err); 7934031bb98SShilei Tian return nullptr; 7943de4cd12SGeorge Rokos } 7953de4cd12SGeorge Rokos 7963de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to %s (" DPxMOD ")\n", 7974031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(Func)); 7983de4cd12SGeorge Rokos 7993de4cd12SGeorge Rokos // default value GENERIC (in case symbol is missing from cubin file) 8003de4cd12SGeorge Rokos int8_t ExecModeVal = ExecutionModeType::GENERIC; 8014031bb98SShilei Tian std::string ExecModeNameStr(E->name); 8023de4cd12SGeorge Rokos ExecModeNameStr += "_exec_mode"; 8033de4cd12SGeorge Rokos const char *ExecModeName = ExecModeNameStr.c_str(); 8043de4cd12SGeorge Rokos 8053de4cd12SGeorge Rokos CUdeviceptr ExecModePtr; 8064031bb98SShilei Tian size_t CUSize; 80795f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&ExecModePtr, &CUSize, Module, ExecModeName); 8084031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 8094031bb98SShilei Tian if (CUSize != sizeof(int8_t)) { 8103de4cd12SGeorge Rokos DP("Loading global exec_mode '%s' - size mismatch (%zd != %zd)\n", 8114031bb98SShilei Tian ExecModeName, CUSize, sizeof(int8_t)); 8124031bb98SShilei Tian return nullptr; 8133de4cd12SGeorge Rokos } 8143de4cd12SGeorge Rokos 81595f0d1edSJon Chesterfield Err = cuMemcpyDtoH(&ExecModeVal, ExecModePtr, CUSize); 8164031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8172ce16810SJoseph Huber REPORT("Error when copying data from device to host. Pointers: " 8183de4cd12SGeorge Rokos "host = " DPxMOD ", device = " DPxMOD ", size = %zd\n", 8194031bb98SShilei Tian DPxPTR(&ExecModeVal), DPxPTR(ExecModePtr), CUSize); 8204031bb98SShilei Tian CUDA_ERR_STRING(Err); 8214031bb98SShilei Tian return nullptr; 8223de4cd12SGeorge Rokos } 8233de4cd12SGeorge Rokos 8247d576392SJoseph Huber if (ExecModeVal < 0 || ExecModeVal > 2) { 8253de4cd12SGeorge Rokos DP("Error wrong exec_mode value specified in cubin file: %d\n", 8263de4cd12SGeorge Rokos ExecModeVal); 8274031bb98SShilei Tian return nullptr; 8283de4cd12SGeorge Rokos } 8293de4cd12SGeorge Rokos } else { 8302ce16810SJoseph Huber REPORT("Loading global exec_mode '%s' - symbol missing, using default " 8314031bb98SShilei Tian "value GENERIC (1)\n", 8324031bb98SShilei Tian ExecModeName); 8334031bb98SShilei Tian CUDA_ERR_STRING(Err); 8343de4cd12SGeorge Rokos } 8353de4cd12SGeorge Rokos 8364031bb98SShilei Tian KernelsList.emplace_back(Func, ExecModeVal); 8373de4cd12SGeorge Rokos 8384031bb98SShilei Tian __tgt_offload_entry Entry = *E; 8394031bb98SShilei Tian Entry.addr = &KernelsList.back(); 8404031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 8413de4cd12SGeorge Rokos } 8423de4cd12SGeorge Rokos 843e1c7a46dSGuansong Zhang // send device environment data to the device 844e1c7a46dSGuansong Zhang { 8454031bb98SShilei Tian omptarget_device_environmentTy DeviceEnv{0}; 846e1c7a46dSGuansong Zhang 847e1c7a46dSGuansong Zhang #ifdef OMPTARGET_DEBUG 8484031bb98SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_DEVICE_RTL_DEBUG")) 8494031bb98SShilei Tian DeviceEnv.debug_level = std::stoi(EnvStr); 850e1c7a46dSGuansong Zhang #endif 851e1c7a46dSGuansong Zhang 8524031bb98SShilei Tian const char *DeviceEnvName = "omptarget_device_environment"; 8534031bb98SShilei Tian CUdeviceptr DeviceEnvPtr; 8544031bb98SShilei Tian size_t CUSize; 855e1c7a46dSGuansong Zhang 85695f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&DeviceEnvPtr, &CUSize, Module, DeviceEnvName); 8574031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 8584031bb98SShilei Tian if (CUSize != sizeof(DeviceEnv)) { 8592ce16810SJoseph Huber REPORT( 8602ce16810SJoseph Huber "Global device_environment '%s' - size mismatch (%zu != %zu)\n", 8614031bb98SShilei Tian DeviceEnvName, CUSize, sizeof(int32_t)); 8624031bb98SShilei Tian CUDA_ERR_STRING(Err); 8634031bb98SShilei Tian return nullptr; 864e1c7a46dSGuansong Zhang } 865e1c7a46dSGuansong Zhang 86695f0d1edSJon Chesterfield Err = cuMemcpyHtoD(DeviceEnvPtr, &DeviceEnv, CUSize); 8674031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8682ce16810SJoseph Huber REPORT("Error when copying data from host to device. Pointers: " 869e1c7a46dSGuansong Zhang "host = " DPxMOD ", device = " DPxMOD ", size = %zu\n", 8704031bb98SShilei Tian DPxPTR(&DeviceEnv), DPxPTR(DeviceEnvPtr), CUSize); 8714031bb98SShilei Tian CUDA_ERR_STRING(Err); 8724031bb98SShilei Tian return nullptr; 873e1c7a46dSGuansong Zhang } 874e1c7a46dSGuansong Zhang 8754031bb98SShilei Tian DP("Sending global device environment data %zu bytes\n", CUSize); 876e1c7a46dSGuansong Zhang } else { 8774031bb98SShilei Tian DP("Finding global device environment '%s' - symbol missing.\n", 8784031bb98SShilei Tian DeviceEnvName); 8794031bb98SShilei Tian DP("Continue, considering this is a device RTL which does not accept " 8804031bb98SShilei Tian "environment setting.\n"); 881e1c7a46dSGuansong Zhang } 882e1c7a46dSGuansong Zhang } 883e1c7a46dSGuansong Zhang 8844031bb98SShilei Tian return getOffloadEntriesTable(DeviceId); 8853de4cd12SGeorge Rokos } 8863de4cd12SGeorge Rokos 887a15f8589SJoseph Huber void *dataAlloc(const int DeviceId, const int64_t Size, 888a15f8589SJoseph Huber const TargetAllocTy Kind) { 889a15f8589SJoseph Huber switch (Kind) { 890a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 891a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 8920871d6d5SShilei Tian if (UseMemoryManager) 8930871d6d5SShilei Tian return MemoryManagers[DeviceId]->allocate(Size, nullptr); 894a15f8589SJoseph Huber else 895a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 896a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 897a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 898a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 899a15f8589SJoseph Huber } 9004031bb98SShilei Tian 901a15f8589SJoseph Huber REPORT("Invalid target data allocation kind or requested allocator not " 902a15f8589SJoseph Huber "implemented yet\n"); 903a15f8589SJoseph Huber 904a15f8589SJoseph Huber return nullptr; 9053de4cd12SGeorge Rokos } 9063de4cd12SGeorge Rokos 9074031bb98SShilei Tian int dataSubmit(const int DeviceId, const void *TgtPtr, const void *HstPtr, 9085449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 9095449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 9103de4cd12SGeorge Rokos 9114031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 9124031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 9133de4cd12SGeorge Rokos return OFFLOAD_FAIL; 9143de4cd12SGeorge Rokos 9155449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 9163de4cd12SGeorge Rokos 91795f0d1edSJon Chesterfield Err = cuMemcpyHtoDAsync((CUdeviceptr)TgtPtr, HstPtr, Size, Stream); 9184031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 91929338459SJoseph Huber DP("Error when copying data from host to device. Pointers: host " 9202ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 9214031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 9224031bb98SShilei Tian CUDA_ERR_STRING(Err); 9233de4cd12SGeorge Rokos return OFFLOAD_FAIL; 9243de4cd12SGeorge Rokos } 9253de4cd12SGeorge Rokos 9263de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 9273de4cd12SGeorge Rokos } 9283de4cd12SGeorge Rokos 9294031bb98SShilei Tian int dataRetrieve(const int DeviceId, void *HstPtr, const void *TgtPtr, 9305449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 9315449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 9324031bb98SShilei Tian 9334031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 9344031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 93503ff643dSShilei Tian return OFFLOAD_FAIL; 93603ff643dSShilei Tian 9375449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 93803ff643dSShilei Tian 93995f0d1edSJon Chesterfield Err = cuMemcpyDtoHAsync(HstPtr, (CUdeviceptr)TgtPtr, Size, Stream); 9404031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 94129338459SJoseph Huber DP("Error when copying data from device to host. Pointers: host " 9422ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 9434031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 9444031bb98SShilei Tian CUDA_ERR_STRING(Err); 9453de4cd12SGeorge Rokos return OFFLOAD_FAIL; 9463de4cd12SGeorge Rokos } 9473de4cd12SGeorge Rokos 9484031bb98SShilei Tian return OFFLOAD_SUCCESS; 9494031bb98SShilei Tian } 9504031bb98SShilei Tian 951a014fbbcSShilei Tian int dataExchange(int SrcDevId, const void *SrcPtr, int DstDevId, void *DstPtr, 9525449fbb5SJohannes Doerfert int64_t Size, __tgt_async_info *AsyncInfo) const { 9535449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 954a014fbbcSShilei Tian 955a014fbbcSShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[SrcDevId].Context); 956a014fbbcSShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 957a014fbbcSShilei Tian return OFFLOAD_FAIL; 958a014fbbcSShilei Tian 9595449fbb5SJohannes Doerfert CUstream Stream = getStream(SrcDevId, AsyncInfo); 960a014fbbcSShilei Tian 961a014fbbcSShilei Tian // If they are two devices, we try peer to peer copy first 962a014fbbcSShilei Tian if (SrcDevId != DstDevId) { 963a014fbbcSShilei Tian int CanAccessPeer = 0; 964a014fbbcSShilei Tian Err = cuDeviceCanAccessPeer(&CanAccessPeer, SrcDevId, DstDevId); 965a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 9662ce16810SJoseph Huber REPORT("Error returned from cuDeviceCanAccessPeer. src = %" PRId32 967a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 968a014fbbcSShilei Tian SrcDevId, DstDevId); 969a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 970a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 971a014fbbcSShilei Tian } 972a014fbbcSShilei Tian 973a014fbbcSShilei Tian if (!CanAccessPeer) { 974a014fbbcSShilei Tian DP("P2P memcpy not supported so fall back to D2D memcpy"); 975a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 976a014fbbcSShilei Tian } 977a014fbbcSShilei Tian 978a014fbbcSShilei Tian Err = cuCtxEnablePeerAccess(DeviceData[DstDevId].Context, 0); 979a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 9802ce16810SJoseph Huber REPORT("Error returned from cuCtxEnablePeerAccess. src = %" PRId32 981a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 982a014fbbcSShilei Tian SrcDevId, DstDevId); 983a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 984a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 985a014fbbcSShilei Tian } 986a014fbbcSShilei Tian 987a014fbbcSShilei Tian Err = cuMemcpyPeerAsync((CUdeviceptr)DstPtr, DeviceData[DstDevId].Context, 988a014fbbcSShilei Tian (CUdeviceptr)SrcPtr, DeviceData[SrcDevId].Context, 989a014fbbcSShilei Tian Size, Stream); 990a014fbbcSShilei Tian if (Err == CUDA_SUCCESS) 991a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 992a014fbbcSShilei Tian 99329338459SJoseph Huber DP("Error returned from cuMemcpyPeerAsync. src_ptr = " DPxMOD 99429338459SJoseph Huber ", src_id =%" PRId32 ", dst_ptr = " DPxMOD ", dst_id =%" PRId32 "\n", 99545bb073dSYe Luo DPxPTR(SrcPtr), SrcDevId, DPxPTR(DstPtr), DstDevId); 996a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 997a014fbbcSShilei Tian } 998a014fbbcSShilei Tian 999a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1000a014fbbcSShilei Tian } 1001a014fbbcSShilei Tian 10020871d6d5SShilei Tian int dataDelete(const int DeviceId, void *TgtPtr) { 10030871d6d5SShilei Tian if (UseMemoryManager) 10040871d6d5SShilei Tian return MemoryManagers[DeviceId]->free(TgtPtr); 10054031bb98SShilei Tian 10060871d6d5SShilei Tian return DeviceAllocators[DeviceId].free(TgtPtr); 10074031bb98SShilei Tian } 10084031bb98SShilei Tian 1009aa27cfc1SJohannes Doerfert int runTargetTeamRegion(const int DeviceId, void *TgtEntryPtr, void **TgtArgs, 1010aa27cfc1SJohannes Doerfert ptrdiff_t *TgtOffsets, const int ArgNum, 1011aa27cfc1SJohannes Doerfert const int TeamNum, const int ThreadLimit, 10124031bb98SShilei Tian const unsigned int LoopTripCount, 10134031bb98SShilei Tian __tgt_async_info *AsyncInfo) const { 10144031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 10154031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 10164031bb98SShilei Tian return OFFLOAD_FAIL; 10174031bb98SShilei Tian 10183de4cd12SGeorge Rokos // All args are references. 10194031bb98SShilei Tian std::vector<void *> Args(ArgNum); 10204031bb98SShilei Tian std::vector<void *> Ptrs(ArgNum); 10213de4cd12SGeorge Rokos 10224031bb98SShilei Tian for (int I = 0; I < ArgNum; ++I) { 10234031bb98SShilei Tian Ptrs[I] = (void *)((intptr_t)TgtArgs[I] + TgtOffsets[I]); 10244031bb98SShilei Tian Args[I] = &Ptrs[I]; 10251546d319SGeorge Rokos } 10263de4cd12SGeorge Rokos 1027aa27cfc1SJohannes Doerfert KernelTy *KernelInfo = reinterpret_cast<KernelTy *>(TgtEntryPtr); 10283de4cd12SGeorge Rokos 1029aa27cfc1SJohannes Doerfert int CudaThreadsPerBlock; 10304031bb98SShilei Tian if (ThreadLimit > 0) { 10314031bb98SShilei Tian DP("Setting CUDA threads per block to requested %d\n", ThreadLimit); 10324031bb98SShilei Tian CudaThreadsPerBlock = ThreadLimit; 10333de4cd12SGeorge Rokos // Add master warp if necessary 10343de4cd12SGeorge Rokos if (KernelInfo->ExecutionMode == GENERIC) { 10354031bb98SShilei Tian DP("Adding master warp: +%d threads\n", DeviceData[DeviceId].WarpSize); 10364031bb98SShilei Tian CudaThreadsPerBlock += DeviceData[DeviceId].WarpSize; 10373de4cd12SGeorge Rokos } 1038c13df8e5SGeorge Rokos } else { 1039c13df8e5SGeorge Rokos DP("Setting CUDA threads per block to default %d\n", 10404031bb98SShilei Tian DeviceData[DeviceId].NumThreads); 10414031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].NumThreads; 1042c13df8e5SGeorge Rokos } 10433de4cd12SGeorge Rokos 10444031bb98SShilei Tian if (CudaThreadsPerBlock > DeviceData[DeviceId].ThreadsPerBlock) { 10453de4cd12SGeorge Rokos DP("Threads per block capped at device limit %d\n", 10464031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 10474031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].ThreadsPerBlock; 10483de4cd12SGeorge Rokos } 10493de4cd12SGeorge Rokos 1050aa27cfc1SJohannes Doerfert if (!KernelInfo->MaxThreadsPerBlock) { 1051aa27cfc1SJohannes Doerfert Err = cuFuncGetAttribute(&KernelInfo->MaxThreadsPerBlock, 10524031bb98SShilei Tian CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, 10534031bb98SShilei Tian KernelInfo->Func); 1054aa27cfc1SJohannes Doerfert if (!checkResult(Err, "Error returned from cuFuncGetAttribute\n")) 1055aa27cfc1SJohannes Doerfert return OFFLOAD_FAIL; 1056aa27cfc1SJohannes Doerfert } 1057aa27cfc1SJohannes Doerfert 1058aa27cfc1SJohannes Doerfert if (KernelInfo->MaxThreadsPerBlock < CudaThreadsPerBlock) { 1059aa27cfc1SJohannes Doerfert DP("Threads per block capped at kernel limit %d\n", 1060aa27cfc1SJohannes Doerfert KernelInfo->MaxThreadsPerBlock); 1061aa27cfc1SJohannes Doerfert CudaThreadsPerBlock = KernelInfo->MaxThreadsPerBlock; 10623de4cd12SGeorge Rokos } 10633de4cd12SGeorge Rokos 10644031bb98SShilei Tian unsigned int CudaBlocksPerGrid; 10654031bb98SShilei Tian if (TeamNum <= 0) { 10664031bb98SShilei Tian if (LoopTripCount > 0 && EnvNumTeams < 0) { 1067c13df8e5SGeorge Rokos if (KernelInfo->ExecutionMode == SPMD) { 10684031bb98SShilei Tian // We have a combined construct, i.e. `target teams distribute 10694031bb98SShilei Tian // parallel for [simd]`. We launch so many teams so that each thread 10704031bb98SShilei Tian // will execute one iteration of the loop. round up to the nearest 10714031bb98SShilei Tian // integer 10724031bb98SShilei Tian CudaBlocksPerGrid = ((LoopTripCount - 1) / CudaThreadsPerBlock) + 1; 10737d576392SJoseph Huber } else if (KernelInfo->ExecutionMode == GENERIC) { 1074c13df8e5SGeorge Rokos // If we reach this point, then we have a non-combined construct, i.e. 1075c13df8e5SGeorge Rokos // `teams distribute` with a nested `parallel for` and each team is 1076c13df8e5SGeorge Rokos // assigned one iteration of the `distribute` loop. E.g.: 1077c13df8e5SGeorge Rokos // 1078c13df8e5SGeorge Rokos // #pragma omp target teams distribute 1079c13df8e5SGeorge Rokos // for(...loop_tripcount...) { 1080c13df8e5SGeorge Rokos // #pragma omp parallel for 1081c13df8e5SGeorge Rokos // for(...) {} 1082c13df8e5SGeorge Rokos // } 1083c13df8e5SGeorge Rokos // 1084c13df8e5SGeorge Rokos // Threads within a team will execute the iterations of the `parallel` 1085c13df8e5SGeorge Rokos // loop. 10864031bb98SShilei Tian CudaBlocksPerGrid = LoopTripCount; 10877d576392SJoseph Huber } else if (KernelInfo->ExecutionMode == SPMD_GENERIC) { 10887d576392SJoseph Huber // If we reach this point, then we are executing a kernel that was 10897d576392SJoseph Huber // transformed from Generic-mode to SPMD-mode. This kernel has 10907d576392SJoseph Huber // SPMD-mode execution, but needs its blocks to be scheduled 10917d576392SJoseph Huber // differently because the current loop trip count only applies to the 10927d576392SJoseph Huber // `teams distribute` region and will create var too few blocks using 10937d576392SJoseph Huber // the regular SPMD-mode method. 10947d576392SJoseph Huber CudaBlocksPerGrid = LoopTripCount; 1095a158d366SJoseph Huber } else { 1096a158d366SJoseph Huber REPORT("Unknown execution mode: %d\n", KernelInfo->ExecutionMode); 1097a158d366SJoseph Huber return OFFLOAD_FAIL; 1098c13df8e5SGeorge Rokos } 109945bb073dSYe Luo DP("Using %d teams due to loop trip count %" PRIu32 11004031bb98SShilei Tian " and number of threads per block %d\n", 11014031bb98SShilei Tian CudaBlocksPerGrid, LoopTripCount, CudaThreadsPerBlock); 11023de4cd12SGeorge Rokos } else { 11034031bb98SShilei Tian DP("Using default number of teams %d\n", DeviceData[DeviceId].NumTeams); 11044031bb98SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].NumTeams; 11053de4cd12SGeorge Rokos } 11064031bb98SShilei Tian } else if (TeamNum > DeviceData[DeviceId].BlocksPerGrid) { 11073de4cd12SGeorge Rokos DP("Capping number of teams to team limit %d\n", 11084031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 11094031bb98SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].BlocksPerGrid; 11103de4cd12SGeorge Rokos } else { 11114031bb98SShilei Tian DP("Using requested number of teams %d\n", TeamNum); 11124031bb98SShilei Tian CudaBlocksPerGrid = TeamNum; 11133de4cd12SGeorge Rokos } 11143de4cd12SGeorge Rokos 1115119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 1116ae209397SJoseph Huber "Launching kernel %s with %d blocks and %d threads in %s " 1117ae209397SJoseph Huber "mode\n", 1118ae209397SJoseph Huber (getOffloadEntry(DeviceId, TgtEntryPtr)) 1119ae209397SJoseph Huber ? getOffloadEntry(DeviceId, TgtEntryPtr)->name 1120ae209397SJoseph Huber : "(null)", 1121ae209397SJoseph Huber CudaBlocksPerGrid, CudaThreadsPerBlock, 11227d576392SJoseph Huber (KernelInfo->ExecutionMode != SPMD 11237d576392SJoseph Huber ? (KernelInfo->ExecutionMode == GENERIC ? "Generic" : "SPMD-Generic") 11247d576392SJoseph Huber : "SPMD")); 11253de4cd12SGeorge Rokos 11264031bb98SShilei Tian CUstream Stream = getStream(DeviceId, AsyncInfo); 11274031bb98SShilei Tian Err = cuLaunchKernel(KernelInfo->Func, CudaBlocksPerGrid, /* gridDimY */ 1, 11284031bb98SShilei Tian /* gridDimZ */ 1, CudaThreadsPerBlock, 11294031bb98SShilei Tian /* blockDimY */ 1, /* blockDimZ */ 1, 11304031bb98SShilei Tian /* sharedMemBytes */ 0, Stream, &Args[0], nullptr); 11314031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuLaunchKernel\n")) 11324031bb98SShilei Tian return OFFLOAD_FAIL; 11334031bb98SShilei Tian 11344031bb98SShilei Tian DP("Launch of entry point at " DPxMOD " successful!\n", 11354031bb98SShilei Tian DPxPTR(TgtEntryPtr)); 11364031bb98SShilei Tian 11374031bb98SShilei Tian return OFFLOAD_SUCCESS; 11384031bb98SShilei Tian } 11394031bb98SShilei Tian 11405449fbb5SJohannes Doerfert int synchronize(const int DeviceId, __tgt_async_info *AsyncInfo) const { 11415449fbb5SJohannes Doerfert CUstream Stream = reinterpret_cast<CUstream>(AsyncInfo->Queue); 11424031bb98SShilei Tian CUresult Err = cuStreamSynchronize(Stream); 11433de4cd12SGeorge Rokos 11444031bb98SShilei Tian // Once the stream is synchronized, return it to stream pool and reset 11455449fbb5SJohannes Doerfert // AsyncInfo. This is to make sure the synchronization only works for its 11464031bb98SShilei Tian // own tasks. 11475449fbb5SJohannes Doerfert StreamManager->returnStream(DeviceId, 11485449fbb5SJohannes Doerfert reinterpret_cast<CUstream>(AsyncInfo->Queue)); 11495449fbb5SJohannes Doerfert AsyncInfo->Queue = nullptr; 11503de4cd12SGeorge Rokos 115116064e71SJoseph Huber if (Err != CUDA_SUCCESS) { 115229338459SJoseph Huber DP("Error when synchronizing stream. stream = " DPxMOD 115316064e71SJoseph Huber ", async info ptr = " DPxMOD "\n", 115416064e71SJoseph Huber DPxPTR(Stream), DPxPTR(AsyncInfo)); 115516064e71SJoseph Huber CUDA_ERR_STRING(Err); 115616064e71SJoseph Huber } 115716064e71SJoseph Huber return (Err == CUDA_SUCCESS) ? OFFLOAD_SUCCESS : OFFLOAD_FAIL; 11583de4cd12SGeorge Rokos } 11594031bb98SShilei Tian }; 11604031bb98SShilei Tian 11614031bb98SShilei Tian DeviceRTLTy DeviceRTL; 11624031bb98SShilei Tian } // namespace 11634031bb98SShilei Tian 11644031bb98SShilei Tian // Exposed library API function 11654031bb98SShilei Tian #ifdef __cplusplus 11664031bb98SShilei Tian extern "C" { 11674031bb98SShilei Tian #endif 11684031bb98SShilei Tian 11694031bb98SShilei Tian int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *image) { 11704031bb98SShilei Tian return elf_check_machine(image, /* EM_CUDA */ 190); 11714031bb98SShilei Tian } 11724031bb98SShilei Tian 11734031bb98SShilei Tian int32_t __tgt_rtl_number_of_devices() { return DeviceRTL.getNumOfDevices(); } 11744031bb98SShilei Tian 11754031bb98SShilei Tian int64_t __tgt_rtl_init_requires(int64_t RequiresFlags) { 117640470eb2SGeorge Rokos DP("Init requires flags to %" PRId64 "\n", RequiresFlags); 11774031bb98SShilei Tian DeviceRTL.setRequiresFlag(RequiresFlags); 11784031bb98SShilei Tian return RequiresFlags; 11794031bb98SShilei Tian } 11804031bb98SShilei Tian 1181a014fbbcSShilei Tian int32_t __tgt_rtl_is_data_exchangable(int32_t src_dev_id, int dst_dev_id) { 1182a014fbbcSShilei Tian if (DeviceRTL.isValidDeviceId(src_dev_id) && 1183a014fbbcSShilei Tian DeviceRTL.isValidDeviceId(dst_dev_id)) 1184a014fbbcSShilei Tian return 1; 1185a014fbbcSShilei Tian 1186a014fbbcSShilei Tian return 0; 1187a014fbbcSShilei Tian } 1188a014fbbcSShilei Tian 11894031bb98SShilei Tian int32_t __tgt_rtl_init_device(int32_t device_id) { 11904031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 11914031bb98SShilei Tian 11924031bb98SShilei Tian return DeviceRTL.initDevice(device_id); 11934031bb98SShilei Tian } 11944031bb98SShilei Tian 11954031bb98SShilei Tian __tgt_target_table *__tgt_rtl_load_binary(int32_t device_id, 11964031bb98SShilei Tian __tgt_device_image *image) { 11974031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 11984031bb98SShilei Tian 11994031bb98SShilei Tian return DeviceRTL.loadBinary(device_id, image); 12004031bb98SShilei Tian } 12014031bb98SShilei Tian 12022468fdd9SGeorge Rokos void *__tgt_rtl_data_alloc(int32_t device_id, int64_t size, void *, 12032468fdd9SGeorge Rokos int32_t kind) { 12044031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12054031bb98SShilei Tian 1206a15f8589SJoseph Huber return DeviceRTL.dataAlloc(device_id, size, (TargetAllocTy)kind); 12074031bb98SShilei Tian } 12084031bb98SShilei Tian 12094031bb98SShilei Tian int32_t __tgt_rtl_data_submit(int32_t device_id, void *tgt_ptr, void *hst_ptr, 12104031bb98SShilei Tian int64_t size) { 12114031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12124031bb98SShilei Tian 12135449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 12144031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_submit_async(device_id, tgt_ptr, hst_ptr, 12155449fbb5SJohannes Doerfert size, &AsyncInfo); 12164031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 12174031bb98SShilei Tian return OFFLOAD_FAIL; 12184031bb98SShilei Tian 12195449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 12204031bb98SShilei Tian } 12214031bb98SShilei Tian 12224031bb98SShilei Tian int32_t __tgt_rtl_data_submit_async(int32_t device_id, void *tgt_ptr, 12234031bb98SShilei Tian void *hst_ptr, int64_t size, 12244031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 12254031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12264031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 12274031bb98SShilei Tian 12284031bb98SShilei Tian return DeviceRTL.dataSubmit(device_id, tgt_ptr, hst_ptr, size, 12294031bb98SShilei Tian async_info_ptr); 12304031bb98SShilei Tian } 12314031bb98SShilei Tian 12324031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve(int32_t device_id, void *hst_ptr, void *tgt_ptr, 12334031bb98SShilei Tian int64_t size) { 12344031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12354031bb98SShilei Tian 12365449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 12374031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_retrieve_async(device_id, hst_ptr, tgt_ptr, 12385449fbb5SJohannes Doerfert size, &AsyncInfo); 12394031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 12404031bb98SShilei Tian return OFFLOAD_FAIL; 12414031bb98SShilei Tian 12425449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 12434031bb98SShilei Tian } 12444031bb98SShilei Tian 12454031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve_async(int32_t device_id, void *hst_ptr, 12464031bb98SShilei Tian void *tgt_ptr, int64_t size, 12474031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 12484031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12494031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 12504031bb98SShilei Tian 12514031bb98SShilei Tian return DeviceRTL.dataRetrieve(device_id, hst_ptr, tgt_ptr, size, 12524031bb98SShilei Tian async_info_ptr); 12534031bb98SShilei Tian } 12544031bb98SShilei Tian 1255a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange_async(int32_t src_dev_id, void *src_ptr, 1256a014fbbcSShilei Tian int dst_dev_id, void *dst_ptr, 1257a014fbbcSShilei Tian int64_t size, 12585449fbb5SJohannes Doerfert __tgt_async_info *AsyncInfo) { 1259a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1260a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 12615449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 1262a014fbbcSShilei Tian 1263a014fbbcSShilei Tian return DeviceRTL.dataExchange(src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, 12645449fbb5SJohannes Doerfert AsyncInfo); 1265a014fbbcSShilei Tian } 1266a014fbbcSShilei Tian 1267a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange(int32_t src_dev_id, void *src_ptr, 1268a014fbbcSShilei Tian int32_t dst_dev_id, void *dst_ptr, 1269a014fbbcSShilei Tian int64_t size) { 1270a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1271a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 1272a014fbbcSShilei Tian 12735449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 1274a014fbbcSShilei Tian const int32_t rc = __tgt_rtl_data_exchange_async( 12755449fbb5SJohannes Doerfert src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, &AsyncInfo); 1276a014fbbcSShilei Tian if (rc != OFFLOAD_SUCCESS) 1277a014fbbcSShilei Tian return OFFLOAD_FAIL; 1278a014fbbcSShilei Tian 12795449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(src_dev_id, &AsyncInfo); 1280a014fbbcSShilei Tian } 1281a014fbbcSShilei Tian 12824031bb98SShilei Tian int32_t __tgt_rtl_data_delete(int32_t device_id, void *tgt_ptr) { 12834031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12844031bb98SShilei Tian 12854031bb98SShilei Tian return DeviceRTL.dataDelete(device_id, tgt_ptr); 12864031bb98SShilei Tian } 12874031bb98SShilei Tian 12884031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region(int32_t device_id, void *tgt_entry_ptr, 12894031bb98SShilei Tian void **tgt_args, 12904031bb98SShilei Tian ptrdiff_t *tgt_offsets, 12914031bb98SShilei Tian int32_t arg_num, int32_t team_num, 12924031bb98SShilei Tian int32_t thread_limit, 12934031bb98SShilei Tian uint64_t loop_tripcount) { 12944031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 12954031bb98SShilei Tian 12965449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 12974031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_team_region_async( 12984031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 12995449fbb5SJohannes Doerfert thread_limit, loop_tripcount, &AsyncInfo); 13004031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 13014031bb98SShilei Tian return OFFLOAD_FAIL; 13024031bb98SShilei Tian 13035449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 13044031bb98SShilei Tian } 13054031bb98SShilei Tian 13064031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region_async( 13074031bb98SShilei Tian int32_t device_id, void *tgt_entry_ptr, void **tgt_args, 13084031bb98SShilei Tian ptrdiff_t *tgt_offsets, int32_t arg_num, int32_t team_num, 13094031bb98SShilei Tian int32_t thread_limit, uint64_t loop_tripcount, 13104031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 13114031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 13124031bb98SShilei Tian 13134031bb98SShilei Tian return DeviceRTL.runTargetTeamRegion( 13144031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 13154031bb98SShilei Tian thread_limit, loop_tripcount, async_info_ptr); 13164031bb98SShilei Tian } 13173de4cd12SGeorge Rokos 13183de4cd12SGeorge Rokos int32_t __tgt_rtl_run_target_region(int32_t device_id, void *tgt_entry_ptr, 131932ed2927SShilei Tian void **tgt_args, ptrdiff_t *tgt_offsets, 132003ff643dSShilei Tian int32_t arg_num) { 13214031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 13224031bb98SShilei Tian 13235449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 13244031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_region_async( 13255449fbb5SJohannes Doerfert device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, &AsyncInfo); 132603ff643dSShilei Tian if (rc != OFFLOAD_SUCCESS) 132703ff643dSShilei Tian return OFFLOAD_FAIL; 132803ff643dSShilei Tian 13295449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 133003ff643dSShilei Tian } 133103ff643dSShilei Tian 133203ff643dSShilei Tian int32_t __tgt_rtl_run_target_region_async(int32_t device_id, 133303ff643dSShilei Tian void *tgt_entry_ptr, void **tgt_args, 133403ff643dSShilei Tian ptrdiff_t *tgt_offsets, 133532ed2927SShilei Tian int32_t arg_num, 13364031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 13374031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 13384031bb98SShilei Tian 133903ff643dSShilei Tian return __tgt_rtl_run_target_team_region_async( 13404031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, 13414031bb98SShilei Tian /* team num*/ 1, /* thread_limit */ 1, /* loop_tripcount */ 0, 13424031bb98SShilei Tian async_info_ptr); 134332ed2927SShilei Tian } 134432ed2927SShilei Tian 13454031bb98SShilei Tian int32_t __tgt_rtl_synchronize(int32_t device_id, 13464031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 13474031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 13484031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 13494031bb98SShilei Tian assert(async_info_ptr->Queue && "async_info_ptr->Queue is nullptr"); 135032ed2927SShilei Tian 13514031bb98SShilei Tian return DeviceRTL.synchronize(device_id, async_info_ptr); 13523de4cd12SGeorge Rokos } 13533de4cd12SGeorge Rokos 13542b6f2008SJoseph Huber void __tgt_rtl_set_info_flag(uint32_t NewInfoLevel) { 135559b68490SJoseph Huber std::atomic<uint32_t> &InfoLevel = getInfoLevelInternal(); 13562b6f2008SJoseph Huber InfoLevel.store(NewInfoLevel); 13572b6f2008SJoseph Huber } 13582b6f2008SJoseph Huber 13593de4cd12SGeorge Rokos #ifdef __cplusplus 13603de4cd12SGeorge Rokos } 13613de4cd12SGeorge Rokos #endif 1362