2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
20 #include "cldnn_defs.h"
21 #include "compounds.h"
22 #include "primitive.hpp"
26 /// @addtogroup cpp_api C++ API
29 /// @defgroup cpp_topology Network Topology
32 /// @brief Network topology to be defined by user.
35 /// @brief Constructs empty network topology.
37 : _impl(check_status<cldnn_topology>("failed to create topology", cldnn_create_topology))
40 /// @brief Constructs topology containing primitives provided in argument(s).
41 template<class ...Args>
42 topology(const Args&... args)
45 add<Args...>(args...);
48 /// @brief Copy construction.
49 topology(const topology& other) :_impl(other._impl)
54 /// @brief Copy assignment.
55 topology& operator=(const topology& other)
57 if (_impl == other._impl) return *this;
64 /// Construct C++ topology based on C API @p cldnn_topology
65 topology(const cldnn_topology& other)
68 if (_impl == nullptr) throw std::invalid_argument("implementation pointer should not be null");
71 /// @brief Releases wrapped C API @ref cldnn_topology.
77 friend bool operator==(const topology& lhs, const topology& rhs) { return lhs._impl == rhs._impl; }
78 friend bool operator!=(const topology& lhs, const topology& rhs) { return !(lhs == rhs); }
80 /// @brief Adds a primitive to topology.
82 void add(PType const& desc)
84 check_status<void>("primitive add failed", [&](status_t* status) { cldnn_add_primitive(_impl, desc.get_dto(), status); });
87 /// @brief Adds primitives to topology.
88 template<class PType, class ...Args>
89 void add(PType const& desc, Args const&... args)
91 check_status<void>("primitive add failed", [&](status_t* status) { cldnn_add_primitive(_impl, desc.get_dto(), status); });
92 add<Args...>(args...);
95 /// @brief Returns wrapped C API @ref cldnn_topology.
96 cldnn_topology get() const { return _impl; }
98 const std::vector<primitive_id> get_primitive_ids() const
101 status_t err_invalid_arg = CLDNN_SUCCESS;
102 cldnn_get_primitive_ids(_impl, nullptr, 0, &size_ret, &err_invalid_arg);
103 assert(err_invalid_arg == CLDNN_INVALID_ARG);
104 assert(size_ret > 0);
105 std::vector<char> names_buf(size_ret);
107 check_status<void>("get topology ids failed", [&](status_t* status)
109 cldnn_get_primitive_ids(_impl, names_buf.data(), names_buf.size(), &size_ret, status);
111 assert(names_buf.size() == size_ret);
113 std::vector<primitive_id> result;
114 for (auto buf_ptr = names_buf.data(); *buf_ptr != 0; buf_ptr += result.back().size() + 1)
116 result.emplace_back(buf_ptr);
121 void change_input_layout(primitive_id id, layout new_layout)
123 check_status<void>("Change input layout failed.", [&](status_t* status)
125 cldnn_change_input_layout(_impl, id.c_str(), new_layout, status);
130 friend struct engine;
131 friend struct network;
132 cldnn_topology _impl;
136 check_status<void>("retain topology failed", [=](status_t* status) { cldnn_retain_topology(_impl, status); });
140 check_status<void>("retain topology failed", [=](status_t* status) { cldnn_release_topology(_impl, status); });
144 CLDNN_API_CLASS(topology)