• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 #ifndef MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_FACTORY_H_
17 #define MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_FACTORY_H_
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include "utils/ms_utils.h"
25 #include "backend/session/session_basic.h"
26 namespace mindspore {
27 namespace session {
28 using SessionCreator = std::function<std::shared_ptr<SessionBasic>()>;
29 class SessionFactory {
30  public:
31   static SessionFactory &Get();
32   void Register(const std::string &device_name, SessionCreator &&session_creator);
33   std::shared_ptr<SessionBasic> Create(const std::string &device_name);
34 
35  private:
36   SessionFactory() = default;
37   ~SessionFactory() = default;
38   DISABLE_COPY_AND_ASSIGN(SessionFactory)
39   std::map<std::string, SessionCreator> session_creators_;
40 };
41 
42 class SessionRegistrar {
43  public:
SessionRegistrar(const std::string & device_name,SessionCreator && session_creator)44   SessionRegistrar(const std::string &device_name, SessionCreator &&session_creator) {
45     SessionFactory::Get().Register(device_name, std::move(session_creator));
46   }
47   ~SessionRegistrar() = default;
48 };
49 
50 #define MS_REG_SESSION(DEVICE_NAME, SESSION_CLASS)                           \
51   static const SessionRegistrar g_session_registrar__##DEVICE_NAME##_##_reg( \
52     DEVICE_NAME, []() { return std::make_shared<SESSION_CLASS>(); });
53 }  // namespace session
54 }  // namespace mindspore
55 
56 #endif  // MINDSPORE_CCSRC_BACKEND_SESSION_SESSION_FACTORY_H_
57