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 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "data_inst.h"
19 #include "primitive_type_base.h"
20 #include "memory_impl.h"
22 #include "json_object.h"
28 primitive_type_id data::type_id() {
29 static primitive_type_base<data> instance;
34 memory_impl::ptr attach_or_copy_data(network_impl& network, memory_impl& mem) {
35 auto& engine = network.get_engine();
36 if (mem.is_allocated_by(engine))
37 return (memory_impl::ptr) &mem;
39 memory_impl::ptr result = engine.allocate_memory(mem.get_layout(), network.get_stream_id());
40 mem_lock<char> src(mem);
41 mem_lock<char> dst(result);
42 std::copy(src.begin(), src.end(), dst.begin());
47 data_node::typed_program_node(const std::shared_ptr<data> dprim, program_impl& prog)
48 : parent(dprim, prog), mem(dprim->mem.get()) {
50 can_share_buffer(false);
51 recalc_output_layout(false);
54 void data_node::attach_memory(memory_impl& new_mem, bool invalidate_users_if_changed) {
55 mem = (memory_impl::ptr) &new_mem;
56 recalc_output_layout(invalidate_users_if_changed);
59 std::string data_inst::to_string(data_node const& node) {
60 auto node_info = node.desc_to_json();
62 std::stringstream primitive_description;
64 node_info->dump(primitive_description);
65 return primitive_description.str();
68 data_inst::typed_primitive_inst(network_impl& network, data_node const& node)
69 : parent(network, node, *attach_or_copy_data(network, node.get_attached_memory())) {}