From d71e04451f3b4bd88cfd38b14c700689816ebccd Mon Sep 17 00:00:00 2001 From: liming-vie Date: Mon, 9 May 2016 00:09:19 +0800 Subject: [PATCH] Use template trait for table creater --- Test/main.cpp | 14 +-- include/multiverso/table/array_table.h | 14 +++ include/multiverso/table_factory.h | 159 ++++--------------------- include/multiverso/table_interface.h | 14 +-- src/table.cpp | 10 -- src/table_factory.cpp | 71 +++-------- 6 files changed, 66 insertions(+), 216 deletions(-) diff --git a/Test/main.cpp b/Test/main.cpp index b5db476..f25b534 100644 --- a/Test/main.cpp +++ b/Test/main.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -86,8 +87,10 @@ void TestArray(int argc, char* argv[]) { MV_Init(&argc, argv); - ArrayWorker* shared_array = new ArrayWorker(50000000); - ArrayServer* server_array = new ArrayServer(50000000); + ArrayTableInitOption option{ 50000000 }; + ArrayWorker* shared_array = TableFactory::CreateTable(option); + //ArrayWorker* shared_array = new ArrayWorker(50000000); +// ArrayServer* server_array = new ArrayServer(50000000); MV_Barrier(); Log::Info("Create tables OK\n"); @@ -274,11 +277,8 @@ void TestCheckPoint(int argc, char* argv[], bool restore){ int num_row = 11, num_col = 10; int size = num_row * num_col; - MatrixWorkerTable* worker_table = - static_cast*>((new MatrixTableHelper(num_row, num_col))->CreateTable()); - if (worker_table == nullptr) { - //no worker in this node - } + MatrixWorkerTable* worker_table = new MatrixWorkerTable(num_row, num_col); + MatrixServerTable* server_table = new MatrixServerTable(num_row, num_col); //if restore = true, will restore server data and return the next iter number of last dump file //else do nothing and return 0 diff --git a/include/multiverso/table/array_table.h b/include/multiverso/table/array_table.h index 9d85ef1..d62cf0c 100644 --- a/include/multiverso/table/array_table.h +++ b/include/multiverso/table/array_table.h @@ -69,6 +69,20 @@ protected: } size_t size_; }; + +struct ArrayTableInitOption { + size_t size; +}; + +namespace trait { +template +struct OptionTrait { + static std::string type; + typedef ArrayWorker worker_table_type; + typedef ArrayServer server_table_type; +}; +} + } #endif // MULTIVERSO_ARRAY_TABLE_H_ diff --git a/include/multiverso/table_factory.h b/include/multiverso/table_factory.h index f564efa..97a95aa 100644 --- a/include/multiverso/table_factory.h +++ b/include/multiverso/table_factory.h @@ -2,36 +2,44 @@ #define MULTIVERSO_TABLE_FACTORY_H_ #include "multiverso/table_interface.h" +#include "multiverso.h" #include namespace multiverso { -enum EleType { - kInt = 0, kFloat, kDouble -}; -typedef WorkerTable* (*worker_table_creater_t)(void**table_args); -typedef ServerTable* (*server_table_creater_t)(void**table_args); +typedef WorkerTable* (*worker_table_creater_t)(void*table_args); +typedef ServerTable* (*server_table_creater_t)(void*table_args); class TableFactory { public: - static WorkerTable* CreateTable( - EleType ele_type, - std::string& type, - void**table_args); - static WorkerTable* CreateTable( - EleType ele_type1, - EleType ele_type2, - std::string& type, - void**table_args); + template + static typename trait::OptionTrait::worker_table_type* + CreateTable(const OptionType& option) { + std::string typestr = typeid(EleType).name() + '_' + + trait::OptionTrait::type; + return InnerCreateTable(typestr, option); + } static void RegisterTable( std::string& type, worker_table_creater_t wt, server_table_creater_t st); private: - static WorkerTable* CreateTable( - std::string& type, - void**table_args); + template + static typename trait::OptionTrait::worker_table_type* + InnerCreateTable(const std::string& type, + const OptionType& table_args) { + CHECK(table_creaters_.find(type) != table_creaters_.end()); + + if (MV_ServerId() >= 0) { + table_creaters_[type].second((void*)&table_args); + } + if (MV_WorkerId() >= 0) { + return reinterpret_cast::worker_table_type*> + (table_creaters_[type].first((void*)&table_args)); + } + return nullptr; + } TableFactory() = default; static std::unordered_map< std::string, @@ -50,119 +58,4 @@ struct TableRegister { } // namespace multiverso -#endif // MULTIVERSO_TABLE_FACTORY_H_ - - -//#ifndef MULTIVERSO_INCLUDE_TABLE_FACTORY_H_ -//#define MULTIVERSO_INCLUDE_TABLE_FACTORY_H_ -// -//#include -//#include -//#include -// -//#include "multiverso/util/log.h" -//#include "multiverso/table/array_table.h" -// -//namespace multiverso { -// -//class WorkerTable; -// -//class TableFactory { -//public: -// virtual WorkerTable* CreateTable(const std::string& args) = 0; -// static TableFactory* GetFactory(const std::string& type); -//}; -// -// -//void MV_CreateTable(const std::string& type, const std::string& args, void** out) { -// TableFactory* tf = TableFactory::GetFactory(type); -// *out = static_cast(tf->CreateTable(args)); -//} -// -//class ArrayTableFactory : public TableFactory { -//public: -// WorkerTable* CreateTable(const std::string& args) override { -// // new ArrayServer() -// } -//} -//// TODO(feiga): Refine -// -//// TODO(feiga): provide better table creator method -//// Abstract Factory to create server and worker -////class TableFactory { -//// // static TableFactory* GetTableFactory(); -//// virtual WorkerTable* CreateWorker() = 0; -//// virtual ServerTable* CreateServer() = 0; -//// static TableFactory* fatory_; -////}; -// -////namespace table { -// -////} -// -////class TableBuilder { -////public: -//// TableBuilder& SetArribute(const std::string& name, const std::string& val); -//// WorkerTable* WorkerTableBuild(); -//// ServerTable* ServerTableBuild(); -////private: -//// std::string Get(const std::string& name) const; -//// std::unordered_map params_; -////}; -// -////class Context { -////public: -//// Context& SetArribute(const std::string& name, const std::string& val); -//// -//// int get_int(const std::string& name) { -//// CHECK(params_.find(name) != params_.end()); -//// return atoi(params_[name].c_str()); -//// } -//// -////private: -//// std::string get(const std::string& name) const; -//// std::map params_; -////}; -//// -////class WorkerTable; -//// -////class TableRegistry { -////public: -//// typedef WorkerTable* (*Creator)(const Context&); -//// typedef std::map Registry; -//// static TableRegistry* Global(); -//// -//// static void AddCreator(const std::string& type, Creator creator) { -//// Registry& r = registry(); -//// r[type] = creator; -//// } -//// -//// static Registry& registry() { -//// static Registry instance; -//// return instance; -//// } -//// -//// static WorkerTable* CreateTable(const std::string& type, const Context& context) { -//// Registry& r = registry(); -//// return r[type](context); -//// -//// } -//// -////private: -//// TableRegistry() {} -////}; -//// -////class TableRegisterer { -////public: -//// TableRegisterer(const std::string& type, -//// WorkerTable* (*creator)(const Context&)) { -//// TableRegistry::AddCreator(type, creator); -//// } -////}; -//// -////#define REGISTER_TABLE_CREATOR(type, creator) \ -//// static TableRegisterer(type, creator) g_creator_##type(#type, creator); -//// -//} -// -//#endif // MULTIVERSO_INCLUDE_TABLE_FACTORY_H_ \ No newline at end of file +#endif // MULTIVERSO_TABLE_FACTORY_H_ \ No newline at end of file diff --git a/include/multiverso/table_interface.h b/include/multiverso/table_interface.h index e8e5071..b14437a 100644 --- a/include/multiverso/table_interface.h +++ b/include/multiverso/table_interface.h @@ -70,17 +70,11 @@ public: virtual void ProcessGet(const std::vector& data, std::vector* result) = 0; }; - -class TableHelper { -public: - TableHelper() {} - WorkerTable* CreateTable(); - virtual ~TableHelper() {} -protected: - virtual WorkerTable* CreateWorkerTable() = 0; - virtual ServerTable* CreateServerTable() = 0; -}; +namespace trait { +template +struct OptionTrait; +} } // namespace multiverso diff --git a/src/table.cpp b/src/table.cpp index e104d2d..d5939d2 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -106,14 +106,4 @@ void WorkerTable::Notify(int id) { m_.unlock(); } -WorkerTable* TableHelper::CreateTable() { - if (MV_ServerId() >= 0) { - CreateServerTable(); - } - if (MV_WorkerId() >= 0) { - return CreateWorkerTable(); - } - return nullptr; -} - } // namespace multiverso diff --git a/src/table_factory.cpp b/src/table_factory.cpp index 69eb48e..3c321e4 100644 --- a/src/table_factory.cpp +++ b/src/table_factory.cpp @@ -9,50 +9,6 @@ std::unordered_map > TableFactory::table_creaters_; -std::string ele_type_str(EleType ele_type) { - switch (ele_type) { - case kInt: - return "int_"; - case kFloat: - return "float_"; - case kDouble: - return "double_"; - } - return "unknown"; -} - -WorkerTable* TableFactory::CreateTable( - std::string& type, - void**table_args) { - CHECK(table_creaters_.find(type) != table_creaters_.end()); - - if (MV_ServerId() >= 0) { - table_creaters_[type].second(table_args); - } - if (MV_WorkerId() >= 0) { - return table_creaters_[type].first(table_args); - } - return nullptr; -} - -WorkerTable* TableFactory::CreateTable( - EleType ele_type, - std::string& type, - void**table_args) { - std::string typestr = ele_type_str(ele_type) + type; - return CreateTable(typestr, table_args); -} - -WorkerTable* TableFactory::CreateTable( - EleType ele_type1, - EleType ele_type2, - std::string& type, - void**table_args) { - std::string typestr = ele_type_str(ele_type1) - + ele_type_str(ele_type2) + type; - return CreateTable(typestr, table_args); -} - void TableFactory::RegisterTable( std::string& type, worker_table_creater_t wt, @@ -80,23 +36,26 @@ void TableFactory::RegisterTable( server_table_creater); template -WorkerTable* create_array_worker(void **args) { - return new ArrayWorker(*(size_t*)(*args)); +WorkerTable* create_array_worker(void *args) { + return new ArrayWorker(((ArrayTableInitOption*)args)->size); } template -ServerTable* create_array_server(void **args) { - return new ArrayServer(*(size_t*)(*args)); +ServerTable* create_array_server(void *args) { + return new ArrayServer(((ArrayTableInitOption*)args)->size); } MV_REGISTER_TABLE_WITH_BASIC_TYPE(array, create_array_worker, create_array_server); -template -WorkerTable* create_matrix_worker(void **args) { - return new MatrixWorkerTable(*(integer_t*)(*args), *(integer_t*)(*(args+1))); +namespace trait { +#define DECLARE_OPTION_TRAIT_TYPE(eletype, optiontype, str) \ + template<> \ + std::string OptionTrait::type = #str; + +#define DECLARE_OPTION_TRAIT_WITH_BASIC_TYPE(optiontype, str) \ + DECLARE_OPTION_TRAIT_TYPE(int, optiontype, str) \ + DECLARE_OPTION_TRAIT_TYPE(float, optiontype, str) \ + DECLARE_OPTION_TRAIT_TYPE(double, optiontype, str) \ + +DECLARE_OPTION_TRAIT_WITH_BASIC_TYPE(ArrayTableInitOption, array); } -template -ServerTable* create_matrix_server(void **args) { - return new MatrixServerTable(*(integer_t*)(*args), *(integer_t*)(*(args + 1))); -} -MV_REGISTER_TABLE_WITH_BASIC_TYPE(matrix, create_matrix_worker, create_matrix_server); } // namespace multiverso \ No newline at end of file