blob: e9dc1791cb0f4653e4336bb9d45d790f89a2f44a [file] [log] [blame]
Michael Butler60296322019-01-17 17:54:51 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Butler89e99ba2019-01-24 02:36:37 -080017#define LOG_TAG "ExecutionBurstServer"
18
Michael Butler60296322019-01-17 17:54:51 -080019#include "ExecutionBurstServer.h"
20
21#include <android-base/logging.h>
Michael Butler3260db92019-04-26 17:51:23 -070022
Michael Butlerc932ebb2019-04-11 14:24:06 -070023#include <limits>
Michael Butler3260db92019-04-26 17:51:23 -070024#include <map>
25
Michael Butler3db6fe52019-01-29 11:20:30 -080026#include "Tracing.h"
Michael Butler60296322019-01-17 17:54:51 -080027
Michael Butler3db6fe52019-01-29 11:20:30 -080028namespace android::nn {
Michael Butler238fe722019-03-21 12:17:27 -070029namespace {
Michael Butler60296322019-01-17 17:54:51 -080030
Michael Butlerc932ebb2019-04-11 14:24:06 -070031constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(),
32 std::numeric_limits<uint64_t>::max()};
33
Michael Butler238fe722019-03-21 12:17:27 -070034// DefaultBurstExecutorWithCache adapts an IPreparedModel so that it can be
35// used as an IBurstExecutorWithCache. Specifically, the cache simply stores the
36// hidl_memory object, and the execution forwards calls to the provided
37// IPreparedModel's "executeSynchronously" method. With this class, hidl_memory
38// must be mapped and unmapped for each execution.
39class DefaultBurstExecutorWithCache : public ExecutionBurstServer::IBurstExecutorWithCache {
40 public:
41 DefaultBurstExecutorWithCache(IPreparedModel* preparedModel) : mpPreparedModel(preparedModel) {}
Michael Butler60296322019-01-17 17:54:51 -080042
Michael Butler238fe722019-03-21 12:17:27 -070043 bool isCacheEntryPresent(int32_t slot) const override {
Michael Butler3260db92019-04-26 17:51:23 -070044 const auto it = mMemoryCache.find(slot);
45 if (it == mMemoryCache.end()) {
46 return false;
47 }
48 return it->second.valid();
Michael Butler238fe722019-03-21 12:17:27 -070049 }
Michael Butler47c988f62019-03-14 17:34:48 -070050
Michael Butler238fe722019-03-21 12:17:27 -070051 void addCacheEntry(const hidl_memory& memory, int32_t slot) override {
Michael Butler238fe722019-03-21 12:17:27 -070052 mMemoryCache[slot] = memory;
53 }
Michael Butler60296322019-01-17 17:54:51 -080054
Michael Butler3260db92019-04-26 17:51:23 -070055 void removeCacheEntry(int32_t slot) override { mMemoryCache.erase(slot); }
Michael Butler238fe722019-03-21 12:17:27 -070056
57 std::tuple<ErrorStatus, hidl_vec<OutputShape>, Timing> execute(
58 const Request& request, const std::vector<int32_t>& slots,
59 MeasureTiming measure) override {
60 // convert slots to pools
61 hidl_vec<hidl_memory> pools(slots.size());
Michael Butler3260db92019-04-26 17:51:23 -070062 std::transform(slots.begin(), slots.end(), pools.begin(),
63 [this](int32_t slot) { return mMemoryCache[slot]; });
Michael Butler238fe722019-03-21 12:17:27 -070064
65 // create full request
66 Request fullRequest = request;
67 fullRequest.pools = std::move(pools);
68
69 // setup execution
70 ErrorStatus returnedStatus = ErrorStatus::GENERAL_FAILURE;
71 hidl_vec<OutputShape> returnedOutputShapes;
72 Timing returnedTiming;
73 auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming](
74 ErrorStatus status, const hidl_vec<OutputShape>& outputShapes,
75 const Timing& timing) {
76 returnedStatus = status;
77 returnedOutputShapes = outputShapes;
78 returnedTiming = timing;
Michael Butler47c988f62019-03-14 17:34:48 -070079 };
Michael Butler60296322019-01-17 17:54:51 -080080
Michael Butler238fe722019-03-21 12:17:27 -070081 // execute
82 const Return<void> ret = mpPreparedModel->executeSynchronously(fullRequest, measure, cb);
83 if (!ret.isOk() || returnedStatus != ErrorStatus::NONE) {
84 LOG(ERROR) << "IPreparedModelAdapter::execute -- Error executing";
85 return {ErrorStatus::GENERAL_FAILURE, {}, {}};
Michael Butler89e99ba2019-01-24 02:36:37 -080086 }
Michael Butler60296322019-01-17 17:54:51 -080087
Michael Butler238fe722019-03-21 12:17:27 -070088 return std::make_tuple(returnedStatus, std::move(returnedOutputShapes), returnedTiming);
Michael Butler60296322019-01-17 17:54:51 -080089 }
90
Michael Butler238fe722019-03-21 12:17:27 -070091 private:
92 IPreparedModel* const mpPreparedModel;
Michael Butler3260db92019-04-26 17:51:23 -070093 std::map<int32_t, hidl_memory> mMemoryCache;
Michael Butler238fe722019-03-21 12:17:27 -070094};
Michael Butler47c988f62019-03-14 17:34:48 -070095
Michael Butler238fe722019-03-21 12:17:27 -070096} // anonymous namespace
Michael Butler60296322019-01-17 17:54:51 -080097
Michael Butler60296322019-01-17 17:54:51 -080098// serialize result
Michael Butlerc932ebb2019-04-11 14:24:06 -070099std::vector<FmqResultDatum> serialize(ErrorStatus errorStatus,
100 const std::vector<OutputShape>& outputShapes, Timing timing) {
Michael Butler60296322019-01-17 17:54:51 -0800101 // count how many elements need to be sent for a request
102 size_t count = 2 + outputShapes.size();
103 for (const auto& outputShape : outputShapes) {
104 count += outputShape.dimensions.size();
105 }
106
107 // create buffer to temporarily store elements
108 std::vector<FmqResultDatum> data;
109 data.reserve(count);
110
111 // package packetInfo
112 {
113 FmqResultDatum datum;
114 datum.packetInformation({/*.packetSize=*/static_cast<uint32_t>(count),
115 /*.errorStatus=*/errorStatus,
116 /*.numberOfOperands=*/static_cast<uint32_t>(outputShapes.size())});
117 data.push_back(datum);
118 }
119
120 // package output shape data
121 for (const auto& operand : outputShapes) {
122 // package operand information
123 FmqResultDatum datum;
124 datum.operandInformation(
125 {/*.isSufficient=*/operand.isSufficient,
126 /*.numberOfDimensions=*/static_cast<uint32_t>(operand.dimensions.size())});
127 data.push_back(datum);
128
129 // package operand dimensions
130 for (uint32_t dimension : operand.dimensions) {
131 FmqResultDatum datum;
132 datum.operandDimensionValue(dimension);
133 data.push_back(datum);
134 }
135 }
136
137 // package executionTiming
138 {
139 FmqResultDatum datum;
140 datum.executionTiming(timing);
141 data.push_back(datum);
142 }
143
144 // return result
145 return data;
146}
147
Michael Butlerc932ebb2019-04-11 14:24:06 -0700148// deserialize request
149std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>> deserialize(
150 const std::vector<FmqRequestDatum>& data) {
151 using discriminator = FmqRequestDatum::hidl_discriminator;
Michael Butler60296322019-01-17 17:54:51 -0800152
Michael Butlerc932ebb2019-04-11 14:24:06 -0700153 size_t index = 0;
154
155 // validate packet information
Michael Butler3260db92019-04-26 17:51:23 -0700156 if (data.size() == 0 || data[index].getDiscriminator() != discriminator::packetInformation) {
Michael Butlerc932ebb2019-04-11 14:24:06 -0700157 LOG(ERROR) << "FMQ Request packet ill-formed";
158 return std::nullopt;
159 }
160
161 // unpackage packet information
162 const FmqRequestDatum::PacketInformation& packetInfo = data[index].packetInformation();
163 index++;
164 const uint32_t packetSize = packetInfo.packetSize;
165 const uint32_t numberOfInputOperands = packetInfo.numberOfInputOperands;
166 const uint32_t numberOfOutputOperands = packetInfo.numberOfOutputOperands;
167 const uint32_t numberOfPools = packetInfo.numberOfPools;
168
Michael Butler3260db92019-04-26 17:51:23 -0700169 // verify packet size
170 if (data.size() != packetSize) {
171 LOG(ERROR) << "FMQ Request packet ill-formed";
172 return std::nullopt;
173 }
174
Michael Butlerc932ebb2019-04-11 14:24:06 -0700175 // unpackage input operands
176 std::vector<RequestArgument> inputs;
177 inputs.reserve(numberOfInputOperands);
178 for (size_t operand = 0; operand < numberOfInputOperands; ++operand) {
179 // validate input operand information
180 if (data[index].getDiscriminator() != discriminator::inputOperandInformation) {
181 LOG(ERROR) << "FMQ Request packet ill-formed";
182 return std::nullopt;
Michael Butler60296322019-01-17 17:54:51 -0800183 }
184
Michael Butlerc932ebb2019-04-11 14:24:06 -0700185 // unpackage operand information
186 const FmqRequestDatum::OperandInformation& operandInfo =
187 data[index].inputOperandInformation();
188 index++;
189 const bool hasNoValue = operandInfo.hasNoValue;
190 const DataLocation location = operandInfo.location;
191 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
Michael Butler3db6fe52019-01-29 11:20:30 -0800192
Michael Butlerc932ebb2019-04-11 14:24:06 -0700193 // unpackage operand dimensions
194 std::vector<uint32_t> dimensions;
195 dimensions.reserve(numberOfDimensions);
196 for (size_t i = 0; i < numberOfDimensions; ++i) {
197 // validate dimension
198 if (data[index].getDiscriminator() != discriminator::inputOperandDimensionValue) {
199 LOG(ERROR) << "FMQ Request packet ill-formed";
200 return std::nullopt;
201 }
202
203 // unpackage dimension
204 const uint32_t dimension = data[index].inputOperandDimensionValue();
205 index++;
206
207 // store result
208 dimensions.push_back(dimension);
209 }
210
211 // store result
212 inputs.push_back(
213 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
214 }
215
216 // unpackage output operands
217 std::vector<RequestArgument> outputs;
218 outputs.reserve(numberOfOutputOperands);
219 for (size_t operand = 0; operand < numberOfOutputOperands; ++operand) {
220 // validate output operand information
221 if (data[index].getDiscriminator() != discriminator::outputOperandInformation) {
222 LOG(ERROR) << "FMQ Request packet ill-formed";
223 return std::nullopt;
224 }
225
226 // unpackage operand information
227 const FmqRequestDatum::OperandInformation& operandInfo =
228 data[index].outputOperandInformation();
229 index++;
230 const bool hasNoValue = operandInfo.hasNoValue;
231 const DataLocation location = operandInfo.location;
232 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
233
234 // unpackage operand dimensions
235 std::vector<uint32_t> dimensions;
236 dimensions.reserve(numberOfDimensions);
237 for (size_t i = 0; i < numberOfDimensions; ++i) {
238 // validate dimension
239 if (data[index].getDiscriminator() != discriminator::outputOperandDimensionValue) {
240 LOG(ERROR) << "FMQ Request packet ill-formed";
241 return std::nullopt;
242 }
243
244 // unpackage dimension
245 const uint32_t dimension = data[index].outputOperandDimensionValue();
246 index++;
247
248 // store result
249 dimensions.push_back(dimension);
250 }
251
252 // store result
253 outputs.push_back(
254 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
255 }
256
257 // unpackage pools
258 std::vector<int32_t> slots;
259 slots.reserve(numberOfPools);
260 for (size_t pool = 0; pool < numberOfPools; ++pool) {
261 // validate input operand information
262 if (data[index].getDiscriminator() != discriminator::poolIdentifier) {
263 LOG(ERROR) << "FMQ Request packet ill-formed";
264 return std::nullopt;
265 }
266
267 // unpackage operand information
268 const int32_t poolId = data[index].poolIdentifier();
269 index++;
270
271 // store result
272 slots.push_back(poolId);
273 }
274
275 // validate measureTiming
276 if (data[index].getDiscriminator() != discriminator::measureTiming) {
277 LOG(ERROR) << "FMQ Request packet ill-formed";
278 return std::nullopt;
279 }
280
281 // unpackage measureTiming
282 const MeasureTiming measure = data[index].measureTiming();
283 index++;
284
285 // validate packet information
286 if (index != packetSize) {
287 LOG(ERROR) << "FMQ Result packet ill-formed";
288 return std::nullopt;
289 }
290
291 // return request
292 Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}};
293 return std::make_tuple(std::move(request), std::move(slots), measure);
294}
295
296// RequestChannelReceiver methods
297
298std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create(
299 const FmqRequestDescriptor& requestChannel) {
300 std::unique_ptr<FmqRequestChannel> fmqRequestChannel =
301 std::make_unique<FmqRequestChannel>(requestChannel);
302 if (!fmqRequestChannel->isValid()) {
303 LOG(ERROR) << "Unable to create RequestChannelReceiver";
304 return nullptr;
305 }
306 const bool blocking = fmqRequestChannel->getEventFlagWord() != nullptr;
307 return std::make_unique<RequestChannelReceiver>(std::move(fmqRequestChannel), blocking);
308}
309
310RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel,
311 bool blocking)
312 : mFmqRequestChannel(std::move(fmqRequestChannel)), mBlocking(blocking) {}
313
314std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>>
315RequestChannelReceiver::getBlocking() {
316 const auto packet = getPacketBlocking();
317 if (!packet) {
318 return std::nullopt;
319 }
320
321 return deserialize(*packet);
322}
323
324void RequestChannelReceiver::invalidate() {
325 mTeardown = true;
326
327 // force unblock
328 // ExecutionBurstServer is by default waiting on a request packet. If the
329 // client process destroys its burst object, the server will still be
330 // waiting on the futex (assuming mBlocking is true). This force unblock
331 // wakes up any thread waiting on the futex.
332 if (mBlocking) {
333 // TODO: look for a different/better way to signal/notify the futex to
334 // wake up any thread waiting on it
335 FmqRequestDatum datum;
336 datum.packetInformation({/*.packetSize=*/0, /*.numberOfInputOperands=*/0,
337 /*.numberOfOutputOperands=*/0, /*.numberOfPools=*/0});
338 mFmqRequestChannel->writeBlocking(&datum, 1);
339 }
340}
341
342std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() {
343 using discriminator = FmqRequestDatum::hidl_discriminator;
344
345 if (mTeardown) {
346 return std::nullopt;
347 }
348
349 // wait for request packet and read first element of request packet
Michael Butlerc932ebb2019-04-11 14:24:06 -0700350 FmqRequestDatum datum;
351 bool success = false;
352 if (mBlocking) {
353 success = mFmqRequestChannel->readBlocking(&datum, 1);
354 } else {
355 while ((success = !mTeardown.load(std::memory_order_relaxed)) &&
356 !mFmqRequestChannel->read(&datum, 1)) {
357 }
358 }
359
Michael Butlerc932ebb2019-04-11 14:24:06 -0700360 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet");
361
Michael Butlerc932ebb2019-04-11 14:24:06 -0700362 // retrieve remaining elements
363 // NOTE: all of the data is already available at this point, so there's no
364 // need to do a blocking wait to wait for more data. This is known because
365 // in FMQ, all writes are published (made available) atomically. Currently,
366 // the producer always publishes the entire packet in one function call, so
367 // if the first element of the packet is available, the remaining elements
368 // are also available.
Michael Butler3260db92019-04-26 17:51:23 -0700369 const size_t count = mFmqRequestChannel->availableToRead();
370 std::vector<FmqRequestDatum> packet(count + 1);
Michael Butlerc932ebb2019-04-11 14:24:06 -0700371 packet.front() = datum;
Michael Butler3260db92019-04-26 17:51:23 -0700372 success &= mFmqRequestChannel->read(packet.data() + 1, count);
Michael Butlerc932ebb2019-04-11 14:24:06 -0700373
Michael Butler3260db92019-04-26 17:51:23 -0700374 // terminate loop
375 if (mTeardown) {
376 return std::nullopt;
377 }
378
379 // ensure packet was successfully received
Michael Butlerc932ebb2019-04-11 14:24:06 -0700380 if (!success) {
Michael Butler3260db92019-04-26 17:51:23 -0700381 LOG(ERROR) << "Error receiving packet";
382 return std::nullopt;
Michael Butlerc932ebb2019-04-11 14:24:06 -0700383 }
384
385 return packet;
386}
387
388// ResultChannelSender methods
389
390std::unique_ptr<ResultChannelSender> ResultChannelSender::create(
391 const FmqResultDescriptor& resultChannel) {
392 std::unique_ptr<FmqResultChannel> fmqResultChannel =
393 std::make_unique<FmqResultChannel>(resultChannel);
394 if (!fmqResultChannel->isValid()) {
395 LOG(ERROR) << "Unable to create RequestChannelSender";
396 return nullptr;
397 }
398 const bool blocking = fmqResultChannel->getEventFlagWord() != nullptr;
399 return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel), blocking);
400}
401
402ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel,
403 bool blocking)
404 : mFmqResultChannel(std::move(fmqResultChannel)), mBlocking(blocking) {}
405
406bool ResultChannelSender::send(ErrorStatus errorStatus,
407 const std::vector<OutputShape>& outputShapes, Timing timing) {
408 const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing);
409 return sendPacket(serialized);
410}
411
412bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) {
Michael Butler3260db92019-04-26 17:51:23 -0700413 if (packet.size() > mFmqResultChannel->availableToWrite()) {
414 LOG(ERROR)
415 << "ResultChannelSender::sendPacket -- packet size exceeds size available in FMQ";
416 const std::vector<FmqResultDatum> errorPacket =
417 serialize(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
418 return mFmqResultChannel->writeBlocking(errorPacket.data(), errorPacket.size());
419 }
420
Michael Butlerc932ebb2019-04-11 14:24:06 -0700421 if (mBlocking) {
422 return mFmqResultChannel->writeBlocking(packet.data(), packet.size());
423 } else {
424 return mFmqResultChannel->write(packet.data(), packet.size());
425 }
426}
427
428// ExecutionBurstServer methods
429
430sp<ExecutionBurstServer> ExecutionBurstServer::create(
431 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
432 const MQDescriptorSync<FmqResultDatum>& resultChannel,
433 std::shared_ptr<IBurstExecutorWithCache> executorWithCache) {
434 // check inputs
435 if (callback == nullptr || executorWithCache == nullptr) {
436 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
437 return nullptr;
438 }
439
440 // create FMQ objects
441 std::unique_ptr<RequestChannelReceiver> requestChannelReceiver =
442 RequestChannelReceiver::create(requestChannel);
443 std::unique_ptr<ResultChannelSender> resultChannelSender =
444 ResultChannelSender::create(resultChannel);
445
446 // check FMQ objects
447 if (!requestChannelReceiver || !resultChannelSender) {
448 LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue";
449 return nullptr;
450 }
451
452 // make and return context
453 return new ExecutionBurstServer(callback, std::move(requestChannelReceiver),
454 std::move(resultChannelSender), std::move(executorWithCache));
455}
456
457sp<ExecutionBurstServer> ExecutionBurstServer::create(
458 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
459 const MQDescriptorSync<FmqResultDatum>& resultChannel, IPreparedModel* preparedModel) {
460 // check relevant input
461 if (preparedModel == nullptr) {
462 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
463 return nullptr;
464 }
465
466 // adapt IPreparedModel to have caching
467 const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter =
468 std::make_shared<DefaultBurstExecutorWithCache>(preparedModel);
469
470 // make and return context
471 return ExecutionBurstServer::create(callback, requestChannel, resultChannel,
472 preparedModelAdapter);
473}
474
475ExecutionBurstServer::ExecutionBurstServer(
476 const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel,
477 std::unique_ptr<ResultChannelSender> resultChannel,
478 std::shared_ptr<IBurstExecutorWithCache> executorWithCache)
479 : mCallback(callback),
480 mRequestChannelReceiver(std::move(requestChannel)),
481 mResultChannelSender(std::move(resultChannel)),
482 mExecutorWithCache(std::move(executorWithCache)) {
483 // TODO: highly document the threading behavior of this class
484 mWorker = std::thread([this] { task(); });
485}
486
487ExecutionBurstServer::~ExecutionBurstServer() {
488 // set teardown flag
489 mTeardown = true;
490 mRequestChannelReceiver->invalidate();
491
492 // wait for task thread to end
493 mWorker.join();
494}
495
496Return<void> ExecutionBurstServer::freeMemory(int32_t slot) {
497 mExecutorWithCache->removeCacheEntry(slot);
498 return Void();
499}
500
501void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) {
502 const auto slotIsKnown = [this](int32_t slot) {
503 return mExecutorWithCache->isCacheEntryPresent(slot);
504 };
505
506 // find unique unknown slots
507 std::vector<int32_t> unknownSlots = slots;
508 auto unknownSlotsEnd = unknownSlots.end();
509 std::sort(unknownSlots.begin(), unknownSlotsEnd);
510 unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd);
511 unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
512 unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
513
514 // quick-exit if all slots are known
515 if (unknownSlots.empty()) {
516 return;
517 }
518
519 ErrorStatus errorStatus = ErrorStatus::GENERAL_FAILURE;
520 std::vector<hidl_memory> returnedMemories;
521 auto cb = [&errorStatus, &returnedMemories](ErrorStatus status,
522 const hidl_vec<hidl_memory>& memories) {
523 errorStatus = status;
524 returnedMemories = memories;
525 };
526
527 const Return<void> ret = mCallback->getMemories(unknownSlots, cb);
528
529 if (!ret.isOk() || errorStatus != ErrorStatus::NONE ||
530 returnedMemories.size() != unknownSlots.size()) {
531 LOG(ERROR) << "Error retrieving memories";
532 return;
533 }
534
535 // add memories to unknown slots
536 for (size_t i = 0; i < unknownSlots.size(); ++i) {
537 mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]);
538 }
539}
540
541void ExecutionBurstServer::task() {
542 // loop until the burst object is being destroyed
543 while (!mTeardown) {
544 // receive request
545 auto arguments = mRequestChannelReceiver->getBlocking();
546
547 // if the request packet was not properly received, return a generic
548 // error and skip the execution
549 //
550 // if the burst is being torn down, skip the execution exection so the
551 // "task" function can end
552 if (!arguments) {
553 if (!mTeardown) {
554 mResultChannelSender->send(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
555 }
556 continue;
557 }
558
559 // otherwise begin tracing execution
560 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
561 "ExecutionBurstServer getting memory, executing, and returning results");
562
563 // unpack the arguments; types are Request, std::vector<int32_t>, and
Michael Butler238fe722019-03-21 12:17:27 -0700564 // MeasureTiming, respectively
Michael Butlerc932ebb2019-04-11 14:24:06 -0700565 const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments);
Michael Butler60296322019-01-17 17:54:51 -0800566
Michael Butler238fe722019-03-21 12:17:27 -0700567 // ensure executor with cache has required memory
568 std::lock_guard<std::mutex> hold(mMutex);
569 ensureCacheEntriesArePresentLocked(slotsOfPools);
570
571 // perform computation; types are ErrorStatus, hidl_vec<OutputShape>,
572 // and Timing, respectively
573 const auto [errorStatus, outputShapes, returnedTiming] =
574 mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure);
Michael Butler60296322019-01-17 17:54:51 -0800575
576 // return result
Michael Butlerc932ebb2019-04-11 14:24:06 -0700577 mResultChannelSender->send(errorStatus, outputShapes, returnedTiming);
Michael Butler60296322019-01-17 17:54:51 -0800578 }
579}
580
Michael Butler3db6fe52019-01-29 11:20:30 -0800581} // namespace android::nn