1 //===----------------- Utils.h - Utilities for Remote 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 // Utilities for data transfer through protobuf and debugging. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef UTILS_H 14 #define UTILS_H 15 16 #include "Debug.h" 17 #include "omptarget.h" 18 #include "openmp.grpc.pb.h" 19 #include "openmp.pb.h" 20 #include "rtl.h" 21 #include <string> 22 23 #define CLIENT_DBG(...) \ 24 { \ 25 if (DebugLevel > 0) { \ 26 fprintf(stderr, "[[Client]] --> "); \ 27 fprintf(stderr, __VA_ARGS__); \ 28 fprintf(stderr, "\n"); \ 29 } \ 30 } 31 32 #define SERVER_DBG(...) \ 33 { \ 34 if (DebugLevel > 0) { \ 35 fprintf(stderr, "[[Server]] --> "); \ 36 fprintf(stderr, __VA_ARGS__); \ 37 fprintf(stderr, "\n"); \ 38 } \ 39 } 40 41 namespace RemoteOffloading { 42 43 using namespace openmp::libomptarget::remote; 44 45 using openmp::libomptarget::remote::DeviceOffloadEntry; 46 using openmp::libomptarget::remote::TargetBinaryDescription; 47 using openmp::libomptarget::remote::TargetOffloadEntry; 48 using openmp::libomptarget::remote::TargetTable; 49 50 struct ClientManagerConfigTy { 51 std::vector<std::string> ServerAddresses; 52 uint64_t MaxSize; 53 uint64_t BlockSize; 54 int Timeout; 55 ClientManagerConfigTyClientManagerConfigTy56 ClientManagerConfigTy() 57 : ServerAddresses({"0.0.0.0:50051"}), MaxSize(1 << 30), 58 BlockSize(1 << 20), Timeout(5) { 59 // TODO: Error handle for incorrect inputs 60 if (const char *Env = std::getenv("LIBOMPTARGET_RPC_ADDRESS")) { 61 ServerAddresses.clear(); 62 std::string AddressString = Env; 63 const std::string Delimiter = ","; 64 65 size_t Pos; 66 std::string Token; 67 while ((Pos = AddressString.find(Delimiter)) != std::string::npos) { 68 Token = AddressString.substr(0, Pos); 69 ServerAddresses.push_back(Token); 70 AddressString.erase(0, Pos + Delimiter.length()); 71 } 72 ServerAddresses.push_back(AddressString); 73 } 74 if (const char *Env = std::getenv("LIBOMPTARGET_RPC_ALLOCATOR_MAX")) 75 MaxSize = std::stoi(Env); 76 if (const char *Env = std::getenv("LIBOMPTARGET_RPC_BLOCK_SIZE")) 77 BlockSize = std::stoi(Env); 78 if (const char *Env1 = std::getenv("LIBOMPTARGET_RPC_LATENCY")) 79 Timeout = std::stoi(Env1); 80 } 81 }; 82 83 /// Loads a target binary description into protobuf. 84 void loadTargetBinaryDescription(const __tgt_bin_desc *Desc, 85 TargetBinaryDescription &Request); 86 87 /// Unload a target binary description from protobuf. The map is used to keep 88 /// track of already copied device images. 89 void unloadTargetBinaryDescription( 90 const TargetBinaryDescription *Request, __tgt_bin_desc *Desc, 91 std::unordered_map<const void *, __tgt_device_image *> 92 &HostToRemoteDeviceImage); 93 94 /// Frees argument as constructed by loadTargetBinaryDescription 95 void freeTargetBinaryDescription(__tgt_bin_desc *Desc); 96 97 /// Copies from TargetOffloadEntry protobuf to a tgt_bin_desc during unloading. 98 void copyOffloadEntry(const TargetOffloadEntry &EntryResponse, 99 __tgt_offload_entry *Entry); 100 101 /// Copies from tgt_bin_desc into TargetOffloadEntry protobuf during loading. 102 void copyOffloadEntry(const __tgt_offload_entry *Entry, 103 TargetOffloadEntry *EntryResponse); 104 105 /// Shallow copy of offload entry from tgt_bin_desc to TargetOffloadEntry 106 /// during loading. 107 void shallowCopyOffloadEntry(const __tgt_offload_entry *Entry, 108 TargetOffloadEntry *EntryResponse); 109 110 /// Copies DeviceOffloadEntries into table during unloading. 111 void copyOffloadEntry(const DeviceOffloadEntry &EntryResponse, 112 __tgt_offload_entry *Entry); 113 114 /// Loads tgt_target_table into a TargetTable protobuf message. 115 void loadTargetTable(__tgt_target_table *Table, TargetTable &TableResponse, 116 __tgt_device_image *Image); 117 118 /// Unloads from a target_table from protobuf. 119 void unloadTargetTable( 120 TargetTable &TableResponse, __tgt_target_table *Table, 121 std::unordered_map<void *, void *> &HostToRemoteTargetTableMap); 122 123 /// Frees argument as constructed by unloadTargetTable 124 void freeTargetTable(__tgt_target_table *Table); 125 126 void dump(const void *Start, const void *End); 127 void dump(__tgt_offload_entry *Entry); 128 void dump(TargetOffloadEntry Entry); 129 void dump(__tgt_target_table *Table); 130 void dump(__tgt_device_image *Image); 131 } // namespace RemoteOffloading 132 133 #endif 134