Publishing 2019 R3 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 #include <string>
24 #include <memory>
25 #include <algorithm>
26
27 namespace cldnn {
28 primitive_type_id data::type_id() {
29     static primitive_type_base<data> instance;
30     return &instance;
31 }
32
33 namespace {
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;
38
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());
43     return result;
44 }
45 }  // namespace
46
47 data_node::typed_program_node(const std::shared_ptr<data> dprim, program_impl& prog)
48     : parent(dprim, prog), mem(dprim->mem.get()) {
49     constant = true;
50     can_share_buffer(false);
51     recalc_output_layout(false);
52 }
53
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);
57 }
58
59 std::string data_inst::to_string(data_node const& node) {
60     auto node_info = node.desc_to_json();
61
62     std::stringstream primitive_description;
63
64     node_info->dump(primitive_description);
65     return primitive_description.str();
66 }
67
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())) {}
70
71 }  // namespace cldnn