forked from Cambricon/mlu-ops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_env.h
119 lines (106 loc) · 3.92 KB
/
config_env.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*************************************************************************
* Copyright (C) [2024] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#pragma once
#include <type_traits>
#include "core/tool.h"
#include "core/preprocessor.h"
#ifdef STR
#undef STR
#endif
#define STR(e) #e
namespace mluop {
namespace cfg {
#define MLUOP_CONFIG_ENV_TYPE_LIST \
MLUOP_TRACE_ENABLE, MLUOP_TRACE_ENABLE_API, MLUOP_TRACE_ENABLE_KERNEL, \
MLUOP_TRACE_DATA_DIR, MLUOP_DEBUG_KERNEL_TRACING, \
MLUOP_EVENT_ENABLE_API, MLUOP_EVENT_ENABLE_KERNEL, MLUOP_DUMP_API_COUNT
#define ENUM_CASE_CONFIG_ENV_TYPE(e) \
case ConfigEnvType::e: { \
return STR(e); \
}
#define CFG_ENUM_TO_STR(e) ConfigEnvTypeReflection(ConfigEnvType::e)
// wrap env into single file
enum ConfigEnvType {
MLUOP_TRACE_ENABLE,
MLUOP_TRACE_ENABLE_API,
MLUOP_TRACE_ENABLE_KERNEL,
MLUOP_TRACE_DATA_DIR,
MLUOP_DEBUG_KERNEL_TRACING,
MLUOP_EVENT_ENABLE_API,
MLUOP_EVENT_ENABLE_KERNEL,
MLUOP_DUMP_API_COUNT,
};
static const char* ConfigEnvTypeReflection(enum ConfigEnvType evt) {
switch (evt) {
MLUOP_PP_MAP(ENUM_CASE_CONFIG_ENV_TYPE, (MLUOP_CONFIG_ENV_TYPE_LIST));
}
return NULL;
}
class Config {
public:
static Config& instance() {
static Config cfg;
return cfg;
}
template <
enum ConfigEnvType T,
typename std::enable_if<T == ConfigEnvType::MLUOP_EVENT_ENABLE_API ||
T == ConfigEnvType::MLUOP_EVENT_ENABLE_KERNEL,
bool>::type = true>
static void set_event(bool enable) {
switch (T) {
case ConfigEnvType::MLUOP_EVENT_ENABLE_API:
instance().event_enable_api_ = enable;
break;
case ConfigEnvType::MLUOP_EVENT_ENABLE_KERNEL:
instance().event_enable_kernel_ = enable;
break;
default:
__builtin_unreachable();
}
}
template <
enum ConfigEnvType T,
typename std::enable_if<T == ConfigEnvType::MLUOP_EVENT_ENABLE_API ||
T == ConfigEnvType::MLUOP_EVENT_ENABLE_KERNEL,
bool>::type = true>
static bool get_event() {
if (T == MLUOP_EVENT_ENABLE_API) {
return instance().event_enable_api_;
}
if (T == MLUOP_EVENT_ENABLE_KERNEL) {
return instance().event_enable_kernel_;
}
}
private:
Config() = default;
// TODO(NONE): expose event enable/disable interface (not just env)
// TODO(NONE): should be refined: add direct mapping which could avoid
// 'if-else'/'switch-case' pattern
bool event_enable_api_ =
mluop::getBoolEnvVar(CFG_ENUM_TO_STR(MLUOP_EVENT_ENABLE_API), false);
bool event_enable_kernel_ =
mluop::getBoolEnvVar(CFG_ENUM_TO_STR(MLUOP_EVENT_ENABLE_KERNEL), true);
};
} // namespace cfg
} // namespace mluop