Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / gna_plugin / gna_device.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include "gna-api-dumper.h"
8 #include "gna-api-instrumentation.h"
9 #include "ie_common.h"
10 #include <memory>
11 #include <string>
12 #include <map>
13 #include <thread>
14
15 /**
16  * holds gna - style handle in RAII way
17  */
18 class GNADeviceHelper {
19     intel_gna_status_t nGNAStatus = GNA_NOERROR;
20     intel_gna_handle_t nGNAHandle = 0;
21     intel_gna_proc_t nGNAProcType = GNA_AUTO;
22     intel_gna_perf_t nGNAPerfResults;
23     intel_gna_perf_t nGNAPerfResultsTotal;
24     const uint32_t GNA_TIMEOUT = MAX_TIMEOUT;
25     bool isPerformanceMeasuring;
26
27  public:
28     explicit GNADeviceHelper(intel_gna_proc_t proc_type = GNA_AUTO,
29                             uint8_t lib_async_n_threads = 1,
30                             bool use_openmp = false,
31                             bool isPerformanceMeasuring = false) :
32                                     nGNAProcType(proc_type),
33                                     isPerformanceMeasuring(isPerformanceMeasuring) {
34         initGnaPerfCounters();
35         open(lib_async_n_threads);
36
37         if (use_openmp) {
38             uint8_t num_cores = std::thread::hardware_concurrency();
39             setOMPThreads((num_cores != 0) ? num_cores : 1);
40         }
41     }
42
43     ~GNADeviceHelper() {
44         close();
45     }
46
47     uint8_t *alloc(uint32_t size_requested, uint32_t *size_granted);
48
49     void propagateSync(const intel_nnet_type_t *pNeuralNetwork,
50                        const uint32_t *pActiveIndices,
51                        uint32_t nActiveIndices);
52
53     uint32_t propagate(const intel_nnet_type_t *pNeuralNetwork,
54                        const uint32_t *pActiveIndices,
55                        uint32_t nActiveIndices);
56
57     void wait(uint32_t id);
58
59
60     struct DumpResult {
61         intel_gna_model_header header;
62         std::shared_ptr<void> model;
63     };
64
65     DumpResult dumpXnn(const intel_nnet_type_t *pNeuralNetwork,
66                  const uint32_t *pActiveIndices,
67                  uint32_t nActiveIndices);
68
69
70     void free() {
71         GNAFree(nGNAHandle);
72     }
73     void updateGnaPerfCounters();
74     void getGnaPerfCounters(std::map<std::string,
75                         InferenceEngine::InferenceEngineProfileInfo>& retPerfCounters);
76
77  private:
78     void open(uint8_t const n_threads);
79
80     void close();
81
82     void checkStatus() const;
83
84     void setOMPThreads(uint8_t const n_threads);
85
86     void initGnaPerfCounters() {
87         nGNAPerfResults = {{0, 0, 0, 0, 0, 0, 0}, {0, 0}, {0, 0, 0}, {0, 0}};
88         nGNAPerfResultsTotal = {{0, 0, 0, 0, 0, 0, 0}, {0, 0}, {0, 0, 0}, {0, 0}};
89     }
90 };
91