Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / data.cpp
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
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "data_inst.h"
19 #include "primitive_type_base.h"
20 #include "memory_impl.h"
21
22 #include "json_object.h"
23
24 namespace cldnn
25 {
26 primitive_type_id data_type_id()
27 {
28     static primitive_type_base<data> instance;
29     return &instance;
30 }
31
32 namespace {
33     memory_impl::ptr attach_or_copy_data(network_impl& network, memory_impl& mem)
34     {
35         auto& engine = network.get_engine();
36         if (mem.is_allocated_by(engine))
37             return &mem;
38
39         memory_impl::ptr result = engine.allocate_memory(mem.get_layout());
40         mem_lock<char> src(mem);
41         mem_lock<char> dst(result);
42         std::copy(src.begin(), src.end(), dst.begin());
43         return result;
44     }
45 }
46
47 data_node::typed_program_node(const std::shared_ptr<data> dprim, program_impl& prog)
48     : parent(dprim, prog), mem(api_cast(dprim->mem.get()))
49 {
50     constant = true;
51     can_share_buffer(false);
52     recalc_output_layout(false);
53 }
54
55 void data_node::attach_memory(memory_impl& new_mem, bool invalidate_users_if_changed)
56 {
57     mem = &new_mem;
58     recalc_output_layout(invalidate_users_if_changed);
59 }
60
61 std::string data_inst::to_string(data_node const& node)
62 {
63     auto node_info = node.desc_to_json();
64
65     std::stringstream primitive_description;
66     
67     node_info->dump(primitive_description);
68     return primitive_description.str();
69 }
70
71 data_inst::typed_primitive_inst(network_impl& network, data_node const& node)
72     : parent(network, node, *attach_or_copy_data(network, node.get_attached_memory()))
73 {
74 }
75
76 }