• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_DF_GRAPH_MANAGER_H_
17 #define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_DF_GRAPH_MANAGER_H_
18 
19 #include <set>
20 #include <string>
21 #include <memory>
22 #include <vector>
23 #include <mutex>
24 #include <map>
25 #include <utility>
26 #include "transform/graph_ir/types.h"
27 #include "ir/anf.h"
28 
29 namespace mindspore {
30 const char BROADCAST_GRAPH_NAME[] = "broadcast_subgraph";
31 
32 namespace transform {
33 class GraphRunner;
34 using OptionMap = std::map<std::string, std::string>;
35 
36 struct DfGraphWrapper {
37  public:
38   DfGraphWrapper(const std::string &name, const int &id, const DfGraphPtr &graph_ptr, const OptionMap &options);
~DfGraphWrapperDfGraphWrapper39   ~DfGraphWrapper() {}
40 
41   std::string name_;
42   int id_;
43   DfGraphPtr graph_ptr_;
44   OptionMap options_ = {};
45 };
46 
47 using DfGraphWrapperPtr = std::shared_ptr<DfGraphWrapper>;
48 
49 class DfGraphManager {
50  public:
51   ~DfGraphManager();
52   void ClearGraph() noexcept;
53 
54   static DfGraphManager &GetInstance();
55   Status AddGraph(const std::string &name, const DfGraphPtr &graph, const OptionMap &options = {});
56   std::vector<DfGraphWrapperPtr> GetAllGraphs();
57   std::set<string> GetSavedGraphs();
58   void AddSavedGraphs(const std::string &id);
59   DfGraphWrapperPtr GetGraphByName(const std::string &name);
60   DfGraphManager(const DfGraphManager &) = delete;
61   void SetAnfGraph(const std::string &name, const AnfGraphPtr &anf_graph_ptr);
62   AnfGraphPtr GetAnfGraph(uint32_t graph_id);
63   std::shared_ptr<transform::GraphRunner> GetGraphRunner();
64   void SetGraphRunner(const std::shared_ptr<transform::GraphRunner> &graph_runner_ptr) noexcept;
65   void DeleteGraphRunner() noexcept;
66   void SetGeSession(const std::shared_ptr<ge::Session> &sess_ptr);
67   std::shared_ptr<ge::Session> GetGeSession();
68   void DeleteGeSession() noexcept;
69   void EraseAnfGraph();
70 
71  private:
72   DfGraphManager();
73   int GenerateId();
74 
75   std::mutex lock_;
76   std::map<std::string, DfGraphWrapperPtr> graphs_;
77   std::set<string> saved_graphs_;
78   int graph_id_;
79   std::map<uint32_t, AnfGraphPtr> anf_graphs_;
80   std::shared_ptr<transform::GraphRunner> graph_runner_ptr_;
81   std::shared_ptr<ge::Session> sess_ptr_;
82 };
83 }  // namespace transform
84 }  // namespace mindspore
85 
86 #endif  // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_DF_GRAPH_MANAGER_H_
87