ptrendx commented on a change in pull request #19269: URL: https://github.com/apache/incubator-mxnet/pull/19269#discussion_r506722767
########## File path: src/imperative/simple_partition_pass.cc ########## @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2020 by Contributors + * \file simple_partition_pass.cc + * \brief Utilities used in simple partition pass + * \author Przemyslaw Tredak + */ + +#include "./simple_partition_pass.h" +#include <memory> +#include <utility> + +namespace mxnet { +namespace exec { + +namespace detail { + +void PrintSets(const IntervalVec* const sets_p) { + if (sets_p == nullptr || sets_p->size() == 0) { + std::cout << "{}" << std::endl; + return; + } + const auto& sets = *sets_p; + std::cout << "{"; + for (size_t i = 0; i < sets.size() - 1; ++i) { + std::cout << "[" << sets[i].first << "," << sets[i].second << "], "; + } + std::cout << "[" << sets[sets.size()-1].first << "," + << sets[sets.size()-1].second << "]}" << std::endl; +} + +const IntervalVec* LargerSet(const IntervalVec* const first, + const IntervalVec* const second) noexcept { + const IntervalVec* ret = nullptr; + auto first_iter = first->begin(); + auto second_iter = second->begin(); + while (first_iter != first->end() && + second_iter != second->end()) { + if (*first_iter == *second_iter) { + ++first_iter; + ++second_iter; + } else { + // Entry in first set not seen in the second set + if (first_iter->second < second_iter->first) { + if (ret == first || ret == nullptr) { + ret = first; + ++first_iter; + } else { + return nullptr; + } + continue; + } + // Entry in second set not seen in the first set + if (second_iter->second < first_iter->first) { + if (ret == second || ret == nullptr) { + ret = second; + ++second_iter; + } else { + return nullptr; + } + continue; + } + // Entry in first set fully encloses the entry in the second set + if (first_iter->first <= second_iter->first && + first_iter->second >= second_iter->second) { + if (ret == first || ret == nullptr) { + ret = first; + ++second_iter; + } else { + return nullptr; + } + continue; + } + // Entry in second set fully encloses the entry in the first set + if (second_iter->first <= first_iter->first && + second_iter->second >= first_iter->second) { + if (ret == second || ret == nullptr) { + ret = second; + ++first_iter; + } else { + return nullptr; + } + continue; + } + // Entries intersect but one is not fully enclosed in the other + return nullptr; + } + } + if (ret == nullptr) { + // The common part is the same + return second_iter == second->end() ? first : second; + } else { + if ((ret == first && second_iter == second->end()) || + (ret == second && first_iter == first->end())) { + return ret; + } + } + return nullptr; +} + +void MergeSets(const IntervalVec** const my_set, + const IntervalVec* const other_set, + std::vector<std::unique_ptr<const IntervalVec>>* const storage) noexcept { + if ((*my_set == nullptr) || (*my_set)->size() == 0) { + *my_set = other_set; + return; + } + if (other_set == nullptr || other_set->size() == 0) { + return; + } + auto* larger_set = LargerSet(*my_set, other_set); + if (larger_set != nullptr) { + *my_set = larger_set; + return; + } + auto my_iter = (*my_set)->cbegin(); + auto other_iter = other_set->cbegin(); + auto new_set = IntervalVec(); + int last_end = -10; // less than -1 + while (my_iter != (*my_set)->cend() && + other_iter != other_set->cend()) { + const auto& mine = *my_iter; + const auto& other = *other_iter; + if (other.second < mine.first - 1) { + // other interval is before ours + if (last_end >= other.first - 1) { + new_set.back().second = other.second; + } else { + new_set.emplace_back(other); + } + last_end = other.second; + ++other_iter; + } else if (other.first > mine.second + 1) { + // other interval is after ours + if (last_end >= mine.first - 1) { + new_set.back().second = std::max(mine.second, last_end); + } else { + new_set.emplace_back(mine); + } + last_end = new_set.back().second; + ++my_iter; + } else { + // Intervals can be merged together + Interval n(std::min(mine.first, other.first), + std::max(mine.second, other.second)); + if (last_end >= n.first - 1) { + new_set.back().second = n.second; + } else { + new_set.emplace_back(n); + } + last_end = n.second; + if (other.second >= mine.second) { + ++my_iter; + } + if (mine.second >= other.second) { + ++other_iter; + } + } + } + // Add the rest of entries + for (; my_iter != (*my_set)->cend(); ++my_iter) { + auto& mine = new_set.back(); + const auto& other = *my_iter; + if (other.second < mine.first - 1) { + // other interval is before ours, should never happen + continue; + } else if (other.first > mine.second + 1) { + // other interval is after ours + new_set.emplace_back(other); + } else { + // Intervals can be merged together + mine.first = std::min(mine.first, other.first); + mine.second = std::max(mine.second, other.second); + } + } + for (; other_iter != other_set->cend(); ++other_iter) { Review comment: Yes, I refactored it to have only 1 loop. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
