Halide  12.0.1
Halide compiler and libraries
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 = nullptr;
55 
56  ParamArg() = default;
57  ParamArg(const ParamMapping &pm)
58  : mapped_param(pm.parameter->type(), false, 0, pm.parameter->name()) {
59  mapped_param.set_scalar(pm.parameter->type(), pm.value);
60  }
61  ParamArg(Buffer<> *buf_ptr)
62  : buf_out_param(buf_ptr) {
63  }
64  ParamArg(const ParamArg &) = default;
65  };
66  mutable std::map<const Internal::Parameter, ParamArg> mapping;
67 
68  void set(const ImageParam &p, const Buffer<> &buf, Buffer<> *buf_out_param);
69 
70 public:
71  ParamMap() = default;
72 
73  ParamMap(const std::initializer_list<ParamMapping> &init);
74 
75  template<typename T>
76  void set(const Param<T> &p, T val) {
77  Internal::Parameter v(p.type(), false, 0, p.name());
78  v.set_scalar<T>(val);
79  ParamArg pa;
80  pa.mapped_param = v;
81  pa.buf_out_param = nullptr;
82  mapping[p.parameter()] = pa;
83  }
84 
85  void set(const ImageParam &p, const Buffer<> &buf) {
86  set(p, buf, nullptr);
87  }
88 
89  size_t size() const {
90  return mapping.size();
91  }
92 
93  /** If there is an entry in the ParamMap for this Parameter, return it.
94  * Otherwise return the parameter itself. */
95  // @{
96  const Internal::Parameter &map(const Internal::Parameter &p, Buffer<> *&buf_out_param) const;
97 
98  Internal::Parameter &map(Internal::Parameter &p, Buffer<> *&buf_out_param) const;
99  // @}
100 
101  /** A const ref to an empty ParamMap. Useful for default function
102  * arguments, which would otherwise require a copy constructor
103  * (with llvm in c++98 mode) */
104  static const ParamMap &empty_map() {
105  static ParamMap empty_param_map;
106  return empty_param_map;
107  }
108 };
109 
110 } // namespace Halide
111 
112 #endif
Classes for declaring scalar parameters to halide pipelines.
Defines the internal representation of parameters to halide piplines.
A Halide::Buffer is a named shared reference to a Halide::Runtime::Buffer.
Definition: Buffer.h:115
An Image parameter to a halide pipeline.
Definition: ImageParam.h:23
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:29
HALIDE_NO_USER_CODE_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:91
A scalar parameter to a halide pipeline.
Definition: Param.h:22
const std::string & name() const
Get the name of this parameter.
Definition: Param.h:165
const Internal::Parameter & parameter() const
Definition: Param.h:283
Type type() const
Get the halide type of the Param.
Definition: Param.h:221
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.
void set(const Param< T > &p, T val)
Definition: ParamMap.h:76
size_t size() const
Definition: ParamMap.h:89
ParamMap()=default
static const ParamMap & empty_map()
A const ref to an empty ParamMap.
Definition: ParamMap.h:104
Internal::Parameter & map(Internal::Parameter &p, Buffer<> *&buf_out_param) const
void set(const ImageParam &p, const Buffer<> &buf)
Definition: ParamMap.h:85
ParamMap(const std::initializer_list< ParamMapping > &init)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
char * buf
Definition: printer.h:32
const ImageParam * image_param
Definition: ParamMap.h:21
ParamMapping(const ImageParam &p, Buffer<> *buf_ptr)
Definition: ParamMap.h:41
ParamMapping(const ImageParam &p, Buffer<> &buf)
Definition: ParamMap.h:32
const Internal::Parameter * parameter
Definition: ParamMap.h:20
ParamMapping(const Param< T > &p, const T &val)
Definition: ParamMap.h:27
ParamMapping(const ImageParam &p, Buffer< T > &buf)
Definition: ParamMap.h:37
halide_scalar_value_t value
Definition: ParamMap.h:22
ParamMapping(const ImageParam &p, Buffer< T > *buf_ptr)
Definition: ParamMap.h:46
halide_scalar_value_t is a simple union able to represent all the well-known scalar values in a filte...