13#ifndef ARCCORE_ALINA_ALINAUTILS_H
14#define ARCCORE_ALINA_ALINAUTILS_H
27#pragma GCC diagnostic ignored "-Wconversion"
28#pragma GCC diagnostic ignored "-Wsign-compare"
30#include "arccore/alina/AlinaGlobal.h"
33#include "arccore/base/FatalErrorException.h"
34#include "arccore/base/ForLoopRunInfo.h"
35#include "arccore/concurrency/ParallelFor.h"
45namespace Arcane::Alina
52class ARCCORE_ALINA_EXPORT SolverResult
56 SolverResult() =
default;
57 SolverResult(
const std::tuple<size_t, double>& v)
58 : m_nb_iteration(get<0>(v))
59 , m_residual(get<1>(v))
61 SolverResult(
const std::tuple<size_t, float>& v)
62 : m_nb_iteration(get<0>(v))
63 , m_residual(get<1>(v))
65 SolverResult(
size_t nb_iteration,
double residual)
66 : m_nb_iteration(nb_iteration)
67 , m_residual(residual)
70 operator std::tuple<size_t, double>()
const {
return { m_nb_iteration, m_residual }; }
74 constexpr Int32 nbIteration()
const {
return static_cast<Int32>(m_nb_iteration); }
75 constexpr double residual()
const {
return m_residual; }
79 size_t m_nb_iteration = 0;
80 double m_residual = 0.0;
88 class PropertyWrapper;
90 class ARCCORE_ALINA_EXPORT empty_params
111class ARCCORE_ALINA_EXPORT PropertyTree
121 PropertyTree(
const PropertyTree& rhs);
126 Int32 get(
const char* param_type,
Int32 default_value)
const;
127 Int64 get(
const char* param_type,
Int64 default_value)
const;
128 size_t get(
const char* param_type,
size_t default_value)
const
130 return get(param_type,
static_cast<Int64>(default_value));
132 double get(
const char* param_type,
double default_value)
const;
133 double* get(
const char* param_type,
double* default_value)
const;
134 void* get(
const char* param_type,
void* default_value)
const;
135 std::string get(
const char* param_type,
const std::string& default_value)
const;
137 template <
typename DataType> DataType
138 get(
const char* param_type,
const DataType& default_value)
const
139 requires(std::is_enum_v<DataType>)
141 std::ostringstream default_ostr;
142 default_ostr << default_value;
143 std::string s = get(param_type, default_ostr.str());
144 std::istringstream istr(s);
152 void put(
const std::string& path,
Int32 value);
153 void put(
const std::string& path,
Int64 value);
154 void put(
const std::string& path,
size_t value)
156 put(path,
static_cast<Int64>(value));
159 void put(
const std::string& path,
double value);
160 void put(
const std::string& path,
const std::string& value);
161 void put(
const std::string& path,
double* value);
162 void put(
const std::string& path,
void* value);
164 template <
typename DataType>
void
165 put(
const std::string& path,
const DataType& value)
166 requires(std::is_enum_v<DataType>)
169 std::ostringstream ostr;
171 put(path, ostr.str());
175 void putKeyValue(
const std::string& param);
177 PropertyTree get_child_empty(
const std::string& path)
const;
178 bool erase(
const char* name);
179 size_t count(
const char* name)
const;
182 void _addChild(
const std::string& path,
const char* name,
const PropertyTree& obj);
186 void read_json(
const std::string& filename);
190 void check_params(
const std::set<std::string>& names)
const;
191 void check_params(
const std::set<std::string>& names,
const std::set<std::string>& opt_names)
const;
192 ARCCORE_ALINA_EXPORT
friend std::ostream& operator<<(std::ostream& o,
const PropertyTree& obj);
196 void* m_property_tree =
nullptr;
197 bool m_is_own =
false;
205#include "arccore/alina/ScopedStreamModifier.h"
219#ifdef ARCCORE_ALINA_PROFILING
220#if !defined(ARCCORE_ALINA_TIC) || !defined(ARCCORE_ALINA_TOC)
221#include "arccore/alina/Profiler.h"
222#define ARCCORE_ALINA_TIC(name) ::Arcane::Alina::Profiler::globalTic(name);
223#define ARCCORE_ALINA_TOC(name) ::Arcane::Alina::Profiler::globalToc(name);
227#ifndef ARCCORE_ALINA_TIC
228#define ARCCORE_ALINA_TIC(name)
230#ifndef ARCCORE_ALINA_TOC
231#define ARCCORE_ALINA_TOC(name)
234#define ARCCORE_ALINA_DEBUG_SHOW(x) \
235 std::cout << std::setw(20) << #x << ": " \
236 << std::setw(15) << std::setprecision(8) << std::scientific \
242namespace Arcane::Alina
249template <
class Condition,
class Message>
250void precondition(
const Condition& condition,
const Message& message)
254#pragma warning(disable : 4800)
257 throw std::runtime_error(message);
266#define ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, name) \
267 name(p.get(#name, params().name))
269#define ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, name) \
270 name(p.get_child_empty(#name))
272#define ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, name) \
273 p.put(std::string(path) + #name, name)
278 template <
typename T>
279 inline void params_export_child(PropertyTree& ap,
280 const std::string& path,
281 const char* name,
const T& obj)
283 obj.get(ap, std::string(path) + name +
".");
288 inline void params_export_child(PropertyTree& ap,
289 const std::string& path,
const char* name,
290 const PropertyTree& obj)
292 ap._addChild(path, name, obj);
297#define ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, name) \
298 ::Arcane::Alina::detail::params_export_child(p, path, #name, name)
304#ifndef ARCCORE_ALINA_PARAM_MISSING
305#define ARCCORE_ALINA_PARAM_MISSING(name) (void)0
312template <
class T,
int N>
315 static_assert(N > 0,
"Wrong number of dimensions");
319 template <
class... I>
322 static_assert(
sizeof...(I) == N,
"Wrong number of dimensions");
323 buf.resize(init(n...));
331 int stride(
int i)
const
336 template <
class... I>
337 T operator()(I... i)
const
339 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
340 return buf[index(i...)];
343 template <
class... I>
344 T& operator()(I... i)
346 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
347 return buf[index(i...)];
350 const T* data()
const
362 std::array<int, N> strides;
365 template <
class... I>
366 int index(
int i, I... tail)
const
368 return strides[N -
sizeof...(I) - 1] * i + index(tail...);
371 int index(
int i)
const
373 return strides[N - 1] * i;
376 template <
class... I>
377 int init(
int i, I... tail)
379 int size = init(tail...);
380 strides[N -
sizeof...(I) - 1] = size;
396 circular_buffer(
size_t n)
407 void push_back(
const T& v)
409 if (buf.size() < buf.capacity()) {
414 start = (start + 1) % buf.capacity();
418 const T& operator[](
size_t i)
const
420 return buf[(start + i) % buf.capacity()];
423 T& operator[](
size_t i)
425 return buf[(start + i) % buf.capacity()];
446 return 2 * std::numeric_limits<T>::epsilon() * n;
453template <
class T>
struct is_complex<std::complex<T>> : std::true_type
456inline std::string human_readable_memory(
size_t bytes)
458 static const char* suffix[] = {
"B",
"K",
"M",
"G",
"T" };
461 double m =
static_cast<double>(bytes);
462 for (; i < 4 && m >= 1024.0; ++i, m /= 1024.0)
465 std::ostringstream s;
466 s << std::fixed << std::setprecision(2) << m <<
" " << suffix[i];
477 non_copyable() =
default;
478 ~non_copyable() =
default;
480 non_copyable(non_copyable
const&) =
delete;
481 void operator=(non_copyable
const& x) =
delete;
487 template <
typename Col,
typename Val>
488 void sort_row(Col* col, Val* val,
int n)
490 for (
int j = 1; j < n; ++j) {
496 while (i >= 0 && col[i] > c) {
524inline istream&
operator>>(istream& is, T*& ptr)
529 is >> std::hex >> val;
531 ptr =
reinterpret_cast<T*
>(val);
#define ARCCORE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Gestion des références à une classe C++.
Classe pour stocker les paramètres sous forme d'arbre hiérarchique clé/valeur.
Save ostream flags in constructor, restore in destructor.
Classe pour gérer une liste de paramètres vide.
std::int64_t Int64
Type entier signé sur 64 bits.
std::istream & operator>>(std::istream &istr, eItemKind &item_kind)
Opérateur d'entrée depuis un flot.
std::int32_t Int32
Type entier signé sur 32 bits.