Skip to content

Commit

Permalink
[cpp] Make precision a runtime parameter for sim2d
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Sep 28, 2024
1 parent 906c60f commit a6ef269
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion run_2d.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cd "$model_dir"
python "$sim_setup"

# Run sim
DPCPP_CPU_PLACES=cores DPCPP_CPU_CU_AFFINITY=spread DPCPP_CPU_NUM_CUS=$jobs OMP_NUM_THREADS=$jobs "$engine_exe" sim2d -s "$sim_dir" -e sycl
DPCPP_CPU_PLACES=cores DPCPP_CPU_CU_AFFINITY=spread DPCPP_CPU_NUM_CUS=$jobs OMP_NUM_THREADS=$jobs "$engine_exe" sim2d -p 32 -s "$sim_dir" -e sycl
# pffdtd sim2d run --sim_dir "$sim_dir" --video

# Post-process
Expand Down
1 change: 1 addition & 0 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ target_sources(pffdtd
pffdtd/exception.hpp
pffdtd/hdf.hpp
pffdtd/mdspan.hpp
pffdtd/precision.hpp
pffdtd/progress.cpp
pffdtd/progress.hpp
pffdtd/simulation_2d.cpp
Expand Down
38 changes: 19 additions & 19 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pffdtd/engine_cpu_3d.hpp"
#include "pffdtd/exception.hpp"
#include "pffdtd/hdf.hpp"
#include "pffdtd/precision.hpp"
#include "pffdtd/simulation_2d.hpp"
#include "pffdtd/simulation_3d.hpp"
#include "pffdtd/time.hpp"
Expand Down Expand Up @@ -33,19 +34,14 @@

namespace {

enum struct Precision {
Float,
Double,
};

[[nodiscard]] auto precisionOptions() {
return std::map<std::string, Precision>{
{"32", Precision::Float},
{"64", Precision::Double},
return std::map<std::string, pffdtd::Precision>{
{"32", pffdtd::Precision::Float},
{"64", pffdtd::Precision::Double},
};
}

[[nodiscard]] auto toString(Precision precision) -> std::string {
[[nodiscard]] auto toString(pffdtd::Precision precision) -> std::string {
for (auto const& p : precisionOptions()) {
if (p.second == precision) {
return p.first;
Expand All @@ -55,15 +51,15 @@ enum struct Precision {
}

[[nodiscard]] auto getEngines2D() {
using pffdtd::Simulation2D;
using Callback = std::function<stdex::mdarray<double, stdex::dextents<size_t, 2>>(Simulation2D const&)>;
using namespace pffdtd;
using Callback = std::function<stdex::mdarray<double, stdex::dextents<size_t, 2>>(Simulation2D const&, Precision)>;
auto engines = std::map<std::string, Callback>{};
engines["cpu"] = pffdtd::EngineCPU2D{};
engines["cpu"] = EngineCPU2D{};
#if defined(PFFDTD_HAS_METAL)
engines["metal"] = pffdtd::EngineMETAL2D{};
engines["metal"] = EngineMETAL2D{};
#endif
#if defined(PFFDTD_HAS_SYCL)
engines["sycl"] = pffdtd::EngineSYCL2D{};
engines["sycl"] = EngineSYCL2D{};
#endif
return engines;
}
Expand Down Expand Up @@ -97,12 +93,13 @@ struct Arguments {
std::string simDir;
std::string engine{"cpu"};
std::string out{"out.h5"};
pffdtd::Precision precision{pffdtd::Precision::Double};
};

struct Sim3D {
std::string simDir;
std::string engine{"cpu"};
Precision precision{Precision::Double};
pffdtd::Precision precision{pffdtd::Precision::Double};
};

Sim2D sim2d;
Expand Down Expand Up @@ -142,6 +139,9 @@ auto main(int argc, char** argv) -> int {
sim2d->add_option("-s,--sim_dir", args.sim2d.simDir)->required()->check(CLI::ExistingDirectory);
sim2d->add_option("-e,--engine", args.sim2d.engine)->transform(toLower);
sim2d->add_option("-o,--out", args.sim2d.out);
sim2d->add_option("-p,--precision", args.sim3d.precision)
->required()
->transform(CLI::CheckedTransformer(precisionOptions(), CLI::ignore_case));

auto* sim3d = app.add_subcommand("sim3d", "Run 3D simulation");
sim3d->add_option("-s,--sim_dir", args.sim3d.simDir)->required()->check(CLI::ExistingDirectory);
Expand All @@ -154,14 +154,14 @@ auto main(int argc, char** argv) -> int {
CLI11_PARSE(app, argc, argv);

if (*sim2d) {
fmt::println("Using engine: {}", args.sim2d.engine);
fmt::println("Using engine: {} with precision {}", args.sim2d.engine, toString(args.sim2d.precision));
auto const engines = getEngines2D();
auto const& engine = engines.at(args.sim2d.engine);

auto const start = pffdtd::getTime();
auto const simDir = std::filesystem::path{args.sim2d.simDir};
auto const sim = pffdtd::loadSimulation2D(simDir);
auto const out = engine(sim);
auto const out = engine(sim, args.sim2d.precision);

auto results = pffdtd::HDF5Writer{simDir / args.sim2d.out};
results.write("out", out);
Expand All @@ -172,9 +172,9 @@ auto main(int argc, char** argv) -> int {
}

if (*sim3d) {
if (args.sim3d.precision == Precision::Float) {
if (args.sim3d.precision == pffdtd::Precision::Float) {
run3D<float>(args.sim3d);
} else if (args.sim3d.precision == Precision::Double) {
} else if (args.sim3d.precision == pffdtd::Precision::Double) {
run3D<double>(args.sim3d);
} else {
pffdtd::raisef<std::invalid_argument>("invalid precision '{}'", toString(args.sim3d.precision));
Expand Down
19 changes: 17 additions & 2 deletions src/cpp/pffdtd/engine_cpu_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "engine_cpu_2d.hpp"

#include "pffdtd/double.hpp"
#include "pffdtd/exception.hpp"
#include "pffdtd/progress.hpp"
#include "pffdtd/time.hpp"

Expand All @@ -17,9 +18,10 @@

namespace pffdtd {

auto EngineCPU2D::operator()(Simulation2D const& sim) const -> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
using Real = double;
namespace {

template<typename Real>
auto run(Simulation2D const& sim) {
summary(sim);

auto const Nx = sim.Nx;
Expand Down Expand Up @@ -145,4 +147,17 @@ auto EngineCPU2D::operator()(Simulation2D const& sim) const -> stdex::mdarray<do
return out_buf;
}

} // namespace

auto EngineCPU2D::operator()(Simulation2D const& sim, Precision precision) const
-> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
if (precision == Precision::Float) {
return run<float>(sim);
} else if (precision == Precision::Double) {
return run<double>(sim);
} else {
raisef<std::invalid_argument>("invalid precision {}", static_cast<int>(precision));
}
}

} // namespace pffdtd
4 changes: 3 additions & 1 deletion src/cpp/pffdtd/engine_cpu_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
#pragma once

#include "pffdtd/mdspan.hpp"
#include "pffdtd/precision.hpp"
#include "pffdtd/simulation_2d.hpp"

#include <cstddef>

namespace pffdtd {

struct EngineCPU2D {
[[nodiscard]] auto operator()(Simulation2D const& sim) const -> stdex::mdarray<double, stdex::dextents<size_t, 2>>;
[[nodiscard]] auto operator()(Simulation2D const& sim, Precision precision) const
-> stdex::mdarray<double, stdex::dextents<size_t, 2>>;
};

} // namespace pffdtd
1 change: 1 addition & 0 deletions src/cpp/pffdtd/engine_metal_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif

#include "pffdtd/mdspan.hpp"
#include "pffdtd/precision.hpp"
#include "pffdtd/simulation_2d.hpp"

namespace pffdtd {
Expand Down
4 changes: 3 additions & 1 deletion src/cpp/pffdtd/engine_metal_2d.mm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ auto run(Simulation2D const& sim) {

} // namespace

auto EngineMETAL2D::operator()(Simulation2D const& sim) const -> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
auto EngineMETAL2D::operator()(Simulation2D const& sim, Precision precision) const
-> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
PFFDTD_ASSERT(precision == Precision::Float);
return run(sim);
}

Expand Down
21 changes: 16 additions & 5 deletions src/cpp/pffdtd/engine_sycl_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "pffdtd/assert.hpp"
#include "pffdtd/double.hpp"
#include "pffdtd/exception.hpp"
#include "pffdtd/progress.hpp"
#include "pffdtd/sycl.hpp"
#include "pffdtd/time.hpp"
Expand All @@ -19,11 +20,8 @@ namespace {

[[nodiscard]] constexpr auto to_ixy(auto x, auto y, auto /*Nx*/, auto Ny) { return x * Ny + y; }

} // namespace

auto EngineSYCL2D::operator()(Simulation2D const& sim) const -> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
using Real = double;

template<typename Real>
auto run(Simulation2D const& sim) {
summary(sim);

auto const Nx = sim.Nx;
Expand Down Expand Up @@ -191,4 +189,17 @@ auto EngineSYCL2D::operator()(Simulation2D const& sim) const -> stdex::mdarray<d

return outputs;
}

} // namespace

auto EngineSYCL2D::operator()(Simulation2D const& sim, Precision precision) const
-> stdex::mdarray<double, stdex::dextents<size_t, 2>> {
if (precision == Precision::Float) {
return run<float>(sim);
} else if (precision == Precision::Double) {
return run<double>(sim);
} else {
raisef<std::invalid_argument>("invalid precision {}", static_cast<int>(precision));
}
}
} // namespace pffdtd
4 changes: 3 additions & 1 deletion src/cpp/pffdtd/engine_sycl_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
#pragma once

#include "pffdtd/mdspan.hpp"
#include "pffdtd/precision.hpp"
#include "pffdtd/simulation_2d.hpp"

#include <cstddef>

namespace pffdtd {

struct EngineSYCL2D {
[[nodiscard]] auto operator()(Simulation2D const& sim) const -> stdex::mdarray<double, stdex::dextents<size_t, 2>>;
[[nodiscard]] auto operator()(Simulation2D const& sim, Precision precision) const
-> stdex::mdarray<double, stdex::dextents<size_t, 2>>;
};

} // namespace pffdtd
13 changes: 13 additions & 0 deletions src/cpp/pffdtd/precision.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Tobias Hienzsch

#pragma once

namespace pffdtd {

enum struct Precision {
Float,
Double,
};

} // namespace pffdtd
2 changes: 1 addition & 1 deletion src/python/test/test_sim2d_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_sim2d_engines(tmp_path):
assert exe.is_file()

result = subprocess.run(
args=[str(exe), 'sim2d', '-s', str(tmp_path), '-o', 'out-cpp.h5'],
args=[str(exe), 'sim2d', '-p', '64', '-s', str(tmp_path), '-o', 'out-cpp.h5'],
capture_output=True,
text=True,
check=True,
Expand Down

0 comments on commit a6ef269

Please sign in to comment.