Skip to content

Commit e36e5ba

Browse files
authored
Allow print to work on any set (#937)
1 parent 351e03a commit e36e5ba

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

include/ddc/print.hpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#include <ostream>
1111
#include <sstream>
1212
#include <type_traits>
13+
#include <typeinfo>
1314
#include <utility>
1415

1516
#include "chunk_span.hpp"
16-
#include "discrete_domain.hpp"
17+
#include "discrete_vector.hpp"
1718

1819
#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
1920
# include <cxxabi.h>
@@ -259,49 +260,43 @@ class ChunkPrinter
259260
}
260261
};
261262

262-
#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
263-
template <class Type>
264-
void print_demangled_type_name(std::ostream& os)
263+
inline void print_demangled_type_name(std::ostream& os, char const* const mangled_name)
265264
{
265+
#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
266266
int status;
267267

268-
std::unique_ptr<char, decltype(std::free)*> const demangled_name(
269-
abi::__cxa_demangle(typeid(Type).name(), nullptr, nullptr, &status),
270-
std::free);
268+
std::unique_ptr<char, decltype(std::free)*> const
269+
demangled_name(abi::__cxa_demangle(mangled_name, nullptr, nullptr, &status), std::free);
271270
if (status != 0) {
272271
os << "Error demangling dimension name: " << status;
273272
return;
274273
}
275274

276275
os << demangled_name.get();
277-
}
278276
#else
279-
template <class Type>
280-
void print_demangled_type_name(std::ostream& os)
281-
{
282-
os << typeid(Type).name();
283-
}
277+
os << mangled_name;
284278
#endif
279+
}
285280

286-
inline void print_dim_name(std::ostream& os, DiscreteDomain<> const)
281+
inline void print_single_dim_name(
282+
std::ostream& os,
283+
std::type_info const& dim,
284+
DiscreteVectorElement const size)
287285
{
288-
os << "Scalar";
286+
print_demangled_type_name(os, dim.name());
287+
os << '(' << size << ')';
289288
}
290289

291-
template <class Dim>
292-
void print_dim_name(std::ostream& os, DiscreteDomain<Dim> const dd)
290+
inline void print_dim_name(std::ostream& os, DiscreteVector<> const&)
293291
{
294-
print_demangled_type_name<Dim>(os);
295-
os << '(' << dd.size() << ')';
292+
os << "Scalar";
296293
}
297294

298-
template <class Dim0, class Dim1, class... Dims>
299-
void print_dim_name(std::ostream& os, DiscreteDomain<Dim0, Dim1, Dims...> const dd)
295+
template <class Dim0, class... Dims>
296+
void print_dim_name(std::ostream& os, DiscreteVector<Dim0, Dims...> const& dd)
300297
{
301-
print_demangled_type_name<Dim0>(os);
302-
DiscreteDomain<Dim1, Dims...> const smaller_dd(dd);
303-
os << '(' << dd.size() / smaller_dd.size() << "";
304-
print_dim_name(os, smaller_dd);
298+
print_single_dim_name(os, typeid(Dim0), get<Dim0>(dd));
299+
((os << "×", print_single_dim_name(os, typeid(Dims), get<Dims>(dd))), ...);
305300
}
306301

307302
} // namespace detail
@@ -339,9 +334,9 @@ std::ostream& print_type_info(
339334
std::ostream& os,
340335
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk_span)
341336
{
342-
ddc::detail::print_dim_name(os, chunk_span.domain());
337+
ddc::detail::print_dim_name(os, chunk_span.extents());
343338
os << '\n';
344-
ddc::detail::print_demangled_type_name<decltype(chunk_span)>(os);
339+
ddc::detail::print_demangled_type_name(os, typeid(chunk_span).name());
345340
os << '\n';
346341

347342
return os;

tests/print.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ struct Dim5
3838

3939
} // namespace anonymous_namespace_workaround_print_cpp
4040

41+
TEST(Print, ValidDemangledTypeName)
42+
{
43+
std::stringstream ss;
44+
ddc::detail::print_demangled_type_name(ss, typeid(ddc::DiscreteDomain<>).name());
45+
#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
46+
EXPECT_EQ(ss.str(), "ddc::DiscreteDomain<>");
47+
#elif defined(KOKKOS_COMPILER_MSVC)
48+
EXPECT_EQ(ss.str(), "class ddc::DiscreteDomain<>");
49+
#else
50+
GTEST_SKIP();
51+
#endif
52+
}
53+
54+
TEST(Print, InvalidDemangledTypeName)
55+
{
56+
std::stringstream ss;
57+
ddc::detail::print_demangled_type_name(ss, "0");
58+
#if defined(KOKKOS_COMPILER_GNU) || defined(KOKKOS_COMPILER_CLANG)
59+
EXPECT_EQ(ss.str(), "Error demangling dimension name: -2");
60+
#else
61+
EXPECT_EQ(ss.str(), "0");
62+
#endif
63+
}
64+
4165
template <typename ElementType>
4266
void PrintTestCheckOutput0d()
4367
{

0 commit comments

Comments
 (0)