From 2c56b3a5e30a5e992d937de69ffea578885074ac Mon Sep 17 00:00:00 2001 From: Yuanming Hu Date: Mon, 12 Oct 2020 01:23:59 -0400 Subject: [PATCH 1/3] . --- taichi/ir/type_factory.cpp | 17 +++++++++++++++++ taichi/ir/type_factory.h | 18 ++++++++++++++++++ taichi/lang_util.cpp | 13 +++++++++---- taichi/program/program.cpp | 1 + taichi/program/program.h | 5 +++++ 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 taichi/ir/type_factory.cpp create mode 100644 taichi/ir/type_factory.h diff --git a/taichi/ir/type_factory.cpp b/taichi/ir/type_factory.cpp new file mode 100644 index 0000000000000..a17afa81f4b30 --- /dev/null +++ b/taichi/ir/type_factory.cpp @@ -0,0 +1,17 @@ +#include "taichi/ir/type_factory.h" + +TLANG_NAMESPACE_BEGIN + +Type *TypeFactory::get_primitive_type( + taichi::lang::PrimitiveType::primitive_type id) { + std::lock_guard _(mut); + + TI_TAG; + + if (primitive_types_.find(id) == primitive_types_.end()) { + primitive_types_[id] = std::make_unique(id); + } + return primitive_types_[id].get(); +} + +TLANG_NAMESPACE_END diff --git a/taichi/ir/type_factory.h b/taichi/ir/type_factory.h new file mode 100644 index 0000000000000..e5d069f234877 --- /dev/null +++ b/taichi/ir/type_factory.h @@ -0,0 +1,18 @@ +#include "taichi/lang_util.h" + +#include + +TLANG_NAMESPACE_BEGIN + +class TypeFactory { + public: + Type *get_primitive_type(PrimitiveType::primitive_type id); + + private: + std::unordered_map> + primitive_types_; + + std::mutex mut; +}; + +TLANG_NAMESPACE_END diff --git a/taichi/lang_util.cpp b/taichi/lang_util.cpp index cceca900654b9..d6305ca1b822a 100644 --- a/taichi/lang_util.cpp +++ b/taichi/lang_util.cpp @@ -4,6 +4,7 @@ #include "taichi/math/linalg.h" #include "taichi/program/arch.h" +#include "taichi/program/program.h" #include "taichi/program/compile_config.h" #include "taichi/system/timer.h" @@ -31,10 +32,13 @@ real default_measurement_time = 1; // Note: these primitive types should never be freed. They are supposed to live // together with the process. This is a temporary solution. Later we should -// manage its ownership more systematically -#define PER_TYPE(x) \ - DataType PrimitiveType::x = \ - DataType(new PrimitiveType(PrimitiveType::primitive_type::x)); +// manage its ownership more systematically. + +// This part doesn't look good, but we will remove it soon anyway. +#define PER_TYPE(x) \ + DataType PrimitiveType::x = \ + DataType(Program::type_factory.get_primitive_type( \ + PrimitiveType::primitive_type::x)); #include "taichi/inc/data_type.inc.h" #undef PER_TYPE @@ -355,6 +359,7 @@ namespace { class TypePromotionMapping { public: TypePromotionMapping() { + TI_TAG; #define TRY_SECOND(x, y) \ mapping[std::make_pair(to_primitive_type(get_data_type()), \ to_primitive_type(get_data_type()))] = \ diff --git a/taichi/program/program.cpp b/taichi/program/program.cpp index f4782fb802a60..0aa29b0441245 100644 --- a/taichi/program/program.cpp +++ b/taichi/program/program.cpp @@ -58,6 +58,7 @@ inline uint64 *allocate_result_buffer_default(Program *prog) { Program *current_program = nullptr; std::atomic Program::num_instances; +TypeFactory Program::type_factory; Program::Program(Arch desired_arch) { TI_TRACE("Program initializing..."); diff --git a/taichi/program/program.h b/taichi/program/program.h index 59ea056985861..17c21972580e3 100644 --- a/taichi/program/program.h +++ b/taichi/program/program.h @@ -8,6 +8,7 @@ #define TI_RUNTIME_HOST #include "taichi/ir/ir.h" +#include "taichi/ir/type_factory.h" #include "taichi/ir/snode.h" #include "taichi/lang_util.h" #include "taichi/llvm/llvm_context.h" @@ -105,6 +106,10 @@ class Program { jit_evaluator_cache; std::mutex jit_evaluator_cache_mut; + // Note: for now we let all Programs share a single TypeFactory for smooth + // migration. In the future each program should have its own copy. + static TypeFactory type_factory; + Program() : Program(default_compile_config.arch) { } From 332cd661536be8756b962f958c368f7cb8f4f43d Mon Sep 17 00:00:00 2001 From: Yuanming Hu Date: Mon, 12 Oct 2020 01:35:01 -0400 Subject: [PATCH 2/3] finalize --- taichi/ir/type_factory.cpp | 5 ++--- taichi/ir/type_factory.h | 2 +- taichi/lang_util.cpp | 8 ++++---- taichi/program/program.cpp | 7 ++++++- taichi/program/program.h | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/taichi/ir/type_factory.cpp b/taichi/ir/type_factory.cpp index a17afa81f4b30..c758eec989320 100644 --- a/taichi/ir/type_factory.cpp +++ b/taichi/ir/type_factory.cpp @@ -4,13 +4,12 @@ TLANG_NAMESPACE_BEGIN Type *TypeFactory::get_primitive_type( taichi::lang::PrimitiveType::primitive_type id) { - std::lock_guard _(mut); - - TI_TAG; + std::lock_guard _(mut_); if (primitive_types_.find(id) == primitive_types_.end()) { primitive_types_[id] = std::make_unique(id); } + return primitive_types_[id].get(); } diff --git a/taichi/ir/type_factory.h b/taichi/ir/type_factory.h index e5d069f234877..8d1a5903b7cdf 100644 --- a/taichi/ir/type_factory.h +++ b/taichi/ir/type_factory.h @@ -12,7 +12,7 @@ class TypeFactory { std::unordered_map> primitive_types_; - std::mutex mut; + std::mutex mut_; }; TLANG_NAMESPACE_END diff --git a/taichi/lang_util.cpp b/taichi/lang_util.cpp index d6305ca1b822a..7d319f92a6bbb 100644 --- a/taichi/lang_util.cpp +++ b/taichi/lang_util.cpp @@ -35,10 +35,11 @@ real default_measurement_time = 1; // manage its ownership more systematically. // This part doesn't look good, but we will remove it soon anyway. -#define PER_TYPE(x) \ - DataType PrimitiveType::x = \ - DataType(Program::type_factory.get_primitive_type( \ +#define PER_TYPE(x) \ + DataType PrimitiveType::x = \ + DataType(Program::get_type_factory().get_primitive_type( \ PrimitiveType::primitive_type::x)); + #include "taichi/inc/data_type.inc.h" #undef PER_TYPE @@ -359,7 +360,6 @@ namespace { class TypePromotionMapping { public: TypePromotionMapping() { - TI_TAG; #define TRY_SECOND(x, y) \ mapping[std::make_pair(to_primitive_type(get_data_type()), \ to_primitive_type(get_data_type()))] = \ diff --git a/taichi/program/program.cpp b/taichi/program/program.cpp index 0aa29b0441245..313e929b41c8b 100644 --- a/taichi/program/program.cpp +++ b/taichi/program/program.cpp @@ -58,7 +58,6 @@ inline uint64 *allocate_result_buffer_default(Program *prog) { Program *current_program = nullptr; std::atomic Program::num_instances; -TypeFactory Program::type_factory; Program::Program(Arch desired_arch) { TI_TRACE("Program initializing..."); @@ -195,6 +194,12 @@ Program::Program(Arch desired_arch) { arch_name(arch)); } +TypeFactory &Program::get_type_factory() { + // type_factory should never be destroyed, hence the raw new operator. + static TypeFactory *type_factory = new TypeFactory; + return *type_factory; +} + FunctionType Program::compile(Kernel &kernel) { auto start_t = Time::get_time(); TI_AUTO_PROF; diff --git a/taichi/program/program.h b/taichi/program/program.h index 17c21972580e3..0d79a898c093d 100644 --- a/taichi/program/program.h +++ b/taichi/program/program.h @@ -108,7 +108,7 @@ class Program { // Note: for now we let all Programs share a single TypeFactory for smooth // migration. In the future each program should have its own copy. - static TypeFactory type_factory; + static TypeFactory &get_type_factory(); Program() : Program(default_compile_config.arch) { } From 4dc54474d158fd70a2f4f4baa8922e72deb1e1e0 Mon Sep 17 00:00:00 2001 From: Yuanming Hu Date: Mon, 12 Oct 2020 01:37:06 -0400 Subject: [PATCH 3/3] finalize --- taichi/ir/type_factory.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/taichi/ir/type_factory.cpp b/taichi/ir/type_factory.cpp index c758eec989320..bb4755cab7575 100644 --- a/taichi/ir/type_factory.cpp +++ b/taichi/ir/type_factory.cpp @@ -2,8 +2,7 @@ TLANG_NAMESPACE_BEGIN -Type *TypeFactory::get_primitive_type( - taichi::lang::PrimitiveType::primitive_type id) { +Type *TypeFactory::get_primitive_type(PrimitiveType::primitive_type id) { std::lock_guard _(mut_); if (primitive_types_.find(id) == primitive_types_.end()) {