1ec8f4a38SAtmn Patel //===----------------- Server.cpp - Server Implementation -----------------===//
2ec8f4a38SAtmn Patel //
3ec8f4a38SAtmn Patel // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ec8f4a38SAtmn Patel // See https://llvm.org/LICENSE.txt for license information.
5ec8f4a38SAtmn Patel // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ec8f4a38SAtmn Patel //
7ec8f4a38SAtmn Patel //===----------------------------------------------------------------------===//
8ec8f4a38SAtmn Patel //
9ec8f4a38SAtmn Patel // Offloading gRPC server for remote host.
10ec8f4a38SAtmn Patel //
11ec8f4a38SAtmn Patel //===----------------------------------------------------------------------===//
12ec8f4a38SAtmn Patel
13ec8f4a38SAtmn Patel #include <cmath>
14ec8f4a38SAtmn Patel #include <future>
15ec8f4a38SAtmn Patel
16ec8f4a38SAtmn Patel #include "Server.h"
17ec8f4a38SAtmn Patel #include "omptarget.h"
18ec8f4a38SAtmn Patel #include "openmp.grpc.pb.h"
19ec8f4a38SAtmn Patel #include "openmp.pb.h"
20ec8f4a38SAtmn Patel
21ec8f4a38SAtmn Patel using grpc::WriteOptions;
22ec8f4a38SAtmn Patel
23ec8f4a38SAtmn Patel extern std::promise<void> ShutdownPromise;
24ec8f4a38SAtmn Patel
Shutdown(ServerContext * Context,const Null * Request,I32 * Reply)25ec8f4a38SAtmn Patel Status RemoteOffloadImpl::Shutdown(ServerContext *Context, const Null *Request,
26ec8f4a38SAtmn Patel I32 *Reply) {
2721e92612SAtmn Patel SERVER_DBG("Shutting down the server")
28ec8f4a38SAtmn Patel
29ec8f4a38SAtmn Patel Reply->set_number(0);
30ec8f4a38SAtmn Patel ShutdownPromise.set_value();
31ec8f4a38SAtmn Patel return Status::OK;
32ec8f4a38SAtmn Patel }
33ec8f4a38SAtmn Patel
34ec8f4a38SAtmn Patel Status
RegisterLib(ServerContext * Context,const TargetBinaryDescription * Description,I32 * Reply)35ec8f4a38SAtmn Patel RemoteOffloadImpl::RegisterLib(ServerContext *Context,
36ec8f4a38SAtmn Patel const TargetBinaryDescription *Description,
37ec8f4a38SAtmn Patel I32 *Reply) {
38ec8f4a38SAtmn Patel auto Desc = std::make_unique<__tgt_bin_desc>();
39ec8f4a38SAtmn Patel
40ec8f4a38SAtmn Patel unloadTargetBinaryDescription(Description, Desc.get(),
41ec8f4a38SAtmn Patel HostToRemoteDeviceImage);
42ec8f4a38SAtmn Patel PM->RTLs.RegisterLib(Desc.get());
43ec8f4a38SAtmn Patel
44ec8f4a38SAtmn Patel if (Descriptions.find((void *)Description->bin_ptr()) != Descriptions.end())
45ec8f4a38SAtmn Patel freeTargetBinaryDescription(
46ec8f4a38SAtmn Patel Descriptions[(void *)Description->bin_ptr()].get());
47ec8f4a38SAtmn Patel else
48ec8f4a38SAtmn Patel Descriptions[(void *)Description->bin_ptr()] = std::move(Desc);
49ec8f4a38SAtmn Patel
5021e92612SAtmn Patel SERVER_DBG("Registered library")
51ec8f4a38SAtmn Patel Reply->set_number(0);
52ec8f4a38SAtmn Patel return Status::OK;
53ec8f4a38SAtmn Patel }
54ec8f4a38SAtmn Patel
UnregisterLib(ServerContext * Context,const Pointer * Request,I32 * Reply)55ec8f4a38SAtmn Patel Status RemoteOffloadImpl::UnregisterLib(ServerContext *Context,
56ec8f4a38SAtmn Patel const Pointer *Request, I32 *Reply) {
57ec8f4a38SAtmn Patel if (Descriptions.find((void *)Request->number()) == Descriptions.end()) {
58ec8f4a38SAtmn Patel Reply->set_number(1);
59ec8f4a38SAtmn Patel return Status::OK;
60ec8f4a38SAtmn Patel }
61ec8f4a38SAtmn Patel
62ec8f4a38SAtmn Patel PM->RTLs.UnregisterLib(Descriptions[(void *)Request->number()].get());
63ec8f4a38SAtmn Patel freeTargetBinaryDescription(Descriptions[(void *)Request->number()].get());
64ec8f4a38SAtmn Patel Descriptions.erase((void *)Request->number());
65ec8f4a38SAtmn Patel
6621e92612SAtmn Patel SERVER_DBG("Unregistered library")
67ec8f4a38SAtmn Patel Reply->set_number(0);
68ec8f4a38SAtmn Patel return Status::OK;
69ec8f4a38SAtmn Patel }
70ec8f4a38SAtmn Patel
IsValidBinary(ServerContext * Context,const TargetDeviceImagePtr * DeviceImage,I32 * IsValid)71ec8f4a38SAtmn Patel Status RemoteOffloadImpl::IsValidBinary(ServerContext *Context,
72ec8f4a38SAtmn Patel const TargetDeviceImagePtr *DeviceImage,
73ec8f4a38SAtmn Patel I32 *IsValid) {
74ec8f4a38SAtmn Patel __tgt_device_image *Image =
75ec8f4a38SAtmn Patel HostToRemoteDeviceImage[(void *)DeviceImage->image_ptr()];
76ec8f4a38SAtmn Patel
77ec8f4a38SAtmn Patel IsValid->set_number(0);
78ec8f4a38SAtmn Patel
79ec8f4a38SAtmn Patel for (auto &RTL : PM->RTLs.AllRTLs)
80ec8f4a38SAtmn Patel if (auto Ret = RTL.is_valid_binary(Image)) {
81ec8f4a38SAtmn Patel IsValid->set_number(Ret);
82ec8f4a38SAtmn Patel break;
83ec8f4a38SAtmn Patel }
84ec8f4a38SAtmn Patel
85ec8f4a38SAtmn Patel SERVER_DBG("Checked if binary (%p) is valid",
8621e92612SAtmn Patel (void *)(DeviceImage->image_ptr()))
87ec8f4a38SAtmn Patel return Status::OK;
88ec8f4a38SAtmn Patel }
89ec8f4a38SAtmn Patel
GetNumberOfDevices(ServerContext * Context,const Null * Null,I32 * NumberOfDevices)90ec8f4a38SAtmn Patel Status RemoteOffloadImpl::GetNumberOfDevices(ServerContext *Context,
91ec8f4a38SAtmn Patel const Null *Null,
92ec8f4a38SAtmn Patel I32 *NumberOfDevices) {
93ec8f4a38SAtmn Patel std::call_once(PM->RTLs.initFlag, &RTLsTy::LoadRTLs, &PM->RTLs);
94ec8f4a38SAtmn Patel
95ec8f4a38SAtmn Patel int32_t Devices = 0;
96ec8f4a38SAtmn Patel PM->RTLsMtx.lock();
97ec8f4a38SAtmn Patel for (auto &RTL : PM->RTLs.AllRTLs)
98ec8f4a38SAtmn Patel Devices += RTL.NumberOfDevices;
99ec8f4a38SAtmn Patel PM->RTLsMtx.unlock();
100ec8f4a38SAtmn Patel
101ec8f4a38SAtmn Patel NumberOfDevices->set_number(Devices);
102ec8f4a38SAtmn Patel
10321e92612SAtmn Patel SERVER_DBG("Got number of devices")
104ec8f4a38SAtmn Patel return Status::OK;
105ec8f4a38SAtmn Patel }
106ec8f4a38SAtmn Patel
InitDevice(ServerContext * Context,const I32 * DeviceNum,I32 * Reply)107ec8f4a38SAtmn Patel Status RemoteOffloadImpl::InitDevice(ServerContext *Context,
108ec8f4a38SAtmn Patel const I32 *DeviceNum, I32 *Reply) {
109*489894f3SAtmn Patel Reply->set_number(PM->Devices[DeviceNum->number()]->RTL->init_device(
110ec8f4a38SAtmn Patel mapHostRTLDeviceId(DeviceNum->number())));
111ec8f4a38SAtmn Patel
11221e92612SAtmn Patel SERVER_DBG("Initialized device %d", DeviceNum->number())
113ec8f4a38SAtmn Patel return Status::OK;
114ec8f4a38SAtmn Patel }
115ec8f4a38SAtmn Patel
InitRequires(ServerContext * Context,const I64 * RequiresFlag,I32 * Reply)116ec8f4a38SAtmn Patel Status RemoteOffloadImpl::InitRequires(ServerContext *Context,
117ec8f4a38SAtmn Patel const I64 *RequiresFlag, I32 *Reply) {
118ec8f4a38SAtmn Patel for (auto &Device : PM->Devices)
119*489894f3SAtmn Patel if (Device->RTL->init_requires)
120*489894f3SAtmn Patel Device->RTL->init_requires(RequiresFlag->number());
121ec8f4a38SAtmn Patel Reply->set_number(RequiresFlag->number());
122ec8f4a38SAtmn Patel
12321e92612SAtmn Patel SERVER_DBG("Initialized requires for devices")
124ec8f4a38SAtmn Patel return Status::OK;
125ec8f4a38SAtmn Patel }
126ec8f4a38SAtmn Patel
LoadBinary(ServerContext * Context,const Binary * Binary,TargetTable * Reply)127ec8f4a38SAtmn Patel Status RemoteOffloadImpl::LoadBinary(ServerContext *Context,
128ec8f4a38SAtmn Patel const Binary *Binary, TargetTable *Reply) {
129ec8f4a38SAtmn Patel __tgt_device_image *Image =
130ec8f4a38SAtmn Patel HostToRemoteDeviceImage[(void *)Binary->image_ptr()];
131ec8f4a38SAtmn Patel
132*489894f3SAtmn Patel Table = PM->Devices[Binary->device_id()]->RTL->load_binary(
133ec8f4a38SAtmn Patel mapHostRTLDeviceId(Binary->device_id()), Image);
134ec8f4a38SAtmn Patel if (Table)
135ec8f4a38SAtmn Patel loadTargetTable(Table, *Reply, Image);
136ec8f4a38SAtmn Patel
137ec8f4a38SAtmn Patel SERVER_DBG("Loaded binary (%p) to device %d", (void *)Binary->image_ptr(),
13821e92612SAtmn Patel Binary->device_id())
139ec8f4a38SAtmn Patel return Status::OK;
140ec8f4a38SAtmn Patel }
141ec8f4a38SAtmn Patel
IsDataExchangeable(ServerContext * Context,const DevicePair * Request,I32 * Reply)142ec8f4a38SAtmn Patel Status RemoteOffloadImpl::IsDataExchangeable(ServerContext *Context,
143ec8f4a38SAtmn Patel const DevicePair *Request,
144ec8f4a38SAtmn Patel I32 *Reply) {
145ec8f4a38SAtmn Patel Reply->set_number(-1);
146ec8f4a38SAtmn Patel if (PM->Devices[mapHostRTLDeviceId(Request->src_dev_id())]
147*489894f3SAtmn Patel ->RTL->is_data_exchangable)
148ec8f4a38SAtmn Patel Reply->set_number(PM->Devices[mapHostRTLDeviceId(Request->src_dev_id())]
149*489894f3SAtmn Patel ->RTL->is_data_exchangable(Request->src_dev_id(),
150ec8f4a38SAtmn Patel Request->dst_dev_id()));
151ec8f4a38SAtmn Patel
15221e92612SAtmn Patel SERVER_DBG("Checked if data exchangeable between device %d and device %d",
15321e92612SAtmn Patel Request->src_dev_id(), Request->dst_dev_id())
154ec8f4a38SAtmn Patel return Status::OK;
155ec8f4a38SAtmn Patel }
156ec8f4a38SAtmn Patel
DataAlloc(ServerContext * Context,const AllocData * Request,Pointer * Reply)157ec8f4a38SAtmn Patel Status RemoteOffloadImpl::DataAlloc(ServerContext *Context,
158ec8f4a38SAtmn Patel const AllocData *Request, Pointer *Reply) {
159*489894f3SAtmn Patel uint64_t TgtPtr =
160*489894f3SAtmn Patel (uint64_t)PM->Devices[Request->device_id()]->RTL->data_alloc(
161ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->device_id()), Request->size(),
16221e92612SAtmn Patel (void *)Request->hst_ptr(), TARGET_ALLOC_DEFAULT);
163ec8f4a38SAtmn Patel Reply->set_number(TgtPtr);
164ec8f4a38SAtmn Patel
16521e92612SAtmn Patel SERVER_DBG("Allocated at " DPxMOD "", DPxPTR((void *)TgtPtr))
166ec8f4a38SAtmn Patel
167ec8f4a38SAtmn Patel return Status::OK;
168ec8f4a38SAtmn Patel }
169ec8f4a38SAtmn Patel
DataSubmit(ServerContext * Context,ServerReader<SubmitData> * Reader,I32 * Reply)17021e92612SAtmn Patel Status RemoteOffloadImpl::DataSubmit(ServerContext *Context,
17121e92612SAtmn Patel ServerReader<SubmitData> *Reader,
172ec8f4a38SAtmn Patel I32 *Reply) {
17321e92612SAtmn Patel SubmitData Request;
174ec8f4a38SAtmn Patel uint8_t *HostCopy = nullptr;
175ec8f4a38SAtmn Patel while (Reader->Read(&Request)) {
176ec8f4a38SAtmn Patel if (Request.start() == 0 && Request.size() == Request.data().size()) {
177ec8f4a38SAtmn Patel Reader->SendInitialMetadata();
178ec8f4a38SAtmn Patel
179*489894f3SAtmn Patel Reply->set_number(PM->Devices[Request.device_id()]->RTL->data_submit(
180ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request.device_id()), (void *)Request.tgt_ptr(),
181ec8f4a38SAtmn Patel (void *)Request.data().data(), Request.data().size()));
182ec8f4a38SAtmn Patel
183ec8f4a38SAtmn Patel SERVER_DBG("Submitted %lu bytes async to (%p) on device %d",
184ec8f4a38SAtmn Patel Request.data().size(), (void *)Request.tgt_ptr(),
18521e92612SAtmn Patel Request.device_id())
186ec8f4a38SAtmn Patel
187ec8f4a38SAtmn Patel return Status::OK;
188ec8f4a38SAtmn Patel }
189ec8f4a38SAtmn Patel if (!HostCopy) {
190ec8f4a38SAtmn Patel HostCopy = new uint8_t[Request.size()];
191ec8f4a38SAtmn Patel Reader->SendInitialMetadata();
192ec8f4a38SAtmn Patel }
193ec8f4a38SAtmn Patel
194ec8f4a38SAtmn Patel memcpy((void *)((char *)HostCopy + Request.start()), Request.data().data(),
195ec8f4a38SAtmn Patel Request.data().size());
196ec8f4a38SAtmn Patel }
197ec8f4a38SAtmn Patel
198*489894f3SAtmn Patel Reply->set_number(PM->Devices[Request.device_id()]->RTL->data_submit(
199ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request.device_id()), (void *)Request.tgt_ptr(),
200ec8f4a38SAtmn Patel HostCopy, Request.size()));
201ec8f4a38SAtmn Patel
202ec8f4a38SAtmn Patel delete[] HostCopy;
203ec8f4a38SAtmn Patel
204ec8f4a38SAtmn Patel SERVER_DBG("Submitted %lu bytes to (%p) on device %d", Request.data().size(),
20521e92612SAtmn Patel (void *)Request.tgt_ptr(), Request.device_id())
206ec8f4a38SAtmn Patel
207ec8f4a38SAtmn Patel return Status::OK;
208ec8f4a38SAtmn Patel }
209ec8f4a38SAtmn Patel
DataRetrieve(ServerContext * Context,const RetrieveData * Request,ServerWriter<Data> * Writer)21021e92612SAtmn Patel Status RemoteOffloadImpl::DataRetrieve(ServerContext *Context,
21121e92612SAtmn Patel const RetrieveData *Request,
212ec8f4a38SAtmn Patel ServerWriter<Data> *Writer) {
213ec8f4a38SAtmn Patel auto HstPtr = std::make_unique<char[]>(Request->size());
21421e92612SAtmn Patel
215*489894f3SAtmn Patel auto Ret = PM->Devices[Request->device_id()]->RTL->data_retrieve(
216ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->device_id()), HstPtr.get(),
217ec8f4a38SAtmn Patel (void *)Request->tgt_ptr(), Request->size());
218ec8f4a38SAtmn Patel
219ec8f4a38SAtmn Patel if (Arena->SpaceAllocated() >= MaxSize)
220ec8f4a38SAtmn Patel Arena->Reset();
221ec8f4a38SAtmn Patel
222ec8f4a38SAtmn Patel if (Request->size() > BlockSize) {
223ec8f4a38SAtmn Patel uint64_t Start = 0, End = BlockSize;
224ec8f4a38SAtmn Patel for (auto I = 0; I < ceil((float)Request->size() / BlockSize); I++) {
225ec8f4a38SAtmn Patel auto *Reply = protobuf::Arena::CreateMessage<Data>(Arena.get());
226ec8f4a38SAtmn Patel
227ec8f4a38SAtmn Patel Reply->set_start(Start);
228ec8f4a38SAtmn Patel Reply->set_size(Request->size());
229ec8f4a38SAtmn Patel Reply->set_data((char *)HstPtr.get() + Start, End - Start);
230ec8f4a38SAtmn Patel Reply->set_ret(Ret);
231ec8f4a38SAtmn Patel
232ec8f4a38SAtmn Patel if (!Writer->Write(*Reply)) {
23321e92612SAtmn Patel CLIENT_DBG("Broken stream when submitting data")
234ec8f4a38SAtmn Patel }
235ec8f4a38SAtmn Patel
236ec8f4a38SAtmn Patel SERVER_DBG("Retrieved %lu-%lu/%lu bytes from (%p) on device %d", Start,
237ec8f4a38SAtmn Patel End, Request->size(), (void *)Request->tgt_ptr(),
23821e92612SAtmn Patel mapHostRTLDeviceId(Request->device_id()))
239ec8f4a38SAtmn Patel
240ec8f4a38SAtmn Patel Start += BlockSize;
241ec8f4a38SAtmn Patel End += BlockSize;
242ec8f4a38SAtmn Patel if (End >= Request->size())
243ec8f4a38SAtmn Patel End = Request->size();
244ec8f4a38SAtmn Patel }
245ec8f4a38SAtmn Patel } else {
246ec8f4a38SAtmn Patel auto *Reply = protobuf::Arena::CreateMessage<Data>(Arena.get());
247ec8f4a38SAtmn Patel
248ec8f4a38SAtmn Patel Reply->set_start(0);
249ec8f4a38SAtmn Patel Reply->set_size(Request->size());
250ec8f4a38SAtmn Patel Reply->set_data((char *)HstPtr.get(), Request->size());
251ec8f4a38SAtmn Patel Reply->set_ret(Ret);
252ec8f4a38SAtmn Patel
253ec8f4a38SAtmn Patel SERVER_DBG("Retrieved %lu bytes from (%p) on device %d", Request->size(),
254ec8f4a38SAtmn Patel (void *)Request->tgt_ptr(),
25521e92612SAtmn Patel mapHostRTLDeviceId(Request->device_id()))
256ec8f4a38SAtmn Patel
257ec8f4a38SAtmn Patel Writer->WriteLast(*Reply, WriteOptions());
258ec8f4a38SAtmn Patel }
259ec8f4a38SAtmn Patel
260ec8f4a38SAtmn Patel return Status::OK;
261ec8f4a38SAtmn Patel }
262ec8f4a38SAtmn Patel
DataExchange(ServerContext * Context,const ExchangeData * Request,I32 * Reply)26321e92612SAtmn Patel Status RemoteOffloadImpl::DataExchange(ServerContext *Context,
26421e92612SAtmn Patel const ExchangeData *Request,
265ec8f4a38SAtmn Patel I32 *Reply) {
266*489894f3SAtmn Patel if (PM->Devices[Request->src_dev_id()]->RTL->data_exchange) {
267*489894f3SAtmn Patel int32_t Ret = PM->Devices[Request->src_dev_id()]->RTL->data_exchange(
268ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->src_dev_id()), (void *)Request->src_ptr(),
269ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->dst_dev_id()), (void *)Request->dst_ptr(),
270ec8f4a38SAtmn Patel Request->size());
271ec8f4a38SAtmn Patel Reply->set_number(Ret);
272ec8f4a38SAtmn Patel } else
273ec8f4a38SAtmn Patel Reply->set_number(-1);
274ec8f4a38SAtmn Patel
275ec8f4a38SAtmn Patel SERVER_DBG(
276ec8f4a38SAtmn Patel "Exchanged data asynchronously from device %d (%p) to device %d (%p) of "
277ec8f4a38SAtmn Patel "size %lu",
278ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->src_dev_id()), (void *)Request->src_ptr(),
279ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->dst_dev_id()), (void *)Request->dst_ptr(),
28021e92612SAtmn Patel Request->size())
281ec8f4a38SAtmn Patel return Status::OK;
282ec8f4a38SAtmn Patel }
283ec8f4a38SAtmn Patel
DataDelete(ServerContext * Context,const DeleteData * Request,I32 * Reply)284ec8f4a38SAtmn Patel Status RemoteOffloadImpl::DataDelete(ServerContext *Context,
285ec8f4a38SAtmn Patel const DeleteData *Request, I32 *Reply) {
286*489894f3SAtmn Patel auto Ret = PM->Devices[Request->device_id()]->RTL->data_delete(
287ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->device_id()), (void *)Request->tgt_ptr());
288ec8f4a38SAtmn Patel Reply->set_number(Ret);
289ec8f4a38SAtmn Patel
290ec8f4a38SAtmn Patel SERVER_DBG("Deleted data from (%p) on device %d", (void *)Request->tgt_ptr(),
29121e92612SAtmn Patel mapHostRTLDeviceId(Request->device_id()))
292ec8f4a38SAtmn Patel return Status::OK;
293ec8f4a38SAtmn Patel }
294ec8f4a38SAtmn Patel
RunTargetRegion(ServerContext * Context,const TargetRegion * Request,I32 * Reply)29521e92612SAtmn Patel Status RemoteOffloadImpl::RunTargetRegion(ServerContext *Context,
29621e92612SAtmn Patel const TargetRegion *Request,
297ec8f4a38SAtmn Patel I32 *Reply) {
298ec8f4a38SAtmn Patel std::vector<uint8_t> TgtArgs(Request->arg_num());
299ec8f4a38SAtmn Patel for (auto I = 0; I < Request->arg_num(); I++)
300ec8f4a38SAtmn Patel TgtArgs[I] = (uint64_t)Request->tgt_args()[I];
301ec8f4a38SAtmn Patel
302ec8f4a38SAtmn Patel std::vector<ptrdiff_t> TgtOffsets(Request->arg_num());
303ec8f4a38SAtmn Patel const auto *TgtOffsetItr = Request->tgt_offsets().begin();
304ec8f4a38SAtmn Patel for (auto I = 0; I < Request->arg_num(); I++, TgtOffsetItr++)
305ec8f4a38SAtmn Patel TgtOffsets[I] = (ptrdiff_t)*TgtOffsetItr;
306ec8f4a38SAtmn Patel
307ec8f4a38SAtmn Patel void *TgtEntryPtr = ((__tgt_offload_entry *)Request->tgt_entry_ptr())->addr;
308ec8f4a38SAtmn Patel
309*489894f3SAtmn Patel int32_t Ret = PM->Devices[Request->device_id()]->RTL->run_region(
310ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->device_id()), TgtEntryPtr,
311ec8f4a38SAtmn Patel (void **)TgtArgs.data(), TgtOffsets.data(), Request->arg_num());
312ec8f4a38SAtmn Patel
313ec8f4a38SAtmn Patel Reply->set_number(Ret);
314ec8f4a38SAtmn Patel
31521e92612SAtmn Patel SERVER_DBG("Ran TargetRegion on device %d with %d args",
31621e92612SAtmn Patel mapHostRTLDeviceId(Request->device_id()), Request->arg_num())
317ec8f4a38SAtmn Patel return Status::OK;
318ec8f4a38SAtmn Patel }
319ec8f4a38SAtmn Patel
RunTargetTeamRegion(ServerContext * Context,const TargetTeamRegion * Request,I32 * Reply)32021e92612SAtmn Patel Status RemoteOffloadImpl::RunTargetTeamRegion(ServerContext *Context,
32121e92612SAtmn Patel const TargetTeamRegion *Request,
32221e92612SAtmn Patel I32 *Reply) {
323ec8f4a38SAtmn Patel std::vector<uint64_t> TgtArgs(Request->arg_num());
324ec8f4a38SAtmn Patel for (auto I = 0; I < Request->arg_num(); I++)
325ec8f4a38SAtmn Patel TgtArgs[I] = (uint64_t)Request->tgt_args()[I];
326ec8f4a38SAtmn Patel
327ec8f4a38SAtmn Patel std::vector<ptrdiff_t> TgtOffsets(Request->arg_num());
328ec8f4a38SAtmn Patel const auto *TgtOffsetItr = Request->tgt_offsets().begin();
329ec8f4a38SAtmn Patel for (auto I = 0; I < Request->arg_num(); I++, TgtOffsetItr++)
330ec8f4a38SAtmn Patel TgtOffsets[I] = (ptrdiff_t)*TgtOffsetItr;
331ec8f4a38SAtmn Patel
332ec8f4a38SAtmn Patel void *TgtEntryPtr = ((__tgt_offload_entry *)Request->tgt_entry_ptr())->addr;
33321e92612SAtmn Patel
334*489894f3SAtmn Patel int32_t Ret = PM->Devices[Request->device_id()]->RTL->run_team_region(
335ec8f4a38SAtmn Patel mapHostRTLDeviceId(Request->device_id()), TgtEntryPtr,
336ec8f4a38SAtmn Patel (void **)TgtArgs.data(), TgtOffsets.data(), Request->arg_num(),
337ec8f4a38SAtmn Patel Request->team_num(), Request->thread_limit(), Request->loop_tripcount());
338ec8f4a38SAtmn Patel
339ec8f4a38SAtmn Patel Reply->set_number(Ret);
340ec8f4a38SAtmn Patel
34121e92612SAtmn Patel SERVER_DBG("Ran TargetTeamRegion on device %d with %d args",
34221e92612SAtmn Patel mapHostRTLDeviceId(Request->device_id()), Request->arg_num())
343ec8f4a38SAtmn Patel return Status::OK;
344ec8f4a38SAtmn Patel }
345ec8f4a38SAtmn Patel
mapHostRTLDeviceId(int32_t RTLDeviceID)346ec8f4a38SAtmn Patel int32_t RemoteOffloadImpl::mapHostRTLDeviceId(int32_t RTLDeviceID) {
347ec8f4a38SAtmn Patel for (auto &RTL : PM->RTLs.UsedRTLs) {
348ec8f4a38SAtmn Patel if (RTLDeviceID - RTL->NumberOfDevices >= 0)
349ec8f4a38SAtmn Patel RTLDeviceID -= RTL->NumberOfDevices;
350ec8f4a38SAtmn Patel else
351ec8f4a38SAtmn Patel break;
352ec8f4a38SAtmn Patel }
353ec8f4a38SAtmn Patel return RTLDeviceID;
354ec8f4a38SAtmn Patel }
355