25#include "absl/log/check.h"
26#include "absl/numeric/bits.h"
27#include "absl/random/distributions.h"
28#include "absl/types/span.h"
40 if (!VLOG_IS_ON(1))
return;
41 std::vector<std::pair<std::string, int64_t>> stats;
43 {
"OrthogonalPackingInfeasibilityDetector/called", num_calls_});
45 {
"OrthogonalPackingInfeasibilityDetector/conflicts", num_conflicts_});
46 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/dff0_conflicts",
47 num_conflicts_dff0_});
48 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/dff2_conflicts",
49 num_conflicts_dff2_});
50 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/trivial_conflicts",
51 num_trivial_conflicts_});
52 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/conflicts_two_items",
53 num_conflicts_two_items_});
54 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/no_energy_conflict",
55 num_scheduling_possible_});
56 stats.push_back({
"OrthogonalPackingInfeasibilityDetector/brute_force_calls",
57 num_brute_force_calls_});
59 {
"OrthogonalPackingInfeasibilityDetector/brute_force_conflicts",
60 num_brute_force_conflicts_});
62 {
"OrthogonalPackingInfeasibilityDetector/brute_force_relaxations",
63 num_brute_force_relaxation_});
69std::optional<std::pair<int, int>> FindPairwiseConflict(
70 absl::Span<const IntegerValue> sizes_x,
71 absl::Span<const IntegerValue> sizes_y,
72 std::pair<IntegerValue, IntegerValue> bounding_box_size,
73 const std::vector<int>& index_by_decreasing_x_size,
74 const std::vector<int>& index_by_decreasing_y_size) {
79 while (x_idx < index_by_decreasing_x_size.size() &&
80 y_idx < index_by_decreasing_y_size.size()) {
81 if (index_by_decreasing_x_size[x_idx] ==
82 index_by_decreasing_y_size[y_idx]) {
83 if (sizes_x[index_by_decreasing_x_size[x_idx]] >
84 sizes_y[index_by_decreasing_x_size[x_idx]]) {
91 const bool overlap_on_x = (sizes_x[index_by_decreasing_x_size[x_idx]] +
92 sizes_x[index_by_decreasing_y_size[y_idx]] >
93 bounding_box_size.first);
94 const bool overlap_on_y = (sizes_y[index_by_decreasing_x_size[x_idx]] +
95 sizes_y[index_by_decreasing_y_size[y_idx]] >
96 bounding_box_size.second);
97 if (overlap_on_x && overlap_on_y) {
98 return std::make_pair(index_by_decreasing_x_size[x_idx],
99 index_by_decreasing_y_size[y_idx]);
100 }
else if (overlap_on_x) {
102 }
else if (overlap_on_y) {
111IntegerValue RoundingLowestInverse(IntegerValue
y, IntegerValue c_k,
112 IntegerValue max_x, IntegerValue k) {
114 DCHECK_LE(
y, 2 * c_k);
115 IntegerValue ret = std::numeric_limits<IntegerValue>::max();
118 if (
y <= c_k && (max_x.value() & 1) == 0) {
119 const IntegerValue inverse_mid = max_x / 2;
120 ret = std::min(ret, inverse_mid);
121 if (
y == c_k &&
y.value() & 1) {
131 const IntegerValue inverse_high = max_x - k * (c_k -
y / 2);
132 if (2 * inverse_high > max_x) {
136 const IntegerValue lowest_inverse_high =
137 std::max(max_x / 2 + 1, inverse_high - k + 1);
138 ret = std::min(ret, lowest_inverse_high);
142 const IntegerValue inverse_low = k *
y / 2;
143 if (2 * inverse_low < max_x) {
144 ret = std::min(ret, inverse_low);
151 return RoundingLowestInverse(
y, c_k_, max_x_, k_);
155 IntegerValue
y)
const {
156 return RoundingLowestInverse(
y, c_k_, max_x_, IntegerValue(1) << log2_k_);
173 absl::Span<const IntegerValue> sizes_x,
174 absl::Span<const IntegerValue> sizes_y,
175 absl::Span<const int> index_by_decreasing_x_size,
176 absl::Span<const IntegerValue> g_x, IntegerValue g_max,
177 IntegerValue x_bb_size, IntegerValue total_energy, IntegerValue bb_area,
178 IntegerValue* best_k) {
182 int num_items = sizes_x.size();
183 auto build_result = [&sizes_x, &sizes_y, num_items, &x_bb_size, &bb_area,
184 &g_max, &g_x](
const IntegerValue k) {
185 std::vector<std::pair<int, IntegerValue>> index_to_energy;
186 index_to_energy.reserve(num_items);
187 for (
int i = 0;
i < num_items;
i++) {
188 IntegerValue point_value;
189 if (sizes_x[
i] > x_bb_size - k) {
191 }
else if (sizes_x[
i] < k) {
194 point_value = g_x[
i];
196 index_to_energy.push_back({
i, point_value * sizes_y[
i]});
198 std::sort(index_to_energy.begin(), index_to_energy.end(),
199 [](
const std::pair<int, IntegerValue>&
a,
200 const std::pair<int, IntegerValue>&
b) {
201 return a.second > b.second;
203 IntegerValue recomputed_energy = 0;
204 for (
int i = 0;
i < index_to_energy.size();
i++) {
205 recomputed_energy += index_to_energy[
i].second;
206 if (recomputed_energy > bb_area) {
207 OrthogonalPackingResult result(
209 result.conflict_type_ = OrthogonalPackingResult::ConflictType::DFF_F0;
210 result.items_participating_on_conflict_.resize(
i + 1);
211 for (
int j = 0; j <=
i; j++) {
212 const int index = index_to_energy[j].first;
213 result.items_participating_on_conflict_[j] = {
215 .size_x = sizes_x[
index],
216 .size_y = sizes_y[
index]};
222 LOG(FATAL) <<
"build_result called with no conflict";
232 IntegerValue current_energy = total_energy;
233 OrthogonalPackingResult best_result;
234 if (current_energy > bb_area) {
235 best_result = build_result(0);
240 int removing_item_index = index_by_decreasing_x_size.size() - 1;
241 int enlarging_item_index = 0;
242 while (enlarging_item_index < index_by_decreasing_x_size.size()) {
243 int index = index_by_decreasing_x_size[enlarging_item_index];
248 const IntegerValue k = x_bb_size -
size + 1;
249 if (2 * k > x_bb_size) {
256 index = index_by_decreasing_x_size[enlarging_item_index];
258 current_energy += (g_max - g_x[
index]) * sizes_y[
index];
259 enlarging_item_index++;
260 }
while (enlarging_item_index < index_by_decreasing_x_size.size() &&
261 sizes_x[index_by_decreasing_x_size[enlarging_item_index]] ==
size);
265 while (removing_item_index >= 0 &&
266 sizes_x[index_by_decreasing_x_size[removing_item_index]] < k) {
267 const int remove_idx = index_by_decreasing_x_size[removing_item_index];
268 current_energy -= g_x[remove_idx] * sizes_y[remove_idx];
269 removing_item_index--;
272 if (current_energy > bb_area) {
273 OrthogonalPackingResult current_result = build_result(k);
274 if (current_result.IsBetterThan(best_result)) {
275 best_result = current_result;
307bool FindHeuristicSchedulingSolution(
308 absl::Span<const IntegerValue> sizes,
309 absl::Span<const IntegerValue> demands,
310 absl::Span<const int> heuristic_order, IntegerValue global_end_max,
311 IntegerValue capacity_max,
312 std::vector<std::pair<IntegerValue, IntegerValue>>& profile,
313 std::vector<std::pair<IntegerValue, IntegerValue>>& new_profile) {
320 for (
int i = 0;
i < heuristic_order.size();
i++) {
321 const IntegerValue event_size = sizes[heuristic_order[
i]];
322 const IntegerValue event_demand = demands[heuristic_order[
i]];
323 const IntegerValue event_start_min = 0;
324 const IntegerValue event_start_max = global_end_max - event_size;
326 std::max(event_start_min, start_of_previous_task);
331 while (profile[current + 1].first <=
start_min ||
332 profile[current].second < event_demand) {
336 const IntegerValue actual_start =
337 std::max(
start_min, profile[current].first);
338 start_of_previous_task = actual_start;
341 if (actual_start > event_start_max)
return false;
343 const IntegerValue actual_end = actual_start + event_size;
346 if (
i == heuristic_order.size() - 1)
break;
350 new_profile.push_back(
351 {actual_start, profile[current].second - event_demand});
354 while (profile[current].first < actual_end) {
355 new_profile.push_back(
356 {profile[current].first, profile[current].second - event_demand});
360 if (profile[current].first > actual_end) {
361 new_profile.push_back(
362 {actual_end, new_profile.back().second + event_demand});
364 while (current < profile.size()) {
365 new_profile.push_back(profile[current]);
368 profile.swap(new_profile);
396void OrthogonalPackingInfeasibilityDetector::GetAllCandidatesForKForDff2(
397 absl::Span<const IntegerValue> sizes, IntegerValue bb_size,
398 IntegerValue sqrt_bb_size, Bitset64<IntegerValue>& candidates) {
400 candidates.ClearAndResize(bb_size / 2 + 2);
403 for (IntegerValue
i = 2;
i <= sqrt_bb_size;
i++) {
406 for (
int i = 1;
i <= sqrt_bb_size;
i++) {
407 const QuickSmallDivision div(
i);
409 candidates.Set(div.DivideByDivisor(bb_size.value()));
411 for (
int k = 0; k < sizes.size(); k++) {
412 IntegerValue
size = sizes[k];
413 if (2 *
size > bb_size &&
size < bb_size) {
414 candidates.Set(div.DivideByDivisor(bb_size.value() -
size.value() + 1));
415 }
else if (2 *
size < bb_size) {
416 candidates.Set(div.DivideByDivisor(
size.value()));
434 candidates.Resize(bb_size / 4 + 1);
435 candidates.Resize(bb_size / 3 + 2);
436 candidates.Set(bb_size / 4 + 1);
438 candidates.Set(bb_size / 3 + 1);
452OrthogonalPackingResult
453OrthogonalPackingInfeasibilityDetector::CheckFeasibilityWithDualFunction2(
454 absl::Span<const IntegerValue> sizes_x,
455 absl::Span<const IntegerValue> sizes_y,
456 absl::Span<const int> index_by_decreasing_x_size, IntegerValue x_bb_size,
457 IntegerValue y_bb_size,
int max_number_of_parameters_to_check) {
458 if (x_bb_size == 1) {
459 return OrthogonalPackingResult();
461 std::vector<IntegerValue> sizes_x_rescaled;
462 if (x_bb_size >= std::numeric_limits<uint16_t>::max()) {
467 absl::bit_width(
static_cast<uint64_t
>(x_bb_size.value() + 1)) - 16 + 1;
468 const RoundingDualFeasibleFunctionPowerOfTwo dff(x_bb_size, log2_k);
469 sizes_x_rescaled.resize(sizes_x.size());
470 for (
int i = 0;
i < sizes_x.size();
i++) {
471 sizes_x_rescaled[
i] = dff(sizes_x[
i]);
473 x_bb_size = dff(x_bb_size);
474 CHECK_LT(x_bb_size, std::numeric_limits<uint16_t>::max());
475 sizes_x = sizes_x_rescaled;
478 Bitset64<IntegerValue> candidates;
480 int num_items = sizes_x.size();
481 const IntegerValue max_possible_number_of_parameters =
482 std::min(x_bb_size / 4 + 1, sqrt_bb_size * num_items);
483 if (5ull * max_number_of_parameters_to_check <
484 max_possible_number_of_parameters) {
488 candidates.Resize(x_bb_size / 4 + 1);
489 int num_candidates = 0;
490 while (num_candidates < max_number_of_parameters_to_check) {
491 const IntegerValue pick =
492 absl::Uniform(random_, 1, x_bb_size.value() / 4);
493 if (!candidates.IsSet(pick)) {
494 candidates.Set(pick);
499 GetAllCandidatesForKForDff2(sizes_x, x_bb_size, sqrt_bb_size, candidates);
501 if (max_number_of_parameters_to_check < max_possible_number_of_parameters) {
505 for (
auto it = candidates.begin(); it != candidates.end(); ++it) {
508 if (count > max_number_of_parameters_to_check) {
509 std::vector<IntegerValue> sampled_candidates(
510 max_number_of_parameters_to_check);
511 std::sample(candidates.begin(), candidates.end(),
512 sampled_candidates.begin(),
513 max_number_of_parameters_to_check, random_);
514 candidates.ClearAll();
515 for (
const IntegerValue k : sampled_candidates) {
521 OrthogonalPackingResult best_result;
524 std::vector<IntegerValue> modified_sizes(num_items);
525 for (
const IntegerValue k : candidates) {
526 const RoundingDualFeasibleFunction dff(x_bb_size, k);
528 for (
int i = 0;
i < num_items;
i++) {
529 modified_sizes[
i] = dff(sizes_x[
i]);
530 energy += modified_sizes[
i] * sizes_y[
i];
532 const IntegerValue modified_x_bb_size = dff(x_bb_size);
535 GetDffConflict(sizes_x, sizes_y, index_by_decreasing_x_size,
536 modified_sizes, modified_x_bb_size, x_bb_size,
energy,
537 modified_x_bb_size * y_bb_size, &dff0_k);
541 DFFComposedF2F0 composed_dff(x_bb_size, dff0_k, k);
542 dff0_res.conflict_type_ = OrthogonalPackingResult::ConflictType::DFF_F2;
543 for (
auto& item : dff0_res.items_participating_on_conflict_) {
545 composed_dff.LowestInverse(composed_dff(sizes_x[item.index]));
549 DCHECK_EQ(composed_dff(item.size_x), composed_dff(sizes_x[item.index]));
550 DCHECK_LE(item.size_x, sizes_x[item.index]);
552 item.size_y = sizes_y[item.index];
554 if (dff0_res.IsBetterThan(best_result)) {
555 best_result = dff0_res;
562bool OrthogonalPackingInfeasibilityDetector::RelaxConflictWithBruteForce(
563 OrthogonalPackingResult& result,
564 std::pair<IntegerValue, IntegerValue> bounding_box_size,
565 int brute_force_threshold) {
566 const int num_items_originally =
567 result.items_participating_on_conflict_.size();
568 if (num_items_originally > 2 * brute_force_threshold) {
572 std::vector<IntegerValue> sizes_x;
573 std::vector<IntegerValue> sizes_y;
574 std::vector<int> indexes;
575 std::vector<bool> to_be_removed(num_items_originally,
false);
577 sizes_x.reserve(num_items_originally - 1);
578 sizes_y.reserve(num_items_originally - 1);
579 for (
int i = 0;
i < num_items_originally;
i++) {
583 for (
int j = 0; j < num_items_originally; j++) {
584 if (
i == j || to_be_removed[j]) {
587 sizes_x.push_back(result.items_participating_on_conflict_[j].size_x);
588 sizes_y.push_back(result.items_participating_on_conflict_[j].size_y);
591 sizes_x, sizes_y, bounding_box_size, brute_force_threshold);
594 to_be_removed[
i] =
true;
597 if (!std::any_of(to_be_removed.begin(), to_be_removed.end(),
598 [](
bool b) { return b; })) {
601 OrthogonalPackingResult original = result;
603 result.conflict_type_ = OrthogonalPackingResult::ConflictType::BRUTE_FORCE;
605 result.items_participating_on_conflict_.clear();
606 for (
int i = 0;
i < num_items_originally;
i++) {
607 if (to_be_removed[
i]) {
610 result.items_participating_on_conflict_.push_back(
611 original.items_participating_on_conflict_[
i]);
616OrthogonalPackingResult
617OrthogonalPackingInfeasibilityDetector::TestFeasibilityImpl(
618 absl::Span<const IntegerValue> sizes_x,
619 absl::Span<const IntegerValue> sizes_y,
620 std::pair<IntegerValue, IntegerValue> bounding_box_size,
621 const OrthogonalPackingOptions& options) {
622 using ConflictType = OrthogonalPackingResult::ConflictType;
624 const int num_items = sizes_x.size();
625 DCHECK_EQ(num_items, sizes_y.size());
626 const IntegerValue bb_area =
627 bounding_box_size.first * bounding_box_size.second;
628 IntegerValue total_energy = 0;
630 auto make_item = [sizes_x, sizes_y](
int i) {
631 return OrthogonalPackingResult::Item{
632 .index =
i, .size_x = sizes_x[
i], .size_y = sizes_y[
i]};
635 index_by_decreasing_x_size_.resize(num_items);
636 index_by_decreasing_y_size_.resize(num_items);
637 for (
int i = 0;
i < num_items;
i++) {
638 total_energy += sizes_x[
i] * sizes_y[
i];
639 index_by_decreasing_x_size_[
i] =
i;
640 index_by_decreasing_y_size_[
i] =
i;
641 if (sizes_x[
i] > bounding_box_size.first ||
642 sizes_y[
i] > bounding_box_size.second) {
643 OrthogonalPackingResult result(
645 result.conflict_type_ = ConflictType::TRIVIAL;
646 result.items_participating_on_conflict_ = {make_item(
i)};
651 if (num_items <= 1) {
655 std::sort(index_by_decreasing_x_size_.begin(),
656 index_by_decreasing_x_size_.end(),
657 [&sizes_x, &sizes_y](
int a,
int b) {
659 return std::tie(sizes_x[a], sizes_y[a]) >
660 std::tie(sizes_x[b], sizes_y[b]);
662 std::sort(index_by_decreasing_y_size_.begin(),
663 index_by_decreasing_y_size_.end(),
664 [&sizes_y, &sizes_x](
int a,
int b) {
665 return std::tie(sizes_y[a], sizes_x[a]) >
666 std::tie(sizes_y[b], sizes_x[b]);
670 if (options.use_pairwise) {
671 if (
auto pair = FindPairwiseConflict(sizes_x, sizes_y, bounding_box_size,
672 index_by_decreasing_x_size_,
673 index_by_decreasing_y_size_);
675 OrthogonalPackingResult result(
677 result.conflict_type_ = ConflictType::PAIRWISE;
678 result.items_participating_on_conflict_ = {
679 make_item(pair.value().first), make_item(pair.value().second)};
682 if (num_items == 2) {
688 if (total_energy > bb_area) {
689 result.conflict_type_ = ConflictType::TRIVIAL;
691 std::vector<std::pair<int, IntegerValue>> index_to_energy;
692 index_to_energy.reserve(num_items);
693 for (
int i = 0;
i < num_items;
i++) {
694 index_to_energy.push_back({
i, sizes_x[
i] * sizes_y[
i]});
696 std::sort(index_to_energy.begin(), index_to_energy.end(),
697 [](
const std::pair<int, IntegerValue>&
a,
698 const std::pair<int, IntegerValue>&
b) {
699 return a.second > b.second;
701 IntegerValue recomputed_energy = 0;
702 for (
int i = 0;
i < index_to_energy.size();
i++) {
703 recomputed_energy += index_to_energy[
i].second;
704 if (recomputed_energy > bb_area) {
705 result.items_participating_on_conflict_.resize(
i + 1);
706 for (
int j = 0; j <=
i; j++) {
707 result.items_participating_on_conflict_[j] =
708 make_item(index_to_energy[j].first);
710 result.slack_ = recomputed_energy - bb_area - 1;
716 const int minimum_conflict_size = options.use_pairwise ? 3 : 2;
717 if (result.items_participating_on_conflict_.size() == minimum_conflict_size) {
721 if (options.use_dff_f0) {
730 GetDffConflict(sizes_x, sizes_y, index_by_decreasing_x_size_, sizes_x,
731 bounding_box_size.first, bounding_box_size.first,
732 total_energy, bb_area, &best_k);
733 if (conflict.IsBetterThan(result)) {
738 GetDffConflict(sizes_y, sizes_x, index_by_decreasing_y_size_, sizes_y,
739 bounding_box_size.second, bounding_box_size.second,
740 total_energy, bb_area, &best_k);
741 for (
auto& item : conflict.items_participating_on_conflict_) {
742 std::swap(item.size_x, item.size_y);
744 if (conflict.IsBetterThan(result)) {
749 if (result.items_participating_on_conflict_.size() == minimum_conflict_size) {
753 bool found_scheduling_solution =
false;
754 if (options.use_dff_f2) {
758 if (FindHeuristicSchedulingSolution(
759 sizes_x, sizes_y, index_by_decreasing_x_size_,
760 bounding_box_size.first, bounding_box_size.second,
761 scheduling_profile_, new_scheduling_profile_) ||
762 FindHeuristicSchedulingSolution(
763 sizes_y, sizes_x, index_by_decreasing_y_size_,
764 bounding_box_size.second, bounding_box_size.first,
765 scheduling_profile_, new_scheduling_profile_)) {
766 num_scheduling_possible_++;
768 found_scheduling_solution =
true;
772 if (!found_scheduling_solution && options.use_dff_f2) {
775 auto conflict = CheckFeasibilityWithDualFunction2(
776 sizes_x, sizes_y, index_by_decreasing_x_size_, bounding_box_size.first,
777 bounding_box_size.second,
778 options.dff2_max_number_of_parameters_to_check);
779 if (conflict.IsBetterThan(result)) {
783 if (result.items_participating_on_conflict_.size() ==
784 minimum_conflict_size) {
787 conflict = CheckFeasibilityWithDualFunction2(
788 sizes_y, sizes_x, index_by_decreasing_y_size_, bounding_box_size.second,
789 bounding_box_size.first,
790 options.dff2_max_number_of_parameters_to_check);
791 for (
auto& item : conflict.items_participating_on_conflict_) {
792 std::swap(item.size_x, item.size_y);
794 if (conflict.IsBetterThan(result)) {
801 sizes_x, sizes_y, bounding_box_size, options.brute_force_threshold);
802 num_brute_force_calls_ +=
805 result.conflict_type_ = ConflictType::BRUTE_FORCE;
807 result.items_participating_on_conflict_.resize(num_items);
808 for (
int i = 0;
i < num_items;
i++) {
809 result.items_participating_on_conflict_[
i] = make_item(
i);
817 num_brute_force_relaxation_ += RelaxConflictWithBruteForce(
818 result, bounding_box_size, options.brute_force_threshold);
825 absl::Span<const IntegerValue> sizes_x,
826 absl::Span<const IntegerValue> sizes_y,
827 std::pair<IntegerValue, IntegerValue> bounding_box_size,
829 using ConflictType = OrthogonalPackingResult::ConflictType;
833 TestFeasibilityImpl(sizes_x, sizes_y, bounding_box_size, options);
837 switch (result.conflict_type_) {
838 case ConflictType::DFF_F0:
839 num_conflicts_dff0_++;
841 case ConflictType::DFF_F2:
842 num_conflicts_dff2_++;
844 case ConflictType::PAIRWISE:
845 num_conflicts_two_items_++;
847 case ConflictType::TRIVIAL:
849 num_trivial_conflicts_++;
851 case ConflictType::BRUTE_FORCE:
852 num_brute_force_conflicts_++;
854 case ConflictType::NO_CONFLICT:
855 LOG(FATAL) <<
"Should never happen";
864 Item& item = items_participating_on_conflict_[
i];
866 const IntegerValue orthogonal_size =
872 const IntegerValue new_size =
874 slack_ -= (
size - new_size) * orthogonal_size;
875 DCHECK_NE(
size, new_size);
876 DCHECK_GE(slack_, 0);
OrthogonalPackingResult TestFeasibility(absl::Span< const IntegerValue > sizes_x, absl::Span< const IntegerValue > sizes_y, std::pair< IntegerValue, IntegerValue > bounding_box_size, const OrthogonalPackingOptions &options=OrthogonalPackingOptions())
~OrthogonalPackingInfeasibilityDetector()
bool TryUseSlackToReduceItemSize(int i, Coord coord, IntegerValue lower_bound=0)
IntegerValue LowestInverse(IntegerValue y) const
IntegerValue LowestInverse(IntegerValue y) const
void AddStats(absl::Span< const std::pair< std::string, int64_t > > stats)
Adds a bunch of stats, adding count for the same key together.
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
int64_t FloorSquareRoot(int64_t a)
The argument must be non-negative.
BruteForceResult BruteForceOrthogonalPacking(absl::Span< const IntegerValue > sizes_x, absl::Span< const IntegerValue > sizes_y, std::pair< IntegerValue, IntegerValue > bounding_box_size, int max_complexity)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
In SWIG mode, we don't want anything besides these top-level includes.