Intel clGPU
dispatcher.hpp
1 // Copyright (c) 2017-2018 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include "errors.hpp"
17 #include "functions_base.hpp"
18 #include "engine.hpp"
19 #include "context.hpp"
20 #include <vector>
21 #include <memory>
22 #include <stdexcept>
23 
24 namespace iclgpu
25 {
28 
30 class dispatcher : public context::element<dispatcher>
31 {
32 public:
33  using element::element;
34 
37  template <class Func>
38  functions::scored_impls_list<Func> select(typename Func::params& params) const
39  {
40  auto selector = context()->get<typename Func::selector>();
41  return selector->select(params);
42  }
43 
49  template <class Func>
50  typename std::enable_if<std::is_base_of<functions::function_impl_execute<Func>, typename Func::impl>::value,
51  std::shared_ptr<event>>::type
52  execute_function(typename Func::params& params, const std::vector<std::shared_ptr<event>>& dep_events = {}) const
53  {
54  auto impls = select<Func>(params);
55  if (impls.size() == 0)
56  {
57  throw error_unsupported("Function parameters are not supported");
58  }
59 
60  return impls[0].second->execute(params, dep_events);
61  }
62 
68  template <class Func>
69  typename std::enable_if<std::is_base_of<functions::function_impl_command<Func>, typename Func::impl>::value,
70  std::shared_ptr<event>>::type
71  execute_function(typename Func::params& params, const std::vector<std::shared_ptr<event>>& dep_events = {}) const
72  {
73  auto impls = select<Func>(params);
74  if (impls.size() == 0)
75  {
76  throw error_unsupported("Function parameters are not supported");
77  }
78 
79  auto cmd_builder = impls[0].second->selected();
80  if (!cmd_builder) throw std::logic_error("selected() is not implemented.");
81  auto cmd = cmd_builder(params);
82  return cmd->submit(dep_events);
83  }
84 };
85 
87 } // namespace iclgpu
functions::scored_impls_list< Func > select(typename Func::params &params) const
Returns list of function implementations which accept specified function parameters.
Definition: dispatcher.hpp:38
std::enable_if< std::is_base_of< functions::function_impl_command< Func >, typename Func::impl >::value, std::shared_ptr< event > >::type execute_function(typename Func::params &params, const std::vector< std::shared_ptr< event >> &dep_events={}) const
Executes a function.
Definition: dispatcher.hpp:71
Global library context provides access to all library objects.
Definition: context.hpp:34
Exception type indicates that provided actual function parameters are not supported.
Definition: errors.hpp:32
Function implementations dispatcher.
Definition: dispatcher.hpp:30
std::enable_if< std::is_base_of< functions::function_impl_execute< Func >, typename Func::impl >::value, std::shared_ptr< event > >::type execute_function(typename Func::params &params, const std::vector< std::shared_ptr< event >> &dep_events={}) const
Executes a function.
Definition: dispatcher.hpp:52
std::enable_if< std::is_base_of< element_base, T >::value, std::shared_ptr< T > >::type get(Args &&...args)
Construct or get an object owned by container (singleton lifetime).
Definition: container.hpp:78