Skip to content

Commit 66e82b9

Browse files
committed
Change implementation to fit sphinx model
1 parent 715c933 commit 66e82b9

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

paddle/fluid/framework/data_type.cc

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "paddle/fluid/framework/data_type.h"
1616
#include <stdint.h>
17-
#include <mutex> // NOLINT
1817
#include <string>
1918
#include <unordered_map>
2019

@@ -28,20 +27,27 @@ struct DataTypeMap {
2827
std::unordered_map<std::type_index, size_t> cpp_to_size_;
2928
};
3029

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+
}
3235

3336
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));
4144
}
4245

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)
4551

4652
// NOTE: Add your customize type here.
4753
RegType(platform::float16, proto::VarType::FP16);
@@ -52,44 +58,38 @@ static int RegisterAllTypes() {
5258
RegType(bool, proto::VarType::BOOL);
5359

5460
#undef RegType
55-
return 0;
61+
return retv;
5662
}
5763

58-
static std::once_flag register_once_flag_;
59-
6064
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()) {
6467
return it->second;
6568
}
6669
PADDLE_THROW("Not support %s as tensor type", type.name());
6770
}
6871

6972
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()) {
7375
return it->second;
7476
}
7577
PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type",
7678
static_cast<int>(type));
7779
}
7880

7981
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()) {
8384
return it->second;
8485
}
8586
PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type",
8687
static_cast<int>(type));
8788
}
8889

8990
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()) {
9393
return it->second;
9494
}
9595
PADDLE_THROW("Not support %s as tensor type", type.name());

0 commit comments

Comments
 (0)