• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2023 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_COMMON_SOMAS_SOMAS_SOLVER_CORE_H_
18 #define MINDSPORE_CCSRC_BACKEND_COMMON_SOMAS_SOMAS_SOLVER_CORE_H_
19 
20 #include <algorithm>
21 #include <chrono>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "utils/hash_map.h"
27 #include "backend/common/somas/somas_solver_alg.h"
28 #include "backend/common/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<VectorBitSet> *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         is_multi_thread_valid_(isMultiThreadValid) {}
48   ~SomasSolverCore() = default;
49 
50   Status MemoryAllocationSolver();
51   Status Verify();
52   bool Verify(const size_t &upperbound);
VerifySolution(const bool verify)53   void VerifySolution(const bool verify) { verify_ = verify; }
54   void SortTensors();
55   void BuildBlocks();
56   void Clean();
SetSortingStrategy(SortingType sort_strategy)57   void SetSortingStrategy(SortingType sort_strategy) { sort_strategy_ = sort_strategy; }
SetFittingStrategy(FittingType branching_strategy)58   void SetFittingStrategy(FittingType branching_strategy) { branching_strategy_ = branching_strategy; }
SetAlgorithmStrategy(AlgorithmType algorithm_strategy)59   void SetAlgorithmStrategy(AlgorithmType algorithm_strategy) { algorithm_ = algorithm_strategy; }
GetUpperbound()60   const size_t &GetUpperbound() const { return upperbound_; }
Getlifelongmemory()61   const size_t &Getlifelongmemory() const { return lifelong_memory_; }
62 
63   uint32_t best_sol_{0};
64   SortingType sort_strategy_;
65   FittingType branching_strategy_;
66   uint32_t sol_count_{0};
67   AlgorithmType algorithm_;
68   int64_t timing_{0};
69 
70  private:
71   const TensorsDescMap &tensors_;
72   vector<BlockTensor> block_tensors_;
73   const std::vector<VectorBitSet> &constraints_;
74   size_t upperbound_{0};
75   size_t lifelong_memory_{0};
76   bool verify_{false};
77   bool is_multi_thread_valid_{true};
78 
79   size_t FindSolutions();
80   size_t Search(const std::shared_ptr<FootPrint> &pFootprint);
81   void AppendLifelongTensors();
82   void Destroy(std::shared_ptr<FootPrint> *pFootprint) const;
83 };
84 }  // namespace somas
85 }  // namespace mindspore
86 
87 #endif  // MINDSPORE_CCSRC_BACKEND_COMMON_SOMAS_SOMAS_SOLVER_CORE_H_
88