Skip to content

Commit

Permalink
wip: vtk export interface [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
kubagalecki committed Jan 4, 2024
1 parent bcd634b commit e674cf2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 88 deletions.
12 changes: 6 additions & 6 deletions benchmarks/Diffusion3DBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ int main(int argc, char* argv[])
constexpr auto field_inds = util::makeIotaArray< size_t, problem_def.n_fields >();
constexpr auto T_inds = std::array< size_t, 1 >{0};
constexpr auto T_grad_inds = std::array< size_t, 3 >{1, 2, 3};
const auto field_inds_view = std::array{std::span< const size_t >{T_inds}, std::span< const size_t >{T_grad_inds}};
constexpr auto field_names = std::array{"T"sv, "gradT"sv};
constexpr auto dof_inds = field_inds;

constexpr auto dom_params = KernelParams{.dimension = 3, .n_equations = 7, .n_unknowns = 4};
constexpr auto diffusion_kernel3d = wrapDomainEquationKernel< dom_params >([](const auto& in, auto& out) {
constexpr auto diffusion_kernel3d = wrapDomainEquationKernel< dom_params >([](const auto&, auto& out) {
auto& [operators, rhs] = out;
auto& [A0, Ax, Ay, Az] = operators;

Expand Down Expand Up @@ -106,7 +104,7 @@ int main(int argc, char* argv[])
});
constexpr auto dbc_params = KernelParams{.dimension = 3, .n_equations = 1};
constexpr auto dirichlet_bc_kernel =
wrapBoundaryResidualKernel< dbc_params >([](const auto& in, auto& out) { out[0] = 0.; });
wrapBoundaryResidualKernel< dbc_params >([](const auto&, auto& out) { out[0] = 0.; });

constexpr auto alg_params = AlgebraicSystemParams{.cond_policy = CondensationPolicy::ElementBoundary};
constexpr auto algpar_ctwrpr = L3STER_WRAP_CTVAL(alg_params);
Expand Down Expand Up @@ -142,8 +140,10 @@ int main(int argc, char* argv[])
}

L3STER_PROFILE_REGION_BEGIN("Export results to VTK");
auto exporter = PvtuExporter{*my_partition};
exporter.exportSolution("Cube_Diffusion.pvtu", comm, solution_manager, field_names, field_inds_view);
auto exporter = PvtuExporter{*my_partition};
const auto field_inds = util::gatherAsCommon(T_inds, T_grad_inds);
constexpr auto field_names = std::array{"T"sv, "gradT"sv};
exporter.exportSolution("Cube_Diffusion.pvtu", comm, solution_manager, field_names, field_inds);
exporter.flushWriteQueue();
L3STER_PROFILE_REGION_END("Export results to VTK");
}
4 changes: 2 additions & 2 deletions benchmarks/GlobalAssemblyBenchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void BM_OwnerOrSharedNodeDeterminationNotGhost(benchmark::State& state)
std::execution::par);

const auto n_parts = state.range(0);
const auto partitions = partitionMesh(mesh, n_parts);
const auto partitions = partitionMesh(mesh, static_cast< idx_t >(n_parts));

for (auto _ : state)
{
Expand Down Expand Up @@ -45,7 +45,7 @@ static void BM_OwnerOrSharedNodeDeterminationShared(benchmark::State& state)
std::execution::par);

const auto n_parts = state.range(0);
const auto partitions = partitionMesh(mesh, n_parts);
const auto partitions = partitionMesh(mesh, static_cast< idx_t >(n_parts));

for (auto _ : state)
{
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/MeshBenchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void BM_MeshPartitioning(benchmark::State& state)
{
auto mesh = readMesh(L3STER_TESTDATA_ABSPATH(sphere.msh), {}, mesh::gmsh_tag);
for (auto _ : state)
benchmark::DoNotOptimize(partitionMesh(mesh, state.range(0)));
benchmark::DoNotOptimize(partitionMesh(mesh, static_cast< idx_t >(state.range(0))));
}
BENCHMARK(BM_MeshPartitioning)
->Unit(benchmark::kMillisecond)
Expand Down
151 changes: 73 additions & 78 deletions include/l3ster/post/VtkExport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class PvtuExporter
public:
template < el_o_t... orders >
explicit PvtuExporter(const mesh::MeshPartition< orders... >& mesh);
template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompIndRange >
void exportSolution(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
Names&& field_names,
CompIndRange&& field_component_inds);
template < std::ranges::range FieldCompInds >
void exportSolution(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
const util::ArrayOwner< std::string >& field_names,
FieldCompInds&& field_component_inds)
requires RangeOfConvertibleTo_c< std::ranges::range_reference_t< FieldCompInds >, size_t >;
template < el_o_t... orders >
void updateNodeCoords(const mesh::MeshPartition< orders... >& mesh);
inline void flushWriteQueue();
Expand All @@ -47,24 +47,18 @@ class PvtuExporter
template < el_o_t... orders >
void initTopo(const mesh::MeshPartition< orders... >& mesh);

template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
void enqueuePvtuFileWrite(std::string_view file_name,
const MpiComm& comm,
Names&& field_names,
CompInds&& field_component_inds);
template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
void enqueueVtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
Names&& field_names,
CompInds&& field_component_inds);
template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
auto makeDataDescription(Names&& field_names,
CompInds&& field_component_inds,
const std::vector< util::ArrayOwner< char > >& encoded_fields) const -> std::string;
inline void enqueuePvtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds);
inline void enqueueVtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds);
inline auto makeDataDescription(const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds,
const std::vector< util::ArrayOwner< char > >& encoded_fields) const -> std::string;

template < ContiguousSizedRangeOf< char > Text >
MPI_Offset enqueueWrite(std::shared_ptr< MpiComm::FileHandle > file, MPI_Offset pos, Text&& text)
Expand Down Expand Up @@ -95,8 +89,9 @@ inline constexpr std::string_view pvtu_preamble = R"(<?xml version="1.0"?>
inline constexpr std::string_view pvtu_postamble = R"(</PUnstructuredGrid>
</VTKFile>)";

template < RangeOfConvertibleTo_c< std::string_view > Names, RangeOfConvertibleTo_c< size_t > FieldComps >
void appendPvtuPointDataDef(std::string& str, Names&& field_names, FieldComps&& n_field_components)
inline void appendPvtuPointDataDef(std::string& str,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< size_t >& n_field_components)
{
bool scal_present = false, vec_present = false; // Only 1 scalar and 1 vector can be present in the header
auto name_it = std::ranges::begin(field_names);
Expand Down Expand Up @@ -147,14 +142,15 @@ inline void appendPvtuPieceDataDef(std::string& str, std::string_view name, int
}
}

template < RangeOfConvertibleTo_c< std::string_view > Names, RangeOfConvertibleTo_c< size_t > FieldComps >
std::string
makePvtuFileContents(std::string_view name, int n_ranks, Names&& field_names, FieldComps&& n_field_components)
inline auto makePvtuFileContents(std::string_view name,
int n_ranks,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< size_t >& n_field_components) -> std::string
{
std::string retval;
retval.reserve(1u << 12);
retval += pvtu_preamble;
appendPvtuPointDataDef(retval, std::forward< Names >(field_names), std::forward< FieldComps >(n_field_components));
appendPvtuPointDataDef(retval, field_names, n_field_components);
appendPvtuPieceDataDef(retval, name, n_ranks);
retval += pvtu_postamble;
return retval;
Expand Down Expand Up @@ -469,11 +465,11 @@ auto encodeSolution(const SolutionManager& solution_manager, FieldComps&& field_
return retval;
}

template < SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
auto makeNumFieldComponentsView(CompInds&& field_component_inds)
inline auto makeNumFieldComponentsView(const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds)
-> util::ArrayOwner< size_t >
{
return std::forward< CompInds >(field_component_inds) |
std::views::transform([](auto cmps) -> size_t { return cmps.size() == 1 ? 1 : 3; });
return field_component_inds |
std::views::transform([](const auto& cmps) -> size_t { return cmps.size() == 1 ? 1 : 3; });
}

inline auto makeEncodedFieldSizeView(const std::vector< util::ArrayOwner< char > >& encoded_fields)
Expand Down Expand Up @@ -501,6 +497,16 @@ inline auto openVtuFile(std::string_view name, const MpiComm& comm)
{
return openFileForExport(makeVtuFileName(name, comm));
}

template < std::ranges::range FieldCompInds >
auto makeFieldIndsArrayOwner(FieldCompInds&& inds) -> util::ArrayOwner< util::ArrayOwner< size_t > >
{
auto retval = util::ArrayOwner< util::ArrayOwner< size_t > >(std::ranges::distance(inds));
std::ranges::transform(std::forward< FieldCompInds >(inds), retval.begin(), []< typename R >(R&& r) {
return util::ArrayOwner< size_t >(std::forward< R >(r) | std::views::all);
});
return retval;
}
} // namespace post::vtk

template < el_o_t... orders >
Expand All @@ -510,26 +516,25 @@ PvtuExporter::PvtuExporter(const mesh::MeshPartition< orders... >& mesh) : m_n_n
updateNodeCoords(mesh);
initTopo(mesh);
}
template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompIndRange >
void PvtuExporter::exportSolution(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
Names&& field_names,
CompIndRange&& field_component_inds)

template < std::ranges::range FieldCompInds >
void PvtuExporter::exportSolution(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
const util::ArrayOwner< std::string >& field_names,
FieldCompInds&& field_component_inds_view)
requires RangeOfConvertibleTo_c< std::ranges::range_reference_t< FieldCompInds >, size_t >
{
L3STER_PROFILE_FUNCTION;
util::throwingAssert(std::ranges::size(field_names) == std::ranges::size(field_component_inds),
const auto field_component_inds =
post::vtk::makeFieldIndsArrayOwner(std::forward< FieldCompInds >(field_component_inds_view));
util::throwingAssert(field_names.size() == field_component_inds.size(),
"Field names and groupings must have the same size");

flushWriteQueue();
if (comm.getRank() == 0)
enqueuePvtuFileWrite(file_name, comm, field_names, field_component_inds);
enqueueVtuFileWrite(file_name,
comm,
solution_manager,
std::forward< Names >(field_names),
std::forward< CompIndRange >(field_component_inds));
enqueueVtuFileWrite(file_name, comm, solution_manager, field_names, field_component_inds);
}

template < el_o_t... orders >
Expand All @@ -552,32 +557,24 @@ void PvtuExporter::initTopo(const mesh::MeshPartition< orders... >& mesh)
m_encoded_topo = std::move(data);
}

template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
void PvtuExporter::enqueuePvtuFileWrite(std::string_view file_name,
const MpiComm& comm,
Names&& field_names,
CompInds&& field_component_inds)
{
auto&& n_field_components_view =
post::vtk::makeNumFieldComponentsView(std::forward< CompInds >(field_component_inds));
auto pvtu_contents =
post::vtk::makePvtuFileContents(file_name,
comm.getSize(),
std::forward< Names >(field_names),
std::forward< decltype(n_field_components_view) >(n_field_components_view));
void PvtuExporter::enqueuePvtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds)
{
const auto n_field_components_view = post::vtk::makeNumFieldComponentsView(field_component_inds);
auto pvtu_contents =
post::vtk::makePvtuFileContents(file_name, comm.getSize(), field_names, n_field_components_view);
const auto pvtu_name = std::filesystem::path(file_name).replace_extension("pvtu").string();
auto pvtu_file = post::vtk::openFileForExport(pvtu_name);
enqueueWrite(std::move(pvtu_file), 0, std::move(pvtu_contents));
}

template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
void PvtuExporter::enqueueVtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
Names&& field_names,
CompInds&& field_component_inds)
void PvtuExporter::enqueueVtuFileWrite(std::string_view file_name,
const MpiComm& comm,
const SolutionManager& solution_manager,
const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds)
{
const auto file_name_vtu_ext = std::filesystem::path{file_name}.replace_extension("vtu").string();
const auto vtu_file_handle = post::vtk::openVtuFile(file_name_vtu_ext, comm);
Expand All @@ -586,23 +583,21 @@ void PvtuExporter::enqueueVtuFileWrite(std::string_view file_name,
};

auto encoded_fields = post::vtk::encodeSolution(solution_manager, field_component_inds);
enqueue_write(makeDataDescription(std::forward< Names >(field_names), field_component_inds, encoded_fields));
enqueue_write(makeDataDescription(field_names, field_component_inds, encoded_fields));
enqueue_write(m_encoded_coords);
enqueue_write(m_encoded_topo);
for (auto& enc_fld : encoded_fields)
enqueue_write(std::move(enc_fld));
enqueue_write(post::vtk::vtu_postamble);
}

template < SizedRangeOfConvertibleTo_c< std::string_view > Names,
SizedRangeOfConvertibleTo_c< std::span< const size_t > > CompInds >
auto PvtuExporter::makeDataDescription(Names&& field_names,
CompInds&& field_component_inds,
const std::vector< util::ArrayOwner< char > >& encoded_fields) const
auto PvtuExporter::makeDataDescription(const util::ArrayOwner< std::string >& field_names,
const util::ArrayOwner< util::ArrayOwner< size_t > >& field_component_inds,
const std::vector< util::ArrayOwner< char > >& encoded_fields) const
-> std::string
{
auto&& n_field_components = post::vtk::makeNumFieldComponentsView(field_component_inds);
auto&& encoded_field_sizes = post::vtk::makeEncodedFieldSizeView(encoded_fields);
const auto n_field_components = post::vtk::makeNumFieldComponentsView(field_component_inds);
auto&& encoded_field_sizes = post::vtk::makeEncodedFieldSizeView(encoded_fields);

std::string retval;
retval.reserve(1u << 12);
Expand Down Expand Up @@ -659,7 +654,7 @@ auto PvtuExporter::makeDataDescription(Names&&

auto sz_it = std::ranges::begin(encoded_field_sizes);
auto n_cmp_it = std::ranges::begin(n_field_components);
for (std::string_view name : std::forward< Names >(field_names))
for (const auto& name : field_names)
append_data_array("Float64", name, *n_cmp_it++, *sz_it++);

retval += "</PointData>\n";
Expand Down
4 changes: 3 additions & 1 deletion include/l3ster/util/ArrayOwner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ArrayOwner
ArrayOwner() = default;
explicit ArrayOwner(std::size_t size) : m_size{size}, m_data{std::make_unique_for_overwrite< T[] >(size)} {}
template < std::ranges::range R >
ArrayOwner(R&& range); // NOLINT implicit conversion and copy are intended
ArrayOwner(R&& range) // NOLINT implicit conversion and copy are intended
requires std::constructible_from< T, std::ranges::range_reference_t< R > >;
template < std::convertible_to< T > Vals >
ArrayOwner(std::initializer_list< Vals > vals);

Expand Down Expand Up @@ -67,6 +68,7 @@ ArrayOwner(std::initializer_list< T >) -> ArrayOwner< T >;
template < std::default_initializable T >
template < std::ranges::range R >
ArrayOwner< T >::ArrayOwner(R&& range)
requires std::constructible_from< T, std::ranges::range_reference_t< R > >
: m_size{static_cast< size_t >(std::ranges::distance(range))}, m_data{std::make_unique_for_overwrite< T[] >(m_size)}
{
std::ranges::copy(std::forward< R >(range), begin());
Expand Down
Loading

0 comments on commit e674cf2

Please sign in to comment.