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.

ps_context.cc 2.4 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "ps/ps_context.h"
  17. #include "utils/log_adapter.h"
  18. #include "utils/ms_utils.h"
  19. namespace mindspore {
  20. namespace ps {
  21. std::shared_ptr<PSContext> PSContext::instance() {
  22. static std::shared_ptr<PSContext> ps_instance = nullptr;
  23. if (ps_instance == nullptr) {
  24. ps_instance.reset(new (std::nothrow) PSContext());
  25. }
  26. return ps_instance;
  27. }
  28. void PSContext::SetPSEnable(bool enabled) {
  29. ps_enabled_ = enabled;
  30. if (ps_enabled_) {
  31. std::string ms_role = common::GetEnv(kEnvRole);
  32. MS_LOG(INFO) << "PS mode is enabled. MS_ROLE is " << ms_role;
  33. if (ms_role == kEnvRoleOfWorker) {
  34. is_worker_ = true;
  35. } else if (ms_role == kEnvRoleOfPServer) {
  36. is_pserver_ = true;
  37. } else if (ms_role == kEnvRoleOfScheduler) {
  38. is_sched_ = true;
  39. } else {
  40. MS_LOG(WARNING) << "MS_ROLE is " << ms_role << ", which is invalid.";
  41. }
  42. } else {
  43. MS_LOG(INFO) << "PS mode is disabled.";
  44. is_worker_ = false;
  45. is_pserver_ = false;
  46. is_sched_ = false;
  47. }
  48. }
  49. bool PSContext::is_ps_enabled() const { return ps_enabled_; }
  50. void PSContext::Reset() {
  51. ps_enabled_ = false;
  52. is_worker_ = false;
  53. is_pserver_ = false;
  54. is_sched_ = false;
  55. }
  56. std::string PSContext::ms_role() const {
  57. if (is_worker_) {
  58. return kEnvRoleOfWorker;
  59. } else if (is_pserver_) {
  60. return kEnvRoleOfPServer;
  61. } else if (is_sched_) {
  62. return kEnvRoleOfScheduler;
  63. } else {
  64. return kEnvRoleOfNotPS;
  65. }
  66. }
  67. bool PSContext::is_role_worker() const { return is_worker_; }
  68. bool PSContext::is_role_pserver() const { return is_pserver_; }
  69. bool PSContext::is_role_sched() const { return is_sched_; }
  70. void PSContext::SetPSRankId(int rank_id) { rank_id_ = rank_id; }
  71. int PSContext::ps_rank_id() const { return rank_id_; }
  72. } // namespace ps
  73. } // namespace mindspore