partition_range_for_parallel_for.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Authors: vitus@google.com (Michael Vitus),
  30. // dmitriy.korchemkin@gmail.com (Dmitriy Korchemkin)
  31. #ifndef CERES_INTERNAL_PARTITION_RANGE_FOR_PARALLEL_FOR_H_
  32. #define CERES_INTERNAL_PARTITION_RANGE_FOR_PARALLEL_FOR_H_
  33. #include <algorithm>
  34. #include <vector>
  35. namespace ceres::internal {
  36. // Check if it is possible to split range [start; end) into at most
  37. // max_num_partitions contiguous partitions of cost not greater than
  38. // max_partition_cost. Inclusive integer cumulative costs are provided by
  39. // cumulative_cost_data objects, with cumulative_cost_offset being a total cost
  40. // of all indices (starting from zero) preceding start element. Cumulative costs
  41. // are returned by cumulative_cost_fun called with a reference to
  42. // cumulative_cost_data element with index from range[start; end), and should be
  43. // non-decreasing. Partition of the range is returned via partition argument
  44. template <typename CumulativeCostData, typename CumulativeCostFun>
  45. bool MaxPartitionCostIsFeasible(int start,
  46. int end,
  47. int max_num_partitions,
  48. int max_partition_cost,
  49. int cumulative_cost_offset,
  50. const CumulativeCostData* cumulative_cost_data,
  51. CumulativeCostFun&& cumulative_cost_fun,
  52. std::vector<int>* partition) {
  53. partition->clear();
  54. partition->push_back(start);
  55. int partition_start = start;
  56. int cost_offset = cumulative_cost_offset;
  57. while (partition_start < end) {
  58. // Already have max_num_partitions
  59. if (partition->size() > max_num_partitions) {
  60. return false;
  61. }
  62. const int target = max_partition_cost + cost_offset;
  63. const int partition_end =
  64. std::partition_point(
  65. cumulative_cost_data + partition_start,
  66. cumulative_cost_data + end,
  67. [&cumulative_cost_fun, target](const CumulativeCostData& item) {
  68. return cumulative_cost_fun(item) <= target;
  69. }) -
  70. cumulative_cost_data;
  71. // Unable to make a partition from a single element
  72. if (partition_end == partition_start) {
  73. return false;
  74. }
  75. const int cost_last =
  76. cumulative_cost_fun(cumulative_cost_data[partition_end - 1]);
  77. partition->push_back(partition_end);
  78. partition_start = partition_end;
  79. cost_offset = cost_last;
  80. }
  81. return true;
  82. }
  83. // Split integer interval [start, end) into at most max_num_partitions
  84. // contiguous intervals, minimizing maximal total cost of a single interval.
  85. // Inclusive integer cumulative costs for each (zero-based) index are provided
  86. // by cumulative_cost_data objects, and are returned by cumulative_cost_fun call
  87. // with a reference to one of the objects from range [start, end)
  88. template <typename CumulativeCostData, typename CumulativeCostFun>
  89. std::vector<int> PartitionRangeForParallelFor(
  90. int start,
  91. int end,
  92. int max_num_partitions,
  93. const CumulativeCostData* cumulative_cost_data,
  94. CumulativeCostFun&& cumulative_cost_fun) {
  95. // Given maximal partition cost, it is possible to verify if it is admissible
  96. // and obtain corresponding partition using MaxPartitionCostIsFeasible
  97. // function. In order to find the lowest admissible value, a binary search
  98. // over all potentially optimal cost values is being performed
  99. const int cumulative_cost_last =
  100. cumulative_cost_fun(cumulative_cost_data[end - 1]);
  101. const int cumulative_cost_offset =
  102. start ? cumulative_cost_fun(cumulative_cost_data[start - 1]) : 0;
  103. const int total_cost = cumulative_cost_last - cumulative_cost_offset;
  104. // Minimal maximal partition cost is not smaller than the average
  105. // We will use non-inclusive lower bound
  106. int partition_cost_lower_bound = total_cost / max_num_partitions - 1;
  107. // Minimal maximal partition cost is not larger than the total cost
  108. // Upper bound is inclusive
  109. int partition_cost_upper_bound = total_cost;
  110. std::vector<int> partition;
  111. // Range partition corresponding to the latest evaluated upper bound.
  112. // A single segment covering the whole input interval [start, end) corresponds
  113. // to minimal maximal partition cost of total_cost.
  114. std::vector<int> partition_upper_bound = {start, end};
  115. // Binary search over partition cost, returning the lowest admissible cost
  116. while (partition_cost_upper_bound - partition_cost_lower_bound > 1) {
  117. partition.reserve(max_num_partitions + 1);
  118. const int partition_cost =
  119. partition_cost_lower_bound +
  120. (partition_cost_upper_bound - partition_cost_lower_bound) / 2;
  121. bool admissible = MaxPartitionCostIsFeasible(
  122. start,
  123. end,
  124. max_num_partitions,
  125. partition_cost,
  126. cumulative_cost_offset,
  127. cumulative_cost_data,
  128. std::forward<CumulativeCostFun>(cumulative_cost_fun),
  129. &partition);
  130. if (admissible) {
  131. partition_cost_upper_bound = partition_cost;
  132. std::swap(partition, partition_upper_bound);
  133. } else {
  134. partition_cost_lower_bound = partition_cost;
  135. }
  136. }
  137. return partition_upper_bound;
  138. }
  139. } // namespace ceres::internal
  140. #endif