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
55class ARCCORE_ALINA_EXPORT SolverResult
59 SolverResult() =
default;
60 SolverResult(
const std::tuple<size_t, double>& v)
61 : m_nb_iteration(get<0>(v))
62 , m_residual(get<1>(v))
64 SolverResult(
const std::tuple<size_t, float>& v)
65 : m_nb_iteration(get<0>(v))
66 , m_residual(get<1>(v))
68 SolverResult(
size_t nb_iteration,
double residual)
69 : m_nb_iteration(nb_iteration)
70 , m_residual(residual)
73 operator std::tuple<size_t, double>()
const {
return { m_nb_iteration, m_residual }; }
77 constexpr Int32 nbIteration()
const {
return static_cast<Int32>(m_nb_iteration); }
78 constexpr double residual()
const {
return m_residual; }
82 size_t m_nb_iteration = 0;
83 double m_residual = 0.0;
91 class PropertyWrapper;
93 class ARCCORE_ALINA_EXPORT empty_params
112class ARCCORE_ALINA_EXPORT PropertyTree
126 PropertyTree(
const PropertyTree& rhs);
127 PropertyTree& operator=(
const PropertyTree& rhs);
132 Int32 get(
const char* param_type,
Int32 default_value)
const;
133 Int64 get(
const char* param_type,
Int64 default_value)
const;
134 size_t get(
const char* param_type,
size_t default_value)
const
136 return get(param_type,
static_cast<Int64>(default_value));
138 double get(
const char* param_type,
double default_value)
const;
139 double* get(
const char* param_type,
double* default_value)
const;
140 void* get(
const char* param_type,
void* default_value)
const;
141 std::string get(
const char* param_type,
const std::string& default_value)
const;
143 template <
typename DataType> DataType
144 get(
const char* param_type,
const DataType& default_value)
const
145 requires(std::is_enum_v<DataType>)
147 std::ostringstream default_ostr;
148 default_ostr << default_value;
149 std::string s = get(param_type, default_ostr.str());
150 std::istringstream istr(s);
158 void put(
const std::string& path,
Int32 value);
159 void put(
const std::string& path,
Int64 value);
160 void put(
const std::string& path,
size_t value)
162 put(path,
static_cast<Int64>(value));
165 void put(
const std::string& path,
double value);
166 void put(
const std::string& path,
const std::string& value);
167 void put(
const std::string& path,
double* value);
168 void put(
const std::string& path,
void* value);
170 template <
typename DataType>
void
171 put(
const std::string& path,
const DataType& value)
172 requires(std::is_enum_v<DataType>)
175 std::ostringstream ostr;
177 put(path, ostr.str());
181 void putKeyValue(
const std::string& param);
183 PropertyTree get_child_empty(
const std::string& path)
const;
184 bool erase(
const char* name);
185 size_t count(
const char* name)
const;
188 void _addChild(
const std::string& path,
const char* name,
const PropertyTree& obj);
192 void read_json(
const std::string& filename);
196 void check_params(
const std::set<std::string>& names)
const;
197 void check_params(
const std::set<std::string>& names,
const std::set<std::string>& opt_names)
const;
198 ARCCORE_ALINA_EXPORT
friend std::ostream& operator<<(std::ostream& o,
const PropertyTree& obj);
203 bool m_is_own =
false;
211#include "arccore/alina/ScopedStreamModifier.h"
225#ifdef ARCCORE_ALINA_PROFILING
226#if !defined(ARCCORE_ALINA_TIC) || !defined(ARCCORE_ALINA_TOC)
227#include "arccore/alina/Profiler.h"
228#define ARCCORE_ALINA_TIC(name) ::Arcane::Alina::Profiler::globalTic(name);
229#define ARCCORE_ALINA_TOC(name) ::Arcane::Alina::Profiler::globalToc(name);
233#ifndef ARCCORE_ALINA_TIC
234#define ARCCORE_ALINA_TIC(name)
236#ifndef ARCCORE_ALINA_TOC
237#define ARCCORE_ALINA_TOC(name)
240#define ARCCORE_ALINA_DEBUG_SHOW(x) \
241 std::cout << std::setw(20) << #x << ": " \
242 << std::setw(15) << std::setprecision(8) << std::scientific \
248namespace Arcane::Alina
255template <
class Condition,
class Message>
256void precondition(
const Condition& condition,
const Message& message)
260#pragma warning(disable : 4800)
263 throw std::runtime_error(message);
272#define ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, name) \
273 name(p.get(#name, params().name))
275#define ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, name) \
276 name(p.get_child_empty(#name))
278#define ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, name) \
279 p.put(std::string(path) + #name, name)
284 template <
typename T>
285 inline void params_export_child(PropertyTree& ap,
286 const std::string& path,
287 const char* name,
const T& obj)
289 obj.get(ap, std::string(path) + name +
".");
294 inline void params_export_child(PropertyTree& ap,
295 const std::string& path,
const char* name,
296 const PropertyTree& obj)
298 ap._addChild(path, name, obj);
303#define ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, name) \
304 ::Arcane::Alina::detail::params_export_child(p, path, #name, name)
310#ifndef ARCCORE_ALINA_PARAM_MISSING
311#define ARCCORE_ALINA_PARAM_MISSING(name) (void)0
318template <
class T,
int N>
321 static_assert(N > 0,
"Wrong number of dimensions");
325 template <
class... I>
328 static_assert(
sizeof...(I) == N,
"Wrong number of dimensions");
329 buf.resize(init(n...));
337 int stride(
int i)
const
342 template <
class... I>
343 T operator()(I... i)
const
345 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
346 return buf[index(i...)];
349 template <
class... I>
350 T& operator()(I... i)
352 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
353 return buf[index(i...)];
356 const T* data()
const
368 std::array<int, N> strides;
371 template <
class... I>
372 int index(
int i, I... tail)
const
374 return strides[N -
sizeof...(I) - 1] * i + index(tail...);
377 int index(
int i)
const
379 return strides[N - 1] * i;
382 template <
class... I>
383 int init(
int i, I... tail)
385 int size = init(tail...);
386 strides[N -
sizeof...(I) - 1] = size;
402 circular_buffer(
size_t n)
413 void push_back(
const T& v)
415 if (buf.size() < buf.capacity()) {
420 start = (start + 1) % buf.capacity();
424 const T& operator[](
size_t i)
const
426 return buf[(start + i) % buf.capacity()];
429 T& operator[](
size_t i)
431 return buf[(start + i) % buf.capacity()];
452 return 2 * std::numeric_limits<T>::epsilon() * n;
459template <
class T>
struct is_complex<std::complex<T>> : std::true_type
462inline std::string human_readable_memory(
size_t bytes)
464 static const char* suffix[] = {
"B",
"K",
"M",
"G",
"T" };
467 double m =
static_cast<double>(bytes);
468 for (; i < 4 && m >= 1024.0; ++i, m /= 1024.0)
471 std::ostringstream s;
472 s << std::fixed << std::setprecision(2) << m <<
" " << suffix[i];
483 non_copyable() =
default;
484 ~non_copyable() =
default;
486 non_copyable(non_copyable
const&) =
delete;
487 void operator=(non_copyable
const& x) =
delete;
493 template <
typename Col,
typename Val>
494 void sort_row(Col* col, Val* val,
int n)
496 for (
int j = 1; j < n; ++j) {
502 while (i >= 0 && col[i] > c) {
530inline istream&
operator>>(istream& is, T*& ptr)
535 is >> std::hex >> val;
537 ptr =
reinterpret_cast<T*
>(val);
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Management of references to a C++ class.
Class to store parameters as a hierarchical key/value tree.
Save ostream flags in constructor, restore in destructor.
Minimal hierarchical property tree with string leaf values.
Class to handle empty parameter list.
std::int64_t Int64
Signed integer type of 64 bits.
std::istream & operator>>(std::istream &istr, eItemKind &item_kind)
Input operator from a stream.
std::int32_t Int32
Signed integer type of 32 bits.