Skip to content

Commit

Permalink
Prepare for multithreaded assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
cbritopacheco committed Nov 12, 2023
1 parent e524fb8 commit 2af5995
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 61 deletions.
48 changes: 28 additions & 20 deletions src/Rodin/Geometry/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ namespace Rodin::Geometry
return *this;
}

Mesh<Context::Serial>::~Mesh()
{
for (auto& mt : m_transformationIndex)
{
mt.write(
[](auto& obj)
{
for (PolytopeTransformation* ptr : obj)
delete ptr;
});
}
}

Mesh<Context::Serial>&
Mesh<Context::Serial>::load(const boost::filesystem::path& filename, IO::FileFormat fmt)
Expand Down Expand Up @@ -285,13 +297,9 @@ namespace Rodin::Geometry
}

Mesh<Context::Serial>& Mesh<Context::Serial>::setPolytopeTransformation(
const std::pair<size_t, Index> p, std::unique_ptr<PolytopeTransformation> trans)
const std::pair<size_t, Index> p, PolytopeTransformation* trans)
{
m_transformationIndex.write(
[&](auto& obj)
{
obj.track(p, std::move(trans));
});
m_transformationIndex[p.first].write([&](auto& obj) { obj[p.second] = trans; });
return *this;
}

Expand Down Expand Up @@ -327,25 +335,25 @@ namespace Rodin::Geometry
const PolytopeTransformation&
Mesh<Context::Serial>::getPolytopeTransformation(size_t dimension, Index idx) const
{
auto it = m_transformationIndex.read().find(dimension, idx);
if (it != m_transformationIndex.read().end(dimension))
assert(dimension < m_transformationIndex.size());
if (m_transformationIndex[dimension].read().size() == 0)
{
m_transformationIndex[dimension].write(
[&](auto& obj) { obj.resize(getPolytopeCount(dimension), nullptr); });
}
assert(0 < m_transformationIndex[dimension].read().size());
assert(idx < m_transformationIndex[dimension].read().size());
const auto& transPtr = m_transformationIndex[dimension].read()[idx];
if (transPtr)
{
assert(m_transformationIndex.read().at(dimension, idx));
return *it->second;
return *transPtr;
}
else
{
PolytopeTransformation* trans = getDefaultPolytopeTransformation(dimension, idx);
std::optional<std::reference_wrapper<const PolytopeTransformation>> res;
m_transformationIndex.write(
[&](auto& obj)
{
auto p = obj.insert(
it, { dimension, idx }, std::unique_ptr<PolytopeTransformation>(trans));
res = *p->second;
});
assert(res.has_value());
return res.value();
m_transformationIndex[dimension].write(
[&](auto& obj) { obj[idx] = trans; });
return *trans;
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/Rodin/Geometry/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ namespace Rodin::Geometry
virtual MeshBase& setVertexCoordinates(Index idx, const Math::SpatialVector& coords) = 0;

virtual MeshBase& setPolytopeTransformation(
const std::pair<size_t, Index> p, std::unique_ptr<PolytopeTransformation> trans) = 0;
const std::pair<size_t, Index> p, PolytopeTransformation* trans) = 0;
};

/// Index containing the indices of boundary cells.
Expand All @@ -487,7 +487,8 @@ namespace Rodin::Geometry
using AttributeIndex = PolytopeIndexed<Geometry::Attribute>;

/// Index containing the transformations of the polytopes.
using TransformationIndex = PolytopeIndexed<std::unique_ptr<PolytopeTransformation>>;
using TransformationIndex =
std::vector<Threads::Mutable<std::vector<PolytopeTransformation*>>>;

/**
*
Expand Down Expand Up @@ -696,6 +697,8 @@ namespace Rodin::Geometry
*/
Mesh(Mesh&& other);

virtual ~Mesh();

Mesh& operator=(const Mesh& other) = delete;

/**
Expand Down Expand Up @@ -733,11 +736,8 @@ namespace Rodin::Geometry

virtual void flush() override
{
m_transformationIndex.write(
[](auto& obj)
{
obj.clear();
});
for (auto& mt : m_transformationIndex)
mt.write([](auto& obj) { obj.clear(); });
}

/**
Expand Down Expand Up @@ -855,7 +855,7 @@ namespace Rodin::Geometry

const TransformationIndex& getTransformationIndex() const
{
return m_transformationIndex.read();
return m_transformationIndex;
}

inline
Expand Down Expand Up @@ -918,8 +918,7 @@ namespace Rodin::Geometry
virtual Mesh& setVertexCoordinates(Index idx, const Math::SpatialVector& coords) override;

virtual Mesh& setPolytopeTransformation(
const std::pair<size_t, Index> p,
std::unique_ptr<PolytopeTransformation> trans) override;
const std::pair<size_t, Index> p, PolytopeTransformation* trans) override;

virtual const PolytopeTransformation& getPolytopeTransformation(
size_t dimension, Index idx) const override;
Expand All @@ -936,7 +935,7 @@ namespace Rodin::Geometry
MeshConnectivity m_connectivity;

AttributeIndex m_attributeIndex;
mutable Threads::Mutable<TransformationIndex> m_transformationIndex;
mutable TransformationIndex m_transformationIndex;

std::vector<FlatSet<Attribute>> m_attributes;
};
Expand Down
4 changes: 2 additions & 2 deletions src/Rodin/Geometry/MeshBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace Rodin::Geometry

// Set indexes
m_attributeIndex.initialize(m_sdim + 1);
m_transformationIndex.initialize(m_sdim + 1);

m_transformationIndex.resize(sdim + 1);
return *this;
}

Expand Down Expand Up @@ -112,7 +112,7 @@ namespace Rodin::Geometry
{
m_connectivity.reserve(d, count);
m_attributeIndex.reserve(d, count);
m_transformationIndex.reserve(d, count);
m_transformationIndex[d].write([&](auto& obj){ obj.reserve(count); });
return *this;
}

Expand Down
84 changes: 56 additions & 28 deletions src/Rodin/Geometry/PolytopeIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@ namespace Rodin::Geometry

Polytope* PolytopeIterator::release()
{
Polytope* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
if (!m_polytope.read() || m_dirty.read())
{
return generate();
}
else
{
Polytope* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
}
}

// ---- CellIterator -------------------------------------------------------
Expand Down Expand Up @@ -157,13 +164,20 @@ namespace Rodin::Geometry

Cell* CellIterator::release()
{
Cell* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
if (!m_polytope.read() || m_dirty.read())
{
return generate();
}
else
{
Cell* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
}
}

// ---- FaceIterator -------------------------------------------------------
Expand Down Expand Up @@ -237,13 +251,20 @@ namespace Rodin::Geometry

Face* FaceIterator::release()
{
Face* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
if (!m_polytope.read() || m_dirty.read())
{
return generate();
}
else
{
Face* polytope = nullptr;
m_polytope.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
}
}

// ---- VertexIterator -----------------------------------------------------
Expand Down Expand Up @@ -317,12 +338,19 @@ namespace Rodin::Geometry

Vertex* VertexIterator::release()
{
Vertex* polytope = nullptr;
m_vertex.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
if (!m_vertex.read() || m_dirty.read())
{
return generate();
}
else
{
Vertex* polytope = nullptr;
m_vertex.write(
[&](auto& obj)
{
polytope = obj.release();
});
return polytope;
}
}
}

0 comments on commit 2af5995

Please sign in to comment.