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