Halide
ParamMap.h
Go to the documentation of this file.
1 #ifndef HALIDE_PARAM_MAP_H
2 #define HALIDE_PARAM_MAP_H
3 
4 /** \file
5  * Defines a collection of parameters to be passed as formal arguments
6  * to a JIT invocation.
7  */
8 #include <map>
9 
10 #include "Param.h"
11 #include "Parameter.h"
12 
13 namespace Halide {
14 
15 class ImageParam;
16 
17 class ParamMap {
18 public:
19  struct ParamMapping {
20  const Internal::Parameter *parameter{nullptr};
21  const ImageParam *image_param{nullptr};
25 
26  template<typename T>
27  ParamMapping(const Param<T> &p, const T &val)
28  : parameter(&p.parameter()) {
29  *((T *)&value) = val;
30  }
31 
33  : image_param(&p), buf(buf), buf_out_param(nullptr) {
34  }
35 
36  template<typename T>
38  : image_param(&p), buf(buf), buf_out_param(nullptr) {
39  }
40 
41  ParamMapping(const ImageParam &p, Buffer<> *buf_ptr)
42  : image_param(&p), buf_out_param(buf_ptr) {
43  }
44 
45  template<typename T>
46  ParamMapping(const ImageParam &p, Buffer<T> *buf_ptr)
47  : image_param(&p), buf_out_param((Buffer<> *)buf_ptr) {
48  }
49  };
50 
51 private:
52  struct ParamArg {
53  Internal::Parameter mapped_param;
54  Buffer<> *buf_out_param;
55 
56  ParamArg()
57  : buf_out_param(nullptr) {
58  }
59  ParamArg(const ParamMapping &pm)
60  : mapped_param(pm.parameter->type(), false, 0, pm.parameter->name()),
61  buf_out_param(nullptr) {
62  mapped_param.set_scalar(pm.parameter->type(), pm.value);
63  }
64  ParamArg(Buffer<> *buf_ptr)
65  : buf_out_param(buf_ptr) {
66  }
67  ParamArg(const ParamArg &) = default;
68  };
69  mutable std::map<const Internal::Parameter, ParamArg> mapping;
70 
71  void set(const ImageParam &p, const Buffer<> &buf, Buffer<> *buf_out_param);
72 
73 public:
75  }
76 
77  ParamMap(const std::initializer_list<ParamMapping> &init);
78 
79  template<typename T>
80  void set(const Param<T> &p, T val) {
81  Internal::Parameter v(p.type(), false, 0, p.name());
82  v.set_scalar<T>(val);
83  ParamArg pa;
84  pa.mapped_param = v;
85  pa.buf_out_param = nullptr;
86  mapping[p.parameter()] = pa;
87  };
88 
89  void set(const ImageParam &p, const Buffer<> &buf) {
90  set(p, buf, nullptr);
91  }
92 
93  size_t size() const {
94  return mapping.size();
95  }
96 
97  /** If there is an entry in the ParamMap for this Parameter, return it.
98  * Otherwise return the parameter itself. */
99  // @{
100  const Internal::Parameter &map(const Internal::Parameter &p, Buffer<> *&buf_out_param) const;
101 
102  Internal::Parameter &map(Internal::Parameter &p, Buffer<> *&buf_out_param) const;
103  // @}
104 
105  /** A const ref to an empty ParamMap. Useful for default function
106  * arguments, which would otherwise require a copy constructor
107  * (with llvm in c++98 mode) */
108  static const ParamMap &empty_map() {
109  static ParamMap empty_param_map;
110  return empty_param_map;
111  }
112 };
113 
114 } // namespace Halide
115 
116 #endif
Halide::Internal::Parameter::set_scalar
HALIDE_NO_USER_CODE_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:90
Parameter.h
Halide::Param::type
Type type() const
Get the halide type of the Param.
Definition: Param.h:221
Halide::Internal::Parameter
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:28
Halide::Param::name
const std::string & name() const
Get the name of this parameter.
Definition: Param.h:165
Halide::ParamMap::set
void set(const ImageParam &p, const Buffer<> &buf)
Definition: ParamMap.h:89
Halide::ImageParam
An Image parameter to a halide pipeline.
Definition: ImageParam.h:23
Halide::ParamMap::ParamMapping::parameter
const Internal::Parameter * parameter
Definition: ParamMap.h:20
Halide::ParamMap
Definition: ParamMap.h:17
Halide::ParamMap::ParamMapping::ParamMapping
ParamMapping(const Param< T > &p, const T &val)
Definition: ParamMap.h:27
Halide::ParamMap::ParamMap
ParamMap()
Definition: ParamMap.h:74
Halide::ParamMap::ParamMapping::ParamMapping
ParamMapping(const ImageParam &p, Buffer< T > &buf)
Definition: ParamMap.h:37
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::ParamMap::ParamMapping::value
halide_scalar_value_t value
Definition: ParamMap.h:22
halide_scalar_value_t
halide_scalar_value_t is a simple union able to represent all the well-known scalar values in a filte...
Definition: HalideRuntime.h:1557
Halide::ParamMap::ParamMapping::image_param
const ImageParam * image_param
Definition: ParamMap.h:21
Halide::ParamMap::ParamMapping
Definition: ParamMap.h:19
Halide::Buffer<>
Halide::Param::parameter
const Internal::Parameter & parameter() const
Definition: Param.h:283
Halide::ParamMap::size
size_t size() const
Definition: ParamMap.h:93
Halide::ParamMap::ParamMapping::ParamMapping
ParamMapping(const ImageParam &p, Buffer<> *buf_ptr)
Definition: ParamMap.h:41
Halide::ParamMap::ParamMapping::buf
Buffer buf
Definition: ParamMap.h:23
Halide::ParamMap::ParamMapping::ParamMapping
ParamMapping(const ImageParam &p, Buffer<> &buf)
Definition: ParamMap.h:32
Halide::ParamMap::ParamMapping::buf_out_param
Buffer * buf_out_param
Definition: ParamMap.h:24
buf
char * buf
Definition: printer.h:32
Param.h
Halide::ParamMap::map
const Internal::Parameter & map(const Internal::Parameter &p, Buffer<> *&buf_out_param) const
If there is an entry in the ParamMap for this Parameter, return it.
Halide::Param
A scalar parameter to a halide pipeline.
Definition: Param.h:22
Halide::ParamMap::set
void set(const Param< T > &p, T val)
Definition: ParamMap.h:80
Halide::ParamMap::ParamMapping::ParamMapping
ParamMapping(const ImageParam &p, Buffer< T > *buf_ptr)
Definition: ParamMap.h:46
Halide::ParamMap::empty_map
static const ParamMap & empty_map()
A const ref to an empty ParamMap.
Definition: ParamMap.h:108