clDNN
topology.hpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
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 
18 #pragma once
19 #include <cstdint>
20 #include "cldnn_defs.h"
21 #include "compounds.h"
22 #include "primitive.hpp"
23 
24 namespace cldnn {
25 
28 
31 
33 struct topology
34 {
37  : _impl(check_status<cldnn_topology>("failed to create topology", cldnn_create_topology))
38  {}
39 
41  template<class ...Args>
42  topology(const Args&... args)
43  : topology()
44  {
45  add<Args...>(args...);
46  }
47 
49  topology(const topology& other) :_impl(other._impl)
50  {
51  retain();
52  }
53 
55  topology& operator=(const topology& other)
56  {
57  if (_impl == other._impl) return *this;
58  release();
59  _impl = other._impl;
60  retain();
61  return *this;
62  }
63 
66  {
67  release();
68  }
69 
70  friend bool operator==(const topology& lhs, const topology& rhs) { return lhs._impl == rhs._impl; }
71  friend bool operator!=(const topology& lhs, const topology& rhs) { return !(lhs == rhs); }
72 
74  template<class PType>
75  void add(PType const& desc)
76  {
77  check_status<void>("primitive add failed", [&](status_t* status) { cldnn_add_primitive(_impl, desc.get_dto(), status); });
78  }
79 
81  template<class PType, class ...Args>
82  void add(PType const& desc, Args const&... args)
83  {
84  check_status<void>("primitive add failed", [&](status_t* status) { cldnn_add_primitive(_impl, desc.get_dto(), status); });
85  add<Args...>(args...);
86  }
87 
89  cldnn_topology get() const { return _impl; }
90 
91  const std::vector<primitive_id> get_primitive_ids() const
92  {
93  size_t size_ret = 0;
94  status_t err_invalid_arg = CLDNN_SUCCESS;
95  cldnn_get_primitive_ids(_impl, nullptr, 0, &size_ret, &err_invalid_arg);
96  assert(err_invalid_arg == CLDNN_INVALID_ARG);
97  assert(size_ret > 0);
98  std::vector<char> names_buf(size_ret);
99 
100  check_status<void>("get topology ids failed", [&](status_t* status)
101  {
102  cldnn_get_primitive_ids(_impl, names_buf.data(), names_buf.size(), &size_ret, status);
103  });
104  assert(names_buf.size() == size_ret);
105 
106  std::vector<primitive_id> result;
107  for (auto buf_ptr = names_buf.data(); *buf_ptr != 0; buf_ptr += result.back().size() + 1)
108  {
109  result.emplace_back(buf_ptr);
110  }
111  return result;
112  }
113 
114 private:
115  friend struct engine;
116  friend struct network;
117  cldnn_topology _impl;
118 
119  topology(cldnn_topology impl) :_impl(impl)
120  {
121  if (_impl == nullptr) throw std::invalid_argument("implementation pointer should not be null");
122  }
123 
124  void retain()
125  {
126  check_status<void>("retain topology failed", [=](status_t* status) { cldnn_retain_topology(_impl, status); });
127  }
128  void release()
129  {
130  check_status<void>("retain topology failed", [=](status_t* status) { cldnn_release_topology(_impl, status); });
131  }
132 };
133 
134 CLDNN_API_CLASS(topology)
137 }
~topology()
Releases wrapped C API cldnn_topology.
Definition: topology.hpp:65
void add(PType const &desc, Args const &... args)
Adds primitives to topology.
Definition: topology.hpp:82
CLDNN_API void cldnn_retain_topology(cldnn_topology topology, cldnn_status *status)
Increment reference counter for the topology object.
CLDNN_API void cldnn_add_primitive(cldnn_topology topology, const struct cldnn_primitive_desc *dto, cldnn_status *status)
Add new primitive to the topology.
CLDNN_API void cldnn_release_topology(cldnn_topology topology, cldnn_status *status)
Decrement reference counter for the topology object. Deletes object when counter becomes zero...
struct cldnn_topology_impl * cldnn_topology
Network topology to be defined by user.
Definition: cldnn.h:107
Network topology to be defined by user.
Definition: topology.hpp:33
topology & operator=(const topology &other)
Copy assignment.
Definition: topology.hpp:55
void add(PType const &desc)
Adds a primitive to topology.
Definition: topology.hpp:75
CLDNN_API cldnn_topology cldnn_create_topology(cldnn_status *status)
Create empty network topology.
topology(const topology &other)
Copy construction.
Definition: topology.hpp:49
topology(const Args &... args)
Constructs topology containing primitives provided in argument(s).
Definition: topology.hpp:42
CLDNN_API void cldnn_get_primitive_ids(cldnn_topology topology, char *ids, size_t size, size_t *size_ret, cldnn_status *status)
Return all primitives id from topology.
topology()
Constructs empty network topology.
Definition: topology.hpp:36