clDNN
network.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 "cldnn_defs.h"
20 #include "compounds.h"
21 #include "memory.hpp"
22 #include "program.hpp"
23 #include "event.hpp"
24 
25 #include <cstdint>
26 #include <algorithm>
27 #include <map>
28 
29 namespace cldnn
30 {
31 
34 
37 
40 {
42  event get_event() const { return _event; }
43 
46  {
47  _event.wait();
48  return _result;
49  }
50 private:
51  event _event;
52  memory _result;
53  network_output(event evt, memory mem): _event(evt), _result(mem){}
54  network_output(cldnn_event evt, cldnn_memory mem): _event(evt), _result(mem){}
55  friend struct network;
56 };
57 
59 struct network
60 {
64  :_impl(check_status<cldnn_network>("network allocation failed", [&](status_t* status)
65  {
66  return cldnn_allocate_network(program.get(), status);
67  }))
68  {}
69 
74  network(const engine& engine, const topology& topology, const build_options& options = build_options())
75  :network(program(engine, topology, options))
76  {}
77 
79  network(cldnn_network impl) :_impl(impl)
80  {
81  if (_impl == nullptr) throw std::invalid_argument("implementation pointer should not be null");
82  }
83 
85  network(const network& other) :_impl(other._impl)
86  {
87  retain();
88  }
89 
91  network& operator=(const network& other)
92  {
93  if (_impl == other._impl) return *this;
94  release();
95  _impl = other._impl;
96  retain();
97  return *this;
98  }
99 
102  {
103  release();
104  }
105 
106  friend bool operator==(const network& lhs, const network& rhs) { return lhs._impl == rhs._impl; }
107  friend bool operator!=(const network& lhs, const network& rhs) { return !(lhs == rhs); }
108 
111  {
112  return check_status<cldnn_engine>("get network engine failed", [&](status_t* status) { return cldnn_get_network_engine(_impl, status); });
113  }
114 
117  {
118  return check_status<cldnn_program>("get network program failed", [&](status_t* status) { return cldnn_get_network_program(_impl, status); });
119  }
120 
122  void set_input_data(const primitive_id& id, const memory& mem) const
123  {
124  check_status<void>("set network input failed", [&](status_t* status) { cldnn_set_network_input(_impl, id.c_str(), mem.get(), status); });
125  }
126 
127 
128  std::string get_primitive_info(const primitive_id& id) const
129  {
130  size_t size_ret = 0;
131  status_t err_invalid_arg = CLDNN_SUCCESS;
132 
133  cldnn_get_primitive_info(_impl, id.c_str(), nullptr, 0, &size_ret, &err_invalid_arg);
134  assert(err_invalid_arg == CLDNN_INVALID_ARG);
135  assert(size_ret > 0);
136  std::vector<char> names_buf(size_ret);
137 
138  check_status<void>("get primitive info failed", [&](status_t* status)
139  {
140  cldnn_get_primitive_info(_impl, id.c_str(), names_buf.data(), names_buf.size(), &size_ret, status);
141  });
142  assert(names_buf.size() == size_ret);
143 
144  std::string result(names_buf.begin(), names_buf.end());
145  return result;
146  }
147 
149  std::vector<primitive_id> get_executed_primitive_ids() const
150  {
151  return get_prim_ids(cldnn_get_network_executed_primitive_names);
152  }
153 
155  std::vector<primitive_id> get_all_primitive_ids() const
156  {
157  return get_prim_ids(cldnn_get_network_all_primitive_names);
158  }
159 
161  std::vector<primitive_id> get_all_primitive_org_ids() const
162  {
163  return get_prim_ids(cldnn_get_network_all_primitive_org_names);
164  }
165 
167  std::vector<primitive_id> get_output_ids() const
168  {
169  return get_prim_ids(cldnn_get_network_output_names);
170  }
171 
173  network_output get_output(const primitive_id& output_id) const
174  {
175  cldnn_network_output output =
176  check_status<cldnn_network_output>("get network output failed", [&](status_t* status)
177  {
178  return cldnn_get_network_output(_impl, output_id.c_str(), status);
179  });
180  return network_output( output.event, output.memory );
181  }
182 
184  memory get_output_memory(const primitive_id& output_id) const
185  {
186  cldnn_memory output =
187  check_status<cldnn_memory>("get output memory failed", [&](status_t* status)
188  {
189  return cldnn_get_network_output_memory(_impl, output_id.c_str(), status);
190  });
191  return output;
192  }
193 
195  event get_primitive_event(const primitive_id& output_id) const
196  {
197  cldnn_event output =
198  check_status<cldnn_event>("get output event failed", [&](status_t* status)
199  {
200  return cldnn_get_network_output_event(_impl, output_id.c_str(), status);
201  });
202  return output;
203  }
204 
206  std::map<primitive_id, event> get_executed_primitives() const
207  {
208  auto primitive_ids = get_executed_primitive_ids();
209  auto all_primitive_ids = get_all_primitive_ids();
210  auto all_primitive_org_ids = get_all_primitive_org_ids();
211  //Get list of optimized prmitives
212  std::vector<primitive_id> optimized_primitives;
213  for (decltype(all_primitive_org_ids.size()) i = 0; i < all_primitive_org_ids.size(); i++)
214  {
215  if (all_primitive_ids[i] == "_optimized_")
216  optimized_primitives.push_back(all_primitive_org_ids[i]);
217  }
218  std::map<primitive_id, event> result;
219  for (auto& id : primitive_ids)
220  {
221  if(std::find(optimized_primitives.begin(), optimized_primitives.end(), id) == optimized_primitives.end())
222  result.emplace(id, get_primitive_event(id));
223  }
224  return result;
225  }
226 
230  std::map<primitive_id, primitive_id> get_all_primitives() const
231  {
232  auto primitive_ids = get_all_primitive_ids();
233  auto primitive_org_ids = get_all_primitive_org_ids();
234  std::map<primitive_id, primitive_id> result;
235  for (decltype(primitive_org_ids.size()) i = 0; i < primitive_org_ids.size(); i++)
236  {
237  result.emplace(primitive_org_ids[i], primitive_ids[i]);
238  }
239  return result;
240  }
241 
246  std::map<primitive_id, network_output> execute(const std::vector<event>& dependencies = {}) const
247  {
248  std::vector<cldnn_event> dep_refs(dependencies.size());
249  for(decltype(dependencies.size()) i = 0; i < dependencies.size(); i++)
250  {
251  dep_refs[i] = dependencies[i].get();
252  }
253 
254  check_status<void>("network execute failed", [&](status_t* status)
255  {
256  return cldnn_execute_network(_impl, dep_refs.data(), dep_refs.size(), status);
257  });
258 
259  auto output_ids = get_output_ids();
260  std::map<primitive_id, network_output> result;
261  for(auto& id : output_ids)
262  {
263  result.emplace(id, get_output(id));
264  }
265  return result;
266  }
267 
269  cldnn_network get() const { return _impl; }
270 
271 private:
272  cldnn_network _impl;
273 
274  typedef void(*get_prim_ids_func_t)(cldnn_network network, char* names, size_t size, size_t* size_ret, cldnn_status* status);
275 
276  void retain()
277  {
278  check_status<void>("retain topology failed", [=](status_t* status) { cldnn_retain_network(_impl, status); });
279  }
280  void release()
281  {
282  check_status<void>("retain topology failed", [=](status_t* status) { cldnn_release_network(_impl, status); });
283  }
284 
285  std::vector<primitive_id> get_prim_ids(get_prim_ids_func_t func) const
286  {
287  size_t size_ret = 0;
288  status_t err_invalid_arg = CLDNN_SUCCESS;
289  func(_impl, nullptr, 0, &size_ret, &err_invalid_arg);
290  assert(err_invalid_arg == CLDNN_INVALID_ARG);
291  assert(size_ret > 0);
292  std::vector<char> names_buf(size_ret);
293 
294  check_status<void>("get network output ids failed", [&](status_t* status)
295  {
296  func(_impl, names_buf.data(), names_buf.size(), &size_ret, status);
297  });
298  assert(names_buf.size() == size_ret);
299 
300  std::vector<primitive_id> result;
301  for (auto buf_ptr = names_buf.data(); *buf_ptr != 0; buf_ptr += result.back().size() + 1)
302  {
303  result.emplace_back(buf_ptr);
304  }
305  return result;
306  }
307 
308 };
309 CLDNN_API_CLASS(network)
312 }
cldnn_memory get() const
C API memory handle.
Definition: memory.hpp:139
CLDNN_API void cldnn_get_network_output_names(cldnn_network network, char *names, size_t size, size_t *size_ret, cldnn_status *status)
Returns names of network outputs.
cldnn_memory memory
Definition: cldnn.h:215
Represents an clDNN Event object.
Definition: event.hpp:35
CLDNN_API void cldnn_get_network_all_primitive_names(cldnn_network network, char *names, size_t size, size_t *size_ret, cldnn_status *status)
Returns names of all primitives in network.
Represents program build options list.
Definition: program.hpp:399
program get_program() const
Returns network internal program.
Definition: network.hpp:116
event get_primitive_event(const primitive_id &output_id) const
Returns event object for particular primitive. Can&#39;t be called before network execution.
Definition: network.hpp:195
CLDNN_API void cldnn_get_network_all_primitive_org_names(cldnn_network network, char *names, size_t size, size_t *size_ret, cldnn_status *status)
Returns names of all primitives in network before graph optimization.
std::map< primitive_id, network_output > execute(const std::vector< event > &dependencies={}) const
Executes network and returns the list of network_output.
Definition: network.hpp:246
CLDNN_API void cldnn_release_network(cldnn_network network, cldnn_status *status)
Decrement reference counter for the network object. Deletes object when counter becomes zero...
void set_input_data(const primitive_id &id, const memory &mem) const
Provides memory for input_layout primitives defined by user in source topology.
Definition: network.hpp:122
CLDNN_API cldnn_memory cldnn_get_network_output_memory(cldnn_network network, const char *name, cldnn_status *status)
Returns memory corresponding to output with name.
std::map< primitive_id, event > get_executed_primitives() const
Returns the list of event for the primitives that were executed in network.
Definition: network.hpp:206
network(cldnn_network impl)
Constructs network object from C API cldnn_network.
Definition: network.hpp:79
Compiled program build from topology by engine.
Definition: program.hpp:497
CLDNN_API void cldnn_get_network_executed_primitive_names(cldnn_network network, char *names, size_t size, size_t *size_ret, cldnn_status *status)
Returns names of executed primitives.
Represents network output returned by network::get_output().
Definition: network.hpp:39
network(const engine &engine, const topology &topology, const build_options &options=build_options())
Constructs network object from implicitly created program object. This is a shorthand for network(pro...
Definition: network.hpp:74
CLDNN_API void cldnn_retain_network(cldnn_network network, cldnn_status *status)
Increment reference counter for the network object.
std::map< primitive_id, primitive_id > get_all_primitives() const
Returns the list of primitive ids before and after graph optimization.
Definition: network.hpp:230
std::vector< primitive_id > get_all_primitive_org_ids() const
Returns the list of all primitives ids in network before graph optimization.
Definition: network.hpp:161
network & operator=(const network &other)
Copy assignment.
Definition: network.hpp:91
CLDNN_API cldnn_event cldnn_get_network_output_event(cldnn_network network, const char *name, cldnn_status *status)
Returns event corresponding to output with name.
memory get_memory() const
Returns memory object of the output. Blocked until associated event is not complete.
Definition: network.hpp:45
~network()
Releases wrapped C API cldnn_network.
Definition: network.hpp:101
struct cldnn_event_impl * cldnn_event
Event object.
Definition: cldnn.h:103
int32_t cldnn_status
Represents errors status for all API calls.
Definition: cldnn.h:82
engine get_engine() const
Returns engine by which network was built.
Definition: network.hpp:110
struct cldnn_memory_impl * cldnn_memory
Memory object.
Definition: cldnn.h:119
network_output get_output(const primitive_id &output_id) const
Returns network_output object for particular output. Can&#39;t be called before network execution...
Definition: network.hpp:173
CLDNN_API void cldnn_execute_network(cldnn_network network, cldnn_event *dependencies, size_t deps_num, cldnn_status *status)
Executes network.
network(program const &program)
Allocate network.
Definition: network.hpp:63
void wait() const
Wait for event completion.
Definition: event.hpp:72
CLDNN_API cldnn_engine cldnn_get_network_engine(cldnn_network network, cldnn_status *status)
Returns engine associated with the network.
Network topology to be defined by user.
Definition: topology.hpp:33
std::vector< primitive_id > get_all_primitive_ids() const
Returns the list of all primitives ids in network.
Definition: network.hpp:155
cldnn_event event
Event to be waited.
Definition: cldnn.h:214
CLDNN_API void cldnn_get_primitive_info(cldnn_network network, cldnn_primitive_id id, char *info, size_t size, size_t *size_ret, cldnn_status *status)
Returns information about particular primitive.
Executable network allocated from program.
Definition: network.hpp:59
std::vector< primitive_id > get_executed_primitive_ids() const
Returns the list of executed primitives.
Definition: network.hpp:149
std::string primitive_id
Unique id of a primitive within a topology.
Definition: primitive.hpp:42
memory get_output_memory(const primitive_id &output_id) const
Returns memory object for particular output. Can be called before network execution.
Definition: network.hpp:184
std::vector< primitive_id > get_output_ids() const
Returns the list of available network outputs.
Definition: network.hpp:167
::cldnn_program get() const
Returns wrapped C API cldnn_program handler.
Definition: program.hpp:543
Represents clDNN engine object.
Definition: engine.hpp:110
network(const network &other)
Copy construction.
Definition: network.hpp:85
CLDNN_API cldnn_network cldnn_allocate_network(cldnn_program program, cldnn_status *status)
Allocates memory for a new network which will be able to execute specified program.
CLDNN_API cldnn_network_output cldnn_get_network_output(cldnn_network network, const char *name, cldnn_status *status)
Returns executed network output information.
Represents buffer with particular layout.
Definition: memory.hpp:42
Output information for executed cldnn_network.
Definition: cldnn.h:212
event get_event() const
Returns event associated with the output.
Definition: network.hpp:42
CLDNN_API cldnn_program cldnn_get_network_program(cldnn_network network, cldnn_status *status)
Returns program associated with the network.
struct cldnn_network_impl * cldnn_network
Executable network allocated from cldnn_program.
Definition: cldnn.h:115
CLDNN_API void cldnn_set_network_input(cldnn_network network, cldnn_primitive_id id, cldnn_memory mem, cldnn_status *status)
Provides user input data to the network (for input_layout primitives).