14
14
15
15
#include " paddle/fluid/framework/data_type.h"
16
16
#include < stdint.h>
17
- #include < mutex> // NOLINT
18
17
#include < string>
19
18
#include < unordered_map>
20
19
@@ -28,20 +27,27 @@ struct DataTypeMap {
28
27
std::unordered_map<std::type_index, size_t > cpp_to_size_;
29
28
};
30
29
31
- static DataTypeMap g_data_type_map_;
30
+ static DataTypeMap* InitDataTypeMap ();
31
+ static DataTypeMap& gDataTypeMap () {
32
+ static DataTypeMap* g_data_type_map_ = InitDataTypeMap ();
33
+ return *g_data_type_map_;
34
+ }
32
35
33
36
template <typename T>
34
- static inline void RegisterType (proto::VarType::Type proto_type ,
35
- const std::string &name) {
36
- g_data_type_map_. proto_to_cpp_ . emplace ( static_cast < int >(proto_type),
37
- typeid (T));
38
- g_data_type_map_. cpp_to_proto_ .emplace (typeid (T), proto_type);
39
- g_data_type_map_. proto_to_str_ .emplace (static_cast <int >(proto_type), name);
40
- g_data_type_map_. cpp_to_size_ .emplace (typeid (T), sizeof (T));
37
+ static inline void RegisterType (DataTypeMap* map ,
38
+ proto::VarType::Type proto_type,
39
+ const std::string& name) {
40
+ map-> proto_to_cpp_ . emplace ( static_cast < int >(proto_type), typeid (T));
41
+ map-> cpp_to_proto_ .emplace (typeid (T), proto_type);
42
+ map-> proto_to_str_ .emplace (static_cast <int >(proto_type), name);
43
+ map-> cpp_to_size_ .emplace (typeid (T), sizeof (T));
41
44
}
42
45
43
- static int RegisterAllTypes () {
44
- #define RegType (cc_type, proto_type ) RegisterType<cc_type>(proto_type, #cc_type)
46
+ static DataTypeMap* InitDataTypeMap () {
47
+ auto retv = new DataTypeMap ();
48
+
49
+ #define RegType (cc_type, proto_type ) \
50
+ RegisterType<cc_type>(retv, proto_type, #cc_type)
45
51
46
52
// NOTE: Add your customize type here.
47
53
RegType (platform::float16, proto::VarType::FP16);
@@ -52,44 +58,38 @@ static int RegisterAllTypes() {
52
58
RegType (bool , proto::VarType::BOOL);
53
59
54
60
#undef RegType
55
- return 0 ;
61
+ return retv ;
56
62
}
57
63
58
- static std::once_flag register_once_flag_;
59
-
60
64
proto::VarType::Type ToDataType (std::type_index type) {
61
- std::call_once (register_once_flag_, RegisterAllTypes);
62
- auto it = g_data_type_map_.cpp_to_proto_ .find (type);
63
- if (it != g_data_type_map_.cpp_to_proto_ .end ()) {
65
+ auto it = gDataTypeMap ().cpp_to_proto_ .find (type);
66
+ if (it != gDataTypeMap ().cpp_to_proto_ .end ()) {
64
67
return it->second ;
65
68
}
66
69
PADDLE_THROW (" Not support %s as tensor type" , type.name ());
67
70
}
68
71
69
72
std::type_index ToTypeIndex (proto::VarType::Type type) {
70
- std::call_once (register_once_flag_, RegisterAllTypes);
71
- auto it = g_data_type_map_.proto_to_cpp_ .find (static_cast <int >(type));
72
- if (it != g_data_type_map_.proto_to_cpp_ .end ()) {
73
+ auto it = gDataTypeMap ().proto_to_cpp_ .find (static_cast <int >(type));
74
+ if (it != gDataTypeMap ().proto_to_cpp_ .end ()) {
73
75
return it->second ;
74
76
}
75
77
PADDLE_THROW (" Not support proto::VarType::Type(%d) as tensor type" ,
76
78
static_cast <int >(type));
77
79
}
78
80
79
81
std::string DataTypeToString (const proto::VarType::Type type) {
80
- std::call_once (register_once_flag_, RegisterAllTypes);
81
- auto it = g_data_type_map_.proto_to_str_ .find (static_cast <int >(type));
82
- if (it != g_data_type_map_.proto_to_str_ .end ()) {
82
+ auto it = gDataTypeMap ().proto_to_str_ .find (static_cast <int >(type));
83
+ if (it != gDataTypeMap ().proto_to_str_ .end ()) {
83
84
return it->second ;
84
85
}
85
86
PADDLE_THROW (" Not support proto::VarType::Type(%d) as tensor type" ,
86
87
static_cast <int >(type));
87
88
}
88
89
89
90
size_t SizeOfType (std::type_index type) {
90
- std::call_once (register_once_flag_, RegisterAllTypes);
91
- auto it = g_data_type_map_.cpp_to_size_ .find (type);
92
- if (it != g_data_type_map_.cpp_to_size_ .end ()) {
91
+ auto it = gDataTypeMap ().cpp_to_size_ .find (type);
92
+ if (it != gDataTypeMap ().cpp_to_size_ .end ()) {
93
93
return it->second ;
94
94
}
95
95
PADDLE_THROW (" Not support %s as tensor type" , type.name ());
0 commit comments