1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include "gna_device.hpp"
11 #include "gna-api-status.h"
14 #include "details/ie_exception.hpp"
15 #include "gna_plugin_log.hpp"
16 #include "gna/gna_config.hpp"
18 uint8_t* GNADeviceHelper::alloc(uint32_t size_requested, uint32_t *size_granted) {
19 return reinterpret_cast<uint8_t *>(GNAAlloc(nGNAHandle, size_requested, size_granted));
22 void GNADeviceHelper::propagateSync(const intel_nnet_type_t *pNeuralNetwork,
23 const uint32_t *pActiveIndices,
24 uint32_t nActiveIndices) {
25 wait(propagate(pNeuralNetwork, pActiveIndices, nActiveIndices));
28 uint32_t GNADeviceHelper::propagate(const intel_nnet_type_t *pNeuralNetwork,
29 const uint32_t *pActiveIndices,
30 uint32_t nActiveIndices) {
32 nGNAStatus = GNAPropagateForward(nGNAHandle, pNeuralNetwork,
33 pActiveIndices, nActiveIndices, &reqId, nGNAProcType);
38 void GNADeviceHelper::wait(uint32_t reqId) {
39 if (isPerformanceMeasuring) {
40 nGNAStatus = GNAWaitPerfRes(nGNAHandle, GNA_TIMEOUT, reqId, &nGNAPerfResults);
41 updateGnaPerfCounters();
43 nGNAStatus = GNAWait(nGNAHandle, 1000000, reqId);
48 GNADeviceHelper::DumpResult GNADeviceHelper::dumpXnn(const intel_nnet_type_t *pNeuralNetwork,
49 const uint32_t *pActiveIndices,
50 uint32_t nActiveIndices) {
52 intel_gna_status_t gna_status;
54 if (!pNeuralNetwork) {
55 THROW_GNA_EXCEPTION<< "GNADumpXnn got invalid NeuralNetwork parameter \n";
57 r.model.reset(GNADumpXnn(pNeuralNetwork,
62 [](size_t count)-> void* {return ::operator new(count);}),
63 [](void * ptr) {::operator delete(ptr);});
67 if (r.model == nullptr) {
68 THROW_GNA_EXCEPTION << "GNADumpXnn returned nullptr";
74 void GNADeviceHelper::checkStatus() const {
75 if ((nGNAStatus != GNA_NOERROR) && (nGNAStatus != GNA_SSATURATE)) {
76 THROW_GNA_EXCEPTION << "Bad GNA status " << nGNAStatus << ", " << GNAStatusName[nGNAStatus];
80 void GNADeviceHelper::open(uint8_t n_threads) {
81 nGNAHandle = GNADeviceOpenSetThreads(&nGNAStatus, n_threads);
86 void GNADeviceHelper::close() {
87 GNADeviceClose(nGNAHandle);
91 void GNADeviceHelper::setOMPThreads(uint8_t const n_threads) {
92 gmmSetThreads(n_threads);
95 void GNADeviceHelper::updateGnaPerfCounters() {
96 nGNAPerfResultsTotal.hw.stall = nGNAPerfResults.hw.stall;
97 nGNAPerfResultsTotal.hw.total = nGNAPerfResults.hw.total;
99 nGNAPerfResultsTotal.lib.submit = nGNAPerfResults.lib.submit;
100 nGNAPerfResultsTotal.lib.preprocess = nGNAPerfResults.lib.preprocess;
101 nGNAPerfResultsTotal.lib.process = nGNAPerfResults.lib.process;
102 nGNAPerfResultsTotal.lib.scoring = nGNAPerfResults.lib.scoring;
103 nGNAPerfResultsTotal.lib.total = nGNAPerfResults.lib.total;
104 nGNAPerfResultsTotal.lib.ioctlSubmit = nGNAPerfResults.lib.ioctlSubmit;
105 nGNAPerfResultsTotal.lib.ioctlWaitOn = nGNAPerfResults.lib.ioctlWaitOn;
107 nGNAPerfResultsTotal.total.start = nGNAPerfResults.total.start;
108 nGNAPerfResultsTotal.total.stop = nGNAPerfResults.total.stop;
110 nGNAPerfResultsTotal.drv.startHW = nGNAPerfResults.drv.startHW;
111 nGNAPerfResultsTotal.drv.scoreHW = nGNAPerfResults.drv.scoreHW;
112 nGNAPerfResultsTotal.drv.intProc = nGNAPerfResults.drv.intProc;
115 void GNADeviceHelper::getGnaPerfCounters(std::map<std::string, InferenceEngine::InferenceEngineProfileInfo>& retPerfCounters) {
116 InferenceEngine::InferenceEngineProfileInfo info;
117 info.status = InferenceEngine::InferenceEngineProfileInfo::EXECUTED;
120 info.realTime_uSec = nGNAPerfResultsTotal.hw.total;
121 retPerfCounters["1.1 Total scoring time in HW"] = info;
123 info.realTime_uSec = nGNAPerfResultsTotal.hw.stall;
124 retPerfCounters["1.2 Stall scoring time in HW"] = info;