Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / gna_plugin / gna_api_wrapper.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-types-xnn.h>
8 #include "gna_plugin_log.hpp"
9 namespace GNAPluginNS {
10
11 /**
12  * represent wrapper that capable to exception save pass c-objects
13  * @tparam T
14  */
15 template <class T>
16 class CPPWrapper {
17 };
18
19 template <>
20 class CPPWrapper<intel_nnet_type_t> {
21  public:
22     intel_nnet_type_t obj;
23
24     CPPWrapper() {
25         obj.nLayers = 0;
26         obj.pLayers = nullptr;
27         obj.nGroup = 0;
28     }
29
30     /**
31      * creates nnet structure of n layers
32      * @param n - number  of layers
33      */
34     explicit CPPWrapper(size_t n) {
35         if (n == 0) {
36             THROW_GNA_EXCEPTION << "Can't allocate array of intel_nnet_layer_t objects of zero length";
37         }
38         obj.pLayers = reinterpret_cast<intel_nnet_layer_t *>(_mm_malloc(n * sizeof(intel_nnet_layer_t), 64));
39         if (obj.pLayers == nullptr) {
40             THROW_GNA_EXCEPTION << "out of memory in while allocating "<< n << " GNA layers";
41         }
42         obj.nLayers = n;
43         for (int i = 0; i < obj.nLayers; i++) {
44             obj.pLayers[i].pLayerStruct = nullptr;
45         }
46     }
47     ~CPPWrapper() {
48         for (int i = 0; i < obj.nLayers; i++) {
49             if (obj.pLayers[i].pLayerStruct != nullptr) {
50                 _mm_free(obj.pLayers[i].pLayerStruct);
51             }
52         }
53         _mm_free(obj.pLayers);
54     }
55     intel_nnet_type_t * operator ->() {
56         return &obj;
57     }
58     intel_nnet_type_t * operator *() {
59         return &obj;
60     }
61     operator  intel_nnet_type_t &() {
62         return *this;
63     }
64 };
65
66 }  // namespace GNAPluginNS