Arcane  v3.15.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
ValueFiller.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ValueFiller.h (C) 2000-2024 */
9/* */
10/* Fonctions pour générer des valeurs (pour les tests). */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_VALUEFILLER_H
13#define ARCCORE_BASE_VALUEFILLER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/base/BFloat16.h"
19#include "arccore/base/Float16.h"
20#include "arccore/base/Float128.h"
21#include "arccore/base/Int128.h"
22
23#include <random>
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28namespace Arccore::ValueFiller::impl
29{
30
31/*---------------------------------------------------------------------------*/
32/*---------------------------------------------------------------------------*/
33
34template <typename DataType>
36{
37 public:
38
39 using ClampValue = std::pair<DataType, DataType>;
40 BuildInfo(const ClampValue& clamp_value)
41 : m_clamp_value(clamp_value)
42 {}
43 ClampValue clampValue() const
44 {
45 return m_clamp_value;
46 }
47
48 private:
49
50 ClampValue m_clamp_value;
51};
52
53template <typename DataType>
54BuildInfo<DataType> _getFloatBuildInfo();
55template <> BuildInfo<Float128> _getFloatBuildInfo<Float128>()
56{
57 return { { -122334353.245, +983973536.324 } };
58}
59template <> BuildInfo<long double> _getFloatBuildInfo<long double>()
60{
61 return { { -334353.245, +73536.324 } };
62}
63template <> BuildInfo<double> _getFloatBuildInfo<double>()
64{
65 return { { -334353.245, +73536.324 } };
66}
67template <> BuildInfo<float> _getFloatBuildInfo<float>()
68{
69 return { { -14353.245f, 3536.324f } };
70}
71template <> BuildInfo<BFloat16> _getFloatBuildInfo<BFloat16>()
72{
73 return { { BFloat16{ -35321.2f }, BFloat16{ 63236.3f } } };
74}
75template <> BuildInfo<Float16> _getFloatBuildInfo<Float16>()
76{
77 return { { Float16{ -353.2f }, Float16{ 636.3f } } };
78}
79template <typename DataType> BuildInfo<DataType> _getIntegerBuildInfo(std::true_type)
80{
81 if constexpr (sizeof(DataType) == 1)
82 return { { -25, 32 } };
83 if constexpr (sizeof(DataType) == 2)
84 return { { -212, 345 } };
85 if constexpr (sizeof(DataType) == 4)
86 return { { -14353, 12345 } };
87 if constexpr (sizeof(DataType) == 8)
88 return { { -234353, 452345 } };
89 if constexpr (sizeof(DataType) == 16)
90 return { { -33435332, 9232023 } };
91}
92template <typename DataType> BuildInfo<DataType> _getIntegerBuildInfo(std::false_type)
93{
94 if constexpr (sizeof(DataType) == 1)
95 return { { 0, 89 } };
96 if constexpr (sizeof(DataType) == 2)
97 return { { 0, 721 } };
98 if constexpr (sizeof(DataType) == 4)
99 return { { 0, 29540 } };
100 if constexpr (sizeof(DataType) == 8)
101 return { { 0, 1290325 } };
102 if constexpr (sizeof(DataType) == 16)
103 return { { 0, 931290325 } };
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109template <typename DataType>
111
112template <typename DataType>
114{
115 public:
116
117 using value_type = DataType;
118
119 public:
120
121 explicit FillTraitsBaseT(const impl::BuildInfo<DataType>& build_info)
122 : m_min_clamp(build_info.clampValue().first)
123 , m_max_clamp(build_info.clampValue().second)
124 {}
125
126 public:
127
128 DataType minClamp() const { return m_min_clamp; }
129 DataType maxClamp() const { return m_max_clamp; }
130
131 private:
132
133 DataType m_min_clamp = {};
134 DataType m_max_clamp = {};
135};
136
137/*---------------------------------------------------------------------------*/
138/*---------------------------------------------------------------------------*/
139
140template <typename DataType, typename RandomGeneratorDataType = DataType>
142: public FillTraitsBaseT<DataType>
143{
144 using BaseClass = FillTraitsBaseT<DataType>;
145
146 public:
147
148 using UniformGeneratorType = std::uniform_int_distribution<RandomGeneratorDataType>;
149
150 public:
151
153 : BaseClass(impl::_getIntegerBuildInfo<DataType>(std::is_signed<DataType>{}))
154 {}
155};
156
157/*---------------------------------------------------------------------------*/
158/*---------------------------------------------------------------------------*/
159
160template <typename DataType, typename RandomGeneratorDataType = DataType>
162: public FillTraitsBaseT<DataType>
163{
164 using BaseClass = FillTraitsBaseT<DataType>;
165
166 public:
167
168 using UniformGeneratorType = std::uniform_real_distribution<RandomGeneratorDataType>;
169
170 public:
171
172 explicit FloatFillTraitsT()
173 : BaseClass(impl::_getFloatBuildInfo<DataType>())
174 {}
175};
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180template <>
181class FillTraitsT<char>
182: public IntegerFillTraitsT<char,std::conditional<std::is_signed_v<char>,short,unsigned short>::type>
183{
184};
185
186template <>
187class FillTraitsT<signed char>
188: public IntegerFillTraitsT<signed char,short>
189{
190};
191
192template <>
193class FillTraitsT<unsigned char>
194: public IntegerFillTraitsT<unsigned char,unsigned short>
195{
196};
197
198template <>
199class FillTraitsT<short>
200: public IntegerFillTraitsT<short>
201{
202};
203
204template <>
205class FillTraitsT<unsigned short>
206: public IntegerFillTraitsT<unsigned short>
207{
208};
209
210template <>
211class FillTraitsT<int>
212: public IntegerFillTraitsT<int>
213{
214};
215
216template <>
217class FillTraitsT<unsigned int>
218: public IntegerFillTraitsT<unsigned int>
219{
220};
221
222template <>
223class FillTraitsT<long>
224: public IntegerFillTraitsT<long>
225{
226};
227
228template <>
229class FillTraitsT<unsigned long>
230: public IntegerFillTraitsT<unsigned long>
231{
232};
233
234template <>
235class FillTraitsT<long long>
236: public IntegerFillTraitsT<long long>
237{
238};
239
240template <>
241class FillTraitsT<unsigned long long>
242: public IntegerFillTraitsT<unsigned long long>
243{
244};
245
246
247template <>
249: public IntegerFillTraitsT<Int128, Int64>
250{
251};
252
253/*---------------------------------------------------------------------------*/
254/*---------------------------------------------------------------------------*/
255
256template <>
257class FillTraitsT<float>
258: public FloatFillTraitsT<float>
259{
260};
261
262template <>
263class FillTraitsT<double>
264: public FloatFillTraitsT<double>
265{
266};
267
268template <>
269class FillTraitsT<long double>
270: public FloatFillTraitsT<long double>
271{
272};
273
274template <>
276: public FloatFillTraitsT<BFloat16, float>
277{
278};
279
280template <>
282: public FloatFillTraitsT<Float16, float>
283{
284};
285
286template <>
288: public FloatFillTraitsT<Float128, long double>
289{
290};
291
292/*---------------------------------------------------------------------------*/
293/*---------------------------------------------------------------------------*/
294
295} // namespace Arccore::ValueFiller::impl
296
297/*---------------------------------------------------------------------------*/
298/*---------------------------------------------------------------------------*/
299
300namespace Arccore::ValueFiller
301{
302
303/*---------------------------------------------------------------------------*/
304/*---------------------------------------------------------------------------*/
305
306template <typename DataType> inline
307void fillRandom(Int64 seed, Span<DataType> values)
308{
309 std::seed_seq rng_seed{ seed };
310 using TraitsType = impl::FillTraitsT<DataType>;
311 using UniformGeneratorType = typename TraitsType::UniformGeneratorType;
312 TraitsType traits_value;
313 std::mt19937_64 randomizer(rng_seed);
314 UniformGeneratorType rng_distrib(traits_value.minClamp(), traits_value.maxClamp());
315
316 Int64 n1 = values.size();
317 for (Int32 i = 0; i < n1; ++i) {
318 values[i] = static_cast<DataType>(rng_distrib(randomizer));
319 }
320}
321
322/*---------------------------------------------------------------------------*/
323/*---------------------------------------------------------------------------*/
324
325} // namespace Arccore::ValueFiller
326
327/*---------------------------------------------------------------------------*/
328/*---------------------------------------------------------------------------*/
329
330#endif
331
Déclarations des types de la composante 'base' de Arccore.
Type flottant demi-précision.
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:209
Vue d'un tableau d'éléments de type T.
Definition Span.h:510
Arccore::Float16 Float16
Type 'Float16' (binary16)
Arccore::BFloat16 BFloat16
Type 'Brain Float16'.
std::int64_t Int64
Type entier signé sur 64 bits.
std::int32_t Int32
Type entier signé sur 32 bits.