Graph Framework
Loading...
Searching...
No Matches
register.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
4//------------------------------------------------------------------------------
5
6#ifndef register_h
7#define register_h
8
9#include <concepts>
10#include <cassert>
11#include <map>
12#include <set>
13#include <sstream>
14#include <complex>
15#include <type_traits>
16#include <limits>
17#include <charconv>
18#include <array>
19#include <utility>
20
21namespace jit {
23 template<typename T>
24 concept complex_scalar = std::same_as<T, std::complex<float>> ||
25 std::same_as<T, std::complex<double>>;
26
28 template<typename T>
29 concept float_scalar = std::floating_point<T> || complex_scalar<T>;
30
32 template<typename T>
33 concept scalar = float_scalar<T> || std::integral<T>;
34
36 template<typename T>
37 concept float_base = std::same_as<T, float> ||
38 std::same_as<T, std::complex<float>>;
39
41 template<typename T>
42 concept double_base = std::same_as<T, double> ||
43 std::same_as<T, std::complex<double>>;
44
46 static bool verbose = USE_VERBOSE;
47
48//------------------------------------------------------------------------------
54//------------------------------------------------------------------------------
55 template<float_scalar T>
56 std::string type_to_string() {
57 if constexpr (float_base<T>) {
58 return "float";
59 } else {
60 return "double";
61 }
62 }
63
64//------------------------------------------------------------------------------
66//------------------------------------------------------------------------------
67 constexpr bool use_cuda() {
68#ifdef USE_CUDA
69 return true;
70#else
71 return false;
72#endif
73 }
74
75//------------------------------------------------------------------------------
79//------------------------------------------------------------------------------
80 template<float_scalar T>
81 constexpr bool use_metal() {
82#if USE_METAL
84#else
85 return false;
86#endif
87 }
88
89//------------------------------------------------------------------------------
93//------------------------------------------------------------------------------
94 template<float_scalar T>
95 constexpr bool use_gpu() {
96 return use_cuda() || use_metal<T> ();
97 }
98
99//------------------------------------------------------------------------------
106//------------------------------------------------------------------------------
107 template<float_scalar T>
108 std::string smallest_int_type(const size_t max_size) {
109 if (max_size <= std::numeric_limits<unsigned char>::max()) {
110 if constexpr (jit::use_metal<T> ()) {
111 return "ushort";
112 } else {
113 return "unsigned char";
114 }
115 } else if (max_size <= std::numeric_limits<unsigned short>::max()) {
116 if constexpr (jit::use_metal<T> ()) {
117 return "ushort";
118 } else {
119 return "unsigned short";
120 }
121 } else if (max_size <= std::numeric_limits<unsigned int>::max()) {
122 if constexpr (jit::use_metal<T> ()) {
123 return "uint";
124 } else {
125 return "unsigned int";
126 }
127 } else {
128 if constexpr (jit::use_metal<T> ()) {
129 return "uint";
130 } else {
131 return "size_t";
132 }
133 }
134 }
135
136//------------------------------------------------------------------------------
142//------------------------------------------------------------------------------
143 template<float_scalar T>
144 std::string get_type_string() {
145 if constexpr (complex_scalar<T>) {
146 if constexpr (use_cuda()) {
147 return "cuda::std::complex<" + type_to_string<T> () + ">";
148 } else {
149 return "std::complex<" + type_to_string<T> () + ">";
150 }
151 } else {
152 return type_to_string<T> ();
153 }
154 }
155
156//------------------------------------------------------------------------------
162//------------------------------------------------------------------------------
163 template<float_scalar T>
164 void add_type(std::basic_ostream<char> &stream) {
165 stream << get_type_string<T> ();
166 }
167
168//------------------------------------------------------------------------------
174//------------------------------------------------------------------------------
175 template<float_scalar T>
176 constexpr int max_digits10() {
177 if constexpr (float_base<T>) {
178 return std::numeric_limits<float>::max_digits10;
179 } else {
180 return std::numeric_limits<double>::max_digits10;
181 }
182 }
183
184//------------------------------------------------------------------------------
190//------------------------------------------------------------------------------
191 template<float_scalar T>
192 constexpr int max_base() {
193 if constexpr (float_base<T>) {
194 return std::numeric_limits<float>::max();
195 } else {
196 return std::numeric_limits<double>::max();
197 }
198 }
199
200//------------------------------------------------------------------------------
209//------------------------------------------------------------------------------
210 template<scalar T>
211 std::string format_to_string(const T value) {
212 std::array<char, 36> buffer;
213 char *end;
214 if constexpr (std::is_same<T, size_t>::value) {
215 end = std::to_chars(buffer.begin(),
216 buffer.end(),
217 value, 16).ptr;
218 } else if constexpr (complex_scalar<T>) {
219 return format_to_string(std::real(value)) + "," +
220 format_to_string(std::imag(value));
221 } else {
222 end = std::to_chars(buffer.begin(), buffer.end(),
223 value, std::chars_format::general,
224 max_digits10<T> ()).ptr;
225 }
226 return std::string(buffer.data(), end);
227 }
228
229//------------------------------------------------------------------------------
243//------------------------------------------------------------------------------
244 template<class NODE>
245 std::string to_string(const char prefix,
246 const NODE *pointer) {
247 assert((prefix == 'r' || prefix == 'v' ||
248 prefix == 'o' || prefix == 'a' ||
249 prefix == 'i' || prefix == 's') &&
250 "Expected a variable (v), register (r), output (o), array (a), index (i), or state (s) prefix.");
251 return std::string(1, prefix) +
252 format_to_string(reinterpret_cast<size_t> (pointer));
253 }
254
256 typedef std::map<void *, std::string> register_map;
258 typedef std::map<void *, size_t> register_usage;
260 typedef std::set<void *> visiter_map;
262 typedef std::map<void *, size_t> texture1d_list;
264 typedef std::map<void *, std::array<size_t,2>> texture2d_list;
265
266//------------------------------------------------------------------------------
270//------------------------------------------------------------------------------
271 template<float_scalar T>
273 public:
274//------------------------------------------------------------------------------
279//------------------------------------------------------------------------------
280 bool operator() (const T &left, const T &right) const {
281 if constexpr (complex_scalar<T>) {
282 return std::abs(left) < std::abs(right);
283 } else {
284 return left < right;
285 }
286 }
287 };
288}
289
290#endif /* register_h */
Define a custom comparitor class.
Definition register.hpp:272
bool operator()(const T &left, const T &right) const
Call operator.
Definition register.hpp:280
Complex scalar concept.
Definition register.hpp:24
Double base concept.
Definition register.hpp:42
float base concept.
Definition register.hpp:37
Float scalar concept.
Definition register.hpp:29
General scalar concept.
Definition register.hpp:33
subroutine assert(test, message)
Assert check.
Definition f_binding_test.f90:38
Name space for JIT functions.
Definition jit.hpp:41
std::string smallest_int_type(const size_t max_size)
Get smallest integer type.
Definition register.hpp:108
std::map< void *, size_t > texture1d_list
Type alias for indexing 1D textures.
Definition register.hpp:262
constexpr bool use_gpu()
Test to use the GPU.
Definition register.hpp:95
constexpr bool use_metal()
Test to use metal.
Definition register.hpp:81
std::string type_to_string()
Convert a base type to a string.
Definition register.hpp:56
constexpr int max_base()
The maximum value for a base type.
Definition register.hpp:192
std::string format_to_string(const T value)
Convert a value to a string while avoiding locale.
Definition register.hpp:211
std::map< void *, std::array< size_t, 2 > > texture2d_list
Type alias for indexing 2D textures.
Definition register.hpp:264
void add_type(std::basic_ostream< char > &stream)
Write out the node base type to a general stream.
Definition register.hpp:164
std::map< void *, size_t > register_usage
Type alias for counting register usage.
Definition register.hpp:258
std::map< void *, std::string > register_map
Type alias for mapping node pointers to register names.
Definition register.hpp:256
std::string get_type_string()
Get the type string.
Definition register.hpp:144
constexpr bool use_cuda()
Test to use Cuda.
Definition register.hpp:67
std::set< void * > visiter_map
Type alias for listing visited nodes.
Definition register.hpp:260
constexpr int max_digits10()
The maximum number of digits to represent a type literal.
Definition register.hpp:176
std::string to_string(const char prefix, const NODE *pointer)
Convert a graph::leaf_node pointer to a string.
Definition register.hpp:245