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" 240c554a47SJon Chesterfield #include "DeviceEnvironment.h" 251546d319SGeorge Rokos #include "omptargetplugin.h" 263de4cd12SGeorge Rokos 273de4cd12SGeorge Rokos #define TARGET_NAME CUDA 28ae95ceebSJoseph Huber #define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL" 2978769ec4SMichael Kruse 300871d6d5SShilei Tian #include "MemoryManager.h" 310871d6d5SShilei Tian 32ca999f71SShilei Tian #include "llvm/Frontend/OpenMP/OMPConstants.h" 33ca999f71SShilei Tian 3478769ec4SMichael Kruse // Utility for retrieving and printing CUDA error string. 35ae95ceebSJoseph Huber #ifdef OMPTARGET_DEBUG 3678769ec4SMichael Kruse #define CUDA_ERR_STRING(err) \ 3778769ec4SMichael Kruse do { \ 38ae95ceebSJoseph Huber if (getDebugLevel() > 0) { \ 3903111e5eSYe Luo const char *errStr = nullptr; \ 4003111e5eSYe Luo CUresult errStr_status = cuGetErrorString(err, &errStr); \ 4103111e5eSYe Luo if (errStr_status == CUDA_ERROR_INVALID_VALUE) \ 422ce16810SJoseph Huber REPORT("Unrecognized CUDA error code: %d\n", err); \ 4303111e5eSYe Luo else if (errStr_status == CUDA_SUCCESS) \ 442ce16810SJoseph Huber REPORT("CUDA error is: %s\n", errStr); \ 4503111e5eSYe Luo else { \ 462ce16810SJoseph Huber REPORT("Unresolved CUDA error code: %d\n", err); \ 472ce16810SJoseph Huber REPORT("Unsuccessful cuGetErrorString return status: %d\n", \ 4803111e5eSYe Luo errStr_status); \ 4903111e5eSYe Luo } \ 502ce16810SJoseph Huber } else { \ 512ce16810SJoseph Huber const char *errStr = nullptr; \ 522ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 532ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 542ce16810SJoseph Huber REPORT("%s \n", errStr); \ 5578769ec4SMichael Kruse } \ 5678769ec4SMichael Kruse } while (false) 57b305d26bSSergey Dmitriev #else // OMPTARGET_DEBUG 582ce16810SJoseph Huber #define CUDA_ERR_STRING(err) \ 592ce16810SJoseph Huber do { \ 602ce16810SJoseph Huber const char *errStr = nullptr; \ 612ce16810SJoseph Huber CUresult errStr_status = cuGetErrorString(err, &errStr); \ 622ce16810SJoseph Huber if (errStr_status == CUDA_SUCCESS) \ 632ce16810SJoseph Huber REPORT("%s \n", errStr); \ 642ce16810SJoseph Huber } while (false) 65b305d26bSSergey Dmitriev #endif // OMPTARGET_DEBUG 663de4cd12SGeorge Rokos 67d2f85d09SJose M Monsalve Diaz #define BOOL2TEXT(b) ((b) ? "Yes" : "No") 68d2f85d09SJose M Monsalve Diaz 69a81c68aeSShilei Tian #include "elf_common.h" 703de4cd12SGeorge Rokos 713de4cd12SGeorge Rokos /// Keep entries table per device. 723de4cd12SGeorge Rokos struct FuncOrGblEntryTy { 733de4cd12SGeorge Rokos __tgt_target_table Table; 743de4cd12SGeorge Rokos std::vector<__tgt_offload_entry> Entries; 753de4cd12SGeorge Rokos }; 763de4cd12SGeorge Rokos 77dc34b1c9SRon Lieberman /// Use a single entity to encode a kernel and a set of flags. 783de4cd12SGeorge Rokos struct KernelTy { 793de4cd12SGeorge Rokos CUfunction Func; 803de4cd12SGeorge Rokos 813de4cd12SGeorge Rokos // execution mode of kernel 82ca999f71SShilei Tian llvm::omp::OMPTgtExecModeFlags ExecutionMode; 833de4cd12SGeorge Rokos 84aa27cfc1SJohannes Doerfert /// Maximal number of threads per block for this kernel. 85aa27cfc1SJohannes Doerfert int MaxThreadsPerBlock = 0; 86aa27cfc1SJohannes Doerfert 87ca999f71SShilei Tian KernelTy(CUfunction _Func, llvm::omp::OMPTgtExecModeFlags _ExecutionMode) 883de4cd12SGeorge Rokos : Func(_Func), ExecutionMode(_ExecutionMode) {} 893de4cd12SGeorge Rokos }; 903de4cd12SGeorge Rokos 91feed674dSShilei Tian namespace { 92feed674dSShilei Tian bool checkResult(CUresult Err, const char *ErrMsg) { 93feed674dSShilei Tian if (Err == CUDA_SUCCESS) 94feed674dSShilei Tian return true; 95feed674dSShilei Tian 962ce16810SJoseph Huber REPORT("%s", ErrMsg); 97feed674dSShilei Tian CUDA_ERR_STRING(Err); 98feed674dSShilei Tian return false; 99feed674dSShilei Tian } 1004031bb98SShilei Tian 101a014fbbcSShilei Tian int memcpyDtoD(const void *SrcPtr, void *DstPtr, int64_t Size, 102a014fbbcSShilei Tian CUstream Stream) { 10395f0d1edSJon Chesterfield CUresult Err = 10495f0d1edSJon Chesterfield cuMemcpyDtoDAsync((CUdeviceptr)DstPtr, (CUdeviceptr)SrcPtr, Size, Stream); 105a014fbbcSShilei Tian 106a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10729338459SJoseph Huber DP("Error when copying data from device to device. Pointers: src " 108a014fbbcSShilei Tian "= " DPxMOD ", dst = " DPxMOD ", size = %" PRId64 "\n", 109a014fbbcSShilei Tian DPxPTR(SrcPtr), DPxPTR(DstPtr), Size); 110a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 111a014fbbcSShilei Tian return OFFLOAD_FAIL; 112a014fbbcSShilei Tian } 113a014fbbcSShilei Tian 114a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 115a014fbbcSShilei Tian } 116a014fbbcSShilei Tian 11729df4ab3SShilei Tian int createEvent(void **P) { 11829df4ab3SShilei Tian CUevent Event = nullptr; 11929df4ab3SShilei Tian 12029df4ab3SShilei Tian CUresult Err = cuEventCreate(&Event, CU_EVENT_DEFAULT); 12129df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 12229df4ab3SShilei Tian DP("Error when creating event event = " DPxMOD "\n", DPxPTR(Event)); 12329df4ab3SShilei Tian CUDA_ERR_STRING(Err); 12429df4ab3SShilei Tian return OFFLOAD_FAIL; 12529df4ab3SShilei Tian } 12629df4ab3SShilei Tian 12729df4ab3SShilei Tian *P = Event; 12829df4ab3SShilei Tian 12929df4ab3SShilei Tian return OFFLOAD_SUCCESS; 13029df4ab3SShilei Tian } 13129df4ab3SShilei Tian 13229df4ab3SShilei Tian int recordEvent(void *EventPtr, __tgt_async_info *AsyncInfo) { 13329df4ab3SShilei Tian CUstream Stream = reinterpret_cast<CUstream>(AsyncInfo->Queue); 13429df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 13529df4ab3SShilei Tian 13629df4ab3SShilei Tian CUresult Err = cuEventRecord(Event, Stream); 13729df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 13829df4ab3SShilei Tian DP("Error when recording event. stream = " DPxMOD ", event = " DPxMOD "\n", 13929df4ab3SShilei Tian DPxPTR(Stream), 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 14729df4ab3SShilei Tian int syncEvent(void *EventPtr) { 14829df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 14929df4ab3SShilei Tian 15029df4ab3SShilei Tian CUresult Err = cuEventSynchronize(Event); 15129df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 15229df4ab3SShilei Tian DP("Error when syncing event = " DPxMOD "\n", DPxPTR(Event)); 15329df4ab3SShilei Tian CUDA_ERR_STRING(Err); 15429df4ab3SShilei Tian return OFFLOAD_FAIL; 15529df4ab3SShilei Tian } 15629df4ab3SShilei Tian 15729df4ab3SShilei Tian return OFFLOAD_SUCCESS; 15829df4ab3SShilei Tian } 15929df4ab3SShilei Tian 16029df4ab3SShilei Tian int destroyEvent(void *EventPtr) { 16129df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 16229df4ab3SShilei Tian 16329df4ab3SShilei Tian CUresult Err = cuEventDestroy(Event); 16429df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 16529df4ab3SShilei Tian DP("Error when destroying event = " DPxMOD "\n", DPxPTR(Event)); 16629df4ab3SShilei Tian CUDA_ERR_STRING(Err); 16729df4ab3SShilei Tian return OFFLOAD_FAIL; 16829df4ab3SShilei Tian } 16929df4ab3SShilei Tian 17029df4ab3SShilei Tian return OFFLOAD_SUCCESS; 17129df4ab3SShilei Tian } 17229df4ab3SShilei Tian 1734031bb98SShilei Tian // Structure contains per-device data 1744031bb98SShilei Tian struct DeviceDataTy { 1755272d29eSJohannes Doerfert /// List that contains all the kernels. 1765272d29eSJohannes Doerfert std::list<KernelTy> KernelsList; 1775272d29eSJohannes Doerfert 1784031bb98SShilei Tian std::list<FuncOrGblEntryTy> FuncGblEntries; 1795272d29eSJohannes Doerfert 1804031bb98SShilei Tian CUcontext Context = nullptr; 1814031bb98SShilei Tian // Device properties 1824031bb98SShilei Tian int ThreadsPerBlock = 0; 1834031bb98SShilei Tian int BlocksPerGrid = 0; 1844031bb98SShilei Tian int WarpSize = 0; 1854031bb98SShilei Tian // OpenMP properties 1864031bb98SShilei Tian int NumTeams = 0; 1874031bb98SShilei Tian int NumThreads = 0; 1884031bb98SShilei Tian }; 189feed674dSShilei Tian 190a697a0a4SShilei Tian /// Resource allocator where \p T is the resource type. 191a697a0a4SShilei Tian /// Functions \p create and \p destroy return OFFLOAD_SUCCESS and OFFLOAD_FAIL 192a697a0a4SShilei Tian /// accordingly. The implementation should not raise any exception. 193a697a0a4SShilei Tian template <typename T> class AllocatorTy { 194a697a0a4SShilei Tian public: 195a697a0a4SShilei Tian /// Create a resource and assign to R. 196a697a0a4SShilei Tian int create(T &R) noexcept; 197a697a0a4SShilei Tian /// Destroy the resource. 198a697a0a4SShilei Tian int destroy(T) noexcept; 199a697a0a4SShilei Tian }; 200feed674dSShilei Tian 201a697a0a4SShilei Tian /// Allocator for CUstream. 202a697a0a4SShilei Tian template <> class AllocatorTy<CUstream> { 203a697a0a4SShilei Tian CUcontext Context; 204feed674dSShilei Tian 205a697a0a4SShilei Tian public: 206a697a0a4SShilei Tian AllocatorTy(CUcontext C) noexcept : Context(C) {} 207a697a0a4SShilei Tian 208a697a0a4SShilei Tian /// See AllocatorTy<T>::create. 209a697a0a4SShilei Tian int create(CUstream &Stream) noexcept { 210a697a0a4SShilei Tian if (!checkResult(cuCtxSetCurrent(Context), 211a697a0a4SShilei Tian "Error returned from cuCtxSetCurrent\n")) 212a697a0a4SShilei Tian return OFFLOAD_FAIL; 213a697a0a4SShilei Tian 214a697a0a4SShilei Tian if (!checkResult(cuStreamCreate(&Stream, CU_STREAM_NON_BLOCKING), 215a697a0a4SShilei Tian "Error returned from cuStreamCreate\n")) 216a697a0a4SShilei Tian return OFFLOAD_FAIL; 217a697a0a4SShilei Tian 218a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 219feed674dSShilei Tian } 220feed674dSShilei Tian 221a697a0a4SShilei Tian /// See AllocatorTy<T>::destroy. 222a697a0a4SShilei Tian int destroy(CUstream Stream) noexcept { 223a697a0a4SShilei Tian if (!checkResult(cuCtxSetCurrent(Context), 224a697a0a4SShilei Tian "Error returned from cuCtxSetCurrent\n")) 225a697a0a4SShilei Tian return OFFLOAD_FAIL; 226a697a0a4SShilei Tian if (!checkResult(cuStreamDestroy(Stream), 227a697a0a4SShilei Tian "Error returned from cuStreamDestroy\n")) 228a697a0a4SShilei Tian return OFFLOAD_FAIL; 2294031bb98SShilei Tian 230a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 231feed674dSShilei Tian } 232a697a0a4SShilei Tian }; 233a697a0a4SShilei Tian 234a697a0a4SShilei Tian /// A generic pool of resources where \p T is the resource type. 235a697a0a4SShilei Tian /// \p T should be copyable as the object is stored in \p std::vector . 236a697a0a4SShilei Tian template <typename T> class ResourcePoolTy { 237a697a0a4SShilei Tian /// Index of the next available resource. 238a697a0a4SShilei Tian size_t Next = 0; 239a697a0a4SShilei Tian /// Mutex to guard the pool. 240a697a0a4SShilei Tian std::mutex Mutex; 241a697a0a4SShilei Tian /// Pool of resources. 242a697a0a4SShilei Tian std::vector<T> Resources; 243a697a0a4SShilei Tian /// A reference to the corresponding allocator. 244a697a0a4SShilei Tian AllocatorTy<T> Allocator; 245a697a0a4SShilei Tian 246a697a0a4SShilei Tian /// If `Resources` is used up, we will fill in more resources. It assumes that 247a697a0a4SShilei Tian /// the new size `Size` should be always larger than the current size. 248a697a0a4SShilei Tian bool resize(size_t Size) { 249a697a0a4SShilei Tian auto CurSize = Resources.size(); 250a697a0a4SShilei Tian assert(Size > CurSize && "Unexpected smaller size"); 251a697a0a4SShilei Tian Resources.reserve(Size); 252a697a0a4SShilei Tian for (auto I = CurSize; I < Size; ++I) { 253a697a0a4SShilei Tian T NewItem; 254a697a0a4SShilei Tian int Ret = Allocator.create(NewItem); 255a697a0a4SShilei Tian if (Ret != OFFLOAD_SUCCESS) 256a697a0a4SShilei Tian return false; 257a697a0a4SShilei Tian Resources.push_back(NewItem); 258a697a0a4SShilei Tian } 259a697a0a4SShilei Tian return true; 260feed674dSShilei Tian } 261feed674dSShilei Tian 262feed674dSShilei Tian public: 263a697a0a4SShilei Tian ResourcePoolTy(AllocatorTy<T> &&A, size_t Size = 0) noexcept 264a697a0a4SShilei Tian : Allocator(std::move(A)) { 265*357c8031SShilei Tian if (Size) 266a697a0a4SShilei Tian (void)resize(Size); 267feed674dSShilei Tian } 268feed674dSShilei Tian 269*357c8031SShilei Tian ~ResourcePoolTy() noexcept { clear(); } 270feed674dSShilei Tian 271a697a0a4SShilei Tian /// Get a resource from pool. `Next` always points to the next available 272a697a0a4SShilei Tian /// resource. That means, `[0, next-1]` have been assigned, and `[id,]` are 273a697a0a4SShilei Tian /// still available. If there is no resource left, we will ask for more. Each 274a697a0a4SShilei Tian /// time a resource is assigned, the id will increase one. 275a697a0a4SShilei Tian /// xxxxxs+++++++++ 276a697a0a4SShilei Tian /// ^ 277a697a0a4SShilei Tian /// Next 278a697a0a4SShilei Tian /// After assignment, the pool becomes the following and s is assigned. 279a697a0a4SShilei Tian /// xxxxxs+++++++++ 280a697a0a4SShilei Tian /// ^ 281a697a0a4SShilei Tian /// Next 282a697a0a4SShilei Tian int acquire(T &R) noexcept { 283a697a0a4SShilei Tian std::lock_guard<std::mutex> LG(Mutex); 284*357c8031SShilei Tian if (Next == Resources.size()) { 285*357c8031SShilei Tian auto NewSize = Resources.size() ? Resources.size() * 2 : 1; 286*357c8031SShilei Tian if (!resize(NewSize)) 287a697a0a4SShilei Tian return OFFLOAD_FAIL; 288*357c8031SShilei Tian } 289*357c8031SShilei Tian 290*357c8031SShilei Tian assert(Next < Resources.size()); 291a697a0a4SShilei Tian 292a697a0a4SShilei Tian R = Resources[Next++]; 293a697a0a4SShilei Tian 294a697a0a4SShilei Tian return OFFLOAD_SUCCESS; 295feed674dSShilei Tian } 296feed674dSShilei Tian 297a697a0a4SShilei Tian /// Return the resource back to the pool. When we return a resource, we need 298a697a0a4SShilei Tian /// to first decrease `Next`, and then copy the resource back. It is worth 299a697a0a4SShilei Tian /// noting that, the order of resources return might be different from that 300a697a0a4SShilei Tian /// they're assigned, that saying, at some point, there might be two identical 301a697a0a4SShilei Tian /// resources. 302a697a0a4SShilei Tian /// xxax+a+++++ 303a697a0a4SShilei Tian /// ^ 304a697a0a4SShilei Tian /// Next 305a697a0a4SShilei Tian /// However, it doesn't matter, because they're always on the two sides of 306a697a0a4SShilei Tian /// `Next`. The left one will in the end be overwritten by another resource. 307a697a0a4SShilei Tian /// Therefore, after several execution, the order of pool might be different 308a697a0a4SShilei Tian /// from its initial state. 309a697a0a4SShilei Tian void release(T R) noexcept { 310a697a0a4SShilei Tian std::lock_guard<std::mutex> LG(Mutex); 311a697a0a4SShilei Tian Resources[--Next] = R; 312feed674dSShilei Tian } 313*357c8031SShilei Tian 314*357c8031SShilei Tian /// Released all stored resources and clear the pool. 315*357c8031SShilei Tian /// Note: This function is not thread safe. Be sure to guard it if necessary. 316*357c8031SShilei Tian void clear() noexcept { 317*357c8031SShilei Tian for (auto &R : Resources) 318*357c8031SShilei Tian (void)Allocator.destroy(R); 319*357c8031SShilei Tian Resources.clear(); 320*357c8031SShilei Tian } 321feed674dSShilei Tian }; 322feed674dSShilei Tian 3234031bb98SShilei Tian class DeviceRTLTy { 3243de4cd12SGeorge Rokos int NumberOfDevices; 3254031bb98SShilei Tian // OpenMP environment properties 3263de4cd12SGeorge Rokos int EnvNumTeams; 3273de4cd12SGeorge Rokos int EnvTeamLimit; 328422adaa8SJoseph Huber int EnvTeamThreadLimit; 3294031bb98SShilei Tian // OpenMP requires flags 330c5fe030cSGheorghe-Teodor Bercea int64_t RequiresFlags; 331f1c821faSJoseph Huber // Amount of dynamic shared memory to use at launch. 332f1c821faSJoseph Huber uint64_t DynamicMemorySize; 333a697a0a4SShilei Tian // Number of initial streams for each device. 334a697a0a4SShilei Tian int NumInitialStreams = 32; 335c5fe030cSGheorghe-Teodor Bercea 3364031bb98SShilei Tian static constexpr const int HardTeamLimit = 1U << 16U; // 64k 3374031bb98SShilei Tian static constexpr const int HardThreadLimit = 1024; 3384031bb98SShilei Tian static constexpr const int DefaultNumTeams = 128; 3394031bb98SShilei Tian static constexpr const int DefaultNumThreads = 128; 3403de4cd12SGeorge Rokos 341a697a0a4SShilei Tian using StreamPoolTy = ResourcePoolTy<CUstream>; 342a697a0a4SShilei Tian std::vector<std::unique_ptr<StreamPoolTy>> StreamPool; 343a697a0a4SShilei Tian 3444031bb98SShilei Tian std::vector<DeviceDataTy> DeviceData; 3454031bb98SShilei Tian std::vector<CUmodule> Modules; 346feed674dSShilei Tian 3470871d6d5SShilei Tian /// A class responsible for interacting with device native runtime library to 3480871d6d5SShilei Tian /// allocate and free memory. 3490871d6d5SShilei Tian class CUDADeviceAllocatorTy : public DeviceAllocatorTy { 3500871d6d5SShilei Tian const int DeviceId; 3510871d6d5SShilei Tian const std::vector<DeviceDataTy> &DeviceData; 352a15f8589SJoseph Huber std::unordered_map<void *, TargetAllocTy> HostPinnedAllocs; 3530871d6d5SShilei Tian 3540871d6d5SShilei Tian public: 3550871d6d5SShilei Tian CUDADeviceAllocatorTy(int DeviceId, std::vector<DeviceDataTy> &DeviceData) 3560871d6d5SShilei Tian : DeviceId(DeviceId), DeviceData(DeviceData) {} 3570871d6d5SShilei Tian 358a15f8589SJoseph Huber void *allocate(size_t Size, void *, TargetAllocTy Kind) override { 3590871d6d5SShilei Tian if (Size == 0) 3600871d6d5SShilei Tian return nullptr; 3610871d6d5SShilei Tian 3620871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 3630871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 3640871d6d5SShilei Tian return nullptr; 3650871d6d5SShilei Tian 366a15f8589SJoseph Huber void *MemAlloc = nullptr; 367a15f8589SJoseph Huber switch (Kind) { 368a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 369a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 3700871d6d5SShilei Tian CUdeviceptr DevicePtr; 37195f0d1edSJon Chesterfield Err = cuMemAlloc(&DevicePtr, Size); 372a15f8589SJoseph Huber MemAlloc = (void *)DevicePtr; 37395f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemAlloc\n")) 3740871d6d5SShilei Tian return nullptr; 375a15f8589SJoseph Huber break; 376a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 377a15f8589SJoseph Huber void *HostPtr; 378a15f8589SJoseph Huber Err = cuMemAllocHost(&HostPtr, Size); 379a15f8589SJoseph Huber MemAlloc = HostPtr; 380a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocHost\n")) 381a15f8589SJoseph Huber return nullptr; 382a15f8589SJoseph Huber HostPinnedAllocs[MemAlloc] = Kind; 383a15f8589SJoseph Huber break; 384a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 385a15f8589SJoseph Huber CUdeviceptr SharedPtr; 386a15f8589SJoseph Huber Err = cuMemAllocManaged(&SharedPtr, Size, CU_MEM_ATTACH_GLOBAL); 387a15f8589SJoseph Huber MemAlloc = (void *)SharedPtr; 388a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemAllocManaged\n")) 389a15f8589SJoseph Huber return nullptr; 390a15f8589SJoseph Huber break; 391a15f8589SJoseph Huber } 3920871d6d5SShilei Tian 393a15f8589SJoseph Huber return MemAlloc; 3940871d6d5SShilei Tian } 3950871d6d5SShilei Tian 3960871d6d5SShilei Tian int free(void *TgtPtr) override { 3970871d6d5SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 3980871d6d5SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 3990871d6d5SShilei Tian return OFFLOAD_FAIL; 4000871d6d5SShilei Tian 401a15f8589SJoseph Huber // Host pinned memory must be freed differently. 402a15f8589SJoseph Huber TargetAllocTy Kind = 403a15f8589SJoseph Huber (HostPinnedAllocs.find(TgtPtr) == HostPinnedAllocs.end()) 404a15f8589SJoseph Huber ? TARGET_ALLOC_DEFAULT 405a15f8589SJoseph Huber : TARGET_ALLOC_HOST; 406a15f8589SJoseph Huber switch (Kind) { 407a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 408a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 409a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 41095f0d1edSJon Chesterfield Err = cuMemFree((CUdeviceptr)TgtPtr); 41195f0d1edSJon Chesterfield if (!checkResult(Err, "Error returned from cuMemFree\n")) 4120871d6d5SShilei Tian return OFFLOAD_FAIL; 413a15f8589SJoseph Huber break; 414a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 415a15f8589SJoseph Huber Err = cuMemFreeHost(TgtPtr); 416a15f8589SJoseph Huber if (!checkResult(Err, "Error returned from cuMemFreeHost\n")) 417a15f8589SJoseph Huber return OFFLOAD_FAIL; 418a15f8589SJoseph Huber break; 419a15f8589SJoseph Huber } 4200871d6d5SShilei Tian 4210871d6d5SShilei Tian return OFFLOAD_SUCCESS; 4220871d6d5SShilei Tian } 4230871d6d5SShilei Tian }; 4240871d6d5SShilei Tian 4250871d6d5SShilei Tian /// A vector of device allocators 4260871d6d5SShilei Tian std::vector<CUDADeviceAllocatorTy> DeviceAllocators; 4270871d6d5SShilei Tian 4280871d6d5SShilei Tian /// A vector of memory managers. Since the memory manager is non-copyable and 4290871d6d5SShilei Tian // non-removable, we wrap them into std::unique_ptr. 4300871d6d5SShilei Tian std::vector<std::unique_ptr<MemoryManagerTy>> MemoryManagers; 4310871d6d5SShilei Tian 4320871d6d5SShilei Tian /// Whether use memory manager 4330871d6d5SShilei Tian bool UseMemoryManager = true; 4340871d6d5SShilei Tian 4353de4cd12SGeorge Rokos // Record entry point associated with device 4364031bb98SShilei Tian void addOffloadEntry(const int DeviceId, const __tgt_offload_entry entry) { 4374031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4383de4cd12SGeorge Rokos E.Entries.push_back(entry); 4393de4cd12SGeorge Rokos } 4403de4cd12SGeorge Rokos 441ae209397SJoseph Huber // Return a pointer to the entry associated with the pointer 442ae209397SJoseph Huber const __tgt_offload_entry *getOffloadEntry(const int DeviceId, 443ae209397SJoseph Huber const void *Addr) const { 4444031bb98SShilei Tian for (const __tgt_offload_entry &Itr : 4454031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.back().Entries) 4464031bb98SShilei Tian if (Itr.addr == Addr) 447ae209397SJoseph Huber return &Itr; 4483de4cd12SGeorge Rokos 449ae209397SJoseph Huber return nullptr; 4503de4cd12SGeorge Rokos } 4513de4cd12SGeorge Rokos 4523de4cd12SGeorge Rokos // Return the pointer to the target entries table 4534031bb98SShilei Tian __tgt_target_table *getOffloadEntriesTable(const int DeviceId) { 4544031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4553de4cd12SGeorge Rokos 4564031bb98SShilei Tian if (E.Entries.empty()) 4574031bb98SShilei Tian return nullptr; 4583de4cd12SGeorge Rokos 4593de4cd12SGeorge Rokos // Update table info according to the entries and return the pointer 4604031bb98SShilei Tian E.Table.EntriesBegin = E.Entries.data(); 4614031bb98SShilei Tian E.Table.EntriesEnd = E.Entries.data() + E.Entries.size(); 4623de4cd12SGeorge Rokos 4633de4cd12SGeorge Rokos return &E.Table; 4643de4cd12SGeorge Rokos } 4653de4cd12SGeorge Rokos 4663de4cd12SGeorge Rokos // Clear entries table for a device 4674031bb98SShilei Tian void clearOffloadEntriesTable(const int DeviceId) { 4684031bb98SShilei Tian DeviceData[DeviceId].FuncGblEntries.emplace_back(); 4694031bb98SShilei Tian FuncOrGblEntryTy &E = DeviceData[DeviceId].FuncGblEntries.back(); 4703de4cd12SGeorge Rokos E.Entries.clear(); 4714031bb98SShilei Tian E.Table.EntriesBegin = E.Table.EntriesEnd = nullptr; 4723de4cd12SGeorge Rokos } 4733de4cd12SGeorge Rokos 4745449fbb5SJohannes Doerfert CUstream getStream(const int DeviceId, __tgt_async_info *AsyncInfo) const { 4755449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 4764031bb98SShilei Tian 477a697a0a4SShilei Tian if (!AsyncInfo->Queue) { 478a697a0a4SShilei Tian CUstream S; 479a697a0a4SShilei Tian if (StreamPool[DeviceId]->acquire(S) != OFFLOAD_SUCCESS) 480a697a0a4SShilei Tian return nullptr; 481a697a0a4SShilei Tian 482a697a0a4SShilei Tian AsyncInfo->Queue = S; 483a697a0a4SShilei Tian } 4844031bb98SShilei Tian 4855449fbb5SJohannes Doerfert return reinterpret_cast<CUstream>(AsyncInfo->Queue); 486b305d26bSSergey Dmitriev } 4874031bb98SShilei Tian 4884031bb98SShilei Tian public: 4894031bb98SShilei Tian // This class should not be copied 4904031bb98SShilei Tian DeviceRTLTy(const DeviceRTLTy &) = delete; 4914031bb98SShilei Tian DeviceRTLTy(DeviceRTLTy &&) = delete; 4924031bb98SShilei Tian 4934031bb98SShilei Tian DeviceRTLTy() 4944031bb98SShilei Tian : NumberOfDevices(0), EnvNumTeams(-1), EnvTeamLimit(-1), 495f1c821faSJoseph Huber EnvTeamThreadLimit(-1), RequiresFlags(OMP_REQ_UNDEFINED), 496f1c821faSJoseph Huber DynamicMemorySize(0) { 497b305d26bSSergey Dmitriev 4983de4cd12SGeorge Rokos DP("Start initializing CUDA\n"); 4993de4cd12SGeorge Rokos 5004031bb98SShilei Tian CUresult Err = cuInit(0); 5017baff00eSJon Chesterfield if (Err == CUDA_ERROR_INVALID_HANDLE) { 5027baff00eSJon Chesterfield // Can't call cuGetErrorString if dlsym failed 5037baff00eSJon Chesterfield DP("Failed to load CUDA shared library\n"); 5047baff00eSJon Chesterfield return; 5057baff00eSJon Chesterfield } 5064031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuInit\n")) { 5073de4cd12SGeorge Rokos return; 5083de4cd12SGeorge Rokos } 5093de4cd12SGeorge Rokos 5104031bb98SShilei Tian Err = cuDeviceGetCount(&NumberOfDevices); 5114031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGetCount\n")) 5123de4cd12SGeorge Rokos return; 5133de4cd12SGeorge Rokos 5143de4cd12SGeorge Rokos if (NumberOfDevices == 0) { 5153de4cd12SGeorge Rokos DP("There are no devices supporting CUDA.\n"); 5163de4cd12SGeorge Rokos return; 5173de4cd12SGeorge Rokos } 5183de4cd12SGeorge Rokos 5194031bb98SShilei Tian DeviceData.resize(NumberOfDevices); 520a697a0a4SShilei Tian StreamPool.resize(NumberOfDevices); 5213de4cd12SGeorge Rokos 5223de4cd12SGeorge Rokos // Get environment variables regarding teams 5234031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_TEAM_LIMIT")) { 5243de4cd12SGeorge Rokos // OMP_TEAM_LIMIT has been set 5254031bb98SShilei Tian EnvTeamLimit = std::stoi(EnvStr); 5263de4cd12SGeorge Rokos DP("Parsed OMP_TEAM_LIMIT=%d\n", EnvTeamLimit); 5273de4cd12SGeorge Rokos } 528422adaa8SJoseph Huber if (const char *EnvStr = getenv("OMP_TEAMS_THREAD_LIMIT")) { 529422adaa8SJoseph Huber // OMP_TEAMS_THREAD_LIMIT has been set 530422adaa8SJoseph Huber EnvTeamThreadLimit = std::stoi(EnvStr); 531422adaa8SJoseph Huber DP("Parsed OMP_TEAMS_THREAD_LIMIT=%d\n", EnvTeamThreadLimit); 532422adaa8SJoseph Huber } 5334031bb98SShilei Tian if (const char *EnvStr = getenv("OMP_NUM_TEAMS")) { 5343de4cd12SGeorge Rokos // OMP_NUM_TEAMS has been set 5354031bb98SShilei Tian EnvNumTeams = std::stoi(EnvStr); 5363de4cd12SGeorge Rokos DP("Parsed OMP_NUM_TEAMS=%d\n", EnvNumTeams); 5373de4cd12SGeorge Rokos } 538f1c821faSJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_SHARED_MEMORY_SIZE")) { 539f1c821faSJoseph Huber // LIBOMPTARGET_SHARED_MEMORY_SIZE has been set 540f1c821faSJoseph Huber DynamicMemorySize = std::stoi(EnvStr); 54149e976c9SShilei Tian DP("Parsed LIBOMPTARGET_SHARED_MEMORY_SIZE = %" PRIu64 "\n", 54249e976c9SShilei Tian DynamicMemorySize); 543f1c821faSJoseph Huber } 544a697a0a4SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_NUM_INITIAL_STREAMS")) { 545a697a0a4SShilei Tian // LIBOMPTARGET_NUM_INITIAL_STREAMS has been set 546a697a0a4SShilei Tian NumInitialStreams = std::stoi(EnvStr); 547a697a0a4SShilei Tian DP("Parsed LIBOMPTARGET_NUM_INITIAL_STREAMS=%d\n", NumInitialStreams); 548a697a0a4SShilei Tian } 5490871d6d5SShilei Tian 5500871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 5510871d6d5SShilei Tian DeviceAllocators.emplace_back(I, DeviceData); 5520871d6d5SShilei Tian 5530871d6d5SShilei Tian // Get the size threshold from environment variable 5540871d6d5SShilei Tian std::pair<size_t, bool> Res = MemoryManagerTy::getSizeThresholdFromEnv(); 5550871d6d5SShilei Tian UseMemoryManager = Res.second; 5560871d6d5SShilei Tian size_t MemoryManagerThreshold = Res.first; 5570871d6d5SShilei Tian 5580871d6d5SShilei Tian if (UseMemoryManager) 5590871d6d5SShilei Tian for (int I = 0; I < NumberOfDevices; ++I) 5600871d6d5SShilei Tian MemoryManagers.emplace_back(std::make_unique<MemoryManagerTy>( 5610871d6d5SShilei Tian DeviceAllocators[I], MemoryManagerThreshold)); 5623de4cd12SGeorge Rokos } 5633de4cd12SGeorge Rokos 5644031bb98SShilei Tian ~DeviceRTLTy() { 5650871d6d5SShilei Tian // We first destruct memory managers in case that its dependent data are 5660871d6d5SShilei Tian // destroyed before it. 5670871d6d5SShilei Tian for (auto &M : MemoryManagers) 5680871d6d5SShilei Tian M.release(); 5690871d6d5SShilei Tian 5704031bb98SShilei Tian for (CUmodule &M : Modules) 5714031bb98SShilei Tian // Close module 5724031bb98SShilei Tian if (M) 5734031bb98SShilei Tian checkResult(cuModuleUnload(M), "Error returned from cuModuleUnload\n"); 5744031bb98SShilei Tian 575a697a0a4SShilei Tian for (auto &S : StreamPool) 576*357c8031SShilei Tian S.reset(); 577a697a0a4SShilei Tian 5784031bb98SShilei Tian for (DeviceDataTy &D : DeviceData) { 5794031bb98SShilei Tian // Destroy context 580c5348aecSYe Luo if (D.Context) { 581c5348aecSYe Luo checkResult(cuCtxSetCurrent(D.Context), 582c5348aecSYe Luo "Error returned from cuCtxSetCurrent\n"); 583c5348aecSYe Luo CUdevice Device; 584c5348aecSYe Luo checkResult(cuCtxGetDevice(&Device), 585c5348aecSYe Luo "Error returned from cuCtxGetDevice\n"); 58695f0d1edSJon Chesterfield checkResult(cuDevicePrimaryCtxRelease(Device), 58795f0d1edSJon Chesterfield "Error returned from cuDevicePrimaryCtxRelease\n"); 588c5348aecSYe Luo } 5893de4cd12SGeorge Rokos } 5903de4cd12SGeorge Rokos } 5913de4cd12SGeorge Rokos 5924031bb98SShilei Tian // Check whether a given DeviceId is valid 5934031bb98SShilei Tian bool isValidDeviceId(const int DeviceId) const { 5944031bb98SShilei Tian return DeviceId >= 0 && DeviceId < NumberOfDevices; 59532ed2927SShilei Tian } 59632ed2927SShilei Tian 597cb038927SShilei Tian int getNumOfDevices() const { return NumberOfDevices; } 5984031bb98SShilei Tian 5994031bb98SShilei Tian void setRequiresFlag(const int64_t Flags) { this->RequiresFlags = Flags; } 6004031bb98SShilei Tian 6014031bb98SShilei Tian int initDevice(const int DeviceId) { 6024031bb98SShilei Tian CUdevice Device; 6034031bb98SShilei Tian 6044031bb98SShilei Tian DP("Getting device %d\n", DeviceId); 6054031bb98SShilei Tian CUresult Err = cuDeviceGet(&Device, DeviceId); 6064031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuDeviceGet\n")) 60732ed2927SShilei Tian return OFFLOAD_FAIL; 6083de4cd12SGeorge Rokos 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 641feed674dSShilei Tian // Initialize stream pool 642a697a0a4SShilei Tian if (!StreamPool[DeviceId]) 643a697a0a4SShilei Tian StreamPool[DeviceId] = std::make_unique<StreamPoolTy>( 644*357c8031SShilei Tian AllocatorTy<CUstream>(DeviceData[DeviceId].Context), 645*357c8031SShilei Tian NumInitialStreams); 646a5153dbcSJohannes Doerfert 647bb51d398SJonas Hahnfeld // Query attributes to determine number of threads/block and blocks/grid. 6484031bb98SShilei Tian int MaxGridDimX; 6494031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxGridDimX, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, 6504031bb98SShilei Tian Device); 6514031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6524031bb98SShilei Tian DP("Error getting max grid dimension, use default value %d\n", 6534031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 6544031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = DeviceRTLTy::DefaultNumTeams; 6554031bb98SShilei Tian } else if (MaxGridDimX <= DeviceRTLTy::HardTeamLimit) { 6564031bb98SShilei Tian DP("Using %d CUDA blocks per grid\n", MaxGridDimX); 6574031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = MaxGridDimX; 6583de4cd12SGeorge Rokos } else { 6593de4cd12SGeorge Rokos DP("Max CUDA blocks per grid %d exceeds the hard team limit %d, capping " 660bb51d398SJonas Hahnfeld "at the hard limit\n", 6614031bb98SShilei Tian MaxGridDimX, DeviceRTLTy::HardTeamLimit); 6624031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = DeviceRTLTy::HardTeamLimit; 6633de4cd12SGeorge Rokos } 6643de4cd12SGeorge Rokos 665bb51d398SJonas Hahnfeld // We are only exploiting threads along the x axis. 6664031bb98SShilei Tian int MaxBlockDimX; 6674031bb98SShilei Tian Err = cuDeviceGetAttribute(&MaxBlockDimX, 6684031bb98SShilei Tian CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, Device); 6694031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6704031bb98SShilei Tian DP("Error getting max block dimension, use default value %d\n", 6714031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 6724031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::DefaultNumThreads; 673422adaa8SJoseph Huber } else { 6744031bb98SShilei Tian DP("Using %d CUDA threads per block\n", MaxBlockDimX); 6754031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = MaxBlockDimX; 676422adaa8SJoseph Huber 677422adaa8SJoseph Huber if (EnvTeamThreadLimit > 0 && 678422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock > EnvTeamThreadLimit) { 679422adaa8SJoseph Huber DP("Max CUDA threads per block %d exceeds the thread limit %d set by " 680422adaa8SJoseph Huber "OMP_TEAMS_THREAD_LIMIT, capping at the limit\n", 681422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, EnvTeamThreadLimit); 682422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock = EnvTeamThreadLimit; 683422adaa8SJoseph Huber } 684422adaa8SJoseph Huber if (DeviceData[DeviceId].ThreadsPerBlock > DeviceRTLTy::HardThreadLimit) { 6854031bb98SShilei Tian DP("Max CUDA threads per block %d exceeds the hard thread limit %d, " 6864031bb98SShilei Tian "capping at the hard limit\n", 687422adaa8SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceRTLTy::HardThreadLimit); 6884031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock = DeviceRTLTy::HardThreadLimit; 6893de4cd12SGeorge Rokos } 690422adaa8SJoseph Huber } 6913de4cd12SGeorge Rokos 6924031bb98SShilei Tian // Get and set warp size 6934031bb98SShilei Tian int WarpSize; 6944031bb98SShilei Tian Err = 6954031bb98SShilei Tian cuDeviceGetAttribute(&WarpSize, CU_DEVICE_ATTRIBUTE_WARP_SIZE, Device); 6964031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 6974031bb98SShilei Tian DP("Error getting warp size, assume default value 32\n"); 6984031bb98SShilei Tian DeviceData[DeviceId].WarpSize = 32; 699bb51d398SJonas Hahnfeld } else { 7004031bb98SShilei Tian DP("Using warp size %d\n", WarpSize); 7014031bb98SShilei Tian DeviceData[DeviceId].WarpSize = WarpSize; 7023de4cd12SGeorge Rokos } 7033de4cd12SGeorge Rokos 7043de4cd12SGeorge Rokos // Adjust teams to the env variables 7054031bb98SShilei Tian if (EnvTeamLimit > 0 && DeviceData[DeviceId].BlocksPerGrid > EnvTeamLimit) { 7063de4cd12SGeorge Rokos DP("Capping max CUDA blocks per grid to OMP_TEAM_LIMIT=%d\n", 7074031bb98SShilei Tian EnvTeamLimit); 7084031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid = EnvTeamLimit; 7093de4cd12SGeorge Rokos } 7103de4cd12SGeorge Rokos 7113817ba13SJoseph Huber size_t StackLimit; 7123817ba13SJoseph Huber size_t HeapLimit; 7133817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_STACK_SIZE")) { 7143817ba13SJoseph Huber StackLimit = std::stol(EnvStr); 7153817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_STACK_SIZE, StackLimit) != CUDA_SUCCESS) 7163817ba13SJoseph Huber return OFFLOAD_FAIL; 7173817ba13SJoseph Huber } else { 7183817ba13SJoseph Huber if (cuCtxGetLimit(&StackLimit, CU_LIMIT_STACK_SIZE) != CUDA_SUCCESS) 7193817ba13SJoseph Huber return OFFLOAD_FAIL; 7203817ba13SJoseph Huber } 7213817ba13SJoseph Huber if (const char *EnvStr = getenv("LIBOMPTARGET_HEAP_SIZE")) { 7223817ba13SJoseph Huber HeapLimit = std::stol(EnvStr); 7233817ba13SJoseph Huber if (cuCtxSetLimit(CU_LIMIT_MALLOC_HEAP_SIZE, HeapLimit) != CUDA_SUCCESS) 7243817ba13SJoseph Huber return OFFLOAD_FAIL; 7253817ba13SJoseph Huber } else { 7263817ba13SJoseph Huber if (cuCtxGetLimit(&HeapLimit, CU_LIMIT_MALLOC_HEAP_SIZE) != CUDA_SUCCESS) 7273817ba13SJoseph Huber return OFFLOAD_FAIL; 7283817ba13SJoseph Huber } 7293817ba13SJoseph Huber 730119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 731ae209397SJoseph Huber "Device supports up to %d CUDA blocks and %d threads with a " 732ae209397SJoseph Huber "warp size of %d\n", 733ae209397SJoseph Huber DeviceData[DeviceId].BlocksPerGrid, 734ae209397SJoseph Huber DeviceData[DeviceId].ThreadsPerBlock, DeviceData[DeviceId].WarpSize); 7353817ba13SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 7363817ba13SJoseph Huber "Device heap size is %d Bytes, device stack size is %d Bytes per " 7373817ba13SJoseph Huber "thread\n", 7383817ba13SJoseph Huber (int)HeapLimit, (int)StackLimit); 7393de4cd12SGeorge Rokos 7403de4cd12SGeorge Rokos // Set default number of teams 7414031bb98SShilei Tian if (EnvNumTeams > 0) { 7423de4cd12SGeorge Rokos DP("Default number of teams set according to environment %d\n", 7434031bb98SShilei Tian EnvNumTeams); 7444031bb98SShilei Tian DeviceData[DeviceId].NumTeams = EnvNumTeams; 7453de4cd12SGeorge Rokos } else { 7464031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceRTLTy::DefaultNumTeams; 7473de4cd12SGeorge Rokos DP("Default number of teams set according to library's default %d\n", 7484031bb98SShilei Tian DeviceRTLTy::DefaultNumTeams); 7493de4cd12SGeorge Rokos } 7504031bb98SShilei Tian 7514031bb98SShilei Tian if (DeviceData[DeviceId].NumTeams > DeviceData[DeviceId].BlocksPerGrid) { 7523de4cd12SGeorge Rokos DP("Default number of teams exceeds device limit, capping at %d\n", 7534031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 7544031bb98SShilei Tian DeviceData[DeviceId].NumTeams = DeviceData[DeviceId].BlocksPerGrid; 7553de4cd12SGeorge Rokos } 7563de4cd12SGeorge Rokos 7573de4cd12SGeorge Rokos // Set default number of threads 7584031bb98SShilei Tian DeviceData[DeviceId].NumThreads = DeviceRTLTy::DefaultNumThreads; 7593de4cd12SGeorge Rokos DP("Default number of threads set according to library's default %d\n", 7604031bb98SShilei Tian DeviceRTLTy::DefaultNumThreads); 7614031bb98SShilei Tian if (DeviceData[DeviceId].NumThreads > 7624031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock) { 7633de4cd12SGeorge Rokos DP("Default number of threads exceeds device limit, capping at %d\n", 7644031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 765c060c634SShilei Tian DeviceData[DeviceId].NumThreads = DeviceData[DeviceId].ThreadsPerBlock; 7663de4cd12SGeorge Rokos } 7673de4cd12SGeorge Rokos 7683de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 7693de4cd12SGeorge Rokos } 7703de4cd12SGeorge Rokos 7714031bb98SShilei Tian __tgt_target_table *loadBinary(const int DeviceId, 7724031bb98SShilei Tian const __tgt_device_image *Image) { 7734031bb98SShilei Tian // Set the context we are using 7744031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 7754031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 7764031bb98SShilei Tian return nullptr; 7773de4cd12SGeorge Rokos 7783de4cd12SGeorge Rokos // Clear the offload table as we are going to create a new one. 7794031bb98SShilei Tian clearOffloadEntriesTable(DeviceId); 7803de4cd12SGeorge Rokos 7813de4cd12SGeorge Rokos // Create the module and extract the function pointers. 7824031bb98SShilei Tian CUmodule Module; 7834031bb98SShilei Tian DP("Load data from image " DPxMOD "\n", DPxPTR(Image->ImageStart)); 7844031bb98SShilei Tian Err = cuModuleLoadDataEx(&Module, Image->ImageStart, 0, nullptr, nullptr); 7854031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuModuleLoadDataEx\n")) 7864031bb98SShilei Tian return nullptr; 7873de4cd12SGeorge Rokos 7883de4cd12SGeorge Rokos DP("CUDA module successfully loaded!\n"); 7894031bb98SShilei Tian 7904031bb98SShilei Tian Modules.push_back(Module); 7913de4cd12SGeorge Rokos 7923de4cd12SGeorge Rokos // Find the symbols in the module by name. 7934031bb98SShilei Tian const __tgt_offload_entry *HostBegin = Image->EntriesBegin; 7944031bb98SShilei Tian const __tgt_offload_entry *HostEnd = Image->EntriesEnd; 7953de4cd12SGeorge Rokos 7965272d29eSJohannes Doerfert std::list<KernelTy> &KernelsList = DeviceData[DeviceId].KernelsList; 7974031bb98SShilei Tian for (const __tgt_offload_entry *E = HostBegin; E != HostEnd; ++E) { 7984031bb98SShilei Tian if (!E->addr) { 7994031bb98SShilei Tian // We return nullptr when something like this happens, the host should 8004031bb98SShilei Tian // have always something in the address to uniquely identify the target 8014031bb98SShilei Tian // region. 8024031bb98SShilei Tian DP("Invalid binary: host entry '<null>' (size = %zd)...\n", E->size); 8034031bb98SShilei Tian return nullptr; 8043de4cd12SGeorge Rokos } 8053de4cd12SGeorge Rokos 8064031bb98SShilei Tian if (E->size) { 8074031bb98SShilei Tian __tgt_offload_entry Entry = *E; 8084031bb98SShilei Tian CUdeviceptr CUPtr; 8094031bb98SShilei Tian size_t CUSize; 81095f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&CUPtr, &CUSize, Module, E->name); 8114031bb98SShilei Tian // We keep this style here because we need the name 8124031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8132ce16810SJoseph Huber REPORT("Loading global '%s' Failed\n", E->name); 8144031bb98SShilei Tian CUDA_ERR_STRING(Err); 8154031bb98SShilei Tian return nullptr; 8163de4cd12SGeorge Rokos } 8173de4cd12SGeorge Rokos 8184031bb98SShilei Tian if (CUSize != E->size) { 8194031bb98SShilei Tian DP("Loading global '%s' - size mismatch (%zd != %zd)\n", E->name, 8204031bb98SShilei Tian CUSize, E->size); 8214031bb98SShilei Tian return nullptr; 8223de4cd12SGeorge Rokos } 8233de4cd12SGeorge Rokos 8243de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to global %s (" DPxMOD ")\n", 8254031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(CUPtr)); 8264031bb98SShilei Tian 8274031bb98SShilei Tian Entry.addr = (void *)(CUPtr); 8283de4cd12SGeorge Rokos 829aace6d28SGheorghe-Teodor Bercea // Note: In the current implementation declare target variables 830aace6d28SGheorghe-Teodor Bercea // can either be link or to. This means that once unified 831aace6d28SGheorghe-Teodor Bercea // memory is activated via the requires directive, the variable 832aace6d28SGheorghe-Teodor Bercea // can be used directly from the host in both cases. 833aace6d28SGheorghe-Teodor Bercea // TODO: when variables types other than to or link are added, 8344c6a098aSKazuaki Ishizaki // the below condition should be changed to explicitly 835aace6d28SGheorghe-Teodor Bercea // check for to and link variables types: 8364031bb98SShilei Tian // (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY && (e->flags & 8374031bb98SShilei Tian // OMP_DECLARE_TARGET_LINK || e->flags == OMP_DECLARE_TARGET_TO)) 8384031bb98SShilei Tian if (RequiresFlags & OMP_REQ_UNIFIED_SHARED_MEMORY) { 839aace6d28SGheorghe-Teodor Bercea // If unified memory is present any target link or to variables 840c5fe030cSGheorghe-Teodor Bercea // can access host addresses directly. There is no longer a 841c5fe030cSGheorghe-Teodor Bercea // need for device copies. 84295f0d1edSJon Chesterfield cuMemcpyHtoD(CUPtr, E->addr, sizeof(void *)); 8434031bb98SShilei Tian DP("Copy linked variable host address (" DPxMOD 8444031bb98SShilei Tian ") to device address (" DPxMOD ")\n", 8454031bb98SShilei Tian DPxPTR(*((void **)E->addr)), DPxPTR(CUPtr)); 846c5fe030cSGheorghe-Teodor Bercea } 847c5fe030cSGheorghe-Teodor Bercea 8484031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 8493de4cd12SGeorge Rokos 8503de4cd12SGeorge Rokos continue; 8513de4cd12SGeorge Rokos } 8523de4cd12SGeorge Rokos 8534031bb98SShilei Tian CUfunction Func; 8544031bb98SShilei Tian Err = cuModuleGetFunction(&Func, Module, E->name); 8554031bb98SShilei Tian // We keep this style here because we need the name 8564031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8572ce16810SJoseph Huber REPORT("Loading '%s' Failed\n", E->name); 8584031bb98SShilei Tian CUDA_ERR_STRING(Err); 8594031bb98SShilei Tian return nullptr; 8603de4cd12SGeorge Rokos } 8613de4cd12SGeorge Rokos 8623de4cd12SGeorge Rokos DP("Entry point " DPxMOD " maps to %s (" DPxMOD ")\n", 8634031bb98SShilei Tian DPxPTR(E - HostBegin), E->name, DPxPTR(Func)); 8643de4cd12SGeorge Rokos 8653de4cd12SGeorge Rokos // default value GENERIC (in case symbol is missing from cubin file) 866ca999f71SShilei Tian llvm::omp::OMPTgtExecModeFlags ExecModeVal; 8674031bb98SShilei Tian std::string ExecModeNameStr(E->name); 8683de4cd12SGeorge Rokos ExecModeNameStr += "_exec_mode"; 8693de4cd12SGeorge Rokos const char *ExecModeName = ExecModeNameStr.c_str(); 8703de4cd12SGeorge Rokos 8713de4cd12SGeorge Rokos CUdeviceptr ExecModePtr; 8724031bb98SShilei Tian size_t CUSize; 87395f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&ExecModePtr, &CUSize, Module, ExecModeName); 8744031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 875ca999f71SShilei Tian if (CUSize != sizeof(llvm::omp::OMPTgtExecModeFlags)) { 8763de4cd12SGeorge Rokos DP("Loading global exec_mode '%s' - size mismatch (%zd != %zd)\n", 877ca999f71SShilei Tian ExecModeName, CUSize, sizeof(llvm::omp::OMPTgtExecModeFlags)); 8784031bb98SShilei Tian return nullptr; 8793de4cd12SGeorge Rokos } 8803de4cd12SGeorge Rokos 88195f0d1edSJon Chesterfield Err = cuMemcpyDtoH(&ExecModeVal, ExecModePtr, CUSize); 8824031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 8832ce16810SJoseph Huber REPORT("Error when copying data from device to host. Pointers: " 8843de4cd12SGeorge Rokos "host = " DPxMOD ", device = " DPxMOD ", size = %zd\n", 8854031bb98SShilei Tian DPxPTR(&ExecModeVal), DPxPTR(ExecModePtr), CUSize); 8864031bb98SShilei Tian CUDA_ERR_STRING(Err); 8874031bb98SShilei Tian return nullptr; 8883de4cd12SGeorge Rokos } 8893de4cd12SGeorge Rokos } else { 890edb8acdcSJoseph Huber DP("Loading global exec_mode '%s' - symbol missing, using default " 8914031bb98SShilei Tian "value GENERIC (1)\n", 8924031bb98SShilei Tian ExecModeName); 8933de4cd12SGeorge Rokos } 8943de4cd12SGeorge Rokos 8954031bb98SShilei Tian KernelsList.emplace_back(Func, ExecModeVal); 8963de4cd12SGeorge Rokos 8974031bb98SShilei Tian __tgt_offload_entry Entry = *E; 8984031bb98SShilei Tian Entry.addr = &KernelsList.back(); 8994031bb98SShilei Tian addOffloadEntry(DeviceId, Entry); 9003de4cd12SGeorge Rokos } 9013de4cd12SGeorge Rokos 902e1c7a46dSGuansong Zhang // send device environment data to the device 903e1c7a46dSGuansong Zhang { 904ec02c34bSJoseph Huber // TODO: The device ID used here is not the real device ID used by OpenMP. 9050c554a47SJon Chesterfield DeviceEnvironmentTy DeviceEnv{0, static_cast<uint32_t>(NumberOfDevices), 9060c554a47SJon Chesterfield static_cast<uint32_t>(DeviceId), 9070c554a47SJon Chesterfield static_cast<uint32_t>(DynamicMemorySize)}; 908e1c7a46dSGuansong Zhang 9094031bb98SShilei Tian if (const char *EnvStr = getenv("LIBOMPTARGET_DEVICE_RTL_DEBUG")) 9100c554a47SJon Chesterfield DeviceEnv.DebugKind = std::stoi(EnvStr); 911e1c7a46dSGuansong Zhang 9124031bb98SShilei Tian const char *DeviceEnvName = "omptarget_device_environment"; 9134031bb98SShilei Tian CUdeviceptr DeviceEnvPtr; 9144031bb98SShilei Tian size_t CUSize; 915e1c7a46dSGuansong Zhang 91695f0d1edSJon Chesterfield Err = cuModuleGetGlobal(&DeviceEnvPtr, &CUSize, Module, DeviceEnvName); 9174031bb98SShilei Tian if (Err == CUDA_SUCCESS) { 9184031bb98SShilei Tian if (CUSize != sizeof(DeviceEnv)) { 9192ce16810SJoseph Huber REPORT( 9202ce16810SJoseph Huber "Global device_environment '%s' - size mismatch (%zu != %zu)\n", 9214031bb98SShilei Tian DeviceEnvName, CUSize, sizeof(int32_t)); 9224031bb98SShilei Tian CUDA_ERR_STRING(Err); 9234031bb98SShilei Tian return nullptr; 924e1c7a46dSGuansong Zhang } 925e1c7a46dSGuansong Zhang 92695f0d1edSJon Chesterfield Err = cuMemcpyHtoD(DeviceEnvPtr, &DeviceEnv, CUSize); 9274031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 9282ce16810SJoseph Huber REPORT("Error when copying data from host to device. Pointers: " 929e1c7a46dSGuansong Zhang "host = " DPxMOD ", device = " DPxMOD ", size = %zu\n", 9304031bb98SShilei Tian DPxPTR(&DeviceEnv), DPxPTR(DeviceEnvPtr), CUSize); 9314031bb98SShilei Tian CUDA_ERR_STRING(Err); 9324031bb98SShilei Tian return nullptr; 933e1c7a46dSGuansong Zhang } 934e1c7a46dSGuansong Zhang 9354031bb98SShilei Tian DP("Sending global device environment data %zu bytes\n", CUSize); 936e1c7a46dSGuansong Zhang } else { 9374031bb98SShilei Tian DP("Finding global device environment '%s' - symbol missing.\n", 9384031bb98SShilei Tian DeviceEnvName); 9394031bb98SShilei Tian DP("Continue, considering this is a device RTL which does not accept " 9404031bb98SShilei Tian "environment setting.\n"); 941e1c7a46dSGuansong Zhang } 942e1c7a46dSGuansong Zhang } 943e1c7a46dSGuansong Zhang 9444031bb98SShilei Tian return getOffloadEntriesTable(DeviceId); 9453de4cd12SGeorge Rokos } 9463de4cd12SGeorge Rokos 947a15f8589SJoseph Huber void *dataAlloc(const int DeviceId, const int64_t Size, 948a15f8589SJoseph Huber const TargetAllocTy Kind) { 949a15f8589SJoseph Huber switch (Kind) { 950a15f8589SJoseph Huber case TARGET_ALLOC_DEFAULT: 951a15f8589SJoseph Huber case TARGET_ALLOC_DEVICE: 9520871d6d5SShilei Tian if (UseMemoryManager) 9530871d6d5SShilei Tian return MemoryManagers[DeviceId]->allocate(Size, nullptr); 954a15f8589SJoseph Huber else 955a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 956a15f8589SJoseph Huber case TARGET_ALLOC_HOST: 957a15f8589SJoseph Huber case TARGET_ALLOC_SHARED: 958a15f8589SJoseph Huber return DeviceAllocators[DeviceId].allocate(Size, nullptr, Kind); 959a15f8589SJoseph Huber } 9604031bb98SShilei Tian 961a15f8589SJoseph Huber REPORT("Invalid target data allocation kind or requested allocator not " 962a15f8589SJoseph Huber "implemented yet\n"); 963a15f8589SJoseph Huber 964a15f8589SJoseph Huber return nullptr; 9653de4cd12SGeorge Rokos } 9663de4cd12SGeorge Rokos 9674031bb98SShilei Tian int dataSubmit(const int DeviceId, const void *TgtPtr, const void *HstPtr, 9685449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 9695449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 9703de4cd12SGeorge Rokos 9714031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 9724031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 9733de4cd12SGeorge Rokos return OFFLOAD_FAIL; 9743de4cd12SGeorge Rokos 9755449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 9763de4cd12SGeorge Rokos 97795f0d1edSJon Chesterfield Err = cuMemcpyHtoDAsync((CUdeviceptr)TgtPtr, HstPtr, Size, Stream); 9784031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 97929338459SJoseph Huber DP("Error when copying data from host to device. Pointers: host " 9802ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 9814031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 9824031bb98SShilei Tian CUDA_ERR_STRING(Err); 9833de4cd12SGeorge Rokos return OFFLOAD_FAIL; 9843de4cd12SGeorge Rokos } 9853de4cd12SGeorge Rokos 9863de4cd12SGeorge Rokos return OFFLOAD_SUCCESS; 9873de4cd12SGeorge Rokos } 9883de4cd12SGeorge Rokos 9894031bb98SShilei Tian int dataRetrieve(const int DeviceId, void *HstPtr, const void *TgtPtr, 9905449fbb5SJohannes Doerfert const int64_t Size, __tgt_async_info *AsyncInfo) const { 9915449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 9924031bb98SShilei Tian 9934031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 9944031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 99503ff643dSShilei Tian return OFFLOAD_FAIL; 99603ff643dSShilei Tian 9975449fbb5SJohannes Doerfert CUstream Stream = getStream(DeviceId, AsyncInfo); 99803ff643dSShilei Tian 99995f0d1edSJon Chesterfield Err = cuMemcpyDtoHAsync(HstPtr, (CUdeviceptr)TgtPtr, Size, Stream); 10004031bb98SShilei Tian if (Err != CUDA_SUCCESS) { 100129338459SJoseph Huber DP("Error when copying data from device to host. Pointers: host " 10022ce16810SJoseph Huber "= " DPxMOD ", device = " DPxMOD ", size = %" PRId64 "\n", 10034031bb98SShilei Tian DPxPTR(HstPtr), DPxPTR(TgtPtr), Size); 10044031bb98SShilei Tian CUDA_ERR_STRING(Err); 10053de4cd12SGeorge Rokos return OFFLOAD_FAIL; 10063de4cd12SGeorge Rokos } 10073de4cd12SGeorge Rokos 10084031bb98SShilei Tian return OFFLOAD_SUCCESS; 10094031bb98SShilei Tian } 10104031bb98SShilei Tian 1011a014fbbcSShilei Tian int dataExchange(int SrcDevId, const void *SrcPtr, int DstDevId, void *DstPtr, 10125449fbb5SJohannes Doerfert int64_t Size, __tgt_async_info *AsyncInfo) const { 10135449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 1014a014fbbcSShilei Tian 1015a014fbbcSShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[SrcDevId].Context); 1016a014fbbcSShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 1017a014fbbcSShilei Tian return OFFLOAD_FAIL; 1018a014fbbcSShilei Tian 10195449fbb5SJohannes Doerfert CUstream Stream = getStream(SrcDevId, AsyncInfo); 1020a014fbbcSShilei Tian 1021a014fbbcSShilei Tian // If they are two devices, we try peer to peer copy first 1022a014fbbcSShilei Tian if (SrcDevId != DstDevId) { 1023a014fbbcSShilei Tian int CanAccessPeer = 0; 1024a014fbbcSShilei Tian Err = cuDeviceCanAccessPeer(&CanAccessPeer, SrcDevId, DstDevId); 1025a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10262ce16810SJoseph Huber REPORT("Error returned from cuDeviceCanAccessPeer. src = %" PRId32 1027a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 1028a014fbbcSShilei Tian SrcDevId, DstDevId); 1029a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1030a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1031a014fbbcSShilei Tian } 1032a014fbbcSShilei Tian 1033a014fbbcSShilei Tian if (!CanAccessPeer) { 1034a014fbbcSShilei Tian DP("P2P memcpy not supported so fall back to D2D memcpy"); 1035a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1036a014fbbcSShilei Tian } 1037a014fbbcSShilei Tian 1038a014fbbcSShilei Tian Err = cuCtxEnablePeerAccess(DeviceData[DstDevId].Context, 0); 1039a014fbbcSShilei Tian if (Err != CUDA_SUCCESS) { 10402ce16810SJoseph Huber REPORT("Error returned from cuCtxEnablePeerAccess. src = %" PRId32 1041a014fbbcSShilei Tian ", dst = %" PRId32 "\n", 1042a014fbbcSShilei Tian SrcDevId, DstDevId); 1043a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1044a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1045a014fbbcSShilei Tian } 1046a014fbbcSShilei Tian 1047a014fbbcSShilei Tian Err = cuMemcpyPeerAsync((CUdeviceptr)DstPtr, DeviceData[DstDevId].Context, 1048a014fbbcSShilei Tian (CUdeviceptr)SrcPtr, DeviceData[SrcDevId].Context, 1049a014fbbcSShilei Tian Size, Stream); 1050a014fbbcSShilei Tian if (Err == CUDA_SUCCESS) 1051a014fbbcSShilei Tian return OFFLOAD_SUCCESS; 1052a014fbbcSShilei Tian 105329338459SJoseph Huber DP("Error returned from cuMemcpyPeerAsync. src_ptr = " DPxMOD 105429338459SJoseph Huber ", src_id =%" PRId32 ", dst_ptr = " DPxMOD ", dst_id =%" PRId32 "\n", 105545bb073dSYe Luo DPxPTR(SrcPtr), SrcDevId, DPxPTR(DstPtr), DstDevId); 1056a014fbbcSShilei Tian CUDA_ERR_STRING(Err); 1057a014fbbcSShilei Tian } 1058a014fbbcSShilei Tian 1059a014fbbcSShilei Tian return memcpyDtoD(SrcPtr, DstPtr, Size, Stream); 1060a014fbbcSShilei Tian } 1061a014fbbcSShilei Tian 10620871d6d5SShilei Tian int dataDelete(const int DeviceId, void *TgtPtr) { 10630871d6d5SShilei Tian if (UseMemoryManager) 10640871d6d5SShilei Tian return MemoryManagers[DeviceId]->free(TgtPtr); 10654031bb98SShilei Tian 10660871d6d5SShilei Tian return DeviceAllocators[DeviceId].free(TgtPtr); 10674031bb98SShilei Tian } 10684031bb98SShilei Tian 1069aa27cfc1SJohannes Doerfert int runTargetTeamRegion(const int DeviceId, void *TgtEntryPtr, void **TgtArgs, 1070aa27cfc1SJohannes Doerfert ptrdiff_t *TgtOffsets, const int ArgNum, 1071aa27cfc1SJohannes Doerfert const int TeamNum, const int ThreadLimit, 10724031bb98SShilei Tian const unsigned int LoopTripCount, 10734031bb98SShilei Tian __tgt_async_info *AsyncInfo) const { 10744031bb98SShilei Tian CUresult Err = cuCtxSetCurrent(DeviceData[DeviceId].Context); 10754031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuCtxSetCurrent\n")) 10764031bb98SShilei Tian return OFFLOAD_FAIL; 10774031bb98SShilei Tian 10783de4cd12SGeorge Rokos // All args are references. 10794031bb98SShilei Tian std::vector<void *> Args(ArgNum); 10804031bb98SShilei Tian std::vector<void *> Ptrs(ArgNum); 10813de4cd12SGeorge Rokos 10824031bb98SShilei Tian for (int I = 0; I < ArgNum; ++I) { 10834031bb98SShilei Tian Ptrs[I] = (void *)((intptr_t)TgtArgs[I] + TgtOffsets[I]); 10844031bb98SShilei Tian Args[I] = &Ptrs[I]; 10851546d319SGeorge Rokos } 10863de4cd12SGeorge Rokos 1087aa27cfc1SJohannes Doerfert KernelTy *KernelInfo = reinterpret_cast<KernelTy *>(TgtEntryPtr); 10883de4cd12SGeorge Rokos 1089ca999f71SShilei Tian const bool IsSPMDGenericMode = 1090ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_GENERIC_SPMD; 1091ca999f71SShilei Tian const bool IsSPMDMode = 1092ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_SPMD; 1093ca999f71SShilei Tian const bool IsGenericMode = 1094ca999f71SShilei Tian KernelInfo->ExecutionMode == llvm::omp::OMP_TGT_EXEC_MODE_GENERIC; 1095ca999f71SShilei Tian 1096aa27cfc1SJohannes Doerfert int CudaThreadsPerBlock; 10974031bb98SShilei Tian if (ThreadLimit > 0) { 10984031bb98SShilei Tian DP("Setting CUDA threads per block to requested %d\n", ThreadLimit); 10994031bb98SShilei Tian CudaThreadsPerBlock = ThreadLimit; 11003de4cd12SGeorge Rokos // Add master warp if necessary 1101ca999f71SShilei Tian if (IsGenericMode) { 11024031bb98SShilei Tian DP("Adding master warp: +%d threads\n", DeviceData[DeviceId].WarpSize); 11034031bb98SShilei Tian CudaThreadsPerBlock += DeviceData[DeviceId].WarpSize; 11043de4cd12SGeorge Rokos } 1105c13df8e5SGeorge Rokos } else { 1106c13df8e5SGeorge Rokos DP("Setting CUDA threads per block to default %d\n", 11074031bb98SShilei Tian DeviceData[DeviceId].NumThreads); 11084031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].NumThreads; 1109c13df8e5SGeorge Rokos } 11103de4cd12SGeorge Rokos 11114031bb98SShilei Tian if (CudaThreadsPerBlock > DeviceData[DeviceId].ThreadsPerBlock) { 11123de4cd12SGeorge Rokos DP("Threads per block capped at device limit %d\n", 11134031bb98SShilei Tian DeviceData[DeviceId].ThreadsPerBlock); 11144031bb98SShilei Tian CudaThreadsPerBlock = DeviceData[DeviceId].ThreadsPerBlock; 11153de4cd12SGeorge Rokos } 11163de4cd12SGeorge Rokos 1117aa27cfc1SJohannes Doerfert if (!KernelInfo->MaxThreadsPerBlock) { 1118aa27cfc1SJohannes Doerfert Err = cuFuncGetAttribute(&KernelInfo->MaxThreadsPerBlock, 11194031bb98SShilei Tian CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, 11204031bb98SShilei Tian KernelInfo->Func); 1121aa27cfc1SJohannes Doerfert if (!checkResult(Err, "Error returned from cuFuncGetAttribute\n")) 1122aa27cfc1SJohannes Doerfert return OFFLOAD_FAIL; 1123aa27cfc1SJohannes Doerfert } 1124aa27cfc1SJohannes Doerfert 1125aa27cfc1SJohannes Doerfert if (KernelInfo->MaxThreadsPerBlock < CudaThreadsPerBlock) { 1126aa27cfc1SJohannes Doerfert DP("Threads per block capped at kernel limit %d\n", 1127aa27cfc1SJohannes Doerfert KernelInfo->MaxThreadsPerBlock); 1128aa27cfc1SJohannes Doerfert CudaThreadsPerBlock = KernelInfo->MaxThreadsPerBlock; 11293de4cd12SGeorge Rokos } 11303de4cd12SGeorge Rokos 11314031bb98SShilei Tian unsigned int CudaBlocksPerGrid; 11324031bb98SShilei Tian if (TeamNum <= 0) { 11334031bb98SShilei Tian if (LoopTripCount > 0 && EnvNumTeams < 0) { 1134ca999f71SShilei Tian if (IsSPMDGenericMode) { 1135ca999f71SShilei Tian // If we reach this point, then we are executing a kernel that was 1136ca999f71SShilei Tian // transformed from Generic-mode to SPMD-mode. This kernel has 1137ca999f71SShilei Tian // SPMD-mode execution, but needs its blocks to be scheduled 1138ca999f71SShilei Tian // differently because the current loop trip count only applies to the 1139ca999f71SShilei Tian // `teams distribute` region and will create var too few blocks using 1140ca999f71SShilei Tian // the regular SPMD-mode method. 1141ca999f71SShilei Tian CudaBlocksPerGrid = LoopTripCount; 1142ca999f71SShilei Tian } else if (IsSPMDMode) { 11434031bb98SShilei Tian // We have a combined construct, i.e. `target teams distribute 11444031bb98SShilei Tian // parallel for [simd]`. We launch so many teams so that each thread 11454031bb98SShilei Tian // will execute one iteration of the loop. round up to the nearest 11464031bb98SShilei Tian // integer 11474031bb98SShilei Tian CudaBlocksPerGrid = ((LoopTripCount - 1) / CudaThreadsPerBlock) + 1; 1148ca999f71SShilei Tian } else if (IsGenericMode) { 1149c13df8e5SGeorge Rokos // If we reach this point, then we have a non-combined construct, i.e. 1150c13df8e5SGeorge Rokos // `teams distribute` with a nested `parallel for` and each team is 1151c13df8e5SGeorge Rokos // assigned one iteration of the `distribute` loop. E.g.: 1152c13df8e5SGeorge Rokos // 1153c13df8e5SGeorge Rokos // #pragma omp target teams distribute 1154c13df8e5SGeorge Rokos // for(...loop_tripcount...) { 1155c13df8e5SGeorge Rokos // #pragma omp parallel for 1156c13df8e5SGeorge Rokos // for(...) {} 1157c13df8e5SGeorge Rokos // } 1158c13df8e5SGeorge Rokos // 1159c13df8e5SGeorge Rokos // Threads within a team will execute the iterations of the `parallel` 1160c13df8e5SGeorge Rokos // loop. 11614031bb98SShilei Tian CudaBlocksPerGrid = LoopTripCount; 1162a158d366SJoseph Huber } else { 1163ca999f71SShilei Tian REPORT("Unknown execution mode: %d\n", 1164ca999f71SShilei Tian static_cast<int8_t>(KernelInfo->ExecutionMode)); 1165a158d366SJoseph Huber return OFFLOAD_FAIL; 1166c13df8e5SGeorge Rokos } 116745bb073dSYe Luo DP("Using %d teams due to loop trip count %" PRIu32 11684031bb98SShilei Tian " and number of threads per block %d\n", 11694031bb98SShilei Tian CudaBlocksPerGrid, LoopTripCount, CudaThreadsPerBlock); 11703de4cd12SGeorge Rokos } else { 11714031bb98SShilei Tian DP("Using default number of teams %d\n", DeviceData[DeviceId].NumTeams); 11724031bb98SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].NumTeams; 11733de4cd12SGeorge Rokos } 11744031bb98SShilei Tian } else if (TeamNum > DeviceData[DeviceId].BlocksPerGrid) { 11753de4cd12SGeorge Rokos DP("Capping number of teams to team limit %d\n", 11764031bb98SShilei Tian DeviceData[DeviceId].BlocksPerGrid); 11774031bb98SShilei Tian CudaBlocksPerGrid = DeviceData[DeviceId].BlocksPerGrid; 11783de4cd12SGeorge Rokos } else { 11794031bb98SShilei Tian DP("Using requested number of teams %d\n", TeamNum); 11804031bb98SShilei Tian CudaBlocksPerGrid = TeamNum; 11813de4cd12SGeorge Rokos } 11823de4cd12SGeorge Rokos 1183119a9ea1SJoseph Huber INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId, 1184ca999f71SShilei Tian "Launching kernel %s with %d blocks and %d threads in %s mode\n", 1185ae209397SJoseph Huber (getOffloadEntry(DeviceId, TgtEntryPtr)) 1186ae209397SJoseph Huber ? getOffloadEntry(DeviceId, TgtEntryPtr)->name 1187ae209397SJoseph Huber : "(null)", 1188ae209397SJoseph Huber CudaBlocksPerGrid, CudaThreadsPerBlock, 1189ca999f71SShilei Tian (!IsSPMDMode ? (IsGenericMode ? "Generic" : "SPMD-Generic") : "SPMD")); 11903de4cd12SGeorge Rokos 11914031bb98SShilei Tian CUstream Stream = getStream(DeviceId, AsyncInfo); 11924031bb98SShilei Tian Err = cuLaunchKernel(KernelInfo->Func, CudaBlocksPerGrid, /* gridDimY */ 1, 11934031bb98SShilei Tian /* gridDimZ */ 1, CudaThreadsPerBlock, 11944031bb98SShilei Tian /* blockDimY */ 1, /* blockDimZ */ 1, 1195f1c821faSJoseph Huber DynamicMemorySize, Stream, &Args[0], nullptr); 11964031bb98SShilei Tian if (!checkResult(Err, "Error returned from cuLaunchKernel\n")) 11974031bb98SShilei Tian return OFFLOAD_FAIL; 11984031bb98SShilei Tian 11994031bb98SShilei Tian DP("Launch of entry point at " DPxMOD " successful!\n", 12004031bb98SShilei Tian DPxPTR(TgtEntryPtr)); 12014031bb98SShilei Tian 12024031bb98SShilei Tian return OFFLOAD_SUCCESS; 12034031bb98SShilei Tian } 12044031bb98SShilei Tian 12055449fbb5SJohannes Doerfert int synchronize(const int DeviceId, __tgt_async_info *AsyncInfo) const { 12065449fbb5SJohannes Doerfert CUstream Stream = reinterpret_cast<CUstream>(AsyncInfo->Queue); 12074031bb98SShilei Tian CUresult Err = cuStreamSynchronize(Stream); 12083de4cd12SGeorge Rokos 12094031bb98SShilei Tian // Once the stream is synchronized, return it to stream pool and reset 12105449fbb5SJohannes Doerfert // AsyncInfo. This is to make sure the synchronization only works for its 12114031bb98SShilei Tian // own tasks. 1212a697a0a4SShilei Tian StreamPool[DeviceId]->release(reinterpret_cast<CUstream>(AsyncInfo->Queue)); 12135449fbb5SJohannes Doerfert AsyncInfo->Queue = nullptr; 12143de4cd12SGeorge Rokos 121516064e71SJoseph Huber if (Err != CUDA_SUCCESS) { 121629338459SJoseph Huber DP("Error when synchronizing stream. stream = " DPxMOD 121716064e71SJoseph Huber ", async info ptr = " DPxMOD "\n", 121816064e71SJoseph Huber DPxPTR(Stream), DPxPTR(AsyncInfo)); 121916064e71SJoseph Huber CUDA_ERR_STRING(Err); 122016064e71SJoseph Huber } 122116064e71SJoseph Huber return (Err == CUDA_SUCCESS) ? OFFLOAD_SUCCESS : OFFLOAD_FAIL; 12223de4cd12SGeorge Rokos } 1223d2f85d09SJose M Monsalve Diaz 1224d2f85d09SJose M Monsalve Diaz void printDeviceInfo(int32_t device_id) { 1225d2f85d09SJose M Monsalve Diaz char TmpChar[1000]; 1226d2f85d09SJose M Monsalve Diaz std::string TmpStr; 1227d2f85d09SJose M Monsalve Diaz size_t TmpSt; 1228d2f85d09SJose M Monsalve Diaz int TmpInt, TmpInt2, TmpInt3; 1229d2f85d09SJose M Monsalve Diaz 1230d2f85d09SJose M Monsalve Diaz CUdevice Device; 1231d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGet(&Device, device_id), 1232d2f85d09SJose M Monsalve Diaz "Error returned from cuCtxGetDevice\n"); 1233d2f85d09SJose M Monsalve Diaz 1234d2f85d09SJose M Monsalve Diaz cuDriverGetVersion(&TmpInt); 1235d2f85d09SJose M Monsalve Diaz printf(" CUDA Driver Version: \t\t%d \n", TmpInt); 1236d2f85d09SJose M Monsalve Diaz printf(" CUDA Device Number: \t\t%d \n", device_id); 1237d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetName(TmpChar, 1000, Device), 1238d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetName\n"); 1239d2f85d09SJose M Monsalve Diaz printf(" Device Name: \t\t\t%s \n", TmpChar); 1240d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceTotalMem(&TmpSt, Device), 1241d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceTotalMem\n"); 1242d2f85d09SJose M Monsalve Diaz printf(" Global Memory Size: \t\t%zu bytes \n", TmpSt); 1243d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1244d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, Device), 1245d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1246d2f85d09SJose M Monsalve Diaz printf(" Number of Multiprocessors: \t\t%d \n", TmpInt); 1247d2f85d09SJose M Monsalve Diaz checkResult( 1248d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_GPU_OVERLAP, Device), 1249d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1250d2f85d09SJose M Monsalve Diaz printf(" Concurrent Copy and Execution: \t%s \n", BOOL2TEXT(TmpInt)); 1251d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1252d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_TOTAL_CONSTANT_MEMORY, Device), 1253d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1254d2f85d09SJose M Monsalve Diaz printf(" Total Constant Memory: \t\t%d bytes\n", TmpInt); 1255d2f85d09SJose M Monsalve Diaz checkResult( 1256d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1257d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK, Device), 1258d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1259d2f85d09SJose M Monsalve Diaz printf(" Max Shared Memory per Block: \t%d bytes \n", TmpInt); 126028939b6aSShilei Tian checkResult( 126128939b6aSShilei Tian cuDeviceGetAttribute( 1262d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK, Device), 1263d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1264d2f85d09SJose M Monsalve Diaz printf(" Registers per Block: \t\t%d \n", TmpInt); 1265d2f85d09SJose M Monsalve Diaz checkResult( 1266d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_WARP_SIZE, Device), 1267d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1268d2f85d09SJose M Monsalve Diaz printf(" Warp Size: \t\t\t\t%d Threads \n", TmpInt); 1269d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1270d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK, Device), 1271d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1272d2f85d09SJose M Monsalve Diaz printf(" Maximum Threads per Block: \t\t%d \n", TmpInt); 127328939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 127428939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X, Device), 1275d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 127628939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 127728939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y, Device), 1278d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 127928939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 128028939b6aSShilei Tian &TmpInt3, CU_DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z, Device), 1281d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1282d2f85d09SJose M Monsalve Diaz printf(" Maximum Block Dimensions: \t\t%d, %d, %d \n", TmpInt, TmpInt2, 1283d2f85d09SJose M Monsalve Diaz TmpInt3); 128428939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 128528939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_X, Device), 1286d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 128728939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 128828939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Y, Device), 1289d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 129028939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 129128939b6aSShilei Tian &TmpInt3, CU_DEVICE_ATTRIBUTE_MAX_GRID_DIM_Z, Device), 1292d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1293d2f85d09SJose M Monsalve Diaz printf(" Maximum Grid Dimensions: \t\t%d x %d x %d \n", TmpInt, TmpInt2, 1294d2f85d09SJose M Monsalve Diaz TmpInt3); 1295d2f85d09SJose M Monsalve Diaz checkResult( 1296d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_MAX_PITCH, Device), 1297d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1298d2f85d09SJose M Monsalve Diaz printf(" Maximum Memory Pitch: \t\t%d bytes \n", TmpInt); 1299d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1300d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT, Device), 1301d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1302d2f85d09SJose M Monsalve Diaz printf(" Texture Alignment: \t\t\t%d bytes \n", TmpInt); 1303d2f85d09SJose M Monsalve Diaz checkResult( 1304d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_CLOCK_RATE, Device), 1305d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1306d2f85d09SJose M Monsalve Diaz printf(" Clock Rate: \t\t\t%d kHz\n", TmpInt); 1307d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1308d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT, Device), 1309d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1310d2f85d09SJose M Monsalve Diaz printf(" Execution Timeout: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1311d2f85d09SJose M Monsalve Diaz checkResult( 1312d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_INTEGRATED, Device), 1313d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1314d2f85d09SJose M Monsalve Diaz printf(" Integrated Device: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1315d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1316d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CAN_MAP_HOST_MEMORY, Device), 1317d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1318d2f85d09SJose M Monsalve Diaz printf(" Can Map Host Memory: \t\t%s \n", BOOL2TEXT(TmpInt)); 1319d2f85d09SJose M Monsalve Diaz checkResult( 1320d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_MODE, Device), 1321d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1322d2f85d09SJose M Monsalve Diaz if (TmpInt == CU_COMPUTEMODE_DEFAULT) 1323d2f85d09SJose M Monsalve Diaz TmpStr = "DEFAULT"; 1324d2f85d09SJose M Monsalve Diaz else if (TmpInt == CU_COMPUTEMODE_PROHIBITED) 1325d2f85d09SJose M Monsalve Diaz TmpStr = "PROHIBITED"; 1326d2f85d09SJose M Monsalve Diaz else if (TmpInt == CU_COMPUTEMODE_EXCLUSIVE_PROCESS) 1327d2f85d09SJose M Monsalve Diaz TmpStr = "EXCLUSIVE PROCESS"; 1328d2f85d09SJose M Monsalve Diaz else 1329d2f85d09SJose M Monsalve Diaz TmpStr = "unknown"; 1330d2f85d09SJose M Monsalve Diaz printf(" Compute Mode: \t\t\t%s \n", TmpStr.c_str()); 1331d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1332d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CONCURRENT_KERNELS, Device), 1333d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1334d2f85d09SJose M Monsalve Diaz printf(" Concurrent Kernels: \t\t%s \n", BOOL2TEXT(TmpInt)); 1335d2f85d09SJose M Monsalve Diaz checkResult( 1336d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_ECC_ENABLED, Device), 1337d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1338d2f85d09SJose M Monsalve Diaz printf(" ECC Enabled: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1339d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1340d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE, Device), 1341d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1342d2f85d09SJose M Monsalve Diaz printf(" Memory Clock Rate: \t\t\t%d kHz\n", TmpInt); 134328939b6aSShilei Tian checkResult( 134428939b6aSShilei Tian cuDeviceGetAttribute( 1345d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH, Device), 1346d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1347d2f85d09SJose M Monsalve Diaz printf(" Memory Bus Width: \t\t\t%d bits\n", TmpInt); 134828939b6aSShilei Tian checkResult(cuDeviceGetAttribute(&TmpInt, CU_DEVICE_ATTRIBUTE_L2_CACHE_SIZE, 134928939b6aSShilei Tian Device), 1350d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1351d2f85d09SJose M Monsalve Diaz printf(" L2 Cache Size: \t\t\t%d bytes \n", TmpInt); 135228939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 135328939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR, 135428939b6aSShilei Tian Device), 1355d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1356d2f85d09SJose M Monsalve Diaz printf(" Max Threads Per SMP: \t\t%d \n", TmpInt); 1357d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1358d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_ASYNC_ENGINE_COUNT, Device), 1359d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1360d2f85d09SJose M Monsalve Diaz printf(" Async Engines: \t\t\t%s (%d) \n", BOOL2TEXT(TmpInt), TmpInt); 1361d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1362d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, Device), 1363d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1364d2f85d09SJose M Monsalve Diaz printf(" Unified Addressing: \t\t%s \n", BOOL2TEXT(TmpInt)); 136528939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 136628939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MANAGED_MEMORY, Device), 1367d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1368d2f85d09SJose M Monsalve Diaz printf(" Managed Memory: \t\t\t%s \n", BOOL2TEXT(TmpInt)); 1369d2f85d09SJose M Monsalve Diaz checkResult( 1370d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1371d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, Device), 1372d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1373d2f85d09SJose M Monsalve Diaz printf(" Concurrent Managed Memory: \t\t%s \n", BOOL2TEXT(TmpInt)); 1374d2f85d09SJose M Monsalve Diaz checkResult( 1375d2f85d09SJose M Monsalve Diaz cuDeviceGetAttribute( 1376d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_PREEMPTION_SUPPORTED, Device), 1377d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1378d2f85d09SJose M Monsalve Diaz printf(" Preemption Supported: \t\t%s \n", BOOL2TEXT(TmpInt)); 1379d2f85d09SJose M Monsalve Diaz checkResult(cuDeviceGetAttribute( 1380d2f85d09SJose M Monsalve Diaz &TmpInt, CU_DEVICE_ATTRIBUTE_COOPERATIVE_LAUNCH, Device), 1381d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1382d2f85d09SJose M Monsalve Diaz printf(" Cooperative Launch: \t\t%s \n", BOOL2TEXT(TmpInt)); 138328939b6aSShilei Tian checkResult(cuDeviceGetAttribute( 138428939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_MULTI_GPU_BOARD, Device), 1385d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1386d2f85d09SJose M Monsalve Diaz printf(" Multi-Device Boars: \t\t%s \n", BOOL2TEXT(TmpInt)); 138728939b6aSShilei Tian checkResult( 138828939b6aSShilei Tian cuDeviceGetAttribute( 138928939b6aSShilei Tian &TmpInt, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, Device), 1390d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 139128939b6aSShilei Tian checkResult( 139228939b6aSShilei Tian cuDeviceGetAttribute( 139328939b6aSShilei Tian &TmpInt2, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, Device), 1394d2f85d09SJose M Monsalve Diaz "Error returned from cuDeviceGetAttribute\n"); 1395d2f85d09SJose M Monsalve Diaz printf(" Compute Capabilities: \t\t%d%d \n", TmpInt, TmpInt2); 1396d2f85d09SJose M Monsalve Diaz } 139729df4ab3SShilei Tian 139829df4ab3SShilei Tian int waitEvent(const int DeviceId, __tgt_async_info *AsyncInfo, 139929df4ab3SShilei Tian void *EventPtr) const { 140029df4ab3SShilei Tian CUstream Stream = getStream(DeviceId, AsyncInfo); 140129df4ab3SShilei Tian CUevent Event = reinterpret_cast<CUevent>(EventPtr); 140229df4ab3SShilei Tian 140329df4ab3SShilei Tian // We don't use CU_EVENT_WAIT_DEFAULT here as it is only available from 140429df4ab3SShilei Tian // specific CUDA version, and defined as 0x0. In previous version, per CUDA 140529df4ab3SShilei Tian // API document, that argument has to be 0x0. 140629df4ab3SShilei Tian CUresult Err = cuStreamWaitEvent(Stream, Event, 0); 140729df4ab3SShilei Tian if (Err != CUDA_SUCCESS) { 140829df4ab3SShilei Tian DP("Error when waiting event. stream = " DPxMOD ", event = " DPxMOD "\n", 140929df4ab3SShilei Tian DPxPTR(Stream), DPxPTR(Event)); 141029df4ab3SShilei Tian CUDA_ERR_STRING(Err); 141129df4ab3SShilei Tian return OFFLOAD_FAIL; 141229df4ab3SShilei Tian } 141329df4ab3SShilei Tian 141429df4ab3SShilei Tian return OFFLOAD_SUCCESS; 141529df4ab3SShilei Tian } 14164031bb98SShilei Tian }; 14174031bb98SShilei Tian 14184031bb98SShilei Tian DeviceRTLTy DeviceRTL; 14194031bb98SShilei Tian } // namespace 14204031bb98SShilei Tian 14214031bb98SShilei Tian // Exposed library API function 14224031bb98SShilei Tian #ifdef __cplusplus 14234031bb98SShilei Tian extern "C" { 14244031bb98SShilei Tian #endif 14254031bb98SShilei Tian 14264031bb98SShilei Tian int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *image) { 14274031bb98SShilei Tian return elf_check_machine(image, /* EM_CUDA */ 190); 14284031bb98SShilei Tian } 14294031bb98SShilei Tian 14304031bb98SShilei Tian int32_t __tgt_rtl_number_of_devices() { return DeviceRTL.getNumOfDevices(); } 14314031bb98SShilei Tian 14324031bb98SShilei Tian int64_t __tgt_rtl_init_requires(int64_t RequiresFlags) { 143340470eb2SGeorge Rokos DP("Init requires flags to %" PRId64 "\n", RequiresFlags); 14344031bb98SShilei Tian DeviceRTL.setRequiresFlag(RequiresFlags); 14354031bb98SShilei Tian return RequiresFlags; 14364031bb98SShilei Tian } 14374031bb98SShilei Tian 1438a014fbbcSShilei Tian int32_t __tgt_rtl_is_data_exchangable(int32_t src_dev_id, int dst_dev_id) { 1439a014fbbcSShilei Tian if (DeviceRTL.isValidDeviceId(src_dev_id) && 1440a014fbbcSShilei Tian DeviceRTL.isValidDeviceId(dst_dev_id)) 1441a014fbbcSShilei Tian return 1; 1442a014fbbcSShilei Tian 1443a014fbbcSShilei Tian return 0; 1444a014fbbcSShilei Tian } 1445a014fbbcSShilei Tian 14464031bb98SShilei Tian int32_t __tgt_rtl_init_device(int32_t device_id) { 14474031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14484031bb98SShilei Tian 14494031bb98SShilei Tian return DeviceRTL.initDevice(device_id); 14504031bb98SShilei Tian } 14514031bb98SShilei Tian 14524031bb98SShilei Tian __tgt_target_table *__tgt_rtl_load_binary(int32_t device_id, 14534031bb98SShilei Tian __tgt_device_image *image) { 14544031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14554031bb98SShilei Tian 14564031bb98SShilei Tian return DeviceRTL.loadBinary(device_id, image); 14574031bb98SShilei Tian } 14584031bb98SShilei Tian 14592468fdd9SGeorge Rokos void *__tgt_rtl_data_alloc(int32_t device_id, int64_t size, void *, 14602468fdd9SGeorge Rokos int32_t kind) { 14614031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14624031bb98SShilei Tian 1463a15f8589SJoseph Huber return DeviceRTL.dataAlloc(device_id, size, (TargetAllocTy)kind); 14644031bb98SShilei Tian } 14654031bb98SShilei Tian 14664031bb98SShilei Tian int32_t __tgt_rtl_data_submit(int32_t device_id, void *tgt_ptr, void *hst_ptr, 14674031bb98SShilei Tian int64_t size) { 14684031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14694031bb98SShilei Tian 14705449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 14714031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_submit_async(device_id, tgt_ptr, hst_ptr, 14725449fbb5SJohannes Doerfert size, &AsyncInfo); 14734031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 14744031bb98SShilei Tian return OFFLOAD_FAIL; 14754031bb98SShilei Tian 14765449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 14774031bb98SShilei Tian } 14784031bb98SShilei Tian 14794031bb98SShilei Tian int32_t __tgt_rtl_data_submit_async(int32_t device_id, void *tgt_ptr, 14804031bb98SShilei Tian void *hst_ptr, int64_t size, 14814031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 14824031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14834031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 14844031bb98SShilei Tian 14854031bb98SShilei Tian return DeviceRTL.dataSubmit(device_id, tgt_ptr, hst_ptr, size, 14864031bb98SShilei Tian async_info_ptr); 14874031bb98SShilei Tian } 14884031bb98SShilei Tian 14894031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve(int32_t device_id, void *hst_ptr, void *tgt_ptr, 14904031bb98SShilei Tian int64_t size) { 14914031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 14924031bb98SShilei Tian 14935449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 14944031bb98SShilei Tian const int32_t rc = __tgt_rtl_data_retrieve_async(device_id, hst_ptr, tgt_ptr, 14955449fbb5SJohannes Doerfert size, &AsyncInfo); 14964031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 14974031bb98SShilei Tian return OFFLOAD_FAIL; 14984031bb98SShilei Tian 14995449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 15004031bb98SShilei Tian } 15014031bb98SShilei Tian 15024031bb98SShilei Tian int32_t __tgt_rtl_data_retrieve_async(int32_t device_id, void *hst_ptr, 15034031bb98SShilei Tian void *tgt_ptr, int64_t size, 15044031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 15054031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15064031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 15074031bb98SShilei Tian 15084031bb98SShilei Tian return DeviceRTL.dataRetrieve(device_id, hst_ptr, tgt_ptr, size, 15094031bb98SShilei Tian async_info_ptr); 15104031bb98SShilei Tian } 15114031bb98SShilei Tian 1512a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange_async(int32_t src_dev_id, void *src_ptr, 1513a014fbbcSShilei Tian int dst_dev_id, void *dst_ptr, 1514a014fbbcSShilei Tian int64_t size, 15155449fbb5SJohannes Doerfert __tgt_async_info *AsyncInfo) { 1516a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1517a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 15185449fbb5SJohannes Doerfert assert(AsyncInfo && "AsyncInfo is nullptr"); 1519a014fbbcSShilei Tian 1520a014fbbcSShilei Tian return DeviceRTL.dataExchange(src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, 15215449fbb5SJohannes Doerfert AsyncInfo); 1522a014fbbcSShilei Tian } 1523a014fbbcSShilei Tian 1524a014fbbcSShilei Tian int32_t __tgt_rtl_data_exchange(int32_t src_dev_id, void *src_ptr, 1525a014fbbcSShilei Tian int32_t dst_dev_id, void *dst_ptr, 1526a014fbbcSShilei Tian int64_t size) { 1527a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(src_dev_id) && "src_dev_id is invalid"); 1528a014fbbcSShilei Tian assert(DeviceRTL.isValidDeviceId(dst_dev_id) && "dst_dev_id is invalid"); 1529a014fbbcSShilei Tian 15305449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 1531a014fbbcSShilei Tian const int32_t rc = __tgt_rtl_data_exchange_async( 15325449fbb5SJohannes Doerfert src_dev_id, src_ptr, dst_dev_id, dst_ptr, size, &AsyncInfo); 1533a014fbbcSShilei Tian if (rc != OFFLOAD_SUCCESS) 1534a014fbbcSShilei Tian return OFFLOAD_FAIL; 1535a014fbbcSShilei Tian 15365449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(src_dev_id, &AsyncInfo); 1537a014fbbcSShilei Tian } 1538a014fbbcSShilei Tian 15394031bb98SShilei Tian int32_t __tgt_rtl_data_delete(int32_t device_id, void *tgt_ptr) { 15404031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15414031bb98SShilei Tian 15424031bb98SShilei Tian return DeviceRTL.dataDelete(device_id, tgt_ptr); 15434031bb98SShilei Tian } 15444031bb98SShilei Tian 15454031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region(int32_t device_id, void *tgt_entry_ptr, 15464031bb98SShilei Tian void **tgt_args, 15474031bb98SShilei Tian ptrdiff_t *tgt_offsets, 15484031bb98SShilei Tian int32_t arg_num, int32_t team_num, 15494031bb98SShilei Tian int32_t thread_limit, 15504031bb98SShilei Tian uint64_t loop_tripcount) { 15514031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15524031bb98SShilei Tian 15535449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 15544031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_team_region_async( 15554031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 15565449fbb5SJohannes Doerfert thread_limit, loop_tripcount, &AsyncInfo); 15574031bb98SShilei Tian if (rc != OFFLOAD_SUCCESS) 15584031bb98SShilei Tian return OFFLOAD_FAIL; 15594031bb98SShilei Tian 15605449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 15614031bb98SShilei Tian } 15624031bb98SShilei Tian 15634031bb98SShilei Tian int32_t __tgt_rtl_run_target_team_region_async( 15644031bb98SShilei Tian int32_t device_id, void *tgt_entry_ptr, void **tgt_args, 15654031bb98SShilei Tian ptrdiff_t *tgt_offsets, int32_t arg_num, int32_t team_num, 15664031bb98SShilei Tian int32_t thread_limit, uint64_t loop_tripcount, 15674031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 15684031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15694031bb98SShilei Tian 15704031bb98SShilei Tian return DeviceRTL.runTargetTeamRegion( 15714031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, team_num, 15724031bb98SShilei Tian thread_limit, loop_tripcount, async_info_ptr); 15734031bb98SShilei Tian } 15743de4cd12SGeorge Rokos 15753de4cd12SGeorge Rokos int32_t __tgt_rtl_run_target_region(int32_t device_id, void *tgt_entry_ptr, 157632ed2927SShilei Tian void **tgt_args, ptrdiff_t *tgt_offsets, 157703ff643dSShilei Tian int32_t arg_num) { 15784031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15794031bb98SShilei Tian 15805449fbb5SJohannes Doerfert __tgt_async_info AsyncInfo; 15814031bb98SShilei Tian const int32_t rc = __tgt_rtl_run_target_region_async( 15825449fbb5SJohannes Doerfert device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, &AsyncInfo); 158303ff643dSShilei Tian if (rc != OFFLOAD_SUCCESS) 158403ff643dSShilei Tian return OFFLOAD_FAIL; 158503ff643dSShilei Tian 15865449fbb5SJohannes Doerfert return __tgt_rtl_synchronize(device_id, &AsyncInfo); 158703ff643dSShilei Tian } 158803ff643dSShilei Tian 158903ff643dSShilei Tian int32_t __tgt_rtl_run_target_region_async(int32_t device_id, 159003ff643dSShilei Tian void *tgt_entry_ptr, void **tgt_args, 159103ff643dSShilei Tian ptrdiff_t *tgt_offsets, 159232ed2927SShilei Tian int32_t arg_num, 15934031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 15944031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 15954031bb98SShilei Tian 159603ff643dSShilei Tian return __tgt_rtl_run_target_team_region_async( 15974031bb98SShilei Tian device_id, tgt_entry_ptr, tgt_args, tgt_offsets, arg_num, 15984031bb98SShilei Tian /* team num*/ 1, /* thread_limit */ 1, /* loop_tripcount */ 0, 15994031bb98SShilei Tian async_info_ptr); 160032ed2927SShilei Tian } 160132ed2927SShilei Tian 16024031bb98SShilei Tian int32_t __tgt_rtl_synchronize(int32_t device_id, 16034031bb98SShilei Tian __tgt_async_info *async_info_ptr) { 16044031bb98SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 16054031bb98SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 16064031bb98SShilei Tian assert(async_info_ptr->Queue && "async_info_ptr->Queue is nullptr"); 160732ed2927SShilei Tian 16084031bb98SShilei Tian return DeviceRTL.synchronize(device_id, async_info_ptr); 16093de4cd12SGeorge Rokos } 16103de4cd12SGeorge Rokos 16112b6f2008SJoseph Huber void __tgt_rtl_set_info_flag(uint32_t NewInfoLevel) { 161259b68490SJoseph Huber std::atomic<uint32_t> &InfoLevel = getInfoLevelInternal(); 16132b6f2008SJoseph Huber InfoLevel.store(NewInfoLevel); 16142b6f2008SJoseph Huber } 16152b6f2008SJoseph Huber 1616d2f85d09SJose M Monsalve Diaz void __tgt_rtl_print_device_info(int32_t device_id) { 1617d2f85d09SJose M Monsalve Diaz assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 1618d2f85d09SJose M Monsalve Diaz DeviceRTL.printDeviceInfo(device_id); 1619d2f85d09SJose M Monsalve Diaz } 1620d2f85d09SJose M Monsalve Diaz 162129df4ab3SShilei Tian int32_t __tgt_rtl_create_event(int32_t device_id, void **event) { 162229df4ab3SShilei Tian assert(event && "event is nullptr"); 162329df4ab3SShilei Tian return createEvent(event); 162429df4ab3SShilei Tian } 162529df4ab3SShilei Tian 162629df4ab3SShilei Tian int32_t __tgt_rtl_record_event(int32_t device_id, void *event_ptr, 162729df4ab3SShilei Tian __tgt_async_info *async_info_ptr) { 162829df4ab3SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 162929df4ab3SShilei Tian assert(async_info_ptr->Queue && "async_info_ptr->Queue is nullptr"); 163029df4ab3SShilei Tian assert(event_ptr && "event_ptr is nullptr"); 163129df4ab3SShilei Tian 163229df4ab3SShilei Tian return recordEvent(event_ptr, async_info_ptr); 163329df4ab3SShilei Tian } 163429df4ab3SShilei Tian 163529df4ab3SShilei Tian int32_t __tgt_rtl_wait_event(int32_t device_id, void *event_ptr, 163629df4ab3SShilei Tian __tgt_async_info *async_info_ptr) { 163729df4ab3SShilei Tian assert(DeviceRTL.isValidDeviceId(device_id) && "device_id is invalid"); 163829df4ab3SShilei Tian assert(async_info_ptr && "async_info_ptr is nullptr"); 163929df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 164029df4ab3SShilei Tian 164129df4ab3SShilei Tian return DeviceRTL.waitEvent(device_id, async_info_ptr, event_ptr); 164229df4ab3SShilei Tian } 164329df4ab3SShilei Tian 164429df4ab3SShilei Tian int32_t __tgt_rtl_sync_event(int32_t device_id, void *event_ptr) { 164529df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 164629df4ab3SShilei Tian 164729df4ab3SShilei Tian return syncEvent(event_ptr); 164829df4ab3SShilei Tian } 164929df4ab3SShilei Tian 165029df4ab3SShilei Tian int32_t __tgt_rtl_destroy_event(int32_t device_id, void *event_ptr) { 165129df4ab3SShilei Tian assert(event_ptr && "event is nullptr"); 165229df4ab3SShilei Tian 165329df4ab3SShilei Tian return destroyEvent(event_ptr); 165429df4ab3SShilei Tian } 165529df4ab3SShilei Tian 16563de4cd12SGeorge Rokos #ifdef __cplusplus 16573de4cd12SGeorge Rokos } 16583de4cd12SGeorge Rokos #endif 1659