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
51class ARCCORE_ALINA_EXPORT SolverResult
55 SolverResult() =
default;
56 SolverResult(
const std::tuple<size_t, double>& v)
57 : m_nb_iteration(get<0>(v))
58 , m_residual(get<1>(v))
60 SolverResult(
const std::tuple<size_t, float>& v)
61 : m_nb_iteration(get<0>(v))
62 , m_residual(get<1>(v))
64 SolverResult(
size_t nb_iteration,
double residual)
65 : m_nb_iteration(nb_iteration)
66 , m_residual(residual)
69 operator std::tuple<size_t, double>()
const {
return { m_nb_iteration, m_residual }; }
73 constexpr Int32 nbIteration()
const {
return static_cast<Int32>(m_nb_iteration); }
74 constexpr double residual()
const {
return m_residual; }
78 size_t m_nb_iteration = 0;
79 double m_residual = 0.0;
87 class PropertyWrapper;
89 class ARCCORE_ALINA_EXPORT empty_params
105class ARCCORE_ALINA_EXPORT PropertyTree
116 PropertyTree(
const PropertyTree& rhs);
122 Int32 get(
const char* param_type,
Int32 default_value)
const;
123 Int64 get(
const char* param_type,
Int64 default_value)
const;
124 size_t get(
const char* param_type,
size_t default_value)
const
126 return get(param_type,
static_cast<Int64>(default_value));
128 double get(
const char* param_type,
double default_value)
const;
129 double* get(
const char* param_type,
double* default_value)
const;
130 void* get(
const char* param_type,
void* default_value)
const;
131 std::string get(
const char* param_type,
const std::string& default_value)
const;
133 template <
typename DataType> DataType
134 get(
const char* param_type,
const DataType& default_value)
const
135 requires(std::is_enum_v<DataType>)
137 std::ostringstream default_ostr;
138 default_ostr << default_value;
139 std::string s = get(param_type, default_ostr.str());
140 std::istringstream istr(s);
144 ARCANE_FATAL(
"Can not convert '{0}' to enumeration", s);
148 void put(
const std::string& path,
Int32 value);
149 void put(
const std::string& path,
Int64 value);
150 void put(
const std::string& path,
size_t value)
152 put(path,
static_cast<Int64>(value));
155 void put(
const std::string& path,
double value);
156 void put(
const std::string& path,
const std::string& value);
157 void put(
const std::string& path,
double* value);
158 void put(
const std::string& path,
void* value);
160 template <
typename DataType>
void
161 put(
const std::string& path,
const DataType& value)
162 requires(std::is_enum_v<DataType>)
165 std::ostringstream ostr;
167 put(path, ostr.str());
171 void putKeyValue(
const std::string& param);
173 PropertyTree get_child_empty(
const std::string& path)
const;
174 bool erase(
const char* name);
175 size_t count(
const char* name)
const;
178 void _addChild(
const std::string& path,
const char* name,
const PropertyTree& obj);
182 void read_json(
const std::string& filename);
186 void check_params(
const std::set<std::string>& names)
const;
187 void check_params(
const std::set<std::string>& names,
const std::set<std::string>& opt_names)
const;
188 ARCCORE_ALINA_EXPORT
friend std::ostream& operator<<(std::ostream& o,
const PropertyTree& obj);
192 void* m_property_tree =
nullptr;
193 bool m_is_own =
false;
201#include "arccore/alina/ScopedStreamModifier.h"
215#ifdef ARCCORE_ALINA_PROFILING
216#if !defined(ARCCORE_ALINA_TIC) || !defined(ARCCORE_ALINA_TOC)
217#include "arccore/alina/Profiler.h"
218#define ARCCORE_ALINA_TIC(name) ::Arcane::Alina::Profiler::globalTic(name);
219#define ARCCORE_ALINA_TOC(name) ::Arcane::Alina::Profiler::globalToc(name);
223#ifndef ARCCORE_ALINA_TIC
224#define ARCCORE_ALINA_TIC(name)
226#ifndef ARCCORE_ALINA_TOC
227#define ARCCORE_ALINA_TOC(name)
230#define ARCCORE_ALINA_DEBUG_SHOW(x) \
231 std::cout << std::setw(20) << #x << ": " \
232 << std::setw(15) << std::setprecision(8) << std::scientific \
238namespace Arcane::Alina
245template <
class Condition,
class Message>
246void precondition(
const Condition& condition,
const Message& message)
250#pragma warning(disable : 4800)
253 throw std::runtime_error(message);
262#define ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, name) \
263 name(p.get(#name, params().name))
265#define ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, name) \
266 name(p.get_child_empty(#name))
268#define ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, name) \
269 p.put(std::string(path) + #name, name)
274 template <
typename T>
275 inline void params_export_child(PropertyTree& ap,
276 const std::string& path,
277 const char* name,
const T& obj)
279 obj.get(ap, std::string(path) + name +
".");
284 inline void params_export_child(PropertyTree& ap,
285 const std::string& path,
const char* name,
286 const PropertyTree& obj)
288 ap._addChild(path, name, obj);
293#define ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, name) \
294 ::Arcane::Alina::detail::params_export_child(p, path, #name, name)
300#ifndef ARCCORE_ALINA_PARAM_MISSING
301#define ARCCORE_ALINA_PARAM_MISSING(name) (void)0
308template <
class T,
int N>
311 static_assert(N > 0,
"Wrong number of dimensions");
315 template <
class... I>
318 static_assert(
sizeof...(I) == N,
"Wrong number of dimensions");
319 buf.resize(init(n...));
327 int stride(
int i)
const
332 template <
class... I>
333 T operator()(I... i)
const
335 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
336 return buf[index(i...)];
339 template <
class... I>
340 T& operator()(I... i)
342 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
343 return buf[index(i...)];
346 const T* data()
const
358 std::array<int, N> strides;
361 template <
class... I>
362 int index(
int i, I... tail)
const
364 return strides[N -
sizeof...(I) - 1] * i + index(tail...);
367 int index(
int i)
const
369 return strides[N - 1] * i;
372 template <
class... I>
373 int init(
int i, I... tail)
375 int size = init(tail...);
376 strides[N -
sizeof...(I) - 1] = size;
392 circular_buffer(
size_t n)
403 void push_back(
const T& v)
405 if (buf.size() < buf.capacity()) {
410 start = (start + 1) % buf.capacity();
414 const T& operator[](
size_t i)
const
416 return buf[(start + i) % buf.capacity()];
419 T& operator[](
size_t i)
421 return buf[(start + i) % buf.capacity()];
442 return 2 * std::numeric_limits<T>::epsilon() * n;
449template <
class T>
struct is_complex<std::complex<T>> : std::true_type
452inline std::string human_readable_memory(
size_t bytes)
454 static const char* suffix[] = {
"B",
"K",
"M",
"G",
"T" };
457 double m =
static_cast<double>(bytes);
458 for (; i < 4 && m >= 1024.0; ++i, m /= 1024.0)
461 std::ostringstream s;
462 s << std::fixed << std::setprecision(2) << m <<
" " << suffix[i];
473 non_copyable() =
default;
474 ~non_copyable() =
default;
476 non_copyable(non_copyable
const&) =
delete;
477 void operator=(non_copyable
const& x) =
delete;
483 template <
typename Col,
typename Val>
484 void sort_row(Col* col, Val* val,
int n)
486 for (
int j = 1; j < n; ++j) {
492 while (i >= 0 && col[i] > c) {
520inline istream&
operator>>(istream& is, T*& ptr)
525 is >> std::hex >> val;
527 ptr =
reinterpret_cast<T*
>(val);
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Gestion des références à une classe C++.
Save ostream flags in constructor, restore in destructor.
Class to handle empty parameters list.
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.