Tensor Comprehensions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mapping_options-inl.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include "tc/core/utils/vararg.h"
19 
20 namespace tc {
21 
22 //
23 // TilingView & Tiling
24 //
25 
26 Tiling::Tiling(const std::vector<uint64_t>& sizes) : TilingView(ownedProto_) {
27  proto.clear_sizes();
28  std::copy(
29  sizes.begin(),
30  sizes.end(),
31  google::protobuf::RepeatedFieldBackInserter(proto.mutable_sizes()));
32 }
33 
34 Tiling::Tiling(std::initializer_list<uint64_t> il)
35  : Tiling(std::vector<uint64_t>(il)) {}
36 
37 std::vector<uint64_t> TilingView::extractVector() const {
38  std::vector<uint64_t> result(proto.sizes().begin(), proto.sizes().end());
39  return result;
40 }
41 
42 size_t TilingView::size() const {
43  return proto.sizes_size();
44 }
45 
47  CHECK_LT(i, proto.sizes_size()) << "index overflow";
49  [this, i](uint64_t u) { this->proto.set_sizes(i, u); },
50  [this, i]() { return this->proto.sizes(i); });
51 }
52 
53 uint64_t TilingView::operator[](size_t i) const {
54  CHECK_LT(i, proto.sizes_size()) << "index overflow";
55  return proto.sizes(i);
56 }
57 
59  proto = view.proto;
60  return *this;
61 }
62 
63 bool TilingView::operator==(const TilingView& view) const {
64  return proto.SerializeAsString() == view.proto.SerializeAsString();
65 }
66 
67 bool TilingView::operator!=(const TilingView& view) const {
68  return !(*this == view);
69 }
70 
71 //
72 // CudaDimView & CudaDim
73 //
74 CudaDim::CudaDim(std::vector<uint64_t> il) : CudaDimView(ownedProto_) {
75  CHECK_GT(il.size(), 0) << "list of values in CudaDimView must be non-empty";
76  CHECK_LE(il.size(), 3) << "at most 3 values allowed in CudaDimView";
77 
78  switch (il.size()) {
79  case 3:
80  proto.set_z(*(il.begin() + 2));
81  case 2:
82  proto.set_y(*(il.begin() + 1));
83  case 1:
84  proto.set_x(*il.begin());
85  break;
86  default:
87  CHECK(false) << "unreachable";
88  }
89 }
90 
91 CudaDim::CudaDim(std::initializer_list<uint64_t> il)
92  : CudaDim(std::vector<uint64_t>(il)) {}
93 
94 CudaDim::CudaDim(uint64_t x, uint64_t y, uint64_t z)
95  : CudaDimView(ownedProto_) {
96  proto.set_x(x);
97  if (y != defaultDim || z != defaultDim) {
98  proto.set_y(y);
99  }
100  if (z != defaultDim) {
101  proto.set_z(z);
102  }
103 }
104 
105 size_t CudaDimView::size() const {
106  CHECK(!(!proto.has_y() && proto.has_z())) << "CudaDimView has z but not y";
107 
108  if (proto.has_z() && proto.has_y()) {
109  return 3;
110  } else if (proto.has_y()) {
111  return 2;
112  }
113  return 1;
114 }
115 
116 std::vector<uint64_t> CudaDimView::extractVector() const {
117  CHECK(!(!proto.has_y() && proto.has_z())) << "CudaDimView has z but not y";
118 
119  std::vector<uint64_t> result;
120  result.push_back(proto.x());
121  if (proto.has_y()) {
122  result.push_back(proto.y());
123  }
124  if (proto.has_z()) {
125  result.push_back(proto.z());
126  }
127  return result;
128 }
129 
130 std::array<uint64_t, 3> CudaDimView::extractDefaultedArray() const {
131  std::array<uint64_t, 3> arr{CudaDimView::defaultDim,
133  CudaDimView::defaultDim};
134  auto v = extractVector();
135  CHECK_LE(v.size(), 3);
136  std::copy(v.begin(), v.end(), arr.begin());
137  return arr;
138 }
139 
141  CHECK_LT(i, 3) << "index overflow";
142  if (i == 0) {
144  [this](uint64_t u) { this->proto.set_x(u); },
145  [this]() { return this->proto.x(); });
146  } else if (i == 1) {
148  [this](uint64_t u) { this->proto.set_y(u); },
149  [this]() {
150  return this->proto.has_y() ? this->proto.y()
152  });
153  } else {
155  [this](uint64_t u) { this->proto.set_z(u); },
156  [this]() {
157  return this->proto.has_z() ? this->proto.z()
159  });
160  }
161 }
162 
163 uint64_t CudaDimView::operator[](size_t i) const {
164  CHECK_LT(i, 3) << "index overflow";
165  if (i == 0) {
166  return proto.x();
167  } else if (i == 1) {
168  return proto.has_y() ? proto.y() : CudaDimView::defaultDim;
169  } else {
170  return proto.has_z() ? proto.z() : CudaDimView::defaultDim;
171  }
172 }
173 
175  proto = view.proto;
176  return *this;
177 }
178 
179 bool CudaDimView::operator==(const CudaDimView& view) const {
180  return proto.SerializeAsString() == view.proto.SerializeAsString();
181 }
182 
183 bool CudaDimView::operator!=(const CudaDimView& view) const {
184  return !(*this == view);
185 }
186 
187 //
188 // SchedulerOptionsView & SchedulerOptions
189 //
191  const SchedulerOptionsView& view) {
192  proto = view.proto;
193  return *this;
194 }
195 
197  return proto.SerializeAsString() == view.proto.SerializeAsString();
198 }
199 
201  return !(*this == view);
202 }
203 
204 //
205 // MappingOptions
206 //
208  : block(*proto.mutable_block()),
209  grid(*proto.mutable_grid()),
210  tiling(*proto.mutable_tiling()),
211  outerScheduleOptions(*proto.mutable_outer_schedule_options()),
212  intraTileScheduleOptions(*proto.mutable_intra_tile_schedule_options()) {}
213 
215  : proto(options.proto),
216  block(*proto.mutable_block()),
217  grid(*proto.mutable_grid()),
218  tiling(*proto.mutable_tiling()),
219  outerScheduleOptions(*proto.mutable_outer_schedule_options()),
220  intraTileScheduleOptions(*proto.mutable_intra_tile_schedule_options()) {}
221 
222 MappingOptions::MappingOptions(const MappingOptionsProto& buf)
223  : proto(buf),
224  block(*proto.mutable_block()),
225  grid(*proto.mutable_grid()),
226  tiling(*proto.mutable_tiling()),
227  outerScheduleOptions(*proto.mutable_outer_schedule_options()),
228  intraTileScheduleOptions(*proto.mutable_intra_tile_schedule_options()) {}
229 
230 MappingOptions::MappingOptions(const std::string& str) : MappingOptions() {
231  bool parsed = proto.ParseFromString(str);
232  CHECK(parsed) << "could not parse protobuf string";
233 }
234 
235 bool MappingOptions::operator==(const MappingOptions& options) const {
236  return proto.SerializeAsString() == options.proto.SerializeAsString();
237 }
238 
239 bool MappingOptions::operator!=(const MappingOptions& options) const {
240  return !(*this == options);
241 }
242 
244  return proto.SerializeAsString();
245 }
246 
247 //
248 // MappingOptions chainable builders.
249 //
250 
251 MappingOptions& MappingOptions::tile(const std::vector<uint64_t>& sizes) {
252  tiling = Tiling(sizes);
253  return *this;
254 }
255 
256 MappingOptions& MappingOptions::tile(std::initializer_list<uint64_t> sizes) {
257  tiling = Tiling(sizes);
258  return *this;
259 }
260 
262  return tile(std::string(str));
263 }
264 
265 template <typename... Args>
267  static_assert(
269  "arguments of tile() must be integers");
270  return tile(vectorFromCastedArgs<uint64_t, Args...>(args...));
271 }
272 
274  std::initializer_list<uint64_t> threads) {
275  block = CudaDim(threads);
276  return *this;
277 }
278 
280 MappingOptions::mapToThreads(uint64_t x, uint64_t y, uint64_t z) {
281  block = CudaDim(x, y, z);
282  return *this;
283 }
284 
286  const std::vector<uint64_t>& threads) {
287  CHECK_GT(threads.size(), 0) << "expected at least one thread size";
288  CHECK_LE(threads.size(), 3) << "expected at most three thread sizes";
289 
290  uint64_t x = threads[0];
291  uint64_t y = threads.size() > 1 ? threads[1] : CudaDimView::defaultDim;
292  uint64_t z = threads.size() > 2 ? threads[2] : CudaDimView::defaultDim;
293  block = CudaDim(x, y, z);
294  return *this;
295 }
296 
298  std::initializer_list<uint64_t> blocks) {
299  grid = CudaDim(blocks);
300  return *this;
301 }
302 
304 MappingOptions::mapToBlocks(uint64_t x, uint64_t y, uint64_t z) {
305  grid = CudaDim(x, y, z);
306  return *this;
307 }
308 
310  const std::vector<uint64_t>& blocks) {
311  CHECK_GT(blocks.size(), 0) << "expected at least one thread size";
312  CHECK_LE(blocks.size(), 3) << "expected at most three thread sizes";
313 
314  uint64_t x = blocks[0];
315  uint64_t y = blocks.size() > 1 ? blocks[1] : CudaDimView::defaultDim;
316  uint64_t z = blocks.size() > 2 ? blocks[2] : CudaDimView::defaultDim;
317  grid = CudaDim(x, y, z);
318  return *this;
319 }
320 
322  proto.set_unroll(size);
323  return *this;
324 }
325 
327  proto.set_use_shared_memory(b);
328  return *this;
329 }
330 
332  proto.set_use_private_memory(b);
333  return *this;
334 }
335 
337  proto.set_max_shared_memory(size);
338  return *this;
339 }
340 
342  proto.set_fix_parameters_before_scheduling(b);
343  return *this;
344 }
345 
347  proto.set_unroll_copy_shared(b);
348  return *this;
349 }
350 
352  proto.set_tile_imperfectly_nested(b);
353  return *this;
354 }
355 
357  proto.set_match_library_calls(b);
358  return *this;
359 }
360 
364  return *this;
365 }
366 
368  FusionStrategy fs;
369  bool couldParse = FusionStrategy_Parse(str, &fs);
370  CHECK(couldParse) << "unknown FusionStrategy " << str;
371  return scheduleFusionStrategy(fs);
372 }
373 
375  outerScheduleOptions.proto.set_fusion_strategy(fs);
376  return *this;
377 }
378 
380  const std::string& str) {
381  FusionStrategy fs;
382  bool couldParse = FusionStrategy_Parse(str, &fs);
383  CHECK(couldParse) << "unknown FusionStrategy " << str;
384  return outerScheduleFusionStrategy(fs);
385 }
386 
388  outerScheduleOptions.proto.set_allow_skewing(b);
389  return *this;
390 }
391 
393  outerScheduleOptions.proto.set_positive_orthant(b);
394  return *this;
395 }
396 
398  FusionStrategy fs) {
399  intraTileScheduleOptions.proto.set_fusion_strategy(fs);
400  return *this;
401 }
402 
404  const std::string& str) {
405  FusionStrategy fs;
406  bool couldParse = FusionStrategy_Parse(str, &fs);
407  CHECK(couldParse) << "unknown FusionStrategy " << str;
409 }
410 
412  intraTileScheduleOptions.proto.set_allow_skewing(b);
413  return *this;
414 }
415 
417  intraTileScheduleOptions.proto.set_positive_orthant(b);
418  return *this;
419 }
420 
421 } // namespace tc
TilingProto & proto
Definition: mapping_options.h:256
MappingOptions & mapToThreads(std::initializer_list< uint64_t > threads)
Definition: mapping_options-inl.h:273
bool operator==(const CudaDimView &view) const
Compare the values with those from another view.
Definition: mapping_options-inl.h:179
&quot;Materialized&quot; TilingView.
Definition: mapping_options.h:260
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
MappingOptions & usePrivateMemory(bool b)
Definition: mapping_options-inl.h:331
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
MappingOptions & unroll(uint64_t size)
Definition: mapping_options-inl.h:321
MappingOptions & useSharedMemory(bool b)
Definition: mapping_options-inl.h:326
CudaDim()
Definition: mapping_options.h:175
MappingOptions & outerScheduleFusionStrategy(FusionStrategy fs)
Definition: mapping_options-inl.h:374
MappingOptions & matchLibraryCalls(bool b)
Definition: mapping_options-inl.h:356
CudaDimView block
Definition: mapping_options.h:440
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
MappingOptionsProto proto
Definition: mapping_options.h:437
size_t size() const
Number of values held.
Definition: mapping_options-inl.h:105
MappingOptions & mapToBlocks(std::initializer_list< uint64_t > blocks)
Definition: mapping_options-inl.h:297
bool operator!=(const MappingOptions &options) const
Definition: mapping_options-inl.h:239
Tiling()
Definition: mapping_options.h:262
TilingView tiling
Definition: mapping_options.h:442
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
MappingOptions & intraTileScheduleFusionStrategy(FusionStrategy fs)
Definition: mapping_options-inl.h:397
bool operator!=(const TilingView &view) const
Definition: mapping_options-inl.h:67
bool operator!=(const CudaDimView &view) const
Definition: mapping_options-inl.h:183
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 & tile(const std::vector< uint64_t > &sizes)
Definition: mapping_options-inl.h:251
MappingOptions & unrollCopyShared(bool b)
Definition: mapping_options-inl.h:346
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
MappingOptions()
Definition: mapping_options-inl.h:207
SchedulerOptionsView outerScheduleOptions
Definition: mapping_options.h:443
CudaDimView grid
Definition: mapping_options.h:441
Definition: mapping_options.h:222
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
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
Definition: mapping_options.h:278
static const uint64_t defaultDim
Definition: mapping_options.h:159
Definition: vararg.h:69
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