Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / engine_impl.h
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 #pragma once
19 #include "api/CPP/memory.hpp"
20 #include "api_impl.h"
21 #include "event_impl.h"
22 #include "refcounted_obj.h"
23 #include "implementation_map.h"
24 #include "memory_pool.h"
25 #include "gpu/engine_info.h"
26
27 #include <memory>
28 #include <set>
29
30 namespace cldnn {
31 namespace gpu { 
32     class gpu_toolkit;
33 }
34
35 class build_options;
36 using gpu_toolkit = gpu::gpu_toolkit;
37
38 struct memory_impl;
39 struct event_impl;
40 struct topology_impl;
41 struct program_impl;
42 struct network_impl;
43 struct program_node;
44
45 template <class>
46 struct typed_program_node;
47
48 struct engine_impl : public refcounted_obj<engine_impl>
49 {
50 public:
51     engine_impl(const engine_configuration& conf);
52     ~engine_impl();
53     engine_types type() const { return engine_types::ocl; }
54     refcounted_obj_ptr<memory_impl> allocate_memory(layout layout);
55     refcounted_obj_ptr<memory_impl> allocate_memory(layout layout, primitive_id, uint32_t, std::set<primitive_id>, bool reusable = true);
56     refcounted_obj_ptr<memory_impl> reinterpret_buffer(const memory_impl& memory, layout new_layout);
57     bool is_the_same_buffer(const memory_impl& mem1, const memory_impl& mem2);
58
59     refcounted_obj_ptr<event_impl> create_user_event(bool set = false);
60     void wait_for_events(std::vector<event_impl::ptr> const& events);
61
62     refcounted_obj_ptr<program_impl> build_program(const topology_impl& topology, const build_options& options, bool is_internal = false, bool no_optimizations = false);
63     refcounted_obj_ptr<program_impl> build_program(const std::set<std::shared_ptr<program_node>>& nodes, const build_options & options, bool is_internal);
64     void compile_program(program_impl& prog);
65
66     refcounted_obj_ptr<network_impl> allocate_network(const program_impl& program, bool is_internal = false);
67     refcounted_obj_ptr<network_impl> build_network(const topology_impl& topology, const build_options& options, bool is_internal = false);
68     refcounted_obj_ptr<network_impl> build_network(const std::set<std::shared_ptr<program_node>>& nodes, const build_options & options, bool is_internal);
69     void flush_network();
70     void release_pending_memory();
71
72     template <class T>
73     std::unique_ptr<primitive_impl> create_primitive_impl(typed_program_node<T> const& node)
74     {
75         if (&node.get_program().get_engine() != this)
76             throw std::invalid_argument("engine_impl::create_primitive_impl: program's engine does not match called engine");
77
78         auto factory = implementation_map<T>::get(type(), node);
79         return std::move(std::unique_ptr<primitive_impl>(factory(node)));
80     }
81
82     template <class T>
83     bool does_an_implementation_exist(typed_program_node<T> const& node)
84     {
85         if (&node.get_program().get_engine() != this)
86           throw std::invalid_argument("engine_impl::create_primitive_impl: program's engine does not match called engine");
87         return implementation_map<T>::check(type(), node);
88     }
89
90     template <class T>
91     bool does_possible_implementation_exist(typed_program_node<T> const& node)
92     {
93         if (&node.get_program().get_engine() != this)
94             throw std::invalid_argument("engine_impl::create_primitive_impl: program's engine does not match called engine");
95         return implementation_map<T>::check_io_eq(type(), node);
96     }
97
98     const engine_configuration& configuration() const { return _configuration; }
99     void set_mem_pool(bool flag) { _configuration.enable_memory_pool = flag; }
100     std::shared_ptr<gpu_toolkit> get_context() const { return _context; }
101     gpu::engine_info_internal get_engine_info() const;
102     memory_pool& get_memory_pool() { return _memory_pool; }
103
104     uint64_t get_max_used_device_memory() const { return _memory_pool.get_max_peak_device_memory_used(); }
105     uint64_t get_used_device_memory() const { return _memory_pool.get_temp_memory_used(); }
106
107     void dump_memory_pool(const program_impl& program, std::string path, std::string dependencies) { _memory_pool.dump_memory_pool(program, path, dependencies); }
108     bool use_memory_pool() const;
109
110 private:
111     engine_configuration _configuration;
112     std::shared_ptr<gpu_toolkit> _context;
113         memory_pool _memory_pool;
114 };
115 }
116
117 API_CAST(::cldnn_engine, cldnn::engine_impl)