blob: 74bc34058d015089656be5af7f29905208d99046 [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 Butler4ef48f12019-05-02 14:09:17 -070023#include <cstring>
Michael Butlerc932ebb2019-04-11 14:24:06 -070024#include <limits>
Michael Butler3260db92019-04-26 17:51:23 -070025#include <map>
26
Michael Butler3db6fe52019-01-29 11:20:30 -080027#include "Tracing.h"
Michael Butler60296322019-01-17 17:54:51 -080028
Michael Butler3db6fe52019-01-29 11:20:30 -080029namespace android::nn {
Michael Butler238fe722019-03-21 12:17:27 -070030namespace {
Michael Butler60296322019-01-17 17:54:51 -080031
Michael Butler19af9d22019-07-11 11:45:01 -070032using namespace hal;
33
Michael Butlerc932ebb2019-04-11 14:24:06 -070034constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(),
35 std::numeric_limits<uint64_t>::max()};
36
Michael Butler238fe722019-03-21 12:17:27 -070037// DefaultBurstExecutorWithCache adapts an IPreparedModel so that it can be
38// used as an IBurstExecutorWithCache. Specifically, the cache simply stores the
39// hidl_memory object, and the execution forwards calls to the provided
40// IPreparedModel's "executeSynchronously" method. With this class, hidl_memory
41// must be mapped and unmapped for each execution.
42class DefaultBurstExecutorWithCache : public ExecutionBurstServer::IBurstExecutorWithCache {
43 public:
44 DefaultBurstExecutorWithCache(IPreparedModel* preparedModel) : mpPreparedModel(preparedModel) {}
Michael Butler60296322019-01-17 17:54:51 -080045
Michael Butler238fe722019-03-21 12:17:27 -070046 bool isCacheEntryPresent(int32_t slot) const override {
Michael Butler3260db92019-04-26 17:51:23 -070047 const auto it = mMemoryCache.find(slot);
Michael Butler1ee58a52019-04-30 13:49:32 -070048 return (it != mMemoryCache.end()) && 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";
Raksit Ashokc1079232019-05-29 12:55:16 -070085 return {returnedStatus, {}, kNoTiming};
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
Steven Moreland393ac6d2019-04-25 15:33:25 -0700123 FmqResultDatum::OperandInformation info{};
124 info.isSufficient = operand.isSufficient;
125 info.numberOfDimensions = static_cast<uint32_t>(operand.dimensions.size());
126
Michael Butler60296322019-01-17 17:54:51 -0800127 FmqResultDatum datum;
Steven Moreland393ac6d2019-04-25 15:33:25 -0700128 datum.operandInformation(info);
Michael Butler60296322019-01-17 17:54:51 -0800129 data.push_back(datum);
130
131 // package operand dimensions
132 for (uint32_t dimension : operand.dimensions) {
133 FmqResultDatum datum;
134 datum.operandDimensionValue(dimension);
135 data.push_back(datum);
136 }
137 }
138
139 // package executionTiming
140 {
141 FmqResultDatum datum;
142 datum.executionTiming(timing);
143 data.push_back(datum);
144 }
145
146 // return result
147 return data;
148}
149
Michael Butlerc932ebb2019-04-11 14:24:06 -0700150// deserialize request
151std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>> deserialize(
152 const std::vector<FmqRequestDatum>& data) {
153 using discriminator = FmqRequestDatum::hidl_discriminator;
Michael Butler60296322019-01-17 17:54:51 -0800154
Michael Butlerc932ebb2019-04-11 14:24:06 -0700155 size_t index = 0;
156
157 // validate packet information
Michael Butler3260db92019-04-26 17:51:23 -0700158 if (data.size() == 0 || data[index].getDiscriminator() != discriminator::packetInformation) {
Michael Butlerc932ebb2019-04-11 14:24:06 -0700159 LOG(ERROR) << "FMQ Request packet ill-formed";
160 return std::nullopt;
161 }
162
163 // unpackage packet information
164 const FmqRequestDatum::PacketInformation& packetInfo = data[index].packetInformation();
165 index++;
166 const uint32_t packetSize = packetInfo.packetSize;
167 const uint32_t numberOfInputOperands = packetInfo.numberOfInputOperands;
168 const uint32_t numberOfOutputOperands = packetInfo.numberOfOutputOperands;
169 const uint32_t numberOfPools = packetInfo.numberOfPools;
170
Michael Butler3260db92019-04-26 17:51:23 -0700171 // verify packet size
172 if (data.size() != packetSize) {
173 LOG(ERROR) << "FMQ Request packet ill-formed";
174 return std::nullopt;
175 }
176
Michael Butlerc932ebb2019-04-11 14:24:06 -0700177 // unpackage input operands
178 std::vector<RequestArgument> inputs;
179 inputs.reserve(numberOfInputOperands);
180 for (size_t operand = 0; operand < numberOfInputOperands; ++operand) {
181 // validate input operand information
182 if (data[index].getDiscriminator() != discriminator::inputOperandInformation) {
183 LOG(ERROR) << "FMQ Request packet ill-formed";
184 return std::nullopt;
Michael Butler60296322019-01-17 17:54:51 -0800185 }
186
Michael Butlerc932ebb2019-04-11 14:24:06 -0700187 // unpackage operand information
188 const FmqRequestDatum::OperandInformation& operandInfo =
189 data[index].inputOperandInformation();
190 index++;
191 const bool hasNoValue = operandInfo.hasNoValue;
192 const DataLocation location = operandInfo.location;
193 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
Michael Butler3db6fe52019-01-29 11:20:30 -0800194
Michael Butlerc932ebb2019-04-11 14:24:06 -0700195 // unpackage operand dimensions
196 std::vector<uint32_t> dimensions;
197 dimensions.reserve(numberOfDimensions);
198 for (size_t i = 0; i < numberOfDimensions; ++i) {
199 // validate dimension
200 if (data[index].getDiscriminator() != discriminator::inputOperandDimensionValue) {
201 LOG(ERROR) << "FMQ Request packet ill-formed";
202 return std::nullopt;
203 }
204
205 // unpackage dimension
206 const uint32_t dimension = data[index].inputOperandDimensionValue();
207 index++;
208
209 // store result
210 dimensions.push_back(dimension);
211 }
212
213 // store result
214 inputs.push_back(
215 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
216 }
217
218 // unpackage output operands
219 std::vector<RequestArgument> outputs;
220 outputs.reserve(numberOfOutputOperands);
221 for (size_t operand = 0; operand < numberOfOutputOperands; ++operand) {
222 // validate output operand information
223 if (data[index].getDiscriminator() != discriminator::outputOperandInformation) {
224 LOG(ERROR) << "FMQ Request packet ill-formed";
225 return std::nullopt;
226 }
227
228 // unpackage operand information
229 const FmqRequestDatum::OperandInformation& operandInfo =
230 data[index].outputOperandInformation();
231 index++;
232 const bool hasNoValue = operandInfo.hasNoValue;
233 const DataLocation location = operandInfo.location;
234 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
235
236 // unpackage operand dimensions
237 std::vector<uint32_t> dimensions;
238 dimensions.reserve(numberOfDimensions);
239 for (size_t i = 0; i < numberOfDimensions; ++i) {
240 // validate dimension
241 if (data[index].getDiscriminator() != discriminator::outputOperandDimensionValue) {
242 LOG(ERROR) << "FMQ Request packet ill-formed";
243 return std::nullopt;
244 }
245
246 // unpackage dimension
247 const uint32_t dimension = data[index].outputOperandDimensionValue();
248 index++;
249
250 // store result
251 dimensions.push_back(dimension);
252 }
253
254 // store result
255 outputs.push_back(
256 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
257 }
258
259 // unpackage pools
260 std::vector<int32_t> slots;
261 slots.reserve(numberOfPools);
262 for (size_t pool = 0; pool < numberOfPools; ++pool) {
263 // validate input operand information
264 if (data[index].getDiscriminator() != discriminator::poolIdentifier) {
265 LOG(ERROR) << "FMQ Request packet ill-formed";
266 return std::nullopt;
267 }
268
269 // unpackage operand information
270 const int32_t poolId = data[index].poolIdentifier();
271 index++;
272
273 // store result
274 slots.push_back(poolId);
275 }
276
277 // validate measureTiming
278 if (data[index].getDiscriminator() != discriminator::measureTiming) {
279 LOG(ERROR) << "FMQ Request packet ill-formed";
280 return std::nullopt;
281 }
282
283 // unpackage measureTiming
284 const MeasureTiming measure = data[index].measureTiming();
285 index++;
286
287 // validate packet information
288 if (index != packetSize) {
289 LOG(ERROR) << "FMQ Result packet ill-formed";
290 return std::nullopt;
291 }
292
293 // return request
294 Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}};
295 return std::make_tuple(std::move(request), std::move(slots), measure);
296}
297
298// RequestChannelReceiver methods
299
300std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create(
301 const FmqRequestDescriptor& requestChannel) {
302 std::unique_ptr<FmqRequestChannel> fmqRequestChannel =
303 std::make_unique<FmqRequestChannel>(requestChannel);
304 if (!fmqRequestChannel->isValid()) {
305 LOG(ERROR) << "Unable to create RequestChannelReceiver";
306 return nullptr;
307 }
308 const bool blocking = fmqRequestChannel->getEventFlagWord() != nullptr;
309 return std::make_unique<RequestChannelReceiver>(std::move(fmqRequestChannel), blocking);
310}
311
312RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel,
313 bool blocking)
314 : mFmqRequestChannel(std::move(fmqRequestChannel)), mBlocking(blocking) {}
315
316std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>>
317RequestChannelReceiver::getBlocking() {
318 const auto packet = getPacketBlocking();
319 if (!packet) {
320 return std::nullopt;
321 }
322
323 return deserialize(*packet);
324}
325
326void RequestChannelReceiver::invalidate() {
327 mTeardown = true;
328
329 // force unblock
330 // ExecutionBurstServer is by default waiting on a request packet. If the
331 // client process destroys its burst object, the server will still be
332 // waiting on the futex (assuming mBlocking is true). This force unblock
333 // wakes up any thread waiting on the futex.
334 if (mBlocking) {
335 // TODO: look for a different/better way to signal/notify the futex to
336 // wake up any thread waiting on it
337 FmqRequestDatum datum;
338 datum.packetInformation({/*.packetSize=*/0, /*.numberOfInputOperands=*/0,
339 /*.numberOfOutputOperands=*/0, /*.numberOfPools=*/0});
340 mFmqRequestChannel->writeBlocking(&datum, 1);
341 }
342}
343
344std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() {
345 using discriminator = FmqRequestDatum::hidl_discriminator;
346
347 if (mTeardown) {
348 return std::nullopt;
349 }
350
351 // wait for request packet and read first element of request packet
Michael Butlerc932ebb2019-04-11 14:24:06 -0700352 FmqRequestDatum datum;
353 bool success = false;
354 if (mBlocking) {
355 success = mFmqRequestChannel->readBlocking(&datum, 1);
356 } else {
357 while ((success = !mTeardown.load(std::memory_order_relaxed)) &&
358 !mFmqRequestChannel->read(&datum, 1)) {
359 }
360 }
361
Michael Butlerc932ebb2019-04-11 14:24:06 -0700362 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet");
363
Michael Butlerc932ebb2019-04-11 14:24:06 -0700364 // retrieve remaining elements
365 // NOTE: all of the data is already available at this point, so there's no
366 // need to do a blocking wait to wait for more data. This is known because
367 // in FMQ, all writes are published (made available) atomically. Currently,
368 // the producer always publishes the entire packet in one function call, so
369 // if the first element of the packet is available, the remaining elements
370 // are also available.
Michael Butler3260db92019-04-26 17:51:23 -0700371 const size_t count = mFmqRequestChannel->availableToRead();
372 std::vector<FmqRequestDatum> packet(count + 1);
Michael Butler4ef48f12019-05-02 14:09:17 -0700373 std::memcpy(&packet.front(), &datum, sizeof(datum));
Michael Butler3260db92019-04-26 17:51:23 -0700374 success &= mFmqRequestChannel->read(packet.data() + 1, count);
Michael Butlerc932ebb2019-04-11 14:24:06 -0700375
Michael Butler3260db92019-04-26 17:51:23 -0700376 // terminate loop
377 if (mTeardown) {
378 return std::nullopt;
379 }
380
381 // ensure packet was successfully received
Michael Butlerc932ebb2019-04-11 14:24:06 -0700382 if (!success) {
Michael Butler3260db92019-04-26 17:51:23 -0700383 LOG(ERROR) << "Error receiving packet";
384 return std::nullopt;
Michael Butlerc932ebb2019-04-11 14:24:06 -0700385 }
386
Michael Butler4ef48f12019-05-02 14:09:17 -0700387 return std::make_optional(std::move(packet));
Michael Butlerc932ebb2019-04-11 14:24:06 -0700388}
389
390// ResultChannelSender methods
391
392std::unique_ptr<ResultChannelSender> ResultChannelSender::create(
393 const FmqResultDescriptor& resultChannel) {
394 std::unique_ptr<FmqResultChannel> fmqResultChannel =
395 std::make_unique<FmqResultChannel>(resultChannel);
396 if (!fmqResultChannel->isValid()) {
397 LOG(ERROR) << "Unable to create RequestChannelSender";
398 return nullptr;
399 }
400 const bool blocking = fmqResultChannel->getEventFlagWord() != nullptr;
401 return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel), blocking);
402}
403
404ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel,
405 bool blocking)
406 : mFmqResultChannel(std::move(fmqResultChannel)), mBlocking(blocking) {}
407
408bool ResultChannelSender::send(ErrorStatus errorStatus,
409 const std::vector<OutputShape>& outputShapes, Timing timing) {
410 const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing);
411 return sendPacket(serialized);
412}
413
414bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) {
Michael Butler3260db92019-04-26 17:51:23 -0700415 if (packet.size() > mFmqResultChannel->availableToWrite()) {
416 LOG(ERROR)
417 << "ResultChannelSender::sendPacket -- packet size exceeds size available in FMQ";
418 const std::vector<FmqResultDatum> errorPacket =
419 serialize(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
Michael Butler1ee58a52019-04-30 13:49:32 -0700420 if (mBlocking) {
421 return mFmqResultChannel->writeBlocking(errorPacket.data(), errorPacket.size());
422 } else {
423 return mFmqResultChannel->write(errorPacket.data(), errorPacket.size());
424 }
Michael Butler3260db92019-04-26 17:51:23 -0700425 }
426
Michael Butlerc932ebb2019-04-11 14:24:06 -0700427 if (mBlocking) {
428 return mFmqResultChannel->writeBlocking(packet.data(), packet.size());
429 } else {
430 return mFmqResultChannel->write(packet.data(), packet.size());
431 }
432}
433
434// ExecutionBurstServer methods
435
436sp<ExecutionBurstServer> ExecutionBurstServer::create(
437 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
438 const MQDescriptorSync<FmqResultDatum>& resultChannel,
439 std::shared_ptr<IBurstExecutorWithCache> executorWithCache) {
440 // check inputs
441 if (callback == nullptr || executorWithCache == nullptr) {
442 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
443 return nullptr;
444 }
445
446 // create FMQ objects
447 std::unique_ptr<RequestChannelReceiver> requestChannelReceiver =
448 RequestChannelReceiver::create(requestChannel);
449 std::unique_ptr<ResultChannelSender> resultChannelSender =
450 ResultChannelSender::create(resultChannel);
451
452 // check FMQ objects
453 if (!requestChannelReceiver || !resultChannelSender) {
454 LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue";
455 return nullptr;
456 }
457
458 // make and return context
459 return new ExecutionBurstServer(callback, std::move(requestChannelReceiver),
460 std::move(resultChannelSender), std::move(executorWithCache));
461}
462
463sp<ExecutionBurstServer> ExecutionBurstServer::create(
464 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
465 const MQDescriptorSync<FmqResultDatum>& resultChannel, IPreparedModel* preparedModel) {
466 // check relevant input
467 if (preparedModel == nullptr) {
468 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
469 return nullptr;
470 }
471
472 // adapt IPreparedModel to have caching
473 const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter =
474 std::make_shared<DefaultBurstExecutorWithCache>(preparedModel);
475
476 // make and return context
477 return ExecutionBurstServer::create(callback, requestChannel, resultChannel,
478 preparedModelAdapter);
479}
480
481ExecutionBurstServer::ExecutionBurstServer(
482 const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel,
483 std::unique_ptr<ResultChannelSender> resultChannel,
484 std::shared_ptr<IBurstExecutorWithCache> executorWithCache)
485 : mCallback(callback),
486 mRequestChannelReceiver(std::move(requestChannel)),
487 mResultChannelSender(std::move(resultChannel)),
488 mExecutorWithCache(std::move(executorWithCache)) {
489 // TODO: highly document the threading behavior of this class
490 mWorker = std::thread([this] { task(); });
491}
492
493ExecutionBurstServer::~ExecutionBurstServer() {
494 // set teardown flag
495 mTeardown = true;
496 mRequestChannelReceiver->invalidate();
497
498 // wait for task thread to end
499 mWorker.join();
500}
501
502Return<void> ExecutionBurstServer::freeMemory(int32_t slot) {
Michael Butlerba59a542019-06-28 17:06:27 -0700503 std::lock_guard<std::mutex> hold(mMutex);
Michael Butlerc932ebb2019-04-11 14:24:06 -0700504 mExecutorWithCache->removeCacheEntry(slot);
505 return Void();
506}
507
508void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) {
509 const auto slotIsKnown = [this](int32_t slot) {
510 return mExecutorWithCache->isCacheEntryPresent(slot);
511 };
512
513 // find unique unknown slots
514 std::vector<int32_t> unknownSlots = slots;
515 auto unknownSlotsEnd = unknownSlots.end();
516 std::sort(unknownSlots.begin(), unknownSlotsEnd);
517 unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd);
518 unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
519 unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
520
521 // quick-exit if all slots are known
522 if (unknownSlots.empty()) {
523 return;
524 }
525
526 ErrorStatus errorStatus = ErrorStatus::GENERAL_FAILURE;
527 std::vector<hidl_memory> returnedMemories;
528 auto cb = [&errorStatus, &returnedMemories](ErrorStatus status,
529 const hidl_vec<hidl_memory>& memories) {
530 errorStatus = status;
531 returnedMemories = memories;
532 };
533
534 const Return<void> ret = mCallback->getMemories(unknownSlots, cb);
535
536 if (!ret.isOk() || errorStatus != ErrorStatus::NONE ||
537 returnedMemories.size() != unknownSlots.size()) {
538 LOG(ERROR) << "Error retrieving memories";
539 return;
540 }
541
542 // add memories to unknown slots
543 for (size_t i = 0; i < unknownSlots.size(); ++i) {
544 mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]);
545 }
546}
547
548void ExecutionBurstServer::task() {
549 // loop until the burst object is being destroyed
550 while (!mTeardown) {
551 // receive request
552 auto arguments = mRequestChannelReceiver->getBlocking();
553
554 // if the request packet was not properly received, return a generic
555 // error and skip the execution
556 //
557 // if the burst is being torn down, skip the execution exection so the
558 // "task" function can end
559 if (!arguments) {
560 if (!mTeardown) {
561 mResultChannelSender->send(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
562 }
563 continue;
564 }
565
566 // otherwise begin tracing execution
567 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
568 "ExecutionBurstServer getting memory, executing, and returning results");
569
570 // unpack the arguments; types are Request, std::vector<int32_t>, and
Michael Butler238fe722019-03-21 12:17:27 -0700571 // MeasureTiming, respectively
Michael Butlerc932ebb2019-04-11 14:24:06 -0700572 const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments);
Michael Butler60296322019-01-17 17:54:51 -0800573
Michael Butler238fe722019-03-21 12:17:27 -0700574 // ensure executor with cache has required memory
575 std::lock_guard<std::mutex> hold(mMutex);
576 ensureCacheEntriesArePresentLocked(slotsOfPools);
577
578 // perform computation; types are ErrorStatus, hidl_vec<OutputShape>,
579 // and Timing, respectively
580 const auto [errorStatus, outputShapes, returnedTiming] =
581 mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure);
Michael Butler60296322019-01-17 17:54:51 -0800582
583 // return result
Michael Butlerc932ebb2019-04-11 14:24:06 -0700584 mResultChannelSender->send(errorStatus, outputShapes, returnedTiming);
Michael Butler60296322019-01-17 17:54:51 -0800585 }
586}
587
Michael Butler3db6fe52019-01-29 11:20:30 -0800588} // namespace android::nn