1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 8 * http://www.apache.org/licenses/LICENSE-2.0 9 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_ 18 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_ 19 20 #include <algorithm> 21 #include <chrono> 22 #include <memory> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 27 #include "backend/optimizer/somas/somas_solver_alg.h" 28 #include "backend/optimizer/somas/somas_solver_pre.h" 29 30 namespace mindspore { 31 namespace somas { 32 33 class SomasSolverCore { 34 public: 35 /// Interface Function: receive parameters, creates the model to solve and then save the result 36 SomasSolverCore(const TensorsDescMap &tensors, const std::vector<DynamicBitSet> *constraints, uint32_t sol, 37 bool isMultiThreadValid = true) 38 : best_sol_(0), 39 sort_strategy_(kGreaterSizeSmallerIndex), 40 branching_strategy_(kBest), 41 sol_count_(sol), 42 algorithm_(kManyObjects), 43 tensors_(tensors), 44 constraints_(*constraints), 45 upperbound_(SIZE_MAX), 46 verify_(false), 47 all_(true), 48 is_multi_thread_valid_(isMultiThreadValid) {} 49 ~SomasSolverCore() = default; 50 51 Status MemoryAllocationSolver(); 52 Status Verify(); 53 bool Verify(const size_t &); VerifySolution(const bool verify)54 void VerifySolution(const bool verify) { verify_ = verify; } 55 void SortTensors(); 56 void BuildBlocks(); 57 void Clean(); SetBestSolution()58 void SetBestSolution() { RestoreSolution(best_sol_); } 59 void RestoreSolution(uint32_t sol_id); SetSortingStrategy(SortingType sort_strategy)60 void SetSortingStrategy(SortingType sort_strategy) { sort_strategy_ = sort_strategy; } SetFittingStrategy(FittingType branching_strategy)61 void SetFittingStrategy(FittingType branching_strategy) { branching_strategy_ = branching_strategy; } SetAlgorithmStrategy(AlgorithmType algorithm_strategy)62 void SetAlgorithmStrategy(AlgorithmType algorithm_strategy) { algorithm_ = algorithm_strategy; } SetAllStrategies(bool all)63 void SetAllStrategies(bool all) { all_ = all; } GetUpperbound()64 const size_t &GetUpperbound() const { return upperbound_; } Getlifelongmemory()65 const size_t &Getlifelongmemory() const { return lifelong_memory_; } 66 67 uint32_t best_sol_{0}; 68 SortingType sort_strategy_; 69 FittingType branching_strategy_; 70 uint32_t sol_count_{0}; 71 AlgorithmType algorithm_; 72 int64_t timing_{0}; 73 74 private: 75 const TensorsDescMap &tensors_; 76 vector<BlockTensor> block_tensors_; 77 const std::vector<DynamicBitSet> &constraints_; 78 size_t upperbound_{0}; 79 size_t lifelong_memory_{0}; 80 bool verify_{false}; 81 bool all_{false}; 82 bool is_multi_thread_valid_{true}; 83 84 size_t FindSolutions(); 85 size_t Search(const std::shared_ptr<FootPrint> &pFootprint); 86 void AppendLifelongTensors(); 87 void Destroy(std::shared_ptr<FootPrint> &); 88 }; 89 } // namespace somas 90 } // namespace mindspore 91 92 #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_SOMAS_SOMAS_SOLVER_CORE_H_ 93