Halide
RegionCosts.h
Go to the documentation of this file.
1 #ifndef HALIDE_INTERNAL_REGION_COSTS_H
2 #define HALIDE_INTERNAL_REGION_COSTS_H
3
4 /** \file
5  *
6  * Defines RegionCosts - used by the auto scheduler to query the cost of
7  * computing some function regions.
8  */
9
10 #include <limits>
11 #include <set>
12
13 #include "AutoScheduleUtils.h"
14 #include "Interval.h"
15 #include "Scope.h"
16
17 namespace Halide {
18 namespace Internal {
19
20 struct Cost {
21  // Estimate of cycles spent doing arithmetic.
23  // Estimate of bytes loaded.
25
27  : arith(arith), memory(memory) {
28  }
30  : arith(std::move(arith)), memory(std::move(memory)) {
31  }
32  Cost() = default;
33
34  inline bool defined() const {
35  return arith.defined() && memory.defined();
36  }
37  void simplify();
38
39  friend std::ostream &operator<<(std::ostream &stream, const Cost &c) {
40  stream << "[arith: " << c.arith << ", memory: " << c.memory << "]";
41  return stream;
42  }
43 };
44
45 /** Auto scheduling component which is used to assign costs for computing a
46  * region of a function or one of its stages. */
47 struct RegionCosts {
48  /** An environment map which contains all functions in the pipeline. */
49  std::map<std::string, Function> env;
50  /** Realization order of functions in the pipeline. The first function to
51  * be realized comes first. */
52  std::vector<std::string> order;
53  /** A map containing the cost of computing a value in each stage of a
54  * function. The number of entries in the vector is equal to the number of
55  * stages in the function. */
56  std::map<std::string, std::vector<Cost>> func_cost;
57  /** A map containing the types of all image inputs in the pipeline. */
58  std::map<std::string, Type> inputs;
59  /** A scope containing the estimated min/extent values of ImageParams
60  * in the pipeline. */
62
63  /** Return the cost of producing a region (specified by 'bounds') of a
64  * function stage (specified by 'func' and 'stage'). 'inlines' specifies
65  * names of all the inlined functions. */
66  Cost stage_region_cost(const std::string &func, int stage, const DimBounds &bounds,
67  const std::set<std::string> &inlines = std::set<std::string>());
68
69  /** Return the cost of producing a region of a function stage (specified
70  * by 'func' and 'stage'). 'inlines' specifies names of all the inlined
71  * functions. */
72  Cost stage_region_cost(const std::string &func, int stage, const Box &region,
73  const std::set<std::string> &inlines = std::set<std::string>());
74
75  /** Return the cost of producing a region of function 'func'. This adds up the
76  * costs of all stages of 'func' required to produce the region. 'inlines'
77  * specifies names of all the inlined functions. */
78  Cost region_cost(const std::string &func, const Box &region,
79  const std::set<std::string> &inlines = std::set<std::string>());
80
81  /** Same as region_cost above but this computes the total cost of many
82  * function regions. */
83  Cost region_cost(const std::map<std::string, Box> &regions,
84  const std::set<std::string> &inlines = std::set<std::string>());
85
86  /** Compute the cost of producing a single value by one stage of 'f'.
87  * 'inlines' specifies names of all the inlined functions. */
88  Cost get_func_stage_cost(const Function &f, int stage,
89  const std::set<std::string> &inlines = std::set<std::string>()) const;
90
91  /** Compute the cost of producing a single value by all stages of 'f'.
92  * 'inlines' specifies names of all the inlined functions. This returns a
93  * vector of costs. Each entry in the vector corresponds to a stage in 'f'. */
94  std::vector<Cost> get_func_cost(const Function &f,
95  const std::set<std::string> &inlines = std::set<std::string>());
96
97  /** Computes the memory costs of computing a region (specified by 'bounds')
98  * of a function stage (specified by 'func' and 'stage'). This returns a map
99  * containing the costs incurred to access each of the functions required
100  * to produce 'func'. */
101  std::map<std::string, Expr>
102  stage_detailed_load_costs(const std::string &func, int stage, DimBounds &bounds,
103  const std::set<std::string> &inlines = std::set<std::string>());
104
105  /** Return a map containing the costs incurred to access each of the functions
106  * required to produce a single value of a function stage. */
107  std::map<std::string, Expr>
108  stage_detailed_load_costs(const std::string &func, int stage,
109  const std::set<std::string> &inlines = std::set<std::string>());
110
111  /** Same as stage_detailed_load_costs above but this computes the cost of a region
112  * of 'func'. */
113  std::map<std::string, Expr>
114  detailed_load_costs(const std::string &func, const Box &region,
115  const std::set<std::string> &inlines = std::set<std::string>());
116
117  /** Same as detailed_load_costs above but this computes the cost of many function
118  * regions and aggregates them. */
119  std::map<std::string, Expr>
120  detailed_load_costs(const std::map<std::string, Box> &regions,
121  const std::set<std::string> &inlines = std::set<std::string>());
122
123  /** Return the size of the region of 'func' in bytes. */
124  Expr region_size(const std::string &func, const Box &region);
125
126  /** Return the size of the peak amount of memory allocated in bytes. This takes
127  * the realization (topological) order of the function regions and the early
128  * free mechanism into account while computing the peak footprint. */
129  Expr region_footprint(const std::map<std::string, Box> &regions,
130  const std::set<std::string> &inlined = std::set<std::string>());
131
132  /** Return the size of the input region in bytes. */
133  Expr input_region_size(const std::string &input, const Box &region);
134
135  /** Return the total size of the many input regions in bytes. */
136  Expr input_region_size(const std::map<std::string, Box> &input_regions);
137
138  /** Display the cost of each function in the pipeline. */
139  void disp_func_costs();
140
141  /** Construct a region cost object for the pipeline. 'env' is a map of all
142  * functions in the pipeline. 'order' is the realization order of functions
143  * in the pipeline. The first function to be realized comes first. */
144  RegionCosts(const std::map<std::string, Function> &env,
145  const std::vector<std::string> &order);
146 };
147
148 /** Return true if the cost of inlining a function is equivalent to the
149  * cost of calling the function directly. */
150 bool is_func_trivial_to_inline(const Function &func);
151
152 } // namespace Internal
153 } // namespace Halide
154
155 #endif
Halide::Internal::RegionCosts::get_func_cost
std::vector< Cost > get_func_cost(const Function &f, const std::set< std::string > &inlines=std::set< std::string >())
Compute the cost of producing a single value by all stages of 'f'.
Halide::Internal::RegionCosts::input_region_size
Expr input_region_size(const std::string &input, const Box &region)
Return the size of the input region in bytes.
Halide::Internal::RegionCosts::func_cost
std::map< std::string, std::vector< Cost > > func_cost
A map containing the cost of computing a value in each stage of a function.
Definition: RegionCosts.h:56
Scope.h
Halide::Internal::Cost::defined
bool defined() const
Definition: RegionCosts.h:34
Halide::Internal::Cost::Cost
Cost(int64_t arith, int64_t memory)
Definition: RegionCosts.h:26
Halide::Internal::RegionCosts::RegionCosts
RegionCosts(const std::map< std::string, Function > &env, const std::vector< std::string > &order)
Construct a region cost object for the pipeline.
Halide::Internal::Cost::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Cost &c)
Definition: RegionCosts.h:39
std::map< std::string, Expr > detailed_load_costs(const std::string &func, const Box &region, const std::set< std::string > &inlines=std::set< std::string >())
Same as stage_detailed_load_costs above but this computes the cost of a region of 'func'.
Halide::Internal::Cost
Definition: RegionCosts.h:20
Halide::Internal::DimBounds
std::map< std::string, Interval > DimBounds
Definition: AutoScheduleUtils.h:21
Halide::Internal::Scope
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition: ModulusRemainder.h:17
Halide::Internal::Cost::Cost
Cost(Expr arith, Expr memory)
Definition: RegionCosts.h:29
Halide::Internal::Cost::memory
Expr memory
Definition: RegionCosts.h:24
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
std::map< std::string, Expr > stage_detailed_load_costs(const std::string &func, int stage, DimBounds &bounds, const std::set< std::string > &inlines=std::set< std::string >())
Computes the memory costs of computing a region (specified by 'bounds') of a function stage (specifie...
Halide::Internal::RegionCosts::order
std::vector< std::string > order
Realization order of functions in the pipeline.
Definition: RegionCosts.h:52
Halide::Internal::Cost::simplify
void simplify()
Halide::Internal::RegionCosts
Auto scheduling component which is used to assign costs for computing a region of a function or one o...
Definition: RegionCosts.h:47
Halide::Internal::RegionCosts::inputs
std::map< std::string, Type > inputs
A map containing the types of all image inputs in the pipeline.
Definition: RegionCosts.h:58
Halide::Internal::Cost::Cost
Cost()=default
int64_t
signed __INT64_TYPE__ int64_t
Definition: runtime_internal.h:18
Halide::Internal::Function
A reference-counted handle to Halide's internal representation of a function.
Definition: Function.h:38
Halide::Internal::RegionCosts::stage_region_cost
Cost stage_region_cost(const std::string &func, int stage, const DimBounds &bounds, const std::set< std::string > &inlines=std::set< std::string >())
Return the cost of producing a region (specified by 'bounds') of a function stage (specified by 'func...
Halide::Internal::RegionCosts::region_size
Expr region_size(const std::string &func, const Box &region)
Return the size of the region of 'func' in bytes.
Halide::Internal::RegionCosts::env
std::map< std::string, Function > env
An environment map which contains all functions in the pipeline.
Definition: RegionCosts.h:49
Halide::Internal::is_func_trivial_to_inline
bool is_func_trivial_to_inline(const Function &func)
Return true if the cost of inlining a function is equivalent to the cost of calling the function dire...
Halide::Internal::RegionCosts::input_estimates
Scope< Interval > input_estimates
A scope containing the estimated min/extent values of ImageParams in the pipeline.
Definition: RegionCosts.h:61
Halide::Internal::RegionCosts::disp_func_costs
void disp_func_costs()
Display the cost of each function in the pipeline.
Halide::Internal::IntrusivePtr::defined
HALIDE_ALWAYS_INLINE bool defined() const
Definition: IntrusivePtr.h:156
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::RegionCosts::region_cost
Cost region_cost(const std::string &func, const Box &region, const std::set< std::string > &inlines=std::set< std::string >())
Return the cost of producing a region of function 'func'.
Interval.h
Halide::Internal::RegionCosts::get_func_stage_cost
Cost get_func_stage_cost(const Function &f, int stage, const std::set< std::string > &inlines=std::set< std::string >()) const
Compute the cost of producing a single value by one stage of 'f'.
AutoScheduleUtils.h
Halide::Internal::Cost::arith
Expr arith
Definition: RegionCosts.h:22
Halide::Internal::RegionCosts::region_footprint
Expr region_footprint(const std::map< std::string, Box > &regions, const std::set< std::string > &inlined=std::set< std::string >())
Return the size of the peak amount of memory allocated in bytes.
Halide::Internal::Box
Represents the bounds of a region of arbitrary dimension.
Definition: Bounds.h:53