123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- #ifndef CNSTREAM_CONFIG_HPP_
- #define CNSTREAM_CONFIG_HPP_
- #include <list>
- #include <memory>
- #include <set>
- #include <string>
- #include <unordered_map>
- #include <utility>
- #include <vector>
- #include "cnstream_common.hpp"
- namespace cnstream {
- using ModuleParamSet = std::unordered_map<std::string, std::string>;
- std::string GetPathRelativeToTheJSONFile(const std::string &path, const ModuleParamSet ¶m_set);
- struct CNConfigBase {
- std::string config_root_dir = "";
-
- bool ParseByJSONFile(const std::string &jfname);
-
- virtual bool ParseByJSONStr(const std::string &jstr) = 0;
- };
- struct ProfilerConfig : public CNConfigBase {
- bool enable_profiling = false;
- bool enable_tracing = false;
- size_t trace_event_capacity = 100000;
-
- bool ParseByJSONStr(const std::string &jstr) override;
- };
- struct CNModuleConfig : public CNConfigBase {
- std::string name;
- std::unordered_map<std::string, std::string>
- parameters;
- int parallelism;
- int maxInputQueueSize;
- std::string className;
- std::set<std::string> next;
-
- bool ParseByJSONStr(const std::string &jstr) override;
- };
- struct CNSubgraphConfig : public CNConfigBase {
- std::string name;
- std::string config_path;
- std::set<std::string> next;
-
- bool ParseByJSONStr(const std::string &jstr) override;
- };
- struct CNGraphConfig : public CNConfigBase {
- std::string name = "";
- ProfilerConfig profiler_config;
- std::vector<CNModuleConfig> module_configs;
- std::vector<CNSubgraphConfig> subgraph_configs;
-
- bool ParseByJSONStr(const std::string &jstr) override;
- };
- class ParamRegister {
- private:
- std::vector<std::pair<std::string , std::string >> module_params_;
- std::string module_desc_;
- public:
-
- void Register(const std::string &key, const std::string &desc) {
- module_params_.push_back(std::make_pair(key, desc));
- }
-
- std::vector<std::pair<std::string, std::string>> GetParams() { return module_params_; }
-
- bool IsRegisted(const std::string &key) const {
- if (strcmp(key.c_str(), CNS_JSON_DIR_PARAM_NAME) == 0) {
- return true;
- }
- for (auto &it : module_params_) {
- if (key == it.first) {
- return true;
- }
- }
- return false;
- }
-
- void SetModuleDesc(const std::string &desc) { module_desc_ = desc; }
-
- std::string GetModuleDesc() { return module_desc_; }
- };
- class ParametersChecker {
- public:
-
- bool CheckPath(const std::string &path, const ModuleParamSet ¶mSet) {
- std::string relative_path = GetPathRelativeToTheJSONFile(path, paramSet);
- if ((access(relative_path.c_str(), R_OK)) == -1) {
- return false;
- }
- return true;
- }
-
- bool IsNum(const std::list<std::string> &check_list, const ModuleParamSet ¶mSet, std::string &err_msg,
- bool greater_than_zero = false) {
- for (auto &it : check_list) {
- if (paramSet.find(it) != paramSet.end()) {
- std::stringstream sin(paramSet.find(it)->second);
- double d;
- char c;
- if (!(sin >> d)) {
- err_msg = "[" + it + "] : " + paramSet.find(it)->second + " is not a number.";
- return false;
- }
- if (sin >> c) {
- err_msg = "[" + it + "] : " + paramSet.find(it)->second + " is not a number.";
- return false;
- }
- if (greater_than_zero) {
- if (d < 0) {
- err_msg = "[" + it + "] : " + paramSet.find(it)->second + " must be greater than zero.";
- return false;
- }
- }
- }
- }
- return true;
- }
- };
- }
- #endif // CNSTREAM_CONFIG_HPP_
|