Turi Create  4.0
optimization_engine.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_SFRAME_QUERY_OPTIMIZATION_ENGINE_H_
7 #define TURI_SFRAME_QUERY_OPTIMIZATION_ENGINE_H_
8 
9 #include <core/storage/query_engine/planning/planner_node.hpp>
10 #include <core/storage/query_engine/planning/materialize_options.hpp>
11 #include <core/storage/query_engine/planning/optimization_node_info.hpp>
12 
13 namespace turi { namespace query_eval {
14 
15 class opt_transform;
16 
17 /** A registry of the different transforms in an indexed location.
18  * Built once at the beginning of the program.
19  */
21 
22  /** Called first to set the number of stages.
23  */
24  void set_num_stages(size_t n);
25 
26  /** Called by the populate_transforms function in
27  * optimization_transforms.cpp.
28  */
29  void register_optimization(const std::vector<size_t>& valid_stages, std::shared_ptr<opt_transform> opt);
30 
31  /** The number of distinct optimization stages in the model.
32  */
33  size_t num_stages() const;
34 
35  /** Returns a vector of possible transforms for a given stage and
36  * node type.
37  */
38  inline const std::vector<std::shared_ptr<opt_transform> >& get_transforms(
39  size_t stage, planner_node_type t) const;
40 
41 
42  private:
43  // nested as possible_transforms[stage][type][transform]
44  std::vector<std::vector<std::vector<std::shared_ptr<opt_transform> > > > possible_transforms;
45 };
46 
47 
48 /**
49  * \ingroup sframe_query_engine
50  * \addtogroup planning Planning, Optimization and Execution
51  * \{
52  */
53 
54 /**
55  * The main engine to power the optimizations.
56  *
57  */
59  public:
60 
61  /** The main function to optimize the graph.
62  */
63  static pnode_ptr optimize_planner_graph(pnode_ptr tip, const materialize_options& exec_params);
64 
65  private:
66 
67  /** Use should only be through the above optimize_planner_graph
68  * static function.
69  */
70  optimization_engine(std::shared_ptr<const optimization_transform_registry>);
71 
73 
74  public:
75 
76  ////////////////////////////////////////////////////////////////////////////////
77  // Routines used by the transforms to manipulate a graph.
78 
79  // Can ensure a particular node goes back on the queue to processed.
80  inline void mark_node_as_active(cnode_info_ptr n);
81 
82  // All graph manipulations go through here. Things are marked as
83  // active in several contexts.
84  void replace_node(cnode_info_ptr old_node, pnode_ptr new_pnode);
85 
86  private:
87 
88  static constexpr size_t num_types() { return int(planner_node_type::INVALID); }
89 
90  std::shared_ptr<const optimization_transform_registry> transform_registry;
91 
92  /** Runs the optimization parameters.
93  */
94  pnode_ptr _run(pnode_ptr tip, const materialize_options& exec_params);
95 
96  /** Initialize and run a stage -- i.e. populate the active nodes,
97  * etc.
98  *
99  */
100  void run_stage(size_t stage, node_info_ptr tip, const materialize_options& exec_params);
101 
102  /** Goes through and populates the active node list.
103  */
104  void populate_active_nodes(cnode_info_ptr tip);
105 
106  /** In a given stage, only nodes with applicable types are added to
107  * the active queue. This cuts down on processing time for each
108  * stage.
109  */
110  std::vector<int> stage_type_active_mask;
111 
112  // Build the active node queue out.
113  void _build_active_node_queue(const cnode_info_ptr& tip);
114  std::vector<cnode_info_ptr> active_nodes;
115 
116  void eliminate_node_and_prune(node_info_ptr n);
117 
118  node_info_ptr build_node_info(pnode_ptr);
119 
120  std::map<pnode_ptr, node_info_ptr> node_lookups;
121  std::vector<node_info_ptr> all_nodes;
122 
123  void release_node(const node_info_ptr& ptr);
124 
125 };
126 
127 /// \}
128 //
129 }}
130 
131 #endif /* _PLAN_OPTIMIZATIONS_H_ */
const std::vector< std::shared_ptr< opt_transform > > & get_transforms(size_t stage, planner_node_type t) const
std::shared_ptr< planner_node > pnode_ptr
A handy typedef.
void register_optimization(const std::vector< size_t > &valid_stages, std::shared_ptr< opt_transform > opt)