You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

session_factory.h 1.9 kB

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