Skip to content

Commit

Permalink
[Type] Refactor Mat (sofa-framework#4396)
Browse files Browse the repository at this point in the history
remove inheritance and use std::array

Co-authored-by: Paul Baksic <30337881+bakpaul@users.noreply.github.com>
  • Loading branch information
fredroy and bakpaul authored Jan 10, 2024
1 parent 42cc4d4 commit 44ad519
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
72 changes: 63 additions & 9 deletions Sofa/framework/Type/src/sofa/type/Mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,38 @@ constexpr Mat<C,P,real> multTranspose(const Mat<L,C,real>& m1, const Mat<L,P,rea


template <sofa::Size L, sofa::Size C, class real>
class Mat : public fixed_array<VecNoInit<C,real>, L>
class Mat
{
public:

static constexpr sofa::Size N = L * C;

typedef typename fixed_array<real, N>::size_type Size;
typedef VecNoInit<C, real> LineNoInit;
using ArrayLineType = std::array<LineNoInit, L>;

typedef real Real;
typedef Vec<C,real> Line;
typedef VecNoInit<C,real> LineNoInit;
typedef Vec<L,real> Col;
typedef sofa::Size Size;

static constexpr Size nbLines = L;
static constexpr Size nbCols = C;

constexpr Mat() noexcept
{
clear();
}
typedef sofa::Size size_type;
typedef Line value_type;
typedef typename ArrayLineType::iterator iterator;
typedef typename ArrayLineType::const_iterator const_iterator;
typedef typename ArrayLineType::reference reference;
typedef typename ArrayLineType::const_reference const_reference;
typedef std::ptrdiff_t difference_type;

static constexpr sofa::Size static_size = L;
static constexpr sofa::Size total_size = L;

static constexpr sofa::Size size() { return static_size; }

ArrayLineType elems{};

constexpr Mat() noexcept = default;

explicit constexpr Mat(NoInit) noexcept
{
Expand Down Expand Up @@ -138,7 +150,7 @@ class Mat : public fixed_array<VecNoInit<C,real>, L>
typename = std::enable_if_t< (sizeof...(ArgsT) == L && sizeof...(ArgsT) > 1) >
>
constexpr Mat(ArgsT&&... r) noexcept
: sofa::type::fixed_array<LineNoInit, L>(std::forward<ArgsT>(r)...)
: elems{ std::forward<ArgsT>(r)... }
{}

/// Constructor from an element
Expand Down Expand Up @@ -796,6 +808,48 @@ class Mat : public fixed_array<VecNoInit<C,real>, L>
this->elems[l][c] = this->elems[c][l] = ( this->elems[l][c] + this->elems[c][l] ) * 0.5f;
}


// direct access to data
constexpr const real* data() const noexcept
{
return elems.data();
}

constexpr typename ArrayLineType::iterator begin() noexcept
{
return elems.begin();
}
constexpr typename ArrayLineType::const_iterator begin() const noexcept
{
return elems.begin();
}

constexpr typename ArrayLineType::iterator end() noexcept
{
return elems.end();
}
constexpr typename ArrayLineType::const_iterator end() const noexcept
{
return elems.end();
}

constexpr reference front()
{
return elems[0];
}
constexpr const_reference front() const
{
return elems[0];
}
constexpr reference back()
{
return elems[N - 1];
}
constexpr const_reference back() const
{
return elems[N - 1];
}

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,19 @@ void ParallelTetrahedronFEMForceField<DataTypes>::addKToMatrix(sofa::linearalgeb

std::mutex mutex;

static constexpr auto S = DataTypes::deriv_total_size; // size of node blocks
static constexpr auto N = Element::size();
using Block = sofa::type::fixed_array<sofa::type::fixed_array<sofa::type::Mat<S, S, double>, 4>, 4>;

sofa::simulation::parallelForEachRange(*m_taskScheduler, indexedElements.begin(), indexedElements.end(),
[&indexedElements, m, &Rot, this, &offset, mat, &mutex, kFactor](const auto& range)
{
constexpr auto S = DataTypes::deriv_total_size; // size of node blocks
constexpr auto N = Element::size();


StiffnessMatrix JKJt,tmp;

auto elementId = std::distance(indexedElements.begin(), range.start);

using Block = sofa::type::fixed_array<sofa::type::fixed_array<sofa::type::Mat<S, S, double>, 4>, 4>;
sofa::type::vector<Block> blocks;
blocks.reserve(std::distance(range.start, range.end));

Expand Down

0 comments on commit 44ad519

Please sign in to comment.