Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
type_convert.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
23
24#if __has_include(<nlohmann/json.hpp>)
25 #include <nlohmann/json.hpp>
26#endif
27
28namespace sham {
29
36 template<class T, int n>
37 std::array<T, n> sycl_vec_to_array(sycl::vec<T, n> v) {
38 if constexpr (n == 1) {
39 return {v[0]};
40 } else if constexpr (n == 2) {
41 return {v[0], v[1]};
42 } else if constexpr (n == 3) {
43 return {v[0], v[1], v[2]};
44 } else if constexpr (n == 4) {
45 return {v[0], v[1], v[2], v[3]};
46 } else if constexpr (n == 8) {
47 return {v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]};
48 } else if constexpr (n == 16) {
49 return {
50 v[0],
51 v[1],
52 v[2],
53 v[3],
54 v[4],
55 v[5],
56 v[6],
57 v[7],
58 v[8],
59 v[9],
60 v[10],
61 v[11],
62 v[12],
63 v[13],
64 v[14],
65 v[15]};
66 } else {
67 static_assert(shambase::always_false_v<T>, "This case is not handled");
68 }
69 }
70
77 template<class T, size_t n>
78 sycl::vec<T, n> array_to_sycl_vec(std::array<T, n> v) {
79 if constexpr (n == 1) {
80 return {v[0]};
81 } else if constexpr (n == 2) {
82 return {v[0], v[1]};
83 } else if constexpr (n == 3) {
84 return {v[0], v[1], v[2]};
85 } else if constexpr (n == 4) {
86 return {v[0], v[1], v[2], v[3]};
87 } else if constexpr (n == 8) {
88 return {v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]};
89 } else if constexpr (n == 16) {
90 return {
91 v[0],
92 v[1],
93 v[2],
94 v[3],
95 v[4],
96 v[5],
97 v[6],
98 v[7],
99 v[8],
100 v[9],
101 v[10],
102 v[11],
103 v[12],
104 v[13],
105 v[14],
106 v[15]};
107 } else {
108 static_assert(shambase::always_false_v<T>, "This case is not handled");
109 }
110 }
111
112} // namespace sham
113
114#if __has_include(<nlohmann/json.hpp>)
115NLOHMANN_JSON_NAMESPACE_BEGIN
116template<typename T, int n>
117struct adl_serializer<sycl::vec<T, n>> {
118 static void to_json(json &j, const sycl::vec<T, n> &p) { j = sham::sycl_vec_to_array(p); }
119
120 static void from_json(const json &j, sycl::vec<T, n> &p) {
121 p = sham::array_to_sycl_vec(j.get<std::array<T, n>>());
122 }
123};
124NLOHMANN_JSON_NAMESPACE_END
125#endif
void to_json(nlohmann::json &j, const PatchSchedulerConfig &p)
Converts a PatchSchedulerConfig object to a JSON object.
void from_json(const nlohmann::json &j, PatchSchedulerConfig &p)
Deserializes a PatchSchedulerConfig object from a JSON object.
namespace for backends this one is named only sham since shambackends is too long to write
std::array< T, n > sycl_vec_to_array(sycl::vec< T, n > v)
Converts a SYCL vector into a C++ standard library array.
sycl::vec< T, n > array_to_sycl_vec(std::array< T, n > v)
Converts a C++ standard library array into a SYCL vector.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
Traits for C++ types.