llvm-project
674 строки · 25.0 Кб
1//===----------- api.cpp - Target independent OpenMP target RTL -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of OpenMP API interface functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PluginManager.h"14#include "device.h"15#include "omptarget.h"16#include "rtl.h"17
18#include "OpenMP/InternalTypes.h"19#include "OpenMP/Mapping.h"20#include "OpenMP/OMPT/Interface.h"21#include "OpenMP/omp.h"22#include "Shared/Profile.h"23
24#include "llvm/ADT/SmallVector.h"25
26#include <climits>27#include <cstdlib>28#include <cstring>29#include <mutex>30
31EXTERN void ompx_dump_mapping_tables() {32ident_t Loc = {0, 0, 0, 0, ";libomptarget;libomptarget;0;0;;"};33auto ExclusiveDevicesAccessor = PM->getExclusiveDevicesAccessor();34for (auto &Device : PM->devices(ExclusiveDevicesAccessor))35dumpTargetPointerMappings(&Loc, Device, true);36}
37
38#ifdef OMPT_SUPPORT39using namespace llvm::omp::target::ompt;40#endif41
42void *targetAllocExplicit(size_t Size, int DeviceNum, int Kind,43const char *Name);44void targetFreeExplicit(void *DevicePtr, int DeviceNum, int Kind,45const char *Name);46void *targetLockExplicit(void *HostPtr, size_t Size, int DeviceNum,47const char *Name);48void targetUnlockExplicit(void *HostPtr, int DeviceNum, const char *Name);49
50// Implemented in libomp, they are called from within __tgt_* functions.
51extern "C" {52int __kmpc_get_target_offload(void) __attribute__((weak));53kmp_task_t *__kmpc_omp_task_alloc(ident_t *loc_ref, int32_t gtid, int32_t flags,54size_t sizeof_kmp_task_t,55size_t sizeof_shareds,56kmp_routine_entry_t task_entry)57__attribute__((weak));58
59kmp_task_t *60__kmpc_omp_target_task_alloc(ident_t *loc_ref, int32_t gtid, int32_t flags,61size_t sizeof_kmp_task_t, size_t sizeof_shareds,62kmp_routine_entry_t task_entry, int64_t device_id)63__attribute__((weak));64
65int32_t __kmpc_omp_task_with_deps(ident_t *loc_ref, int32_t gtid,66kmp_task_t *new_task, int32_t ndeps,67kmp_depend_info_t *dep_list,68int32_t ndeps_noalias,69kmp_depend_info_t *noalias_dep_list)70__attribute__((weak));71}
72
73EXTERN int omp_get_num_devices(void) {74TIMESCOPE();75OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));76size_t NumDevices = PM->getNumDevices();77
78DP("Call to omp_get_num_devices returning %zd\n", NumDevices);79
80return NumDevices;81}
82
83EXTERN int omp_get_device_num(void) {84TIMESCOPE();85OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));86int HostDevice = omp_get_initial_device();87
88DP("Call to omp_get_device_num returning %d\n", HostDevice);89
90return HostDevice;91}
92
93EXTERN int omp_get_initial_device(void) {94TIMESCOPE();95OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));96int HostDevice = omp_get_num_devices();97DP("Call to omp_get_initial_device returning %d\n", HostDevice);98return HostDevice;99}
100
101EXTERN void *omp_target_alloc(size_t Size, int DeviceNum) {102TIMESCOPE_WITH_DETAILS("dst_dev=" + std::to_string(DeviceNum) +103";size=" + std::to_string(Size));104OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));105return targetAllocExplicit(Size, DeviceNum, TARGET_ALLOC_DEFAULT, __func__);106}
107
108EXTERN void *llvm_omp_target_alloc_device(size_t Size, int DeviceNum) {109OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));110return targetAllocExplicit(Size, DeviceNum, TARGET_ALLOC_DEVICE, __func__);111}
112
113EXTERN void *llvm_omp_target_alloc_host(size_t Size, int DeviceNum) {114OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));115return targetAllocExplicit(Size, DeviceNum, TARGET_ALLOC_HOST, __func__);116}
117
118EXTERN void *llvm_omp_target_alloc_shared(size_t Size, int DeviceNum) {119OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));120return targetAllocExplicit(Size, DeviceNum, TARGET_ALLOC_SHARED, __func__);121}
122
123EXTERN void omp_target_free(void *Ptr, int DeviceNum) {124TIMESCOPE();125OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));126return targetFreeExplicit(Ptr, DeviceNum, TARGET_ALLOC_DEFAULT, __func__);127}
128
129EXTERN void llvm_omp_target_free_device(void *Ptr, int DeviceNum) {130OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));131return targetFreeExplicit(Ptr, DeviceNum, TARGET_ALLOC_DEVICE, __func__);132}
133
134EXTERN void llvm_omp_target_free_host(void *Ptr, int DeviceNum) {135OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));136return targetFreeExplicit(Ptr, DeviceNum, TARGET_ALLOC_HOST, __func__);137}
138
139EXTERN void llvm_omp_target_free_shared(void *Ptre, int DeviceNum) {140OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));141return targetFreeExplicit(Ptre, DeviceNum, TARGET_ALLOC_SHARED, __func__);142}
143
144EXTERN void *llvm_omp_target_dynamic_shared_alloc() {145OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));146return nullptr;147}
148
149EXTERN void *llvm_omp_get_dynamic_shared() {150OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));151return nullptr;152}
153
154EXTERN [[nodiscard]] void *llvm_omp_target_lock_mem(void *Ptr, size_t Size,155int DeviceNum) {156OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));157return targetLockExplicit(Ptr, Size, DeviceNum, __func__);158}
159
160EXTERN void llvm_omp_target_unlock_mem(void *Ptr, int DeviceNum) {161OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));162targetUnlockExplicit(Ptr, DeviceNum, __func__);163}
164
165EXTERN int omp_target_is_present(const void *Ptr, int DeviceNum) {166TIMESCOPE();167OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));168DP("Call to omp_target_is_present for device %d and address " DPxMOD "\n",169DeviceNum, DPxPTR(Ptr));170
171if (!Ptr) {172DP("Call to omp_target_is_present with NULL ptr, returning false\n");173return false;174}175
176if (DeviceNum == omp_get_initial_device()) {177DP("Call to omp_target_is_present on host, returning true\n");178return true;179}180
181auto DeviceOrErr = PM->getDevice(DeviceNum);182if (!DeviceOrErr)183FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());184
185// omp_target_is_present tests whether a host pointer refers to storage that186// is mapped to a given device. However, due to the lack of the storage size,187// only check 1 byte. Cannot set size 0 which checks whether the pointer (zero188// lengh array) is mapped instead of the referred storage.189TargetPointerResultTy TPR =190DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), 1,191/*UpdateRefCount=*/false,192/*UseHoldRefCount=*/false);193int Rc = TPR.isPresent();194DP("Call to omp_target_is_present returns %d\n", Rc);195return Rc;196}
197
198EXTERN int omp_target_memcpy(void *Dst, const void *Src, size_t Length,199size_t DstOffset, size_t SrcOffset, int DstDevice,200int SrcDevice) {201TIMESCOPE_WITH_DETAILS("dst_dev=" + std::to_string(DstDevice) +202";src_dev=" + std::to_string(SrcDevice) +203";size=" + std::to_string(Length));204OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));205DP("Call to omp_target_memcpy, dst device %d, src device %d, "206"dst addr " DPxMOD ", src addr " DPxMOD ", dst offset %zu, "207"src offset %zu, length %zu\n",208DstDevice, SrcDevice, DPxPTR(Dst), DPxPTR(Src), DstOffset, SrcOffset,209Length);210
211if (!Dst || !Src || Length <= 0) {212if (Length == 0) {213DP("Call to omp_target_memcpy with zero length, nothing to do\n");214return OFFLOAD_SUCCESS;215}216
217REPORT("Call to omp_target_memcpy with invalid arguments\n");218return OFFLOAD_FAIL;219}220
221int Rc = OFFLOAD_SUCCESS;222void *SrcAddr = (char *)const_cast<void *>(Src) + SrcOffset;223void *DstAddr = (char *)Dst + DstOffset;224
225if (SrcDevice == omp_get_initial_device() &&226DstDevice == omp_get_initial_device()) {227DP("copy from host to host\n");228const void *P = memcpy(DstAddr, SrcAddr, Length);229if (P == NULL)230Rc = OFFLOAD_FAIL;231} else if (SrcDevice == omp_get_initial_device()) {232DP("copy from host to device\n");233auto DstDeviceOrErr = PM->getDevice(DstDevice);234if (!DstDeviceOrErr)235FATAL_MESSAGE(DstDevice, "%s",236toString(DstDeviceOrErr.takeError()).c_str());237AsyncInfoTy AsyncInfo(*DstDeviceOrErr);238Rc = DstDeviceOrErr->submitData(DstAddr, SrcAddr, Length, AsyncInfo);239} else if (DstDevice == omp_get_initial_device()) {240DP("copy from device to host\n");241auto SrcDeviceOrErr = PM->getDevice(SrcDevice);242if (!SrcDeviceOrErr)243FATAL_MESSAGE(SrcDevice, "%s",244toString(SrcDeviceOrErr.takeError()).c_str());245AsyncInfoTy AsyncInfo(*SrcDeviceOrErr);246Rc = SrcDeviceOrErr->retrieveData(DstAddr, SrcAddr, Length, AsyncInfo);247} else {248DP("copy from device to device\n");249auto SrcDeviceOrErr = PM->getDevice(SrcDevice);250if (!SrcDeviceOrErr)251FATAL_MESSAGE(SrcDevice, "%s",252toString(SrcDeviceOrErr.takeError()).c_str());253AsyncInfoTy AsyncInfo(*SrcDeviceOrErr);254auto DstDeviceOrErr = PM->getDevice(DstDevice);255if (!DstDeviceOrErr)256FATAL_MESSAGE(DstDevice, "%s",257toString(DstDeviceOrErr.takeError()).c_str());258// First try to use D2D memcpy which is more efficient. If fails, fall back259// to unefficient way.260if (SrcDeviceOrErr->isDataExchangable(*DstDeviceOrErr)) {261AsyncInfoTy AsyncInfo(*SrcDeviceOrErr);262Rc = SrcDeviceOrErr->dataExchange(SrcAddr, *DstDeviceOrErr, DstAddr,263Length, AsyncInfo);264if (Rc == OFFLOAD_SUCCESS)265return OFFLOAD_SUCCESS;266}267
268void *Buffer = malloc(Length);269{270AsyncInfoTy AsyncInfo(*SrcDeviceOrErr);271Rc = SrcDeviceOrErr->retrieveData(Buffer, SrcAddr, Length, AsyncInfo);272}273if (Rc == OFFLOAD_SUCCESS) {274AsyncInfoTy AsyncInfo(*DstDeviceOrErr);275Rc = DstDeviceOrErr->submitData(DstAddr, Buffer, Length, AsyncInfo);276}277free(Buffer);278}279
280DP("omp_target_memcpy returns %d\n", Rc);281return Rc;282}
283
284// The helper function that calls omp_target_memcpy or omp_target_memcpy_rect
285static int libomp_target_memcpy_async_task(int32_t Gtid, kmp_task_t *Task) {286OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));287if (Task == nullptr)288return OFFLOAD_FAIL;289
290TargetMemcpyArgsTy *Args = (TargetMemcpyArgsTy *)Task->shareds;291
292if (Args == nullptr)293return OFFLOAD_FAIL;294
295// Call blocked version296int Rc = OFFLOAD_SUCCESS;297if (Args->IsRectMemcpy) {298Rc = omp_target_memcpy_rect(299Args->Dst, Args->Src, Args->ElementSize, Args->NumDims, Args->Volume,300Args->DstOffsets, Args->SrcOffsets, Args->DstDimensions,301Args->SrcDimensions, Args->DstDevice, Args->SrcDevice);302
303DP("omp_target_memcpy_rect returns %d\n", Rc);304} else {305Rc = omp_target_memcpy(Args->Dst, Args->Src, Args->Length, Args->DstOffset,306Args->SrcOffset, Args->DstDevice, Args->SrcDevice);307
308DP("omp_target_memcpy returns %d\n", Rc);309}310
311// Release the arguments object312delete Args;313
314return Rc;315}
316
317static int libomp_target_memset_async_task(int32_t Gtid, kmp_task_t *Task) {318OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));319if (!Task)320return OFFLOAD_FAIL;321
322auto *Args = reinterpret_cast<TargetMemsetArgsTy *>(Task->shareds);323if (!Args)324return OFFLOAD_FAIL;325
326// call omp_target_memset()327omp_target_memset(Args->Ptr, Args->C, Args->N, Args->DeviceNum);328
329delete Args;330
331return OFFLOAD_SUCCESS;332}
333
334static inline void335convertDepObjVector(llvm::SmallVector<kmp_depend_info_t> &Vec, int DepObjCount,336omp_depend_t *DepObjList) {337for (int i = 0; i < DepObjCount; ++i) {338omp_depend_t DepObj = DepObjList[i];339Vec.push_back(*((kmp_depend_info_t *)DepObj));340}341}
342
343template <class T>344static inline int345libomp_helper_task_creation(T *Args, int (*Fn)(int32_t, kmp_task_t *),346int DepObjCount, omp_depend_t *DepObjList) {347OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));348// Create global thread ID349int Gtid = __kmpc_global_thread_num(nullptr);350
351// Setup the hidden helper flags352int32_t Flags = 0;353kmp_tasking_flags_t *InputFlags = (kmp_tasking_flags_t *)&Flags;354InputFlags->hidden_helper = 1;355
356// Alloc the helper task357kmp_task_t *Task = __kmpc_omp_target_task_alloc(358nullptr, Gtid, Flags, sizeof(kmp_task_t), 0, Fn, -1);359if (!Task) {360delete Args;361return OFFLOAD_FAIL;362}363
364// Setup the arguments for the helper task365Task->shareds = Args;366
367// Convert types of depend objects368llvm::SmallVector<kmp_depend_info_t> DepObjs;369convertDepObjVector(DepObjs, DepObjCount, DepObjList);370
371// Launch the helper task372int Rc = __kmpc_omp_task_with_deps(nullptr, Gtid, Task, DepObjCount,373DepObjs.data(), 0, nullptr);374
375return Rc;376}
377
378EXTERN void *omp_target_memset(void *Ptr, int ByteVal, size_t NumBytes,379int DeviceNum) {380TIMESCOPE();381OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));382DP("Call to omp_target_memset, device %d, device pointer %p, size %zu\n",383DeviceNum, Ptr, NumBytes);384
385// Behave as a no-op if N==0 or if Ptr is nullptr (as a useful implementation386// of unspecified behavior, see OpenMP spec).387if (!Ptr || NumBytes == 0) {388return Ptr;389}390
391if (DeviceNum == omp_get_initial_device()) {392DP("filling memory on host via memset");393memset(Ptr, ByteVal, NumBytes); // ignore return value, memset() cannot fail394} else {395// TODO: replace the omp_target_memset() slow path with the fast path.396// That will require the ability to execute a kernel from within397// libomptarget.so (which we do not have at the moment).398
399// This is a very slow path: create a filled array on the host and upload400// it to the GPU device.401int InitialDevice = omp_get_initial_device();402void *Shadow = omp_target_alloc(NumBytes, InitialDevice);403if (Shadow) {404(void)memset(Shadow, ByteVal, NumBytes);405(void)omp_target_memcpy(Ptr, Shadow, NumBytes, 0, 0, DeviceNum,406InitialDevice);407(void)omp_target_free(Shadow, InitialDevice);408} else {409// If the omp_target_alloc has failed, let's just not do anything.410// omp_target_memset does not have any good way to fail, so we411// simply avoid a catastrophic failure of the process for now.412DP("omp_target_memset failed to fill memory due to error with "413"omp_target_alloc");414}415}416
417DP("omp_target_memset returns %p\n", Ptr);418return Ptr;419}
420
421EXTERN void *omp_target_memset_async(void *Ptr, int ByteVal, size_t NumBytes,422int DeviceNum, int DepObjCount,423omp_depend_t *DepObjList) {424OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));425DP("Call to omp_target_memset_async, device %d, device pointer %p, size %zu",426DeviceNum, Ptr, NumBytes);427
428// Behave as a no-op if N==0 or if Ptr is nullptr (as a useful implementation429// of unspecified behavior, see OpenMP spec).430if (!Ptr || NumBytes == 0)431return Ptr;432
433// Create the task object to deal with the async invocation434auto *Args = new TargetMemsetArgsTy{Ptr, ByteVal, NumBytes, DeviceNum};435
436// omp_target_memset_async() cannot fail via a return code, so ignore the437// return code of the helper function438(void)libomp_helper_task_creation(Args, &libomp_target_memset_async_task,439DepObjCount, DepObjList);440
441return Ptr;442}
443
444EXTERN int omp_target_memcpy_async(void *Dst, const void *Src, size_t Length,445size_t DstOffset, size_t SrcOffset,446int DstDevice, int SrcDevice,447int DepObjCount, omp_depend_t *DepObjList) {448TIMESCOPE_WITH_DETAILS("dst_dev=" + std::to_string(DstDevice) +449";src_dev=" + std::to_string(SrcDevice) +450";size=" + std::to_string(Length));451OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));452DP("Call to omp_target_memcpy_async, dst device %d, src device %d, "453"dst addr " DPxMOD ", src addr " DPxMOD ", dst offset %zu, "454"src offset %zu, length %zu\n",455DstDevice, SrcDevice, DPxPTR(Dst), DPxPTR(Src), DstOffset, SrcOffset,456Length);457
458// Check the source and dest address459if (Dst == nullptr || Src == nullptr)460return OFFLOAD_FAIL;461
462// Create task object463TargetMemcpyArgsTy *Args = new TargetMemcpyArgsTy(464Dst, Src, Length, DstOffset, SrcOffset, DstDevice, SrcDevice);465
466// Create and launch helper task467int Rc = libomp_helper_task_creation(Args, &libomp_target_memcpy_async_task,468DepObjCount, DepObjList);469
470DP("omp_target_memcpy_async returns %d\n", Rc);471return Rc;472}
473
474EXTERN int475omp_target_memcpy_rect(void *Dst, const void *Src, size_t ElementSize,476int NumDims, const size_t *Volume,477const size_t *DstOffsets, const size_t *SrcOffsets,478const size_t *DstDimensions, const size_t *SrcDimensions,479int DstDevice, int SrcDevice) {480OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));481DP("Call to omp_target_memcpy_rect, dst device %d, src device %d, "482"dst addr " DPxMOD ", src addr " DPxMOD ", dst offsets " DPxMOD ", "483"src offsets " DPxMOD ", dst dims " DPxMOD ", src dims " DPxMOD ", "484"volume " DPxMOD ", element size %zu, num_dims %d\n",485DstDevice, SrcDevice, DPxPTR(Dst), DPxPTR(Src), DPxPTR(DstOffsets),486DPxPTR(SrcOffsets), DPxPTR(DstDimensions), DPxPTR(SrcDimensions),487DPxPTR(Volume), ElementSize, NumDims);488
489if (!(Dst || Src)) {490DP("Call to omp_target_memcpy_rect returns max supported dimensions %d\n",491INT_MAX);492return INT_MAX;493}494
495if (!Dst || !Src || ElementSize < 1 || NumDims < 1 || !Volume ||496!DstOffsets || !SrcOffsets || !DstDimensions || !SrcDimensions) {497REPORT("Call to omp_target_memcpy_rect with invalid arguments\n");498return OFFLOAD_FAIL;499}500
501int Rc;502if (NumDims == 1) {503Rc = omp_target_memcpy(Dst, Src, ElementSize * Volume[0],504ElementSize * DstOffsets[0],505ElementSize * SrcOffsets[0], DstDevice, SrcDevice);506} else {507size_t DstSliceSize = ElementSize;508size_t SrcSliceSize = ElementSize;509for (int I = 1; I < NumDims; ++I) {510DstSliceSize *= DstDimensions[I];511SrcSliceSize *= SrcDimensions[I];512}513
514size_t DstOff = DstOffsets[0] * DstSliceSize;515size_t SrcOff = SrcOffsets[0] * SrcSliceSize;516for (size_t I = 0; I < Volume[0]; ++I) {517Rc = omp_target_memcpy_rect(518(char *)Dst + DstOff + DstSliceSize * I,519(char *)const_cast<void *>(Src) + SrcOff + SrcSliceSize * I,520ElementSize, NumDims - 1, Volume + 1, DstOffsets + 1, SrcOffsets + 1,521DstDimensions + 1, SrcDimensions + 1, DstDevice, SrcDevice);522
523if (Rc) {524DP("Recursive call to omp_target_memcpy_rect returns unsuccessfully\n");525return Rc;526}527}528}529
530DP("omp_target_memcpy_rect returns %d\n", Rc);531return Rc;532}
533
534EXTERN int omp_target_memcpy_rect_async(535void *Dst, const void *Src, size_t ElementSize, int NumDims,536const size_t *Volume, const size_t *DstOffsets, const size_t *SrcOffsets,537const size_t *DstDimensions, const size_t *SrcDimensions, int DstDevice,538int SrcDevice, int DepObjCount, omp_depend_t *DepObjList) {539TIMESCOPE_WITH_DETAILS("dst_dev=" + std::to_string(DstDevice) +540";src_dev=" + std::to_string(SrcDevice) +541";size=" + std::to_string(ElementSize) +542";num_dims=" + std::to_string(NumDims));543OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));544DP("Call to omp_target_memcpy_rect_async, dst device %d, src device %d, "545"dst addr " DPxMOD ", src addr " DPxMOD ", dst offsets " DPxMOD ", "546"src offsets " DPxMOD ", dst dims " DPxMOD ", src dims " DPxMOD ", "547"volume " DPxMOD ", element size %zu, num_dims %d\n",548DstDevice, SrcDevice, DPxPTR(Dst), DPxPTR(Src), DPxPTR(DstOffsets),549DPxPTR(SrcOffsets), DPxPTR(DstDimensions), DPxPTR(SrcDimensions),550DPxPTR(Volume), ElementSize, NumDims);551
552// Need to check this first to not return OFFLOAD_FAIL instead553if (!Dst && !Src) {554DP("Call to omp_target_memcpy_rect returns max supported dimensions %d\n",555INT_MAX);556return INT_MAX;557}558
559// Check the source and dest address560if (Dst == nullptr || Src == nullptr)561return OFFLOAD_FAIL;562
563// Create task object564TargetMemcpyArgsTy *Args = new TargetMemcpyArgsTy(565Dst, Src, ElementSize, NumDims, Volume, DstOffsets, SrcOffsets,566DstDimensions, SrcDimensions, DstDevice, SrcDevice);567
568// Create and launch helper task569int Rc = libomp_helper_task_creation(Args, &libomp_target_memcpy_async_task,570DepObjCount, DepObjList);571
572DP("omp_target_memcpy_rect_async returns %d\n", Rc);573return Rc;574}
575
576EXTERN int omp_target_associate_ptr(const void *HostPtr, const void *DevicePtr,577size_t Size, size_t DeviceOffset,578int DeviceNum) {579TIMESCOPE();580OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));581DP("Call to omp_target_associate_ptr with host_ptr " DPxMOD ", "582"device_ptr " DPxMOD ", size %zu, device_offset %zu, device_num %d\n",583DPxPTR(HostPtr), DPxPTR(DevicePtr), Size, DeviceOffset, DeviceNum);584
585if (!HostPtr || !DevicePtr || Size <= 0) {586REPORT("Call to omp_target_associate_ptr with invalid arguments\n");587return OFFLOAD_FAIL;588}589
590if (DeviceNum == omp_get_initial_device()) {591REPORT("omp_target_associate_ptr: no association possible on the host\n");592return OFFLOAD_FAIL;593}594
595auto DeviceOrErr = PM->getDevice(DeviceNum);596if (!DeviceOrErr)597FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());598
599void *DeviceAddr = (void *)((uint64_t)DevicePtr + (uint64_t)DeviceOffset);600int Rc = DeviceOrErr->getMappingInfo().associatePtr(601const_cast<void *>(HostPtr), const_cast<void *>(DeviceAddr), Size);602DP("omp_target_associate_ptr returns %d\n", Rc);603return Rc;604}
605
606EXTERN int omp_target_disassociate_ptr(const void *HostPtr, int DeviceNum) {607TIMESCOPE();608OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));609DP("Call to omp_target_disassociate_ptr with host_ptr " DPxMOD ", "610"device_num %d\n",611DPxPTR(HostPtr), DeviceNum);612
613if (!HostPtr) {614REPORT("Call to omp_target_associate_ptr with invalid host_ptr\n");615return OFFLOAD_FAIL;616}617
618if (DeviceNum == omp_get_initial_device()) {619REPORT(620"omp_target_disassociate_ptr: no association possible on the host\n");621return OFFLOAD_FAIL;622}623
624auto DeviceOrErr = PM->getDevice(DeviceNum);625if (!DeviceOrErr)626FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());627
628int Rc = DeviceOrErr->getMappingInfo().disassociatePtr(629const_cast<void *>(HostPtr));630DP("omp_target_disassociate_ptr returns %d\n", Rc);631return Rc;632}
633
634EXTERN void *omp_get_mapped_ptr(const void *Ptr, int DeviceNum) {635TIMESCOPE();636OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));637DP("Call to omp_get_mapped_ptr with ptr " DPxMOD ", device_num %d.\n",638DPxPTR(Ptr), DeviceNum);639
640if (!Ptr) {641REPORT("Call to omp_get_mapped_ptr with nullptr.\n");642return nullptr;643}644
645int NumDevices = omp_get_initial_device();646if (DeviceNum == NumDevices) {647DP("Device %d is initial device, returning Ptr " DPxMOD ".\n",648DeviceNum, DPxPTR(Ptr));649return const_cast<void *>(Ptr);650}651
652if (NumDevices <= DeviceNum) {653DP("DeviceNum %d is invalid, returning nullptr.\n", DeviceNum);654return nullptr;655}656
657auto DeviceOrErr = PM->getDevice(DeviceNum);658if (!DeviceOrErr)659FATAL_MESSAGE(DeviceNum, "%s", toString(DeviceOrErr.takeError()).c_str());660
661TargetPointerResultTy TPR =662DeviceOrErr->getMappingInfo().getTgtPtrBegin(const_cast<void *>(Ptr), 1,663/*UpdateRefCount=*/false,664/*UseHoldRefCount=*/false);665if (!TPR.isPresent()) {666DP("Ptr " DPxMOD "is not present on device %d, returning nullptr.\n",667DPxPTR(Ptr), DeviceNum);668return nullptr;669}670
671DP("omp_get_mapped_ptr returns " DPxMOD ".\n", DPxPTR(TPR.TargetPointer));672
673return TPR.TargetPointer;674}
675