AMReX-Codes / amrex

AMReX: Software Framework for Block Structured AMR
https://amrex-codes.github.io/amrex
Other
553 stars 352 forks source link

SmallMatrix: Matrix class with compile time size #4176

Closed WeiqunZhang closed 1 month ago

WeiqunZhang commented 1 month ago

Add amrex::SmallMatrix class with compile time size.

Useful for, e.g.:

zingale commented 1 month ago

it would be good to have a .setVal() method to set all elements to a value.

WeiqunZhang commented 1 month ago

Notes on why SmallMatrix matrix{} is zero initialized.

SmallMatrix is not an aggregate, because it has a user declared default constructor. The rule is that, for SmallMatrix matrix{} with an empty brace-enclosed initializer list, value-initialization is performed. The effects of value-initialization of SmallMatrix (which has a user-declared but not user-provided default constructor) are that the matrix object is first zero-initialized and then the object's default constructor is applied. Since the default constructor does nothing, the final result is the object is zero-initialized.

Why is SmallMatrix's default constructor user-declared not user-provided? It's because we first declare it with SmallMatrix () = default.

Reference: https://en.cppreference.com/w/cpp/language/list_initialization https://en.cppreference.com/w/cpp/language/value_initialization https://en.cppreference.com/w/cpp/language/zero_initialization

ax3l commented 1 month ago

Is SmallMatrix<Real, N, M> m; also zero-defined then or is it allocated but values are undefined?

ax3l commented 1 month ago

FYI @cemitch99

WeiqunZhang commented 1 month ago

It's uninitialized by default.

/**
 * \brief Default constructor
 *
 * The data are uninitialized by default. If you want to initialize
 * to zero, you can do `SmallMatrix<T,NRows,NCols> M{};`.
 */
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
constexpr SmallMatrix () = default;