blob: b29f57badd5400a8aee87d643549cad232a1b90e [file] [log] [blame]
Michael Butler92399812019-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 Butler056a76e2019-01-29 11:20:30 -080017#define LOG_TAG "ExecutionBurstServer"
18
Michael Butler92399812019-01-17 17:54:51 -080019#include "ExecutionBurstServer.h"
20
21#include <android-base/logging.h>
Michael Butler4a88d632019-04-26 17:51:23 -070022
Michael Butler9e3b58e2019-04-11 14:24:06 -070023#include <limits>
Michael Butler4a88d632019-04-26 17:51:23 -070024#include <map>
25
Michael Butler056a76e2019-01-29 11:20:30 -080026#include "Tracing.h"
Michael Butler92399812019-01-17 17:54:51 -080027
Michael Butler056a76e2019-01-29 11:20:30 -080028namespace android::nn {
Michael Butler28ec2962019-03-21 12:17:27 -070029namespace {
Michael Butler92399812019-01-17 17:54:51 -080030
Michael Butler9e3b58e2019-04-11 14:24:06 -070031constexpr Timing kNoTiming = {std::numeric_limits<uint64_t>::max(),
32 std::numeric_limits<uint64_t>::max()};
33
Michael Butler28ec2962019-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 Butler92399812019-01-17 17:54:51 -080042
Michael Butler28ec2962019-03-21 12:17:27 -070043 bool isCacheEntryPresent(int32_t slot) const override {
Michael Butler4a88d632019-04-26 17:51:23 -070044 const auto it = mMemoryCache.find(slot);
Michael Butler724c82c2019-04-30 13:49:32 -070045 return (it != mMemoryCache.end()) && it->second.valid();
Michael Butler28ec2962019-03-21 12:17:27 -070046 }
Michael Butlerda220312019-03-14 17:34:48 -070047
Michael Butler28ec2962019-03-21 12:17:27 -070048 void addCacheEntry(const hidl_memory& memory, int32_t slot) override {
Michael Butler28ec2962019-03-21 12:17:27 -070049 mMemoryCache[slot] = memory;
50 }
Michael Butler92399812019-01-17 17:54:51 -080051
Michael Butler4a88d632019-04-26 17:51:23 -070052 void removeCacheEntry(int32_t slot) override { mMemoryCache.erase(slot); }
Michael Butler28ec2962019-03-21 12:17:27 -070053
54 std::tuple<ErrorStatus, hidl_vec<OutputShape>, Timing> execute(
55 const Request& request, const std::vector<int32_t>& slots,
56 MeasureTiming measure) override {
57 // convert slots to pools
58 hidl_vec<hidl_memory> pools(slots.size());
Michael Butler4a88d632019-04-26 17:51:23 -070059 std::transform(slots.begin(), slots.end(), pools.begin(),
60 [this](int32_t slot) { return mMemoryCache[slot]; });
Michael Butler28ec2962019-03-21 12:17:27 -070061
62 // create full request
63 Request fullRequest = request;
64 fullRequest.pools = std::move(pools);
65
66 // setup execution
67 ErrorStatus returnedStatus = ErrorStatus::GENERAL_FAILURE;
68 hidl_vec<OutputShape> returnedOutputShapes;
69 Timing returnedTiming;
70 auto cb = [&returnedStatus, &returnedOutputShapes, &returnedTiming](
71 ErrorStatus status, const hidl_vec<OutputShape>& outputShapes,
72 const Timing& timing) {
73 returnedStatus = status;
74 returnedOutputShapes = outputShapes;
75 returnedTiming = timing;
Michael Butlerda220312019-03-14 17:34:48 -070076 };
Michael Butler92399812019-01-17 17:54:51 -080077
Michael Butler28ec2962019-03-21 12:17:27 -070078 // execute
79 const Return<void> ret = mpPreparedModel->executeSynchronously(fullRequest, measure, cb);
80 if (!ret.isOk() || returnedStatus != ErrorStatus::NONE) {
81 LOG(ERROR) << "IPreparedModelAdapter::execute -- Error executing";
82 return {ErrorStatus::GENERAL_FAILURE, {}, {}};
Michael Butler056a76e2019-01-29 11:20:30 -080083 }
Michael Butler92399812019-01-17 17:54:51 -080084
Michael Butler28ec2962019-03-21 12:17:27 -070085 return std::make_tuple(returnedStatus, std::move(returnedOutputShapes), returnedTiming);
Michael Butler92399812019-01-17 17:54:51 -080086 }
87
Michael Butler28ec2962019-03-21 12:17:27 -070088 private:
89 IPreparedModel* const mpPreparedModel;
Michael Butler4a88d632019-04-26 17:51:23 -070090 std::map<int32_t, hidl_memory> mMemoryCache;
Michael Butler28ec2962019-03-21 12:17:27 -070091};
Michael Butlerda220312019-03-14 17:34:48 -070092
Michael Butler28ec2962019-03-21 12:17:27 -070093} // anonymous namespace
Michael Butler92399812019-01-17 17:54:51 -080094
Michael Butler92399812019-01-17 17:54:51 -080095// serialize result
Michael Butler9e3b58e2019-04-11 14:24:06 -070096std::vector<FmqResultDatum> serialize(ErrorStatus errorStatus,
97 const std::vector<OutputShape>& outputShapes, Timing timing) {
Michael Butler92399812019-01-17 17:54:51 -080098 // count how many elements need to be sent for a request
99 size_t count = 2 + outputShapes.size();
100 for (const auto& outputShape : outputShapes) {
101 count += outputShape.dimensions.size();
102 }
103
104 // create buffer to temporarily store elements
105 std::vector<FmqResultDatum> data;
106 data.reserve(count);
107
108 // package packetInfo
109 {
110 FmqResultDatum datum;
111 datum.packetInformation({/*.packetSize=*/static_cast<uint32_t>(count),
112 /*.errorStatus=*/errorStatus,
113 /*.numberOfOperands=*/static_cast<uint32_t>(outputShapes.size())});
114 data.push_back(datum);
115 }
116
117 // package output shape data
118 for (const auto& operand : outputShapes) {
119 // package operand information
120 FmqResultDatum datum;
121 datum.operandInformation(
122 {/*.isSufficient=*/operand.isSufficient,
123 /*.numberOfDimensions=*/static_cast<uint32_t>(operand.dimensions.size())});
124 data.push_back(datum);
125
126 // package operand dimensions
127 for (uint32_t dimension : operand.dimensions) {
128 FmqResultDatum datum;
129 datum.operandDimensionValue(dimension);
130 data.push_back(datum);
131 }
132 }
133
134 // package executionTiming
135 {
136 FmqResultDatum datum;
137 datum.executionTiming(timing);
138 data.push_back(datum);
139 }
140
141 // return result
142 return data;
143}
144
Michael Butler9e3b58e2019-04-11 14:24:06 -0700145// deserialize request
146std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>> deserialize(
147 const std::vector<FmqRequestDatum>& data) {
148 using discriminator = FmqRequestDatum::hidl_discriminator;
Michael Butler92399812019-01-17 17:54:51 -0800149
Michael Butler9e3b58e2019-04-11 14:24:06 -0700150 size_t index = 0;
151
152 // validate packet information
Michael Butler4a88d632019-04-26 17:51:23 -0700153 if (data.size() == 0 || data[index].getDiscriminator() != discriminator::packetInformation) {
Michael Butler9e3b58e2019-04-11 14:24:06 -0700154 LOG(ERROR) << "FMQ Request packet ill-formed";
155 return std::nullopt;
156 }
157
158 // unpackage packet information
159 const FmqRequestDatum::PacketInformation& packetInfo = data[index].packetInformation();
160 index++;
161 const uint32_t packetSize = packetInfo.packetSize;
162 const uint32_t numberOfInputOperands = packetInfo.numberOfInputOperands;
163 const uint32_t numberOfOutputOperands = packetInfo.numberOfOutputOperands;
164 const uint32_t numberOfPools = packetInfo.numberOfPools;
165
Michael Butler4a88d632019-04-26 17:51:23 -0700166 // verify packet size
167 if (data.size() != packetSize) {
168 LOG(ERROR) << "FMQ Request packet ill-formed";
169 return std::nullopt;
170 }
171
Michael Butler9e3b58e2019-04-11 14:24:06 -0700172 // unpackage input operands
173 std::vector<RequestArgument> inputs;
174 inputs.reserve(numberOfInputOperands);
175 for (size_t operand = 0; operand < numberOfInputOperands; ++operand) {
176 // validate input operand information
177 if (data[index].getDiscriminator() != discriminator::inputOperandInformation) {
178 LOG(ERROR) << "FMQ Request packet ill-formed";
179 return std::nullopt;
Michael Butler92399812019-01-17 17:54:51 -0800180 }
181
Michael Butler9e3b58e2019-04-11 14:24:06 -0700182 // unpackage operand information
183 const FmqRequestDatum::OperandInformation& operandInfo =
184 data[index].inputOperandInformation();
185 index++;
186 const bool hasNoValue = operandInfo.hasNoValue;
187 const DataLocation location = operandInfo.location;
188 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
Michael Butler056a76e2019-01-29 11:20:30 -0800189
Michael Butler9e3b58e2019-04-11 14:24:06 -0700190 // unpackage operand dimensions
191 std::vector<uint32_t> dimensions;
192 dimensions.reserve(numberOfDimensions);
193 for (size_t i = 0; i < numberOfDimensions; ++i) {
194 // validate dimension
195 if (data[index].getDiscriminator() != discriminator::inputOperandDimensionValue) {
196 LOG(ERROR) << "FMQ Request packet ill-formed";
197 return std::nullopt;
198 }
199
200 // unpackage dimension
201 const uint32_t dimension = data[index].inputOperandDimensionValue();
202 index++;
203
204 // store result
205 dimensions.push_back(dimension);
206 }
207
208 // store result
209 inputs.push_back(
210 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
211 }
212
213 // unpackage output operands
214 std::vector<RequestArgument> outputs;
215 outputs.reserve(numberOfOutputOperands);
216 for (size_t operand = 0; operand < numberOfOutputOperands; ++operand) {
217 // validate output operand information
218 if (data[index].getDiscriminator() != discriminator::outputOperandInformation) {
219 LOG(ERROR) << "FMQ Request packet ill-formed";
220 return std::nullopt;
221 }
222
223 // unpackage operand information
224 const FmqRequestDatum::OperandInformation& operandInfo =
225 data[index].outputOperandInformation();
226 index++;
227 const bool hasNoValue = operandInfo.hasNoValue;
228 const DataLocation location = operandInfo.location;
229 const uint32_t numberOfDimensions = operandInfo.numberOfDimensions;
230
231 // unpackage operand dimensions
232 std::vector<uint32_t> dimensions;
233 dimensions.reserve(numberOfDimensions);
234 for (size_t i = 0; i < numberOfDimensions; ++i) {
235 // validate dimension
236 if (data[index].getDiscriminator() != discriminator::outputOperandDimensionValue) {
237 LOG(ERROR) << "FMQ Request packet ill-formed";
238 return std::nullopt;
239 }
240
241 // unpackage dimension
242 const uint32_t dimension = data[index].outputOperandDimensionValue();
243 index++;
244
245 // store result
246 dimensions.push_back(dimension);
247 }
248
249 // store result
250 outputs.push_back(
251 {/*.hasNoValue=*/hasNoValue, /*.location=*/location, /*.dimensions=*/dimensions});
252 }
253
254 // unpackage pools
255 std::vector<int32_t> slots;
256 slots.reserve(numberOfPools);
257 for (size_t pool = 0; pool < numberOfPools; ++pool) {
258 // validate input operand information
259 if (data[index].getDiscriminator() != discriminator::poolIdentifier) {
260 LOG(ERROR) << "FMQ Request packet ill-formed";
261 return std::nullopt;
262 }
263
264 // unpackage operand information
265 const int32_t poolId = data[index].poolIdentifier();
266 index++;
267
268 // store result
269 slots.push_back(poolId);
270 }
271
272 // validate measureTiming
273 if (data[index].getDiscriminator() != discriminator::measureTiming) {
274 LOG(ERROR) << "FMQ Request packet ill-formed";
275 return std::nullopt;
276 }
277
278 // unpackage measureTiming
279 const MeasureTiming measure = data[index].measureTiming();
280 index++;
281
282 // validate packet information
283 if (index != packetSize) {
284 LOG(ERROR) << "FMQ Result packet ill-formed";
285 return std::nullopt;
286 }
287
288 // return request
289 Request request = {/*.inputs=*/inputs, /*.outputs=*/outputs, /*.pools=*/{}};
290 return std::make_tuple(std::move(request), std::move(slots), measure);
291}
292
293// RequestChannelReceiver methods
294
295std::unique_ptr<RequestChannelReceiver> RequestChannelReceiver::create(
296 const FmqRequestDescriptor& requestChannel) {
297 std::unique_ptr<FmqRequestChannel> fmqRequestChannel =
298 std::make_unique<FmqRequestChannel>(requestChannel);
299 if (!fmqRequestChannel->isValid()) {
300 LOG(ERROR) << "Unable to create RequestChannelReceiver";
301 return nullptr;
302 }
303 const bool blocking = fmqRequestChannel->getEventFlagWord() != nullptr;
304 return std::make_unique<RequestChannelReceiver>(std::move(fmqRequestChannel), blocking);
305}
306
307RequestChannelReceiver::RequestChannelReceiver(std::unique_ptr<FmqRequestChannel> fmqRequestChannel,
308 bool blocking)
309 : mFmqRequestChannel(std::move(fmqRequestChannel)), mBlocking(blocking) {}
310
311std::optional<std::tuple<Request, std::vector<int32_t>, MeasureTiming>>
312RequestChannelReceiver::getBlocking() {
313 const auto packet = getPacketBlocking();
314 if (!packet) {
315 return std::nullopt;
316 }
317
318 return deserialize(*packet);
319}
320
321void RequestChannelReceiver::invalidate() {
322 mTeardown = true;
323
324 // force unblock
325 // ExecutionBurstServer is by default waiting on a request packet. If the
326 // client process destroys its burst object, the server will still be
327 // waiting on the futex (assuming mBlocking is true). This force unblock
328 // wakes up any thread waiting on the futex.
329 if (mBlocking) {
330 // TODO: look for a different/better way to signal/notify the futex to
331 // wake up any thread waiting on it
332 FmqRequestDatum datum;
333 datum.packetInformation({/*.packetSize=*/0, /*.numberOfInputOperands=*/0,
334 /*.numberOfOutputOperands=*/0, /*.numberOfPools=*/0});
335 mFmqRequestChannel->writeBlocking(&datum, 1);
336 }
337}
338
339std::optional<std::vector<FmqRequestDatum>> RequestChannelReceiver::getPacketBlocking() {
340 using discriminator = FmqRequestDatum::hidl_discriminator;
341
342 if (mTeardown) {
343 return std::nullopt;
344 }
345
346 // wait for request packet and read first element of request packet
Michael Butler9e3b58e2019-04-11 14:24:06 -0700347 FmqRequestDatum datum;
348 bool success = false;
349 if (mBlocking) {
350 success = mFmqRequestChannel->readBlocking(&datum, 1);
351 } else {
352 while ((success = !mTeardown.load(std::memory_order_relaxed)) &&
353 !mFmqRequestChannel->read(&datum, 1)) {
354 }
355 }
356
Michael Butler9e3b58e2019-04-11 14:24:06 -0700357 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "ExecutionBurstServer getting packet");
358
Michael Butler9e3b58e2019-04-11 14:24:06 -0700359 // retrieve remaining elements
360 // NOTE: all of the data is already available at this point, so there's no
361 // need to do a blocking wait to wait for more data. This is known because
362 // in FMQ, all writes are published (made available) atomically. Currently,
363 // the producer always publishes the entire packet in one function call, so
364 // if the first element of the packet is available, the remaining elements
365 // are also available.
Michael Butler4a88d632019-04-26 17:51:23 -0700366 const size_t count = mFmqRequestChannel->availableToRead();
367 std::vector<FmqRequestDatum> packet(count + 1);
Michael Butler9e3b58e2019-04-11 14:24:06 -0700368 packet.front() = datum;
Michael Butler4a88d632019-04-26 17:51:23 -0700369 success &= mFmqRequestChannel->read(packet.data() + 1, count);
Michael Butler9e3b58e2019-04-11 14:24:06 -0700370
Michael Butler4a88d632019-04-26 17:51:23 -0700371 // terminate loop
372 if (mTeardown) {
373 return std::nullopt;
374 }
375
376 // ensure packet was successfully received
Michael Butler9e3b58e2019-04-11 14:24:06 -0700377 if (!success) {
Michael Butler4a88d632019-04-26 17:51:23 -0700378 LOG(ERROR) << "Error receiving packet";
379 return std::nullopt;
Michael Butler9e3b58e2019-04-11 14:24:06 -0700380 }
381
382 return packet;
383}
384
385// ResultChannelSender methods
386
387std::unique_ptr<ResultChannelSender> ResultChannelSender::create(
388 const FmqResultDescriptor& resultChannel) {
389 std::unique_ptr<FmqResultChannel> fmqResultChannel =
390 std::make_unique<FmqResultChannel>(resultChannel);
391 if (!fmqResultChannel->isValid()) {
392 LOG(ERROR) << "Unable to create RequestChannelSender";
393 return nullptr;
394 }
395 const bool blocking = fmqResultChannel->getEventFlagWord() != nullptr;
396 return std::make_unique<ResultChannelSender>(std::move(fmqResultChannel), blocking);
397}
398
399ResultChannelSender::ResultChannelSender(std::unique_ptr<FmqResultChannel> fmqResultChannel,
400 bool blocking)
401 : mFmqResultChannel(std::move(fmqResultChannel)), mBlocking(blocking) {}
402
403bool ResultChannelSender::send(ErrorStatus errorStatus,
404 const std::vector<OutputShape>& outputShapes, Timing timing) {
405 const std::vector<FmqResultDatum> serialized = serialize(errorStatus, outputShapes, timing);
406 return sendPacket(serialized);
407}
408
409bool ResultChannelSender::sendPacket(const std::vector<FmqResultDatum>& packet) {
Michael Butler4a88d632019-04-26 17:51:23 -0700410 if (packet.size() > mFmqResultChannel->availableToWrite()) {
411 LOG(ERROR)
412 << "ResultChannelSender::sendPacket -- packet size exceeds size available in FMQ";
413 const std::vector<FmqResultDatum> errorPacket =
414 serialize(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
Michael Butler724c82c2019-04-30 13:49:32 -0700415 if (mBlocking) {
416 return mFmqResultChannel->writeBlocking(errorPacket.data(), errorPacket.size());
417 } else {
418 return mFmqResultChannel->write(errorPacket.data(), errorPacket.size());
419 }
Michael Butler4a88d632019-04-26 17:51:23 -0700420 }
421
Michael Butler9e3b58e2019-04-11 14:24:06 -0700422 if (mBlocking) {
423 return mFmqResultChannel->writeBlocking(packet.data(), packet.size());
424 } else {
425 return mFmqResultChannel->write(packet.data(), packet.size());
426 }
427}
428
429// ExecutionBurstServer methods
430
431sp<ExecutionBurstServer> ExecutionBurstServer::create(
432 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
433 const MQDescriptorSync<FmqResultDatum>& resultChannel,
434 std::shared_ptr<IBurstExecutorWithCache> executorWithCache) {
435 // check inputs
436 if (callback == nullptr || executorWithCache == nullptr) {
437 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
438 return nullptr;
439 }
440
441 // create FMQ objects
442 std::unique_ptr<RequestChannelReceiver> requestChannelReceiver =
443 RequestChannelReceiver::create(requestChannel);
444 std::unique_ptr<ResultChannelSender> resultChannelSender =
445 ResultChannelSender::create(resultChannel);
446
447 // check FMQ objects
448 if (!requestChannelReceiver || !resultChannelSender) {
449 LOG(ERROR) << "ExecutionBurstServer::create failed to create FastMessageQueue";
450 return nullptr;
451 }
452
453 // make and return context
454 return new ExecutionBurstServer(callback, std::move(requestChannelReceiver),
455 std::move(resultChannelSender), std::move(executorWithCache));
456}
457
458sp<ExecutionBurstServer> ExecutionBurstServer::create(
459 const sp<IBurstCallback>& callback, const MQDescriptorSync<FmqRequestDatum>& requestChannel,
460 const MQDescriptorSync<FmqResultDatum>& resultChannel, IPreparedModel* preparedModel) {
461 // check relevant input
462 if (preparedModel == nullptr) {
463 LOG(ERROR) << "ExecutionBurstServer::create passed a nullptr";
464 return nullptr;
465 }
466
467 // adapt IPreparedModel to have caching
468 const std::shared_ptr<DefaultBurstExecutorWithCache> preparedModelAdapter =
469 std::make_shared<DefaultBurstExecutorWithCache>(preparedModel);
470
471 // make and return context
472 return ExecutionBurstServer::create(callback, requestChannel, resultChannel,
473 preparedModelAdapter);
474}
475
476ExecutionBurstServer::ExecutionBurstServer(
477 const sp<IBurstCallback>& callback, std::unique_ptr<RequestChannelReceiver> requestChannel,
478 std::unique_ptr<ResultChannelSender> resultChannel,
479 std::shared_ptr<IBurstExecutorWithCache> executorWithCache)
480 : mCallback(callback),
481 mRequestChannelReceiver(std::move(requestChannel)),
482 mResultChannelSender(std::move(resultChannel)),
483 mExecutorWithCache(std::move(executorWithCache)) {
484 // TODO: highly document the threading behavior of this class
485 mWorker = std::thread([this] { task(); });
486}
487
488ExecutionBurstServer::~ExecutionBurstServer() {
489 // set teardown flag
490 mTeardown = true;
491 mRequestChannelReceiver->invalidate();
492
493 // wait for task thread to end
494 mWorker.join();
495}
496
497Return<void> ExecutionBurstServer::freeMemory(int32_t slot) {
Michael Butlerba59a542019-06-28 17:06:27 -0700498 std::lock_guard<std::mutex> hold(mMutex);
Michael Butler9e3b58e2019-04-11 14:24:06 -0700499 mExecutorWithCache->removeCacheEntry(slot);
500 return Void();
501}
502
503void ExecutionBurstServer::ensureCacheEntriesArePresentLocked(const std::vector<int32_t>& slots) {
504 const auto slotIsKnown = [this](int32_t slot) {
505 return mExecutorWithCache->isCacheEntryPresent(slot);
506 };
507
508 // find unique unknown slots
509 std::vector<int32_t> unknownSlots = slots;
510 auto unknownSlotsEnd = unknownSlots.end();
511 std::sort(unknownSlots.begin(), unknownSlotsEnd);
512 unknownSlotsEnd = std::unique(unknownSlots.begin(), unknownSlotsEnd);
513 unknownSlotsEnd = std::remove_if(unknownSlots.begin(), unknownSlotsEnd, slotIsKnown);
514 unknownSlots.erase(unknownSlotsEnd, unknownSlots.end());
515
516 // quick-exit if all slots are known
517 if (unknownSlots.empty()) {
518 return;
519 }
520
521 ErrorStatus errorStatus = ErrorStatus::GENERAL_FAILURE;
522 std::vector<hidl_memory> returnedMemories;
523 auto cb = [&errorStatus, &returnedMemories](ErrorStatus status,
524 const hidl_vec<hidl_memory>& memories) {
525 errorStatus = status;
526 returnedMemories = memories;
527 };
528
529 const Return<void> ret = mCallback->getMemories(unknownSlots, cb);
530
531 if (!ret.isOk() || errorStatus != ErrorStatus::NONE ||
532 returnedMemories.size() != unknownSlots.size()) {
533 LOG(ERROR) << "Error retrieving memories";
534 return;
535 }
536
537 // add memories to unknown slots
538 for (size_t i = 0; i < unknownSlots.size(); ++i) {
539 mExecutorWithCache->addCacheEntry(returnedMemories[i], unknownSlots[i]);
540 }
541}
542
543void ExecutionBurstServer::task() {
544 // loop until the burst object is being destroyed
545 while (!mTeardown) {
546 // receive request
547 auto arguments = mRequestChannelReceiver->getBlocking();
548
549 // if the request packet was not properly received, return a generic
550 // error and skip the execution
551 //
552 // if the burst is being torn down, skip the execution exection so the
553 // "task" function can end
554 if (!arguments) {
555 if (!mTeardown) {
556 mResultChannelSender->send(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
557 }
558 continue;
559 }
560
561 // otherwise begin tracing execution
562 NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION,
563 "ExecutionBurstServer getting memory, executing, and returning results");
564
565 // unpack the arguments; types are Request, std::vector<int32_t>, and
Michael Butler28ec2962019-03-21 12:17:27 -0700566 // MeasureTiming, respectively
Michael Butler9e3b58e2019-04-11 14:24:06 -0700567 const auto [requestWithoutPools, slotsOfPools, measure] = std::move(*arguments);
Michael Butler92399812019-01-17 17:54:51 -0800568
Michael Butler28ec2962019-03-21 12:17:27 -0700569 // ensure executor with cache has required memory
570 std::lock_guard<std::mutex> hold(mMutex);
571 ensureCacheEntriesArePresentLocked(slotsOfPools);
572
573 // perform computation; types are ErrorStatus, hidl_vec<OutputShape>,
574 // and Timing, respectively
575 const auto [errorStatus, outputShapes, returnedTiming] =
576 mExecutorWithCache->execute(requestWithoutPools, slotsOfPools, measure);
Michael Butler92399812019-01-17 17:54:51 -0800577
578 // return result
Michael Butler9e3b58e2019-04-11 14:24:06 -0700579 mResultChannelSender->send(errorStatus, outputShapes, returnedTiming);
Michael Butler92399812019-01-17 17:54:51 -0800580 }
581}
582
Michael Butler056a76e2019-01-29 11:20:30 -0800583} // namespace android::nn