提交 ce4a26dd 编写于 作者: S sneaxiy

clean code

try to fix mac compile bug?
test=develop
上级 74a8e6b0
......@@ -83,10 +83,7 @@ cc_test(threadpool_test SRCS threadpool_test.cc DEPS threadpool)
cc_library(var_type_traits SRCS var_type_traits DEPS lod_tensor selected_rows framework_proto)
if (WITH_GPU)
target_link_libraries(var_type_traits cudnn)
if (NOT WIN32)
target_link_libraries(var_type_traits nccl)
endif()
target_link_libraries(var_type_traits dynload_cuda)
endif()
cc_test(var_type_traits_test SRCS var_type_traits_test.cc DEPS var_type_traits)
......
......@@ -17,9 +17,58 @@
namespace paddle {
namespace framework {
const char* ToTypeName(int var_id) { return ToTypeIndex(var_id).name(); }
// Besides registering variable type id, it is helpful to register a
// var_id -> std::type_index map (for example, get type names according to id)
namespace detail {
const std::type_index& ToTypeIndex(int var_id) {
template <int kStart, int kEnd, bool kStop>
struct VarIdToTypeIndexMapInitializerImpl {
static void Init(std::unordered_map<int, std::type_index> *m) {
using Type =
typename std::tuple_element<kStart, VarTypeRegistry::ArgTuple>::type;
constexpr int kId = VarTypeTrait<Type>::kId;
if (!std::is_same<Type, void>::value) {
m->emplace(kId, std::type_index(typeid(Type)));
}
VarIdToTypeIndexMapInitializerImpl<kStart + 1, kEnd,
kStart + 1 == kEnd>::Init(m);
}
};
template <int kStart, int kEnd>
struct VarIdToTypeIndexMapInitializerImpl<kStart, kEnd, true> {
static void Init(std::unordered_map<int, std::type_index> *m) {}
};
// VarIdToTypeIndexMapInitializer is designed to initialize var_id ->
// std::type_index map
using VarIdToTypeIndexMapInitializer =
VarIdToTypeIndexMapInitializerImpl<0, VarTypeRegistry::kRegisteredTypeNum,
VarTypeRegistry::kRegisteredTypeNum ==
0>;
struct VarIdToTypeIndexMapHolder {
public:
static const std::type_index &ToTypeIndex(int var_id) {
static const VarIdToTypeIndexMapHolder instance;
auto it = instance.var_type_map_.find(var_id);
PADDLE_ENFORCE(it != instance.var_type_map_.end(),
"VarId %d is not registered.", var_id);
return it->second;
}
private:
VarIdToTypeIndexMapHolder() {
VarIdToTypeIndexMapInitializer::Init(&var_type_map_);
}
std::unordered_map<int, std::type_index> var_type_map_;
};
} // namespace detail
const char *ToTypeName(int var_id) { return ToTypeIndex(var_id).name(); }
const std::type_index &ToTypeIndex(int var_id) {
return detail::VarIdToTypeIndexMapHolder::ToTypeIndex(var_id);
}
......
......@@ -40,6 +40,9 @@
namespace paddle {
namespace framework {
const char *ToTypeName(int var_id);
const std::type_index &ToTypeIndex(int var_id);
namespace detail {
template <bool kStop, int kStart, int kEnd, typename T1, typename T2,
......@@ -145,58 +148,6 @@ REG_PROTO_VAR_TYPE_TRAIT(ReaderHolder, proto::VarType::READER);
/** End of variable type registration */
// Besides register variable id, it is helpful to register a
// var_id -> std::type_index (for example, get var names according to id)
namespace detail {
template <int kStart, int kEnd, bool kStop>
struct VarIdToTypeIndexMapInitializerImpl {
static void Init(std::unordered_map<int, std::type_index> *m) {
using Type =
typename std::tuple_element<kStart, VarTypeRegistry::ArgTuple>::type;
constexpr int kId = VarTypeTrait<Type>::kId;
if (!std::is_same<Type, void>::value) {
m->emplace(kId, std::type_index(typeid(Type)));
}
VarIdToTypeIndexMapInitializerImpl<kStart + 1, kEnd,
kStart + 1 == kEnd>::Init(m);
}
};
template <int kStart, int kEnd>
struct VarIdToTypeIndexMapInitializerImpl<kStart, kEnd, true> {
static void Init(std::unordered_map<int, std::type_index> *m) {}
};
// VarIdToTypeIndexMapInitializer is designed to initialize var_id ->
// std::type_index map
using VarIdToTypeIndexMapInitializer =
VarIdToTypeIndexMapInitializerImpl<0, VarTypeRegistry::kRegisteredTypeNum,
VarTypeRegistry::kRegisteredTypeNum ==
0>;
struct VarIdToTypeIndexMapHolder {
public:
static const std::type_index &ToTypeIndex(int var_id) {
static const VarIdToTypeIndexMapHolder instance;
auto it = instance.var_type_map_.find(var_id);
PADDLE_ENFORCE(it != instance.var_type_map_.end(),
"VarId %d is not registered.", var_id);
return it->second;
}
private:
VarIdToTypeIndexMapHolder() {
VarIdToTypeIndexMapInitializer::Init(&var_type_map_);
}
std::unordered_map<int, std::type_index> var_type_map_;
};
} // namespace detail
const char *ToTypeName(int var_id);
const std::type_index &ToTypeIndex(int var_id);
template <typename T>
inline constexpr bool IsRegisteredVarType() {
return VarTypeRegistry::IsRegistered<T>();
......
......@@ -15,32 +15,46 @@
#include "paddle/fluid/framework/var_type_traits.h"
#include <gtest/gtest.h>
#include <cstdint>
#include <unordered_set>
namespace paddle {
namespace framework {
template <int kPos, int kEnd, bool kStop>
struct TypeIndexChecker {
static void Check() {
template <typename SetType1, typename SetType2>
static void Check(SetType1 *var_id_set, SetType2 *type_index_set) {
using Type =
typename std::tuple_element<kPos, VarTypeRegistry::ArgTuple>::type;
static_assert(std::is_same<typename VarTypeTrait<Type>::Type, Type>::value,
"Type must be the same");
constexpr auto kId = VarTypeTrait<Type>::kId;
if (!std::is_same<Type, void>::value) {
EXPECT_TRUE(ToTypeIndex(VarTypeTrait<Type>::kId) == typeid(Type));
EXPECT_TRUE(std::string(ToTypeName(VarTypeTrait<Type>::kId)) ==
typeid(Type).name());
std::type_index actual_type(typeid(Type));
EXPECT_EQ(std::string(ToTypeName(kId)), std::string(actual_type.name()));
EXPECT_EQ(ToTypeIndex(kId), actual_type);
EXPECT_TRUE(var_id_set->count(kId) == 0); // NOLINT
EXPECT_TRUE(type_index_set->count(actual_type) == 0); // NOLINT
var_id_set->insert(kId);
type_index_set->insert(std::type_index(typeid(Type)));
}
TypeIndexChecker<kPos + 1, kEnd, kPos + 1 == kEnd>::Check();
TypeIndexChecker<kPos + 1, kEnd, kPos + 1 == kEnd>::Check(var_id_set,
type_index_set);
}
};
template <int kPos, int kEnd>
struct TypeIndexChecker<kPos, kEnd, true> {
static void Check() {}
template <typename SetType1, typename SetType2>
static void Check(SetType1 *, SetType2 *) {}
};
TEST(var_type_traits, check_type_index) {
TEST(var_type_traits, check_no_duplicate_registry) {
constexpr size_t kRegisteredNum = VarTypeRegistry::kRegisteredTypeNum;
TypeIndexChecker<0, kRegisteredNum, kRegisteredNum == 0>::Check();
std::unordered_set<int> var_id_set;
std::unordered_set<std::type_index> type_index_set;
TypeIndexChecker<0, kRegisteredNum, kRegisteredNum == 0>::Check(
&var_id_set, &type_index_set);
}
template <typename T>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册