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// Intel C++ Compiler Classic version 2021.2.0 works but 2021.1.2 doesn't
27// Both have __INTEL_COMPILER defined to 2021 so using
28// __INTEL_COMPILER_BUILD_DATE to discriminate.
29// Experimenting on the compiler explorer gave
30// icc version | __INTEL_COMPILER | __INTEL_COMPILER_BUILD_DATE
31// 2021.1.2 | 2021 | 20201208
32// 2021.2.0 | 2021 | 20210228
33// NVCC versions less than 11.3.0 segfault when that header is included
34// NVCC+MSVC doesn't work at all - it simply reports "T" inside type_name
35#if (!defined(KOKKOS_COMPILER_INTEL) || \
36 (__INTEL_COMPILER_BUILD_DATE >= 20210228)) && \
37 (!defined(KOKKOS_COMPILER_NVCC) || (KOKKOS_COMPILER_NVCC >= 1130)) && \
38 (!(defined(KOKKOS_COMPILER_NVCC) && defined(KOKKOS_COMPILER_MSVC)))
39
40#define KOKKOS_ENABLE_IMPL_TYPEINFO
41
42namespace Kokkos::Impl {
43
44template <size_t N>
45constexpr std::array<char, N> to_array(std::string_view src) {
46 std::array<char, N> dst{};
47 for (size_t i = 0; i < N; ++i) {
48 dst[i] = src[i];
49 }
50 return dst;
51}
52
53template <class T>
54constexpr auto type_name() {
55#if defined(__clang__)
56 constexpr std::string_view func = __PRETTY_FUNCTION__;
57 constexpr std::string_view prefix{"[T = "};
58 constexpr std::string_view suffix{"]"};
59#elif defined(__GNUC__)
60 constexpr std::string_view func = __PRETTY_FUNCTION__;
61 constexpr std::string_view prefix{"[with T = "};
62 constexpr std::string_view suffix{"]"};
63#elif defined(_MSC_VER)
64 constexpr std::string_view func = __FUNCSIG__;
65 constexpr std::string_view prefix{"type_name<"};
66 constexpr std::string_view suffix{">(void)"};
67#else
68#error bug
69#endif
70 constexpr auto beg = func.find(prefix) + prefix.size();
71 constexpr auto end = func.rfind(suffix);
72 static_assert(beg != std::string_view::npos);
73 static_assert(end != std::string_view::npos);
74 return to_array<end - beg>(func.substr(beg, end));
75}
76
77template <class T>
78class TypeInfo {
79 static constexpr auto value_ = type_name<T>();
80
81 public:
82 static constexpr std::string_view name() noexcept {
83 return {value_.data(), value_.size()};
84 }
85};
86
87} // namespace Kokkos::Impl
88
89#else // out of luck, using Intel C++ Compiler Classic
90
91namespace Kokkos::Impl {
92
93template <class T>
94class TypeInfo {
95 public:
96 static constexpr std::string_view name() noexcept { return "not supported"; }
97};
98
99} // namespace Kokkos::Impl
100
101#endif
102
103#endif
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...