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
106class ARCCORE_ALINA_EXPORT PropertyTree
117 PropertyTree(
const PropertyTree& rhs);
123 Int32 get(
const char* param_type,
Int32 default_value)
const;
124 Int64 get(
const char* param_type,
Int64 default_value)
const;
125 size_t get(
const char* param_type,
size_t default_value)
const
127 return get(param_type,
static_cast<Int64>(default_value));
129 double get(
const char* param_type,
double default_value)
const;
130 double* get(
const char* param_type,
double* default_value)
const;
131 void* get(
const char* param_type,
void* default_value)
const;
132 std::string get(
const char* param_type,
const std::string& default_value)
const;
134 template <
typename DataType> DataType
135 get(
const char* param_type,
const DataType& default_value)
const
136 requires(std::is_enum_v<DataType>)
138 std::ostringstream default_ostr;
139 default_ostr << default_value;
140 std::string s = get(param_type, default_ostr.str());
141 std::istringstream istr(s);
145 ARCANE_FATAL(
"Can not convert '{0}' to enumeration", s);
149 void put(
const std::string& path,
Int32 value);
150 void put(
const std::string& path,
Int64 value);
151 void put(
const std::string& path,
size_t value)
153 put(path,
static_cast<Int64>(value));
156 void put(
const std::string& path,
double value);
157 void put(
const std::string& path,
const std::string& value);
158 void put(
const std::string& path,
double* value);
159 void put(
const std::string& path,
void* value);
161 template <
typename DataType>
void
162 put(
const std::string& path,
const DataType& value)
163 requires(std::is_enum_v<DataType>)
166 std::ostringstream ostr;
168 put(path, ostr.str());
172 void putKeyValue(
const std::string& param);
174 PropertyTree get_child_empty(
const std::string& path)
const;
175 bool erase(
const char* name);
176 size_t count(
const char* name)
const;
179 void _addChild(
const std::string& path,
const char* name,
const PropertyTree& obj);
183 void read_json(
const std::string& filename);
187 void check_params(
const std::set<std::string>& names)
const;
188 void check_params(
const std::set<std::string>& names,
const std::set<std::string>& opt_names)
const;
189 ARCCORE_ALINA_EXPORT
friend std::ostream& operator<<(std::ostream& o,
const PropertyTree& obj);
193 void* m_property_tree =
nullptr;
194 bool m_is_own =
false;
202#include "arccore/alina/ScopedStreamModifier.h"
216#ifdef ARCCORE_ALINA_PROFILING
217#if !defined(ARCCORE_ALINA_TIC) || !defined(ARCCORE_ALINA_TOC)
218#include "arccore/alina/Profiler.h"
219#define ARCCORE_ALINA_TIC(name) ::Arcane::Alina::Profiler::globalTic(name);
220#define ARCCORE_ALINA_TOC(name) ::Arcane::Alina::Profiler::globalToc(name);
224#ifndef ARCCORE_ALINA_TIC
225#define ARCCORE_ALINA_TIC(name)
227#ifndef ARCCORE_ALINA_TOC
228#define ARCCORE_ALINA_TOC(name)
231#define ARCCORE_ALINA_DEBUG_SHOW(x) \
232 std::cout << std::setw(20) << #x << ": " \
233 << std::setw(15) << std::setprecision(8) << std::scientific \
239namespace Arcane::Alina
246template <
class Condition,
class Message>
247void precondition(
const Condition& condition,
const Message& message)
251#pragma warning(disable : 4800)
254 throw std::runtime_error(message);
263#define ARCCORE_ALINA_PARAMS_IMPORT_VALUE(p, name) \
264 name(p.get(#name, params().name))
266#define ARCCORE_ALINA_PARAMS_IMPORT_CHILD(p, name) \
267 name(p.get_child_empty(#name))
269#define ARCCORE_ALINA_PARAMS_EXPORT_VALUE(p, path, name) \
270 p.put(std::string(path) + #name, name)
275 template <
typename T>
276 inline void params_export_child(PropertyTree& ap,
277 const std::string& path,
278 const char* name,
const T& obj)
280 obj.get(ap, std::string(path) + name +
".");
285 inline void params_export_child(PropertyTree& ap,
286 const std::string& path,
const char* name,
287 const PropertyTree& obj)
289 ap._addChild(path, name, obj);
294#define ARCCORE_ALINA_PARAMS_EXPORT_CHILD(p, path, name) \
295 ::Arcane::Alina::detail::params_export_child(p, path, #name, name)
301#ifndef ARCCORE_ALINA_PARAM_MISSING
302#define ARCCORE_ALINA_PARAM_MISSING(name) (void)0
309template <
class T,
int N>
312 static_assert(N > 0,
"Wrong number of dimensions");
316 template <
class... I>
319 static_assert(
sizeof...(I) == N,
"Wrong number of dimensions");
320 buf.resize(init(n...));
328 int stride(
int i)
const
333 template <
class... I>
334 T operator()(I... i)
const
336 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
337 return buf[index(i...)];
340 template <
class... I>
341 T& operator()(I... i)
343 static_assert(
sizeof...(I) == N,
"Wrong number of indices");
344 return buf[index(i...)];
347 const T* data()
const
359 std::array<int, N> strides;
362 template <
class... I>
363 int index(
int i, I... tail)
const
365 return strides[N -
sizeof...(I) - 1] * i + index(tail...);
368 int index(
int i)
const
370 return strides[N - 1] * i;
373 template <
class... I>
374 int init(
int i, I... tail)
376 int size = init(tail...);
377 strides[N -
sizeof...(I) - 1] = size;
393 circular_buffer(
size_t n)
404 void push_back(
const T& v)
406 if (buf.size() < buf.capacity()) {
411 start = (start + 1) % buf.capacity();
415 const T& operator[](
size_t i)
const
417 return buf[(start + i) % buf.capacity()];
420 T& operator[](
size_t i)
422 return buf[(start + i) % buf.capacity()];
443 return 2 * std::numeric_limits<T>::epsilon() * n;
450template <
class T>
struct is_complex<std::complex<T>> : std::true_type
453inline std::string human_readable_memory(
size_t bytes)
455 static const char* suffix[] = {
"B",
"K",
"M",
"G",
"T" };
458 double m =
static_cast<double>(bytes);
459 for (; i < 4 && m >= 1024.0; ++i, m /= 1024.0)
462 std::ostringstream s;
463 s << std::fixed << std::setprecision(2) << m <<
" " << suffix[i];
474 non_copyable() =
default;
475 ~non_copyable() =
default;
477 non_copyable(non_copyable
const&) =
delete;
478 void operator=(non_copyable
const& x) =
delete;
484 template <
typename Col,
typename Val>
485 void sort_row(Col* col, Val* val,
int n)
487 for (
int j = 1; j < n; ++j) {
493 while (i >= 0 && col[i] > c) {
521inline istream&
operator>>(istream& is, T*& ptr)
526 is >> std::hex >> val;
528 ptr =
reinterpret_cast<T*
>(val);
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Management of references to a C++ class.
Save ostream flags in constructor, restore in destructor.
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.