187 lines · c
1//===----------- device.h - 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-exception6//7//===----------------------------------------------------------------------===//8//9// Declarations for managing devices that are handled by RTL plugins.10//11//===----------------------------------------------------------------------===//12 13#ifndef _OMPTARGET_DEVICE_H14#define _OMPTARGET_DEVICE_H15 16#include <cassert>17#include <cstddef>18#include <cstdint>19#include <cstring>20#include <list>21#include <map>22#include <memory>23#include <mutex>24#include <set>25 26#include "ExclusiveAccess.h"27#include "OffloadEntry.h"28#include "omptarget.h"29#include "rtl.h"30 31#include "OpenMP/Mapping.h"32 33#include "llvm/ADT/DenseMap.h"34#include "llvm/ADT/SmallVector.h"35 36#include "GlobalHandler.h"37#include "PluginInterface.h"38 39using GenericPluginTy = llvm::omp::target::plugin::GenericPluginTy;40 41// Forward declarations.42struct __tgt_bin_desc;43struct __tgt_target_table;44 45struct DeviceTy {46 int32_t DeviceID;47 GenericPluginTy *RTL;48 int32_t RTLDeviceID;49 50 DeviceTy(GenericPluginTy *RTL, int32_t DeviceID, int32_t RTLDeviceID);51 // DeviceTy is not copyable52 DeviceTy(const DeviceTy &D) = delete;53 DeviceTy &operator=(const DeviceTy &D) = delete;54 55 ~DeviceTy();56 57 /// Try to initialize the device and return any failure.58 llvm::Error init();59 60 /// Provide access to the mapping handler.61 MappingInfoTy &getMappingInfo() { return MappingInfo; }62 63 llvm::Expected<__tgt_device_binary> loadBinary(__tgt_device_image *Img);64 65 // device memory allocation/deallocation routines66 /// Allocates \p Size bytes on the device, host or shared memory space67 /// (depending on \p Kind) and returns the address/nullptr when68 /// succeeds/fails. \p HstPtr is an address of the host data which the69 /// allocated target data will be associated with. If it is unknown, the70 /// default value of \p HstPtr is nullptr. Note: this function doesn't do71 /// pointer association. Actually, all the __tgt_rtl_data_alloc72 /// implementations ignore \p HstPtr. \p Kind dictates what allocator should73 /// be used (host, shared, device).74 void *allocData(int64_t Size, void *HstPtr = nullptr,75 int32_t Kind = TARGET_ALLOC_DEFAULT);76 77 /// Deallocates memory which \p TgtPtrBegin points at and returns78 /// OFFLOAD_SUCCESS/OFFLOAD_FAIL when succeeds/fails. p Kind dictates what79 /// allocator should be used (host, shared, device).80 int32_t deleteData(void *TgtPtrBegin, int32_t Kind = TARGET_ALLOC_DEFAULT);81 82 // Data transfer. When AsyncInfo is nullptr, the transfer will be83 // synchronous.84 // Copy data from host to device85 int32_t submitData(void *TgtPtrBegin, void *HstPtrBegin, int64_t Size,86 AsyncInfoTy &AsyncInfo,87 HostDataToTargetTy *Entry = nullptr,88 MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);89 90 // Copy data from device back to host91 int32_t retrieveData(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size,92 AsyncInfoTy &AsyncInfo,93 HostDataToTargetTy *Entry = nullptr,94 MappingInfoTy::HDTTMapAccessorTy *HDTTMapPtr = nullptr);95 96 // Return true if data can be copied to DstDevice directly97 bool isDataExchangable(const DeviceTy &DstDevice);98 99 // Copy data from current device to destination device directly100 int32_t dataExchange(void *SrcPtr, DeviceTy &DstDev, void *DstPtr,101 int64_t Size, AsyncInfoTy &AsyncInfo);102 103 // Insert a data fence between previous data operations and the following104 // operations if necessary for the device.105 int32_t dataFence(AsyncInfoTy &AsyncInfo);106 107 /// Notify the plugin about a new mapping starting at the host address108 /// \p HstPtr and \p Size bytes.109 int32_t notifyDataMapped(void *HstPtr, int64_t Size);110 111 /// Notify the plugin about an existing mapping being unmapped starting at112 /// the host address \p HstPtr.113 int32_t notifyDataUnmapped(void *HstPtr);114 115 // Launch the kernel identified by \p TgtEntryPtr with the given arguments.116 int32_t launchKernel(void *TgtEntryPtr, void **TgtVarsPtr,117 ptrdiff_t *TgtOffsets, KernelArgsTy &KernelArgs,118 AsyncInfoTy &AsyncInfo);119 120 /// Synchronize device/queue/event based on \p AsyncInfo and return121 /// OFFLOAD_SUCCESS/OFFLOAD_FAIL when succeeds/fails.122 int32_t synchronize(AsyncInfoTy &AsyncInfo);123 124 /// Query for device/queue/event based completion on \p AsyncInfo in a125 /// non-blocking manner and return OFFLOAD_SUCCESS/OFFLOAD_FAIL when126 /// succeeds/fails. Must be called multiple times until AsyncInfo is127 /// completed and AsyncInfo.isDone() returns true.128 int32_t queryAsync(AsyncInfoTy &AsyncInfo);129 130 /// Calls the corresponding print device info function in the plugin.131 bool printDeviceInfo();132 133 /// Event related interfaces.134 /// {135 /// Create an event.136 int32_t createEvent(void **Event);137 138 /// Record the event based on status in AsyncInfo->Queue at the moment the139 /// function is called.140 int32_t recordEvent(void *Event, AsyncInfoTy &AsyncInfo);141 142 /// Wait for an event. This function can be blocking or non-blocking,143 /// depending on the implementation. It is expected to set a dependence on the144 /// event such that corresponding operations shall only start once the event145 /// is fulfilled.146 int32_t waitEvent(void *Event, AsyncInfoTy &AsyncInfo);147 148 /// Synchronize the event. It is expected to block the thread.149 int32_t syncEvent(void *Event);150 151 /// Destroy the event.152 int32_t destroyEvent(void *Event);153 /// }154 155 /// Print all offload entries to stderr.156 void dumpOffloadEntries();157 158 /// Ask the device whether the runtime should use auto zero-copy.159 bool useAutoZeroCopy();160 161 /// Ask the device whether the storage is accessible.162 bool isAccessiblePtr(const void *Ptr, size_t Size);163 164 /// Check if there are pending images for this device.165 bool hasPendingImages() const { return HasPendingImages; }166 167 /// Indicate that there are pending images for this device or not.168 void setHasPendingImages(bool V) { HasPendingImages = V; }169 170private:171 /// Deinitialize the device (and plugin).172 void deinit();173 174 /// All offload entries available on this device.175 using DeviceOffloadEntriesMapTy =176 llvm::DenseMap<llvm::StringRef, OffloadEntryTy>;177 ProtectedObj<DeviceOffloadEntriesMapTy> DeviceOffloadEntries;178 179 /// Handler to collect and organize host-2-device mapping information.180 MappingInfoTy MappingInfo;181 182 /// Flag to indicate pending images (true after construction).183 bool HasPendingImages = true;184};185 186#endif187