• 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 #include "utils/ms_utils.h"
17 #include <map>
18 #include <set>
19 #include <string>
20 #include <sstream>
21 #include <ostream>
22 #include <iostream>
23 
24 namespace mindspore {
25 namespace common {
26 namespace {
27 const int CACHED_STR_NUM = 1 << 8;
28 const int CACHED_STR_MASK = CACHED_STR_NUM - 1;
29 std::vector<std::string> STR_HOLDER(CACHED_STR_NUM);
30 }  // namespace
SafeCStr(const std::string && str)31 const char *SafeCStr(const std::string &&str) {
32   static std::atomic<uint32_t> index{0};
33   uint32_t cur_index = index++;
34   cur_index = cur_index & CACHED_STR_MASK;
35   STR_HOLDER[cur_index] = str;
36   return STR_HOLDER[cur_index].c_str();
37 }
38 
39 namespace {
40 class Config {
41  public:
42   static std::string GetValue(const std::string &config, const std::string &config_key);
43   static void Reset(const std::string &config);
44 
45  private:
46   static std::map<std::string, std::map<std::string, std::string>> configs;
47   static std::set<std::string> has_parsed_config;
48 };
49 
50 std::map<std::string, std::map<std::string, std::string>> Config::configs;
51 std::set<std::string> Config::has_parsed_config;
52 
GetValue(const std::string & config,const std::string & config_key)53 std::string Config::GetValue(const std::string &config, const std::string &config_key) {
54   auto ret_val = has_parsed_config.insert(config);
55   if (ret_val.second) {
56     // Parse config.
57     std::string env_value = GetEnv(config);
58     if (env_value.empty()) {
59       return "";
60     }
61 
62     std::ostringstream oss_buf;
63     oss_buf << "[" << config << "]Runtime config:";
64     std::stringstream ss(env_value);
65     std::string item;
66     while (std::getline(ss, item, ',')) {
67       std::size_t delimiterPos = item.find(':');
68       if (delimiterPos != std::string::npos) {
69         std::string key = item.substr(0, delimiterPos);
70         std::string value = item.substr(delimiterPos + 1);
71         oss_buf << "  " << key << ":" << value;
72         configs[config][key] = value;
73       }
74     }
75     std::cout << oss_buf.str() << std::endl;
76   }
77   auto configs_iter = configs.find(config);
78   if (configs_iter == configs.end()) {
79     return "";
80   }
81   if (configs_iter->second.count(config_key) == 0) {
82     return "";
83   }
84   return configs_iter->second.at(config_key);
85 }
86 
Reset(const std::string & config)87 void Config::Reset(const std::string &config) { (void)has_parsed_config.erase(config); }
88 }  // namespace
89 
ResetConfig(const std::string & config)90 MS_CORE_API void ResetConfig(const std::string &config) { Config::Reset(config); }
91 
GetConfigValue(const std::string & config,const std::string & config_key)92 std::string GetConfigValue(const std::string &config, const std::string &config_key) {
93   return Config::GetValue(config, config_key);
94 }
95 
IsEnableRuntimeConfig(const std::string & runtime_config)96 bool IsEnableRuntimeConfig(const std::string &runtime_config) {
97   const auto &value = GetConfigValue(kRuntimeConf, runtime_config);
98   return ((value == "True") || (value == "true"));
99 }
100 
IsDisableRuntimeConfig(const std::string & runtime_config)101 bool IsDisableRuntimeConfig(const std::string &runtime_config) {
102   const auto &value = GetConfigValue(kRuntimeConf, runtime_config);
103   return ((value == "False") || (value == "false"));
104 }
105 
GetAllocConfigValue(const std::string & alloc_config)106 std::string GetAllocConfigValue(const std::string &alloc_config) {
107   const auto &value = GetConfigValue(kAllocConf, alloc_config);
108   return value;
109 }
110 
IsEnableAlllocConfig(const std::string & alloc_config)111 bool IsEnableAlllocConfig(const std::string &alloc_config) {
112   const auto &value = GetAllocConfigValue(alloc_config);
113   return ((value == "True") || (value == "true"));
114 }
115 
IsDisableAlllocConfig(const std::string & alloc_config)116 bool IsDisableAlllocConfig(const std::string &alloc_config) {
117   const auto &value = GetAllocConfigValue(alloc_config);
118   return ((value == "False") || (value == "false"));
119 }
120 }  // namespace common
121 }  // namespace mindspore
122