Arcane  v3.14.10.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
21#include <random>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Arccore::ValueFiller::impl
27{
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32template <typename DataType>
34{
35 public:
36
37 using ClampValue = std::pair<DataType, DataType>;
38 BuildInfo(const ClampValue& clamp_value)
39 : m_clamp_value(clamp_value)
40 {}
41 ClampValue clampValue() const
42 {
43 return m_clamp_value;
44 }
45
46 private:
47
48 ClampValue m_clamp_value;
49};
50
51template <typename DataType>
52BuildInfo<DataType> _getFloatBuildInfo();
53template <> BuildInfo<long double> _getFloatBuildInfo<long double>()
54{
55 return { { -334353.245, +73536.324 } };
56}
57template <> BuildInfo<double> _getFloatBuildInfo<double>()
58{
59 return { { -334353.245, +73536.324 } };
60}
61template <> BuildInfo<float> _getFloatBuildInfo<float>()
62{
63 return { { -14353.245f, 3536.324f } };
64}
65template <> BuildInfo<BFloat16> _getFloatBuildInfo<BFloat16>()
66{
67 return { { BFloat16{ -35321.2f }, BFloat16{ 63236.3f } } };
68}
69template <> BuildInfo<Float16> _getFloatBuildInfo<Float16>()
70{
71 return { { Float16{ -353.2f }, Float16{ 636.3f } } };
72}
73template <typename DataType> BuildInfo<DataType> _getIntegerBuildInfo(std::true_type)
74{
75 if constexpr (sizeof(DataType) == 1)
76 return { { -25, 32 } };
77 if constexpr (sizeof(DataType) == 2)
78 return { { -212, 345 } };
79 if constexpr (sizeof(DataType) == 4)
80 return { { -14353, 12345 } };
81 if constexpr (sizeof(DataType) == 8)
82 return { { -234353, 452345 } };
83}
84template <typename DataType> BuildInfo<DataType> _getIntegerBuildInfo(std::false_type)
85{
86 if constexpr (sizeof(DataType) == 1)
87 return { { 0, 89 } };
88 if constexpr (sizeof(DataType) == 2)
89 return { { 0, 721 } };
90 if constexpr (sizeof(DataType) == 4)
91 return { { 0, 29540 } };
92 if constexpr (sizeof(DataType) == 8)
93 return { { 0, 1290325 } };
94}
95
96/*---------------------------------------------------------------------------*/
97/*---------------------------------------------------------------------------*/
98
99template <typename DataType>
101
102template <typename DataType>
104{
105 public:
106
107 using value_type = DataType;
108
109 public:
110
111 explicit FillTraitsBaseT(const impl::BuildInfo<DataType>& build_info)
112 : m_min_clamp(build_info.clampValue().first)
113 , m_max_clamp(build_info.clampValue().second)
114 {}
115
116 public:
117
118 DataType minClamp() const { return m_min_clamp; }
119 DataType maxClamp() const { return m_max_clamp; }
120
121 private:
122
123 DataType m_min_clamp = {};
124 DataType m_max_clamp = {};
125};
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130template <typename DataType, typename RandomGeneratorDataType = DataType>
132: public FillTraitsBaseT<DataType>
133{
134 using BaseClass = FillTraitsBaseT<DataType>;
135
136 public:
137
138 using UniformGeneratorType = std::uniform_int_distribution<RandomGeneratorDataType>;
139
140 public:
141
143 : BaseClass(impl::_getIntegerBuildInfo<DataType>(std::is_signed<DataType>{}))
144 {}
145};
146
147/*---------------------------------------------------------------------------*/
148/*---------------------------------------------------------------------------*/
149
150template <typename DataType, typename RandomGeneratorDataType = DataType>
152: public FillTraitsBaseT<DataType>
153{
154 using BaseClass = FillTraitsBaseT<DataType>;
155
156 public:
157
158 using UniformGeneratorType = std::uniform_real_distribution<RandomGeneratorDataType>;
159
160 public:
161
162 explicit FloatFillTraitsT()
163 : BaseClass(impl::_getFloatBuildInfo<DataType>())
164 {}
165};
166
167/*---------------------------------------------------------------------------*/
168/*---------------------------------------------------------------------------*/
169
170template <>
171class FillTraitsT<char>
172: public IntegerFillTraitsT<char,std::conditional<std::is_signed_v<char>,short,unsigned short>::type>
173{
174};
175
176template <>
177class FillTraitsT<signed char>
178: public IntegerFillTraitsT<signed char,short>
179{
180};
181
182template <>
183class FillTraitsT<unsigned char>
184: public IntegerFillTraitsT<unsigned char,unsigned short>
185{
186};
187
188template <>
189class FillTraitsT<short>
190: public IntegerFillTraitsT<short>
191{
192};
193
194template <>
195class FillTraitsT<unsigned short>
196: public IntegerFillTraitsT<unsigned short>
197{
198};
199
200template <>
201class FillTraitsT<int>
202: public IntegerFillTraitsT<int>
203{
204};
205
206template <>
207class FillTraitsT<unsigned int>
208: public IntegerFillTraitsT<unsigned int>
209{
210};
211
212template <>
213class FillTraitsT<long>
214: public IntegerFillTraitsT<long>
215{
216};
217
218template <>
219class FillTraitsT<unsigned long>
220: public IntegerFillTraitsT<unsigned long>
221{
222};
223
224template <>
225class FillTraitsT<long long>
226: public IntegerFillTraitsT<long long>
227{
228};
229
230template <>
231class FillTraitsT<unsigned long long>
232: public IntegerFillTraitsT<unsigned long long>
233{
234};
235
236/*---------------------------------------------------------------------------*/
237/*---------------------------------------------------------------------------*/
238
239template <>
240class FillTraitsT<float>
241: public FloatFillTraitsT<float>
242{
243};
244
245template <>
246class FillTraitsT<double>
247: public FloatFillTraitsT<double>
248{
249};
250
251template <>
252class FillTraitsT<long double>
253: public FloatFillTraitsT<long double>
254{
255};
256
257template <>
259: public FloatFillTraitsT<BFloat16, float>
260{
261};
262
263template <>
265: public FloatFillTraitsT<Float16, float>
266{
267};
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
271
272} // namespace Arccore::ValueFiller::impl
273
274/*---------------------------------------------------------------------------*/
275/*---------------------------------------------------------------------------*/
276
277namespace Arccore::ValueFiller
278{
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
282
283template <typename DataType> inline
284void fillRandom(Int64 seed, Span<DataType> values)
285{
286 std::seed_seq rng_seed{ seed };
287 using TraitsType = impl::FillTraitsT<DataType>;
288 using UniformGeneratorType = typename TraitsType::UniformGeneratorType;
289 TraitsType traits_value;
290 std::mt19937_64 randomizer(rng_seed);
291 UniformGeneratorType rng_distrib(traits_value.minClamp(), traits_value.maxClamp());
292
293 Int64 n1 = values.size();
294 for (Int32 i = 0; i < n1; ++i) {
295 values[i] = static_cast<DataType>(rng_distrib(randomizer));
296 }
297}
298
299/*---------------------------------------------------------------------------*/
300/*---------------------------------------------------------------------------*/
301
302} // namespace Arccore::ValueFiller
303
304/*---------------------------------------------------------------------------*/
305/*---------------------------------------------------------------------------*/
306
307#endif
308
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.