Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / gna_plugin / gna_device.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "gna_device.hpp"
6
7 #include <map>
8 #include <string>
9 #include <cstring>
10
11 #include "gna-api-status.h"
12 #include "gna-api.h"
13
14 #include "details/ie_exception.hpp"
15 #include "gna_plugin_log.hpp"
16 #include "gna/gna_config.hpp"
17
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));
20 }
21
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));
26 }
27
28 uint32_t GNADeviceHelper::propagate(const intel_nnet_type_t *pNeuralNetwork,
29                    const uint32_t *pActiveIndices,
30                    uint32_t nActiveIndices) {
31     uint32_t reqId;
32     nGNAStatus = GNAPropagateForward(nGNAHandle, pNeuralNetwork,
33                                      pActiveIndices, nActiveIndices, &reqId, nGNAProcType);
34     checkStatus();
35     return reqId;
36 }
37
38 void GNADeviceHelper::wait(uint32_t reqId) {
39     if (isPerformanceMeasuring) {
40         nGNAStatus = GNAWaitPerfRes(nGNAHandle, GNA_TIMEOUT, reqId, &nGNAPerfResults);
41         updateGnaPerfCounters();
42     } else {
43         nGNAStatus = GNAWait(nGNAHandle, 1000000, reqId);
44     }
45     checkStatus();
46 }
47
48 GNADeviceHelper::DumpResult GNADeviceHelper::dumpXnn(const intel_nnet_type_t *pNeuralNetwork,
49                                     const uint32_t *pActiveIndices,
50                                     uint32_t nActiveIndices) {
51     DumpResult r;
52     intel_gna_status_t gna_status;
53
54     if (!pNeuralNetwork) {
55         THROW_GNA_EXCEPTION<< "GNADumpXnn got invalid NeuralNetwork parameter \n";
56     }
57     r.model.reset(GNADumpXnn(pNeuralNetwork,
58                              pActiveIndices,
59                              nActiveIndices,
60                              &r.header,
61                              &nGNAStatus,
62                              [](size_t count)-> void* {return ::operator new(count);}),
63                              [](void * ptr) {::operator delete(ptr);});
64
65     checkStatus();
66
67     if (r.model == nullptr) {
68         THROW_GNA_EXCEPTION << "GNADumpXnn returned nullptr";
69     }
70
71     return r;
72 }
73
74 void GNADeviceHelper::checkStatus() const {
75     if ((nGNAStatus != GNA_NOERROR) && (nGNAStatus != GNA_SSATURATE)) {
76         THROW_GNA_EXCEPTION << "Bad GNA status " << nGNAStatus << ", " << GNAStatusName[nGNAStatus];
77     }
78 }
79
80 void GNADeviceHelper::open(uint8_t n_threads) {
81     nGNAHandle = GNADeviceOpenSetThreads(&nGNAStatus, n_threads);
82
83     checkStatus();
84 }
85
86 void GNADeviceHelper::close() {
87     GNADeviceClose(nGNAHandle);
88     nGNAHandle = 0;
89 }
90
91 void GNADeviceHelper::setOMPThreads(uint8_t const n_threads) {
92     gmmSetThreads(n_threads);
93 }
94
95 void GNADeviceHelper::updateGnaPerfCounters() {
96     nGNAPerfResultsTotal.hw.stall = nGNAPerfResults.hw.stall;
97     nGNAPerfResultsTotal.hw.total = nGNAPerfResults.hw.total;
98
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;
106
107     nGNAPerfResultsTotal.total.start = nGNAPerfResults.total.start;
108     nGNAPerfResultsTotal.total.stop = nGNAPerfResults.total.stop;
109
110     nGNAPerfResultsTotal.drv.startHW = nGNAPerfResults.drv.startHW;
111     nGNAPerfResultsTotal.drv.scoreHW = nGNAPerfResults.drv.scoreHW;
112     nGNAPerfResultsTotal.drv.intProc = nGNAPerfResults.drv.intProc;
113 }
114
115 void GNADeviceHelper::getGnaPerfCounters(std::map<std::string, InferenceEngine::InferenceEngineProfileInfo>& retPerfCounters) {
116     InferenceEngine::InferenceEngineProfileInfo info;
117     info.status = InferenceEngine::InferenceEngineProfileInfo::EXECUTED;
118
119     // Hardware
120     info.realTime_uSec = nGNAPerfResultsTotal.hw.total;
121     retPerfCounters["1.1 Total scoring time in HW"] = info;
122
123     info.realTime_uSec = nGNAPerfResultsTotal.hw.stall;
124     retPerfCounters["1.2 Stall scoring time in HW"] = info;
125 }