Skip to content

Commit

Permalink
[cpp] Merge engine mains into single binary
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Sep 21, 2024
1 parent a3e04e8 commit ef0ede0
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 147 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jobs:
build_type: "Debug"
cxx_flags: "--coverage -march=native -Og"
python_version: "3.12"
pffdtd_engine_2d: "build/src/cpp/main_2d/pffdtd_2d"
pffdtd_engine_3d: "build/src/cpp/main_3d/pffdtd_3d"
pffdtd_engine_2d: "build/src/cpp/pffdtd-engine"
pffdtd_engine_3d: "build/src/cpp/pffdtd-engine"

- name: macOS-X64
os: macos-13
Expand All @@ -48,7 +48,7 @@ jobs:
build_type: "Release"
cxx_flags: ""
python_version: "3.12"
pffdtd_engine_2d: "build/src/cpp/main_2d/pffdtd_2d"
pffdtd_engine_2d: "build/src/cpp/pffdtd-engine"
pffdtd_engine_3d: ""

- name: macOS-ARM64
Expand All @@ -59,8 +59,8 @@ jobs:
build_type: "Release"
cxx_flags: ""
python_version: "3.12"
pffdtd_engine_2d: "build/src/cpp/main_2d/pffdtd_2d"
pffdtd_engine_3d: "build/src/cpp/main_3d/pffdtd_3d"
pffdtd_engine_2d: "build/src/cpp/pffdtd-engine"
pffdtd_engine_3d: "build/src/cpp/pffdtd-engine"

- name: Windows
os: windows-latest
Expand All @@ -70,8 +70,8 @@ jobs:
build_type: "Release"
cxx_flags: ""
python_version: "3.12"
pffdtd_engine_2d: "build/src/cpp/main_2d/pffdtd_2d.exe"
pffdtd_engine_3d: "build/src/cpp/main_3d/pffdtd_3d.exe"
pffdtd_engine_2d: "build/src/cpp/pffdtd-engine.exe"
pffdtd_engine_3d: "build/src/cpp/pffdtd-engine.exe"

steps:
- name: Checkout code
Expand Down
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ if(PFFDTD_ENABLE_SYCL_ONEAPI)
endif()

add_subdirectory(src/cpp)
add_subdirectory(src/cpp/main_2d)
add_subdirectory(src/cpp/main_3d)
4 changes: 2 additions & 2 deletions run_2d.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build_dir=cmake-build-sycl

root_dir="$(cd "$(dirname "$0")" && pwd)"
python_dir="$root_dir/src/python"
engine_exe="$root_dir/$build_dir/src/cpp/main_2d/pffdtd_2d"
engine_exe="$root_dir/$build_dir/src/cpp/pffdtd-engine"

sim_name="Modes2D"
sim_dir="$root_dir/sim_data/$sim_name/cpu"
Expand All @@ -29,7 +29,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" -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 -s "$sim_dir" -e sycl
# pffdtd sim2d run --sim_dir "$sim_dir" --video

# Post-process
Expand Down
6 changes: 3 additions & 3 deletions run_3d.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
set -e

root_dir="$(cd "$(dirname "$0")" && pwd)"
engine_exe="$root_dir/build/src/cpp/main_3d/pffdtd_3d"
engine_exe="$root_dir/cmake-build-cuda/src/cpp/main_3d/pffdtd_3d"
engine_exe="$root_dir/build/src/cpp/pffdtd-engine"
engine_exe="$root_dir/cmake-build-cuda/src/cpp/pffdtd-engine"

sim_name="ProStudio"
sim_setup="${sim_name}.py"
Expand All @@ -28,7 +28,7 @@ cd "$model_dir"
pffdtd sim3d setup "$sim_setup"

# Run sim
$engine_exe "$sim_dir"
$engine_exe sim3d -s "$sim_dir"

# Post-process
pffdtd sim3d process-outputs --sim_dir="$sim_dir" --fcut_lowpass "$fmax" --order_lowpass=8 --symmetric_lowpass --fcut_lowcut "$fmin" --order_lowcut=4 --air_abs_filter="none" --save_wav --plot
Expand Down
4 changes: 4 additions & 0 deletions src/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ if((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") OR (CMAKE_CXX_COMPILER_FRONTEND_VARIA
else ()
target_compile_options(pffdtd PUBLIC -Wall -Wextra -Wno-deprecated-declarations)
endif ()


add_executable(pffdtd-engine main.cpp)
target_link_libraries(pffdtd-engine PRIVATE CLI11::CLI11 pffdtd::pffdtd)
115 changes: 115 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Tobias Hienzsch

#include "pffdtd/engine_2d_native.hpp"
#include "pffdtd/exception.hpp"
#include "pffdtd/hdf.hpp"
#include "pffdtd/simulation_2d.hpp"
#include "pffdtd/simulation_3d.hpp"
#include "pffdtd/time.hpp"
#include "pffdtd/utility.hpp"

#if PFFDTD_HAS_CUDA
#include "pffdtd/engine_cuda.hpp"
#else
#include "pffdtd/engine_openmp.hpp"
#endif

#if defined(PFFDTD_HAS_SYCL)
#include "pffdtd/engine_2d_sycl.hpp"
#endif

#include <CLI/CLI.hpp>
#include <fmt/format.h>

#include <chrono>
#include <filesystem>
#include <stdexcept>
#include <string>

namespace {

[[nodiscard]] auto getEngines() {
using pffdtd::Simulation2D;
using Callback = std::function<stdex::mdarray<double, stdex::dextents<size_t, 2>>(Simulation2D const&)>;
auto engines = std::map<std::string, Callback>{};
engines["native"] = pffdtd::EngineNative{};
#if defined(PFFDTD_HAS_SYCL)
engines["sycl"] = pffdtd::EngineSYCL{};
#endif
return engines;
}

[[nodiscard]] auto toLower(std::string str) -> std::string {
std::ranges::transform(str, str.begin(), [](auto ch) { return static_cast<char>(std::tolower(ch)); });
return str;
}

struct Arguments {
struct Sim2D {
std::string engine{"native"};
std::string simDir;
std::string out{"out.h5"};
};

struct Sim3D {
std::string simDir;
};

Sim2D sim2d;
Sim3D sim3d;
};
} // namespace

auto main(int argc, char** argv) -> int {
auto app = CLI::App{"pffdtd-2d"};
auto args = Arguments{};

auto* sim2d = app.add_subcommand("sim2d", "Run 2D simulation");
sim2d->add_option("-e,--engine", args.sim2d.engine)->transform(toLower);
sim2d->add_option("-s,--sim_dir", args.sim2d.simDir)->check(CLI::ExistingDirectory)->required();
sim2d->add_option("-o,--out", args.sim2d.out);

auto* sim3d = app.add_subcommand("sim3d", "Run 3D simulation");
sim3d->add_option("-s,--sim_dir", args.sim3d.simDir)->check(CLI::ExistingDirectory)->required();

CLI11_PARSE(app, argc, argv);

if (*sim2d) {
fmt::println("Using engine: {}", args.sim2d.engine);
auto const engines = getEngines();
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 results = pffdtd::H5FWriter{simDir / args.sim2d.out};
results.write("out", out);

auto const stop = pffdtd::getTime();
auto const sec = pffdtd::Seconds(stop - start);
fmt::println("Simulation time: {} s", sec.count());
}

if (*sim3d) {
fmt::println("Running: {} ...", args.sim3d.simDir);
auto const simDir = std::filesystem::path{args.sim3d.simDir};
auto const start = pffdtd::getTime();

auto sim = pffdtd::loadSimulation3D(simDir);
pffdtd::scaleInput(sim);
pffdtd::run(sim);
pffdtd::rescaleOutput(sim);
pffdtd::writeOutputs(sim, simDir);
pffdtd::printLastSample(sim);
pffdtd::freeSimulation3D(sim);

auto const stop = pffdtd::getTime();
auto const sec = pffdtd::Seconds(stop - start);
fmt::println("--Simulation time: {} s", sec.count());
}

return EXIT_SUCCESS;
}
7 changes: 0 additions & 7 deletions src/cpp/main_2d/CMakeLists.txt

This file was deleted.

72 changes: 0 additions & 72 deletions src/cpp/main_2d/main.cpp

This file was deleted.

7 changes: 0 additions & 7 deletions src/cpp/main_3d/CMakeLists.txt

This file was deleted.

45 changes: 0 additions & 45 deletions src/cpp/main_3d/main.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/python/pffdtd/sim3d/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run_engine(sim_dir, engine):
assert exe.is_file()

result = subprocess.run(
args=[str(exe), sim_dir],
args=[str(exe), 'sim3d', '-s', sim_dir],
capture_output=True,
check=True,
)
Expand Down
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), '-s', str(tmp_path), '-o', 'out-cpp.h5'],
args=[str(exe), 'sim2d', '-s', str(tmp_path), '-o', 'out-cpp.h5'],
capture_output=True,
text=True,
check=True,
Expand Down

0 comments on commit ef0ede0

Please sign in to comment.