Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
Kokkos_TypeInfo.hpp
1//@HEADER
2// ************************************************************************
3//
4// Kokkos v. 4.0
5// Copyright (2022) National Technology & Engineering
6// Solutions of Sandia, LLC (NTESS).
7//
8// Under the terms of Contract DE-NA0003525 with NTESS,
9// the U.S. Government retains certain rights in this software.
10//
11// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12// See https://kokkos.org/LICENSE for license information.
13// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14//
15//@HEADER
16
17#ifndef KOKKOS_TYPE_INFO_HPP
18#define KOKKOS_TYPE_INFO_HPP
19
20#include <array>
21#include <string_view>
22#include <utility>
23
24#include <Kokkos_Macros.hpp>
25
26// NVCC versions less than 11.3.0 segfault when that header is included
27// NVCC+MSVC doesn't work at all - it simply reports "T" inside type_name
28#if (!defined(KOKKOS_COMPILER_NVCC) || (KOKKOS_COMPILER_NVCC >= 1130)) && \
29 (!(defined(KOKKOS_COMPILER_NVCC) && defined(KOKKOS_COMPILER_MSVC)))
30
31#define KOKKOS_ENABLE_IMPL_TYPEINFO
32
33namespace Kokkos::Impl {
34
35template <size_t N>
36constexpr std::array<char, N> to_array(std::string_view src) {
37 std::array<char, N> dst{};
38 for (size_t i = 0; i < N; ++i) {
39 dst[i] = src[i];
40 }
41 return dst;
42}
43
44template <class T>
45constexpr auto type_name() {
46#if defined(__clang__)
47 constexpr std::string_view func = __PRETTY_FUNCTION__;
48 constexpr std::string_view prefix{"[T = "};
49 constexpr std::string_view suffix{"]"};
50#elif defined(__GNUC__)
51 constexpr std::string_view func = __PRETTY_FUNCTION__;
52 constexpr std::string_view prefix{"[with T = "};
53 constexpr std::string_view suffix{"]"};
54#elif defined(_MSC_VER)
55 constexpr std::string_view func = __FUNCSIG__;
56 constexpr std::string_view prefix{"type_name<"};
57 constexpr std::string_view suffix{">(void)"};
58#else
59#error bug
60#endif
61 constexpr auto beg = func.find(prefix) + prefix.size();
62 constexpr auto end = func.rfind(suffix);
63 static_assert(beg != std::string_view::npos);
64 static_assert(end != std::string_view::npos);
65 return to_array<end - beg>(func.substr(beg, end));
66}
67
68template <class T>
69class TypeInfo {
70 static constexpr auto value_ = type_name<T>();
71
72 public:
73 static constexpr std::string_view name() noexcept {
74 return {value_.data(), value_.size()};
75 }
76};
77
78} // namespace Kokkos::Impl
79
80#else // out of luck, using Intel C++ Compiler Classic
81
82namespace Kokkos::Impl {
83
84template <class T>
85class TypeInfo {
86 public:
87 static constexpr std::string_view name() noexcept { return "not supported"; }
88};
89
90} // namespace Kokkos::Impl
91
92#endif
93
94#endif
KOKKOS_FORCEINLINE_FUNCTION unsigned size() const
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...