Tensor Comprehensions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mapping_options.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include <mapping_options.pb.h>
19 
20 #include <array>
21 #include <iostream>
22 #include <string>
23 #include <vector>
24 
25 #include "tc/external/isl.h"
26 
27 #include "tc/core/flags.h"
28 #include "tc/core/rtc.h"
29 
81 
82 namespace tc {
83 
93 template <typename T>
95  public:
96  using Setter = std::function<void(T)>;
97  using Getter = std::function<T()>;
98 
99  ValueAccessor(const Setter& s, const Getter& g) : setter_(s), getter_(g) {}
100  ValueAccessor(const ValueAccessor&) = default;
101 
102  operator T() const {
103  return getter_();
104  }
105 
106  ValueAccessor& operator=(const T& t) {
107  setter_(t);
108  return *this;
109  }
110 
111  private:
114 };
115 
120 class CudaDimView {
121  private:
122  CudaDimView() = default;
123 
124  public:
126  CudaDimView(const CudaDimView&) = default;
127  explicit CudaDimView(CudaDimProto& buf) : proto(buf) {}
128 
130  inline size_t size() const;
131 
133  inline std::vector<uint64_t> extractVector() const;
134 
136  inline std::array<uint64_t, 3> extractDefaultedArray() const;
137 
140  inline ValueAccessor<uint64_t> operator[](size_t i);
141 
143  inline uint64_t operator[](size_t i) const;
144 
146  inline CudaDimView& operator=(const CudaDimView& view);
147 
149  inline bool operator==(const CudaDimView& view) const;
150  inline bool operator!=(const CudaDimView& view) const;
151 
153  std::string toCommaSeparatedString() const;
154  friend std::ostream& operator<<(std::ostream& os, const CudaDimView& view);
155 
156  public:
157  CudaDimProto& proto;
158 
159  static const uint64_t defaultDim = 1;
160 };
161 
173 class CudaDim : public CudaDimView {
174  public:
176  CudaDim(const CudaDim& cudaDim)
177  : CudaDimView(ownedProto_), ownedProto_(cudaDim.proto) {}
178  CudaDim(const CudaDimProto& proto)
179  : CudaDimView(ownedProto_), ownedProto_(proto) {}
180  CudaDim(const CudaDimView& view)
182  inline CudaDim(std::initializer_list<uint64_t> il);
183  inline CudaDim(std::vector<uint64_t> il);
184  inline CudaDim(
185  uint64_t x,
186  uint64_t y = CudaDimView::defaultDim,
187  uint64_t z = CudaDimView::defaultDim);
188 
189  using CudaDimView::operator=;
190 
191  private:
192  CudaDimProto ownedProto_;
193 };
194 
196 class Block : public CudaDim {
197  public:
198  Block() = default;
199  Block(const CudaDimView& view) : CudaDim(view.proto) {}
200  Block(const CudaDimProto& proto) : CudaDim(proto) {}
201  Block(std::initializer_list<uint64_t> il) : CudaDim(il) {}
202  Block(std::vector<uint64_t> il) : CudaDim(il) {}
203 
204  using CudaDimView::operator=;
205 };
206 
208 class Grid : public CudaDim {
209  public:
210  Grid() = default;
211  Grid(const CudaDimView& view) : CudaDim(view.proto) {}
212  Grid(const CudaDimProto& proto) : CudaDim(proto) {}
213  Grid(std::initializer_list<uint64_t> il) : CudaDim(il) {}
214  Grid(std::vector<uint64_t> il) : CudaDim(il) {}
215 
216  using CudaDimView::operator=;
217 };
218 
222 class TilingView {
223  private:
224  TilingView() = default;
225 
226  public:
228  TilingView(const TilingView&) = default;
229  explicit TilingView(TilingProto& p) : proto(p) {}
230 
232  inline std::vector<uint64_t> extractVector() const;
233 
235  inline size_t size() const;
236 
239  inline ValueAccessor<uint64_t> operator[](size_t i);
240 
242  inline uint64_t operator[](size_t i) const;
243 
245  inline TilingView& operator=(const TilingView& view);
246 
248  inline bool operator==(const TilingView& view) const;
249  inline bool operator!=(const TilingView& view) const;
250 
252  std::string toCommaSeparatedString() const;
253  friend std::ostream& operator<<(std::ostream& os, const TilingView& view);
254 
255  public:
256  TilingProto& proto;
257 };
258 
260 class Tiling : public TilingView {
261  public:
264  Tiling(const TilingProto& proto)
265  : TilingView(ownedProto_), ownedProto_(proto) {}
266  Tiling(const TilingView& view)
268  inline Tiling(std::initializer_list<uint64_t> il);
269  inline Tiling(const std::vector<uint64_t>& sizes);
270 
271  private:
272  TilingProto ownedProto_;
273 };
274 
279  public:
281  using MergeCallback = std::function<
282  isl_bool(isl_union_map*, isl_union_map*, int, int, int, void*)>;
283  using ConstraintsCallback = std::function<isl_basic_set*(
284  isl_basic_set*,
285  int,
286  int,
287  isl_id_list*,
288  int*,
289  int*,
290  void*)>;
291 
292  private:
293  SchedulerOptionsView() = default;
294 
295  public:
297  SchedulerOptionsView(const SchedulerOptionsView&) = default;
298  SchedulerOptionsView(SchedulerOptionsProto& buf) : proto(buf) {}
299 
302 
304  inline bool operator==(const SchedulerOptionsView& view) const;
305  inline bool operator!=(const SchedulerOptionsView& view) const;
306 
308  friend std::ostream& operator<<(
309  std::ostream& os,
310  const SchedulerOptionsView& options);
311 
312  public:
313  SchedulerOptionsProto& proto;
314 };
315 
318  public:
322  explicit SchedulerOptions(const SchedulerOptionsProto& proto)
326 
327  private:
328  SchedulerOptionsProto ownedProto_;
329 };
330 
337  private:
338  inline MappingOptions();
340 
341  public:
343  inline MappingOptions(const MappingOptions& options);
344  inline explicit MappingOptions(const MappingOptionsProto& buf);
345 
347  inline explicit MappingOptions(const std::string& str);
348 
350  MappingOptions& operator=(const MappingOptions&) = default;
351 
353  inline bool operator==(const MappingOptions& options) const;
354  inline bool operator!=(const MappingOptions& options) const;
355 
357  inline std::string toProtobufSerializedString() const;
358 
364  inline MappingOptions& tile(const std::vector<uint64_t>& sizes);
365  inline MappingOptions& tile(std::initializer_list<uint64_t> sizes);
366  MappingOptions& tile(const std::string& commaSeparatedSizes);
367  inline MappingOptions& tile(const char* commaSeparatedSizes);
368  template <typename... Args>
369  MappingOptions& tile(Args...);
370 
371  inline MappingOptions& mapToThreads(std::initializer_list<uint64_t> threads);
373  uint64_t x,
374  uint64_t y = CudaDimView::defaultDim,
375  uint64_t z = CudaDimView::defaultDim);
376  inline MappingOptions& mapToThreads(const std::vector<uint64_t>& threads);
377  MappingOptions& mapToThreads(const std::string& commaSeparatedSizes);
378 
379  inline MappingOptions& mapToBlocks(std::initializer_list<uint64_t> blocks);
380  inline MappingOptions& mapToBlocks(
381  uint64_t x,
382  uint64_t y = CudaDimView::defaultDim,
383  uint64_t z = CudaDimView::defaultDim);
384  inline MappingOptions& mapToBlocks(const std::vector<uint64_t>& blocks);
385  MappingOptions& mapToBlocks(const std::string& commaSeparatedSizes);
386 
387  inline MappingOptions& unroll(uint64_t size);
388 
389  inline MappingOptions& useSharedMemory(bool b);
390  inline MappingOptions& usePrivateMemory(bool b);
391  inline MappingOptions& maxSharedMemory(uint64_t size);
393  inline MappingOptions& unrollCopyShared(bool b);
394  inline MappingOptions& tileImperfectlyNested(bool b);
395  inline MappingOptions& matchLibraryCalls(bool b);
397 
400  inline MappingOptions& scheduleFusionStrategy(FusionStrategy fs);
401  inline MappingOptions& scheduleFusionStrategy(const std::string& str);
403 
406  inline MappingOptions& outerScheduleFusionStrategy(FusionStrategy fs);
407  inline MappingOptions& outerScheduleFusionStrategy(const std::string& str);
411 
414  inline MappingOptions& intraTileScheduleFusionStrategy(FusionStrategy fs);
416  const std::string& str);
420 
430 
432  friend std::ostream& operator<<(
433  std::ostream& os,
434  const MappingOptions& options);
435 
436  public:
437  MappingOptionsProto proto;
438 
439  // Views of sub-messages.
445 };
446 
447 namespace callbacks {
448 __isl_give isl_basic_set* AddPositiveCoefficientConstraints(
449  __isl_take isl_basic_set* lp,
450  int n_param,
451  int dim,
452  __isl_keep isl_id_list* stmt_ids,
453  int* node_n_params,
454  int* node_n_dims,
455  void*);
456 
458  __isl_take isl_union_map* original_schedule,
459  __isl_take isl_union_map* updated_schedule,
460  int n_updated_coincident,
461  int n_original_coincident,
462  int is_along_edge,
463  void*);
464 
465 isl_bool FuseAll(
466  __isl_take isl_union_map* original_schedule,
467  __isl_take isl_union_map* updated_schedule,
468  int n_updated_coincident,
469  int n_original_coincident,
470  int is_along_edge,
471  void*);
472 
473 isl_bool FuseNone(
474  __isl_take isl_union_map* original_schedule,
475  __isl_take isl_union_map* updated_schedule,
476  int n_updated_coincident,
477  int n_original_coincident,
478  int is_along_edge,
479  void*);
480 } // namespace callbacks
481 
482 } // namespace tc
483 
SchedulerOptions(const SchedulerOptionsProto &proto)
Definition: mapping_options.h:322
TilingProto & proto
Definition: mapping_options.h:256
MappingOptions & mapToThreads(std::initializer_list< uint64_t > threads)
Definition: mapping_options-inl.h:273
SchedulerOptions(const SchedulerOptionsView &view)
Definition: mapping_options.h:324
TilingView()=default
bool operator==(const CudaDimView &view) const
Compare the values with those from another view.
Definition: mapping_options-inl.h:179
isl_bool FuseAll(__isl_take isl_union_map *original_schedule, __isl_take isl_union_map *updated_schedule, int n_updated_coincident, int n_original_coincident, int is_along_edge, void *)
&quot;Materialized&quot; TilingView.
Definition: mapping_options.h:260
static MappingOptions makeUnmappedMappingOptions()
CudaDim(const CudaDimView &view)
Definition: mapping_options.h:180
__isl_give isl_basic_set * AddPositiveCoefficientConstraints(__isl_take isl_basic_set *lp, int n_param, int dim, __isl_keep isl_id_list *stmt_ids, int *node_n_params, int *node_n_dims, void *)
ValueAccessor< uint64_t > operator[](size_t i)
Definition: mapping_options-inl.h:46
MappingOptions & intraTileScheduleAllowSkewing(bool b)
Definition: mapping_options-inl.h:411
size_t size() const
Number of values held.
Definition: mapping_options-inl.h:42
Specializing CudaDim to differentiate between Block and Grid sizes.
Definition: mapping_options.h:208
MappingOptions & usePrivateMemory(bool b)
Definition: mapping_options-inl.h:331
static MappingOptions makeGroupConvolutionMappingOptions()
SchedulerOptionsView intraTileScheduleOptions
Definition: mapping_options.h:444
CudaDimView & operator=(const CudaDimView &view)
Assign the values from another view.
Definition: mapping_options-inl.h:174
Definition: mapping_options.h:120
friend std::ostream & operator<<(std::ostream &os, const MappingOptions &options)
Output operator.
CudaDim(const CudaDimProto &proto)
Definition: mapping_options.h:178
MappingOptions & unroll(uint64_t size)
Definition: mapping_options-inl.h:321
&quot;Materialized&quot; SchedulerOptionsView.
Definition: mapping_options.h:317
MappingOptions & useSharedMemory(bool b)
Definition: mapping_options-inl.h:326
CudaDim()
Definition: mapping_options.h:175
ValueAccessor(const Setter &s, const Getter &g)
Definition: mapping_options.h:99
friend std::ostream & operator<<(std::ostream &os, const CudaDimView &view)
MappingOptions & outerScheduleFusionStrategy(FusionStrategy fs)
Definition: mapping_options-inl.h:374
Tiling(const TilingView &view)
Definition: mapping_options.h:266
static MappingOptions makeNaiveMappingOptions()
std::function< T()> Getter
Definition: mapping_options.h:97
MappingOptions & matchLibraryCalls(bool b)
Definition: mapping_options-inl.h:356
CudaDimView block
Definition: mapping_options.h:440
Block()=default
TilingProto ownedProto_
Definition: mapping_options.h:272
Tiling(const Tiling &t)
Definition: mapping_options.h:263
std::function< isl_basic_set *(isl_basic_set *, int, int, isl_id_list *, int *, int *, void *)> ConstraintsCallback
Definition: mapping_options.h:290
bool operator==(const SchedulerOptionsView &view) const
Compare the values with those from another view.
Definition: mapping_options-inl.h:196
bool operator==(const MappingOptions &options) const
Compare with another message.
Definition: mapping_options-inl.h:235
Block(std::initializer_list< uint64_t > il)
Definition: mapping_options.h:201
TilingView(TilingProto &p)
Definition: mapping_options.h:229
CudaDimView(CudaDimProto &buf)
Definition: mapping_options.h:127
MappingOptionsProto proto
Definition: mapping_options.h:437
size_t size() const
Number of values held.
Definition: mapping_options-inl.h:105
Grid(std::vector< uint64_t > il)
Definition: mapping_options.h:214
MappingOptions & mapToBlocks(std::initializer_list< uint64_t > blocks)
Definition: mapping_options-inl.h:297
Block(const CudaDimProto &proto)
Definition: mapping_options.h:200
isl_bool FuseNone(__isl_take isl_union_map *original_schedule, __isl_take isl_union_map *updated_schedule, int n_updated_coincident, int n_original_coincident, int is_along_edge, void *)
bool operator!=(const MappingOptions &options) const
Definition: mapping_options-inl.h:239
Grid(const CudaDimProto &proto)
Definition: mapping_options.h:212
Grid(const CudaDimView &view)
Definition: mapping_options.h:211
Tiling()
Definition: mapping_options.h:262
TilingView tiling
Definition: mapping_options.h:442
SchedulerOptionsProto ownedProto_
Definition: mapping_options.h:328
static MappingOptions makeMlpMappingOptions()
MappingOptions & intraTileSchedulePositiveOrthant(bool b)
Definition: mapping_options-inl.h:416
Definition: mapping_options.h:336
MappingOptions & outerSchedulePositiveOrthant(bool b)
Definition: mapping_options-inl.h:392
MappingOptions & fixParametersBeforeScheduling(bool b)
Definition: mapping_options-inl.h:341
TilingView & operator=(const TilingView &view)
Assign the values from another view.
Definition: mapping_options-inl.h:58
std::string toProtobufSerializedString() const
Get a string with a serialized protocol buffers message.
Definition: mapping_options-inl.h:243
std::string toCommaSeparatedString() const
Conversion to string and output operators.
MappingOptions & intraTileScheduleFusionStrategy(FusionStrategy fs)
Definition: mapping_options-inl.h:397
Tiling(const TilingProto &proto)
Definition: mapping_options.h:264
bool operator!=(const TilingView &view) const
Definition: mapping_options-inl.h:67
bool operator!=(const CudaDimView &view) const
Definition: mapping_options-inl.h:183
Grid(std::initializer_list< uint64_t > il)
Definition: mapping_options.h:213
CudaDimProto & proto
Definition: mapping_options.h:157
bool operator!=(const SchedulerOptionsView &view) const
Definition: mapping_options-inl.h:200
bool operator==(const TilingView &view) const
Compare the values with those from another view.
Definition: mapping_options-inl.h:63
MappingOptions & maxSharedMemory(uint64_t size)
Definition: mapping_options-inl.h:336
MappingOptions & operator=(const MappingOptions &)=default
Assign from another message.
ValueAccessor & operator=(const T &t)
Definition: mapping_options.h:106
std::string toCommaSeparatedString() const
Conversion to string and output operators.
friend std::ostream & operator<<(std::ostream &os, const TilingView &view)
SchedulerOptions()
Definition: mapping_options.h:319
MappingOptions & tile(const std::vector< uint64_t > &sizes)
Definition: mapping_options-inl.h:251
MappingOptions & unrollCopyShared(bool b)
Definition: mapping_options-inl.h:346
isl_bool FuseAllPreserve3Coincident(__isl_take isl_union_map *original_schedule, __isl_take isl_union_map *updated_schedule, int n_updated_coincident, int n_original_coincident, int is_along_edge, void *)
Definition: mapping_options.h:173
MappingOptions & tileImperfectlyNested(bool b)
Definition: mapping_options-inl.h:351
ValueAccessor< uint64_t > operator[](size_t i)
Definition: mapping_options-inl.h:140
Block(const CudaDimView &view)
Definition: mapping_options.h:199
static MappingOptions makeConvolutionMappingOptions()
MappingOptions()
Definition: mapping_options-inl.h:207
std::function< void(T)> Setter
Definition: mapping_options.h:96
Specializing CudaDim to differentiate between Block and Grid sizes.
Definition: mapping_options.h:196
SchedulerOptions(const SchedulerOptions &options)
Definition: mapping_options.h:320
CudaDimProto ownedProto_
Definition: mapping_options.h:192
Block(std::vector< uint64_t > il)
Definition: mapping_options.h:202
SchedulerOptionsView outerScheduleOptions
Definition: mapping_options.h:443
CudaDimView grid
Definition: mapping_options.h:441
static MappingOptions makePointwiseMappingOptions()
Definition: mapping_options.h:222
SchedulerOptionsView(SchedulerOptionsProto &buf)
Definition: mapping_options.h:298
SchedulerOptionsProto & proto
Definition: mapping_options.h:313
SchedulerOptionsView & operator=(const SchedulerOptionsView &)
Assign the values from another view.
Definition: mapping_options-inl.h:190
std::array< uint64_t, 3 > extractDefaultedArray() const
Return a copy of values as std::array of size 3 padded with defaultDim.
Definition: mapping_options-inl.h:130
Setter setter_
Definition: mapping_options.h:112
Grid()=default
Getter getter_
Definition: mapping_options.h:113
MappingOptions & scheduleFusionStrategy(FusionStrategy fs)
Definition: mapping_options-inl.h:361
std::vector< uint64_t > extractVector() const
Return a copy of values as std::vector.
Definition: mapping_options-inl.h:116
std::function< isl_bool(isl_union_map *, isl_union_map *, int, int, int, void *)> MergeCallback
isl scheduler callback types.
Definition: mapping_options.h:282
Definition: mapping_options.h:278
CudaDimView()=default
CudaDim(const CudaDim &cudaDim)
Definition: mapping_options.h:176
static MappingOptions makeSingleThreadMappingOptions()
friend std::ostream & operator<<(std::ostream &os, const SchedulerOptionsView &options)
Output operators.
static const uint64_t defaultDim
Definition: mapping_options.h:159
MappingOptions & outerScheduleAllowSkewing(bool b)
Definition: mapping_options-inl.h:387
Definition: mapping_options.h:94
std::vector< uint64_t > extractVector() const
Return a copy of values as std::vector.
Definition: mapping_options-inl.h:37