23 template<jit::
float_scalar T,
bool SAFE_MATH=false>
31 std::array<uint32_t, 624>
array;
50 states.push_back(initalize_state(
seed +
i));
102 stream <<
"struct mt_state {" << std::endl
103 <<
" array<uint32_t, 624> array;" << std::endl
104 <<
" uint16_t index;" << std::endl
106 <<
" uint16_t padding[3];" << std::endl
108 <<
"};" << std::endl;
140 std::cout <<
"state";
152 if (registers.find(
this) == registers.end()) {
154 registers[
this] =
name;
155 stream <<
" " <<
name
156 <<
" [label = \"state\", shape = box, style = \"rounded,filled\", fillcolor = black, fontcolor = white];" << std::endl;
186 return states.size();
204 return states.data();
209 std::vector<mt_state> states;
216 static std::string to_string() {
217 return "random_state";
228 state.array[0] =
seed;
230 state.array[
i] = 1812433253U*(state.array[
i - 1]^(state.array[
i - 1] >> 30)) +
i;
248 template<jit::
float_scalar T,
bool SAFE_MATH=false>
251 auto temp = std::make_shared<random_state_node<T, SAFE_MATH>> (size,
seed)->reduce();
253 for (
size_t i =
temp->get_hash();
263#if defined(__clang__) || defined(__GNUC__)
266 assert(
false &&
"Should never reach.");
271 template<jit::
float_scalar T,
bool SAFE_MATH=false>
283 template<jit::
float_scalar T,
bool SAFE_MATH=false>
285 return std::dynamic_pointer_cast<random_state_node<T, SAFE_MATH>> (x);
297 template<jit::
float_scalar T,
bool SAFE_MATH=false>
306 static std::string to_string() {
369 this->
arg->compile_preamble(stream, registers,
374 jit::add_type<T> (stream);
375 stream <<
" random(";
376 if constexpr (jit::use_metal<T> ()) {
379 stream <<
"mt_state &state) {" << std::endl
380 <<
" uint16_t k = state.index;" << std::endl
381 <<
" uint16_t j = (k + 1) % 624;" << std::endl
382 <<
" uint32_t x = (state.array[k] & 0x80000000U) |" << std::endl
383 <<
" (state.array[j] & 0x7fffffffU);" << std::endl
384 <<
" uint32_t xA = x >> 1;" << std::endl
385 <<
" if (x & 0x00000001U) {" << std::endl
386 <<
" xA ^= 0x9908b0dfU;" << std::endl
388 <<
" j = (k + 397) % 624;" << std::endl
389 <<
" x = state.array[j]^xA;" << std::endl
390 <<
" state.array[k] = x;" << std::endl
391 <<
" state.index = (k + 1) % 624;" << std::endl
392 <<
" uint32_t y = x^(x >> 11);" << std::endl
393 <<
" y = y^((y << 7) & 0x9d2c5680U);" << std::endl
394 <<
" y = y^((y << 15) & 0xefc60000U);" << std::endl
395 <<
" return static_cast<";
396 jit::add_type<T> (stream);
397 stream <<
"> (y^(y >> 18));" << std::endl
423 if (registers.find(
this) == registers.end()) {
429 registers[
this] =
"random(" + registers[
a.get()] +
")";
454 std::cout <<
"state";
466 if (registers.find(
this) == registers.end()) {
468 registers[
this] =
name;
469 stream <<
" " <<
name
470 <<
" [label = \"state\", shape = box, style = \"rounded,filled\", fillcolor = black, fontcolor = white];" << std::endl;
472 auto a = this->
arg->to_vizgraph(stream, registers);
473 stream <<
" " <<
name <<
" -- " << registers[
a.get()] <<
";" << std::endl;
507 template<jit::
float_scalar T,
bool SAFE_MATH=false>
509 auto temp = std::make_shared<random_node<T, SAFE_MATH>> (state)->reduce();
511 for (
size_t i =
temp->get_hash();
521#if defined(__clang__) || defined(__GNUC__)
524 assert(
false &&
"Should never reach.");
529 template<jit::
float_scalar T,
bool SAFE_MATH=false>
541 template<jit::
float_scalar T,
bool SAFE_MATH=false>
543 return std::dynamic_pointer_cast<random_node<T, SAFE_MATH>> (x);
554 template<jit::
float_scalar T,
bool SAFE_MATH=false>
Class representing a generic buffer.
Definition backend.hpp:29
Class representing a node leaf.
Definition node.hpp:364
Class representing a random_node leaf.
Definition random.hpp:298
virtual bool is_match(shared_leaf< T, SAFE_MATH > x)
Querey if the nodes match.
Definition random.hpp:446
random_node(shared_random_state< T, SAFE_MATH > x)
Construct a constant node from a vector.
Definition random.hpp:316
virtual backend::buffer< T > evaluate()
Evaluate the results of random node.
Definition random.hpp:324
virtual shared_leaf< T, SAFE_MATH > get_power_exponent() const
Get the exponent of a power.
Definition random.hpp:493
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the random node.
Definition random.hpp:334
virtual void to_latex() const
Convert the node to latex.
Definition random.hpp:453
virtual bool is_all_variables() const
Test if all the subnodes terminate in variables.
Definition random.hpp:484
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition random.hpp:464
virtual void compile_preamble(std::ostringstream &stream, jit::register_map ®isters, jit::visiter_map &visited, jit::register_usage &usage, jit::texture1d_list &textures1d, jit::texture2d_list &textures2d, int &avail_const_mem)
Compile preamble.
Definition random.hpp:361
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, jit::register_map &indices, const jit::register_usage &usage)
Compile the node.
Definition random.hpp:419
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition random.hpp:346
Random state.
Definition random.hpp:24
virtual shared_leaf< T, SAFE_MATH > to_vizgraph(std::stringstream &stream, jit::register_map ®isters)
Convert the node to vizgraph.
Definition random.hpp:150
size_t size()
Get the size of the random state vector in bytes.
Definition random.hpp:185
virtual shared_leaf< T, SAFE_MATH > compile(std::ostringstream &stream, jit::register_map ®isters, jit::register_map &indices, const jit::register_usage &usage)
Compile the node.
Definition random.hpp:129
random_state_node(const size_t size, const uint32_t seed=0)
Construct a constant node from a vector.
Definition random.hpp:46
virtual bool is_all_variables() const
Test if all the subnodes terminate in variables.
Definition random.hpp:167
virtual shared_leaf< T, SAFE_MATH > df(shared_leaf< T, SAFE_MATH > x)
Transform node to derivative.
Definition random.hpp:79
virtual void to_latex() const
Convert the node to latex.
Definition random.hpp:139
virtual backend::buffer< T > evaluate()
Evaluate the results of random_state_node.
Definition random.hpp:59
virtual void compile_preamble(std::ostringstream &stream, jit::register_map ®isters, jit::visiter_map &visited, jit::register_usage &usage, jit::texture1d_list &textures1d, jit::texture2d_list &textures2d, int &avail_const_mem)
Compile preamble.
Definition random.hpp:94
size_t get_size_bytes()
Get the size of the random state vector in bytes.
Definition random.hpp:194
virtual shared_leaf< T, SAFE_MATH > get_power_exponent() const
Get the exponent of a power.
Definition random.hpp:176
virtual shared_leaf< T, SAFE_MATH > reduce()
Reduce the random_state_node.
Definition random.hpp:69
mt_state * data()
Get the size of the random state vector in bytes.
Definition random.hpp:203
Class representing a straight node.
Definition node.hpp:1059
shared_leaf< T, SAFE_MATH > arg
Argument.
Definition node.hpp:1062
subroutine assert(test, message)
Assert check.
Definition f_binding_test.f90:38
Name space for graph nodes.
Definition arithmetic.hpp:13
constexpr shared_leaf< T, SAFE_MATH > random_scale()
Create a random_scale constant.
Definition random.hpp:555
shared_random_state< T, SAFE_MATH > random_state_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a random_state node.
Definition random.hpp:284
constexpr shared_leaf< T, SAFE_MATH > zero()
Forward declare for zero.
Definition node.hpp:994
shared_leaf< T, SAFE_MATH > random(shared_random_state< T, SAFE_MATH > state)
Define random convience function.
Definition random.hpp:508
std::shared_ptr< random_state_node< T, SAFE_MATH > > shared_random_state
Convenience type alias for shared sqrt nodes.
Definition random.hpp:272
std::shared_ptr< random_node< T, SAFE_MATH > > shared_random
Convenience type alias for shared sqrt nodes.
Definition random.hpp:530
constexpr T i
Convinece type for imaginary constant.
Definition node.hpp:1026
std::shared_ptr< leaf_node< T, SAFE_MATH > > shared_leaf
Convenience type alias for shared leaf nodes.
Definition node.hpp:673
shared_random< T, SAFE_MATH > random_cast(shared_leaf< T, SAFE_MATH > x)
Cast to a random node.
Definition random.hpp:542
shared_leaf< T, SAFE_MATH > random_state(const size_t size, const uint32_t seed=0)
Define random_state convience function.
Definition random.hpp:249
std::map< void *, size_t > texture1d_list
Type alias for indexing 1D textures.
Definition register.hpp:262
std::map< void *, std::array< size_t, 2 > > texture2d_list
Type alias for indexing 2D textures.
Definition register.hpp:264
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::set< void * > visiter_map
Type alias for listing visited nodes.
Definition register.hpp:260
std::string to_string(const char prefix, const NODE *pointer)
Convert a graph::leaf_node pointer to a string.
Definition register.hpp:245
Base nodes of graph computation framework.
Random state structure.
Definition random.hpp:29
uint16_t index
State index.
Definition random.hpp:33
std::array< uint32_t, 624 > array
State array.
Definition random.hpp:31