CMS 3D CMS Logo

Batching.cc
Go to the documentation of this file.
1 /*
2  * AOT batching rules and strategies.
3  *
4  * Author: Marcel Rieger, Bogdan Wiederspan
5  */
6 
7 #include <ostream>
8 #include <algorithm>
9 
11 
13 
14 namespace tfaot {
15 
16  BatchRule::BatchRule(size_t batchSize, const std::vector<size_t>& sizes, size_t lastPadding)
17  : batchSize_(batchSize), sizes_(sizes), lastPadding_(lastPadding) {
18  validate();
19  }
20 
21  BatchRule::BatchRule(const std::string& ruleString) {
22  // extract the target batch size from the front
23  std::string rule = ruleString;
24  auto pos = rule.find(":");
25  if (pos == std::string::npos) {
26  throw cms::Exception("InvalidBatchRule") << "invalid batch rule format: " << ruleString;
27  }
28  size_t batchSize = std::stoi(rule.substr(0, pos));
29  rule = rule.substr(pos + 1);
30 
31  // loop through remaining comma-separated sizes
32  std::vector<size_t> sizes;
33  size_t sumSizes = 0;
34  while (!rule.empty()) {
35  pos = rule.find(",");
36  sizes.push_back(std::stoi(rule.substr(0, pos)));
37  sumSizes += sizes.back();
38  rule = pos == std::string::npos ? "" : rule.substr(pos + 1);
39  }
40 
41  // the sum of composite batch sizes should never be smaller than the target batch size
42  if (sumSizes < batchSize) {
43  throw cms::Exception("InvalidBatchRule")
44  << "sum of composite batch sizes is smaller than target batch size: " << ruleString;
45  }
46 
47  // set members and validate
49  sizes_ = sizes;
50  lastPadding_ = sumSizes - batchSize;
51  validate();
52  }
53 
54  void BatchRule::validate() const {
55  // sizes must not be empty
56  if (sizes_.size() == 0) {
57  throw cms::Exception("EmptySizes") << "no batch sizes provided for stitching";
58  }
59 
60  // the padding must be smaller than the last size
61  size_t lastSize = sizes_[sizes_.size() - 1];
62  if (lastPadding_ >= lastSize) {
63  throw cms::Exception("WrongPadding")
64  << "padding " << lastPadding_ << " must be smaller than last size " << lastSize;
65  }
66 
67  // compute the covered batch size
68  size_t sizeSum = 0;
69  for (const size_t& s : sizes_) {
70  sizeSum += s;
71  }
72  if (lastPadding_ > sizeSum) {
73  throw cms::Exception("WrongPadding")
74  << "padding " << lastPadding_ << " must not be larger than sum of sizes " << sizeSum;
75  }
76  sizeSum -= lastPadding_;
77 
78  // compare to given batch size
79  if (batchSize_ != sizeSum) {
80  throw cms::Exception("WrongBatchSize")
81  << "batch size " << batchSize_ << " does not match sum of sizes - padding " << sizeSum;
82  }
83  }
84 
86  const auto it = rules_.find(batchSize);
87  if (it == rules_.end()) {
88  throw cms::Exception("UnknownBatchSize") << "batchSize " << batchSize << " not known to batching strategy";
89  }
90  return it->second;
91  }
92 
93  std::ostream& operator<<(std::ostream& out, const BatchRule& rule) {
94  out << "BatchRule(batchSize=" << rule.getBatchSize() << ", sizes=";
95  for (size_t i = 0; i < rule.nSizes(); i++) {
96  out << (i == 0 ? "" : ",") << rule.getSizes()[i];
97  }
98  return out << ", lastPadding=" << rule.getLastPadding() << ")";
99  }
100 
101  void BatchStrategy::setDefaultRule(size_t batchSize, const std::vector<size_t>& availableBatchSizes) {
102  std::vector<size_t> sizes;
103  size_t lastPadding = 0;
104 
105  // many implementations are possible here, but for simplicity assume the most simple one:
106  // if there is an exact match, use it, and otherwise repeat the smallest available size
107  // n times and potentially add padding
108  if (std::find(availableBatchSizes.begin(), availableBatchSizes.end(), batchSize) != availableBatchSizes.end()) {
109  sizes.push_back(batchSize);
110  } else {
111  size_t smallestBatchSize = *std::min_element(availableBatchSizes.begin(), availableBatchSizes.end());
112  size_t rest = batchSize % smallestBatchSize;
113  size_t n = (batchSize / smallestBatchSize) + (rest == 0 ? 0 : 1);
114  lastPadding = rest == 0 ? 0 : (smallestBatchSize - rest);
115  sizes.resize(n, smallestBatchSize);
116  }
117 
118  // create and register the rule
119  setRule(BatchRule(batchSize, sizes, lastPadding));
120  }
121 
122 } // namespace tfaot
const BatchRule & getRule(size_t batchSize) const
Definition: Batching.cc:85
size_t lastPadding_
Definition: Batching.h:49
void setRule(const BatchRule &rule)
Definition: Batching.h:68
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
Definition: Batching.h:15
BatchRule(size_t batchSize, const std::vector< size_t > &sizes, size_t lastPadding=0)
Definition: Batching.cc:16
size_t getLastPadding() const
Definition: Batching.h:38
size_t getBatchSize() const
Definition: Batching.h:32
size_t nSizes() const
Definition: Batching.h:41
std::ostream & operator<<(std::ostream &out, const BatchRule &rule)
Definition: Batching.cc:93
const std::vector< size_t > & getSizes() const
Definition: Batching.h:35
void validate() const
Definition: Batching.cc:54
std::vector< size_t > sizes_
Definition: Batching.h:48
std::map< size_t, BatchRule > rules_
Definition: Batching.h:83
size_t batchSize_
Definition: Batching.h:47
void setDefaultRule(size_t batchSize, const std::vector< size_t > &availableBatchSizes)
Definition: Batching.cc:101