Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
random.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
18
23#include "shambackends/sycl.hpp"
25#include "shambackends/vec.hpp"
26#include <shambackends/sycl.hpp>
27#include <random>
28
34
35 template<class T, class Engine = std::mt19937>
36 T mock_gaussian(Engine &eng) {
37 using namespace shambase::constants;
38
39 constexpr T _2pi = pi<T> * 2;
40 T r_3 = shamalgs::primitives::mock_value<T>(eng, 0, 1);
41 T r_4 = shamalgs::primitives::mock_value<T>(eng, 0, 1);
42 return sycl::sqrt(-2 * sycl::log(r_3)) * sycl::cos(_2pi * r_4);
43 }
44
45 template<class T, class Engine = std::mt19937>
46 T mock_gaussian_multidim(Engine &eng) {
47 T ret;
48 constexpr int n = shambase::VectorProperties<T>::dimension;
49 using Tscal = shambase::VecComponent<T>;
50#pragma unroll
51 for (int i = 0; i < n; i++) {
52 ret[i] = mock_gaussian<Tscal>(eng);
53 }
54 return ret;
55 }
56
57 template<class T, class Engine = std::mt19937>
58 T mock_unit_vector(Engine &eng) {
59 T ret = mock_gaussian_multidim<T>(eng);
60 auto len = sycl::length(ret);
61
62 auto default_unit_vec = []() {
63 T ret = {};
64 ret[0] = 1;
65 return ret;
66 };
67
68 return (len > 0) ? (ret / len) : default_unit_vec();
69 }
70
71 template<class T>
72 sycl::buffer<T> mock_buffer(u64 seed, u32 len, T min_bound, T max_bound);
73
74 template<class T>
75 sham::DeviceBuffer<T> mock_buffer_usm(
76 const sham::DeviceScheduler_ptr &sched, u64 seed, u32 len, T min_bound, T max_bound);
77
78 template<class T>
79 inline sham::DeviceBuffer<T> mock_buffer_usm(
80 const sham::DeviceScheduler_ptr &sched, u64 seed, u32 len) {
81 using Prop = shambase::VectorProperties<T>;
82 return mock_buffer_usm(sched, seed, len, Prop::get_min(), Prop::get_max());
83 }
84
85 template<class T>
86 inline sycl::buffer<T> mock_buffer(u64 seed, u32 len) {
87 using Prop = shambase::VectorProperties<T>;
88 return mock_buffer(seed, len, Prop::get_min(), Prop::get_max());
89 }
90
91 template<class T>
92 inline std::unique_ptr<sycl::buffer<T>> mock_buffer_ptr(
93 u64 seed, u32 len, T min_bound, T max_bound) {
94 return std::make_unique<sycl::buffer<T>>(mock_buffer(seed, len, min_bound, max_bound));
95 }
96 template<class T>
97 inline std::unique_ptr<sycl::buffer<T>> mock_buffer_ptr(u64 seed, u32 len) {
98 using Prop = shambase::VectorProperties<T>;
99 return mock_buffer_ptr(seed, len, Prop::get_min(), Prop::get_max());
100 }
101
102} // namespace shamalgs::random
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
Class holding the value of numerous constants generated from the following source.
Utility functions for generating random mock values.
T mock_value(Engine &eng, T min_bound, T max_bound)
Generates a random mock value within specified bounds.
namespace to contain utility related to random number generation in shamalgs
Definition random.hpp:33