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 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "api/CPP/memory.hpp"
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"
36 using gpu_toolkit = gpu::gpu_toolkit;
46 struct typed_program_node;
48 struct engine_impl : public refcounted_obj<engine_impl>
51 engine_impl(const engine_configuration& conf);
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);
59 refcounted_obj_ptr<event_impl> create_user_event(bool set = false);
60 void wait_for_events(std::vector<event_impl::ptr> const& events);
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);
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);
70 void release_pending_memory();
73 std::unique_ptr<primitive_impl> create_primitive_impl(typed_program_node<T> const& node)
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");
78 auto factory = implementation_map<T>::get(type(), node);
79 return std::move(std::unique_ptr<primitive_impl>(factory(node)));
83 bool does_an_implementation_exist(typed_program_node<T> const& node)
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);
91 bool does_possible_implementation_exist(typed_program_node<T> const& node)
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);
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; }
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(); }
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;
111 engine_configuration _configuration;
112 std::shared_ptr<gpu_toolkit> _context;
113 memory_pool _memory_pool;
117 API_CAST(::cldnn_engine, cldnn::engine_impl)