From 7d1ef72d94f6600f7e347ab224725739d1e62cc0 Mon Sep 17 00:00:00 2001 From: "Yu-Hsiang M. Tsai" Date: Fri, 23 Aug 2024 17:54:48 +0200 Subject: [PATCH] update documentation and use macro Co-authored-by: Marcel Koch Co-authored-by: Pratik Nayak --- core/config/type_descriptor.cpp | 80 +++++-------------- core/test/config/type_descriptor.cpp | 40 ++++++++-- .../ginkgo/core/config/type_descriptor.hpp | 22 +++-- 3 files changed, 68 insertions(+), 74 deletions(-) diff --git a/core/config/type_descriptor.cpp b/core/config/type_descriptor.cpp index 017cc98ca06..0762271e236 100644 --- a/core/config/type_descriptor.cpp +++ b/core/config/type_descriptor.cpp @@ -5,6 +5,7 @@ #include "ginkgo/core/config/type_descriptor.hpp" #include +#include #include "core/config/type_descriptor_helper.hpp" @@ -45,67 +46,19 @@ type_descriptor make_type_descriptor() type_string::str()}; } -// global_index: void -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, void, void>(); -template type_descriptor -make_type_descriptor, void, void>(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, int32, void>(); -template type_descriptor -make_type_descriptor, int32, void>(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, int64, void>(); -template type_descriptor -make_type_descriptor, int64, void>(); - -// global_index int32 -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, void, int32>(); -template type_descriptor -make_type_descriptor, void, int32>(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, int32, int32>(); -template type_descriptor -make_type_descriptor, int32, int32>(); - -// global_index_type int64 -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, void, int64>(); -template type_descriptor -make_type_descriptor, void, int64>(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, int32, int64>(); -template type_descriptor -make_type_descriptor, int32, int64>(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor make_type_descriptor(); -template type_descriptor -make_type_descriptor, int64, int64>(); -template type_descriptor -make_type_descriptor, int64, int64>(); +#define GKO_DECLARE_MAKE_TYPE_DESCRIPTOR(ValueType, LocalIndexType, \ + GlobalIndexType) \ + type_descriptor \ + make_type_descriptor() +GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( + GKO_DECLARE_MAKE_TYPE_DESCRIPTOR); + +#define GKO_DECLARE_MAKE_VOID_TYPE_DESCRIPTOR(ValueType, LocalIndexType, \ + GlobalIndexType) \ + type_descriptor \ + make_type_descriptor() +GKO_INSTANTIATE_FOR_EACH_LOCAL_GLOBAL_INDEX_TYPE( + GKO_DECLARE_MAKE_VOID_TYPE_DESCRIPTOR); type_descriptor::type_descriptor(std::string value_typestr, @@ -126,6 +79,11 @@ const std::string& type_descriptor::get_index_typestr() const return index_typestr_; } +const std::string& type_descriptor::get_local_index_typestr() const +{ + return this->get_index_typestr(); +} + const std::string& type_descriptor::get_global_index_typestr() const { return global_index_typestr_; diff --git a/core/test/config/type_descriptor.cpp b/core/test/config/type_descriptor.cpp index f044d60716f..e8a7327a6a2 100644 --- a/core/test/config/type_descriptor.cpp +++ b/core/test/config/type_descriptor.cpp @@ -21,6 +21,7 @@ TEST(TypeDescriptor, TemplateCreate) ASSERT_EQ(td.get_value_typestr(), "float64"); ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); ASSERT_EQ(td.get_global_index_typestr(), "int64"); } { @@ -29,6 +30,7 @@ TEST(TypeDescriptor, TemplateCreate) ASSERT_EQ(td.get_value_typestr(), "float32"); ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); ASSERT_EQ(td.get_global_index_typestr(), "int32"); } { @@ -37,24 +39,37 @@ TEST(TypeDescriptor, TemplateCreate) ASSERT_EQ(td.get_value_typestr(), "float32"); ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); ASSERT_EQ(td.get_global_index_typestr(), "int64"); } { - SCOPED_TRACE("specify all template"); + SCOPED_TRACE("specify local index template"); auto td = make_type_descriptor, gko::int64, gko::int64>(); ASSERT_EQ(td.get_value_typestr(), "complex"); ASSERT_EQ(td.get_index_typestr(), "int64"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); ASSERT_EQ(td.get_global_index_typestr(), "int64"); } + { + SCOPED_TRACE("specify global index template"); + auto td = + make_type_descriptor, gko::int32, gko::int32>(); + + ASSERT_EQ(td.get_value_typestr(), "complex"); + ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int32"); + } { SCOPED_TRACE("specify void"); - auto td = make_type_descriptor(); + auto td = make_type_descriptor(); ASSERT_EQ(td.get_value_typestr(), "void"); - ASSERT_EQ(td.get_index_typestr(), "void"); - ASSERT_EQ(td.get_global_index_typestr(), "void"); + ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int64"); } } @@ -67,6 +82,8 @@ TEST(TypeDescriptor, Constructor) ASSERT_EQ(td.get_value_typestr(), "float64"); ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int64"); } { SCOPED_TRACE("specify valuetype"); @@ -74,12 +91,25 @@ TEST(TypeDescriptor, Constructor) ASSERT_EQ(td.get_value_typestr(), "float32"); ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int64"); } { - SCOPED_TRACE("specify all parameters"); + SCOPED_TRACE("specify local index parameters"); type_descriptor td("void", "int64"); ASSERT_EQ(td.get_value_typestr(), "void"); ASSERT_EQ(td.get_index_typestr(), "int64"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int64"); + } + { + SCOPED_TRACE("specify global index parameters"); + type_descriptor td("void", "int32", "int32"); + + ASSERT_EQ(td.get_value_typestr(), "void"); + ASSERT_EQ(td.get_index_typestr(), "int32"); + ASSERT_EQ(td.get_local_index_typestr(), td.get_index_typestr()); + ASSERT_EQ(td.get_global_index_typestr(), "int32"); } } diff --git a/include/ginkgo/core/config/type_descriptor.hpp b/include/ginkgo/core/config/type_descriptor.hpp index aa75b4591fa..5c719340436 100644 --- a/include/ginkgo/core/config/type_descriptor.hpp +++ b/include/ginkgo/core/config/type_descriptor.hpp @@ -25,15 +25,16 @@ namespace config { * auto cg = parse(config, context, type_descriptor("float64", "int32")); * ``` * will have the value type `float64` and the index type `int32`. Any Ginkgo - * object that does not require one of these types will just ignore it. The - * value `void` can be used to specify that no default type is provided. In this - * case, the configuration has to provide the necessary template types. + * object that does not require one of these types will just ignore it. In + * value_type, one additional value `void` can be used to specify that no + * default type is provided. In this case, the configuration has to provide the + * necessary template types. * * If the configuration specifies one field (only allow value_type now): * ``` * value_type: "some_value_type" * ``` - * these types will take precedence over the type_descriptor. + * this type will take precedence over the type_descriptor. */ class type_descriptor final { public: @@ -42,9 +43,8 @@ class type_descriptor final { * `make_type_descriptor` to create the object by template. * * @param value_typestr the value type string. "void" means no default. - * @param index_typestr the index type string. "void" means no default. - * @param global_index_typestr the global index type string. "void" means - * no default. + * @param index_typestr the (local) index type string. + * @param global_index_typestr the global index type string. * * @note there is no way to call the constructor with explicit template, so * we create another free function to handle it. @@ -63,6 +63,12 @@ class type_descriptor final { */ const std::string& get_index_typestr() const; + /** + * Get the local index type string, which gives the same result as + * get_index_typestr() + */ + const std::string& get_local_index_typestr() const; + /** * Get the global index type string */ @@ -83,7 +89,7 @@ class type_descriptor final { * @tparam IndexType the index type in descriptor * @tparam GlobalIndexType the global index type in descriptor */ -template type_descriptor make_type_descriptor();