diff --git a/julia/tests/cpp_bench/BTCS_100_100_1000.cpp b/julia/tests/cpp_bench/BTCS_100_100_1000.cpp index 1af59b6..22cbc53 100644 --- a/julia/tests/cpp_bench/BTCS_100_100_1000.cpp +++ b/julia/tests/cpp_bench/BTCS_100_100_1000.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -35,5 +38,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/cpp_bench/BTCS_1024_1000_100.cpp b/julia/tests/cpp_bench/BTCS_1024_1000_100.cpp index bd5818a..3c68535 100644 --- a/julia/tests/cpp_bench/BTCS_1024_1000_100.cpp +++ b/julia/tests/cpp_bench/BTCS_1024_1000_100.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -41,5 +44,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/cpp_bench/BTCS_1_20_100.cpp b/julia/tests/cpp_bench/BTCS_1_20_100.cpp index a857fc8..33d1a07 100644 --- a/julia/tests/cpp_bench/BTCS_1_20_100.cpp +++ b/julia/tests/cpp_bench/BTCS_1_20_100.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -30,5 +33,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/cpp_bench/BTCS_1_45_75000.cpp b/julia/tests/cpp_bench/BTCS_1_45_75000.cpp index 209e142..1657299 100644 --- a/julia/tests/cpp_bench/BTCS_1_45_75000.cpp +++ b/julia/tests/cpp_bench/BTCS_1_45_75000.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -32,5 +35,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/cpp_bench/BTCS_2027_1999_200.cpp b/julia/tests/cpp_bench/BTCS_2027_1999_200.cpp new file mode 100644 index 0000000..5c20ad1 --- /dev/null +++ b/julia/tests/cpp_bench/BTCS_2027_1999_200.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +using namespace Eigen; +using namespace tug; +using namespace std::chrono; + +int main(int argc, char *argv[]) +{ + // **** GRID **** + int rows = 2027; + int cols = 1999; + Grid64 grid(rows, cols); + + MatrixXd concentrations(rows, cols); + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + concentrations(i, j) = static_cast(i * j) / 1e6; + } + } + concentrations(10, 10) = 15000; + concentrations(2020, 1994) = 7500; + concentrations(10, 1994) = 7500; + concentrations(2020, 10) = 7500; + grid.setConcentrations(concentrations); + + // Complex alpha patterns + MatrixXd alphax = MatrixXd(rows, cols); + MatrixXd alphay = MatrixXd(rows, cols); + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + alphax(i, j) = std::sin(i / 100.0) * std::cos(j / 100.0); + alphay(i, j) = std::cos(i / 100.0) * std::sin(j / 100.0); + } + } + grid.setAlpha(alphax, alphay); + + // **** BOUNDARY **** + Boundary bc = Boundary(grid); + bc.setBoundarySideConstant(BC_SIDE_LEFT, 1.5); + bc.setBoundarySideConstant(BC_SIDE_RIGHT, 1.5); + bc.setBoundarySideConstant(BC_SIDE_TOP, 0.75); + bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0.75); + + // **** SIMULATION **** + Simulation simulation = Simulation(grid, bc); + simulation.setTimestep(0.005); + simulation.setIterations(200); + simulation.setOutputCSV(CSV_OUTPUT_ON); + simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); + + // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); + simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; +} diff --git a/julia/tests/cpp_bench/BTCS_20_20_500.cpp b/julia/tests/cpp_bench/BTCS_20_20_500.cpp index 8038c4d..9df10b8 100644 --- a/julia/tests/cpp_bench/BTCS_20_20_500.cpp +++ b/julia/tests/cpp_bench/BTCS_20_20_500.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -34,5 +37,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/cpp_bench/BTCS_450_670_750.cpp b/julia/tests/cpp_bench/BTCS_450_670_750.cpp index a94b0a0..2ffb814 100644 --- a/julia/tests/cpp_bench/BTCS_450_670_750.cpp +++ b/julia/tests/cpp_bench/BTCS_450_670_750.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include using namespace Eigen; using namespace tug; +using namespace std::chrono; int main(int argc, char *argv[]) { @@ -42,5 +45,10 @@ int main(int argc, char *argv[]) simulation.setOutputConsole(CONSOLE_OUTPUT_OFF); // **** RUN SIMULATION **** + auto start = high_resolution_clock::now(); simulation.run(); + auto stop = high_resolution_clock::now(); + + auto duration = duration_cast(stop - start); + std::cout << duration.count() << std::endl; } diff --git a/julia/tests/julia_bench/BTCS_100_100_1000.jl b/julia/tests/julia_bench/BTCS_100_100_1000.jl index c9e792d..0023be4 100644 --- a/julia/tests/julia_bench/BTCS_100_100_1000.jl +++ b/julia/tests/julia_bench/BTCS_100_100_1000.jl @@ -29,7 +29,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/julia_bench/BTCS_1024_1000_100.jl b/julia/tests/julia_bench/BTCS_1024_1000_100.jl index bbb867b..1eede76 100644 --- a/julia/tests/julia_bench/BTCS_1024_1000_100.jl +++ b/julia/tests/julia_bench/BTCS_1024_1000_100.jl @@ -35,7 +35,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/julia_bench/BTCS_1_20_100.jl b/julia/tests/julia_bench/BTCS_1_20_100.jl index fb8690f..b79f75b 100644 --- a/julia/tests/julia_bench/BTCS_1_20_100.jl +++ b/julia/tests/julia_bench/BTCS_1_20_100.jl @@ -24,7 +24,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/julia_bench/BTCS_1_45_75000.jl b/julia/tests/julia_bench/BTCS_1_45_75000.jl index f3ad0f8..542ca02 100644 --- a/julia/tests/julia_bench/BTCS_1_45_75000.jl +++ b/julia/tests/julia_bench/BTCS_1_45_75000.jl @@ -26,7 +26,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/julia_bench/BTCS_2027_1999_200.jl b/julia/tests/julia_bench/BTCS_2027_1999_200.jl new file mode 100644 index 0000000..7089130 --- /dev/null +++ b/julia/tests/julia_bench/BTCS_2027_1999_200.jl @@ -0,0 +1,38 @@ +include("../../tug/Simulation.jl") + +function main() + # **** GRID **** + rows::Int = 2027 + cols::Int = 1999 + + alphaX = [sin(i / 100) * cos(j / 100) for i in 1:rows, j in 1:cols] + alphaY = [cos(i / 100) * sin(j / 100) for i in 1:rows, j in 1:cols] + + grid::Grid = Grid{Float64}(rows, cols, alphaX, alphaY) + + concentrations = [i * j / 1e6 for i in 1:rows, j in 1:cols] + concentrations[11, 11] = 15000 + concentrations[2021, 1995] = 7500 + concentrations[11, 1995] = 7500 + concentrations[2021, 11] = 7500 + setConcentrations!(grid, concentrations) + + # **** BOUNDARY **** + bc::Boundary = Boundary(grid) + setBoundarySideClosed!(bc, LEFT) + setBoundarySideConstant!(bc, RIGHT, 1.5) + setBoundarySideClosed!(bc, TOP) + setBoundarySideConstant!(bc, BOTTOM, 0.75) + + # **** SIMULATION **** + simulation::Simulation = Simulation(grid, bc) + simulation = setTimestep(simulation, 0.005) + simulation = setIterations(simulation, 200) + simulation = setOutputConsole(simulation, CONSOLE_OUTPUT_OFF) + simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) + + # **** RUN SIMULATION **** + print((@elapsed run(simulation)) * 1e9) +end + +main() diff --git a/julia/tests/julia_bench/BTCS_20_20_500.jl b/julia/tests/julia_bench/BTCS_20_20_500.jl index 333f1e7..3d5c19a 100644 --- a/julia/tests/julia_bench/BTCS_20_20_500.jl +++ b/julia/tests/julia_bench/BTCS_20_20_500.jl @@ -28,7 +28,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/julia_bench/BTCS_450_670_750.jl b/julia/tests/julia_bench/BTCS_450_670_750.jl index b496624..948ee16 100644 --- a/julia/tests/julia_bench/BTCS_450_670_750.jl +++ b/julia/tests/julia_bench/BTCS_450_670_750.jl @@ -36,7 +36,7 @@ function main() simulation = setOutputCSV(simulation, CSV_OUTPUT_ON) # **** RUN SIMULATION **** - run(simulation) + print((@elapsed run(simulation)) * 1e9) end main() diff --git a/julia/tests/test.py b/julia/tests/test.py index 9ca6a35..3b3dfed 100644 --- a/julia/tests/test.py +++ b/julia/tests/test.py @@ -33,19 +33,22 @@ def format_difference(diff): threshold = 1e-5 if diff != 0: if abs(diff) < threshold: - return '{:.2e}'.format(diff).rjust(9) # Scientific notation for small values + return '{:.2e}'.format(diff).rjust(6) # Scientific notation for small values else: - return '{:.5f}'.format(diff).rjust(9) # Fixed-point notation for larger values + return '{:.3f}'.format(diff).rjust(6) # Fixed-point notation for larger values else: - return '0'.rjust(9) + return '0'.rjust(6) -def run_benchmark(command, runs): +def run_benchmark(command, runs, precompile=False): times = [] for _ in range(runs): start_time = time.perf_counter() - subprocess.run(command) + output = subprocess.run(command, capture_output=True, text=True) elapsed = time.perf_counter() - start_time - times.append(elapsed) + if precompile: + times.append(float(output.stdout)*1e-9) # Convert from ns to s + else: + times.append(elapsed) avg_time = sum(times) / len(times) min_time = min(times) @@ -54,7 +57,7 @@ def run_benchmark(command, runs): return avg_time, min_time, max_time, std_dev -def main(tolerance, runs, silent, no_clean): +def main(tolerance, runs, silent, no_clean, precompile): BENCHMARK_DIR = "./cpp_bench" JULIA_DIR = "./julia_bench" COMPILER = "g++" @@ -80,7 +83,7 @@ def main(tolerance, runs, silent, no_clean): if not silent: print(f"Compiling {name}...", end="", flush=True) subprocess.run([COMPILER, *CFLAGS, "-o", f"{BIN_DIR}/{name}", f"{BENCHMARK_DIR}/{benchmark}"]) if not silent: print(" Running...", end="", flush=True) - cpp_times[name] = run_benchmark([f"./{BIN_DIR}/{name}"], runs) + cpp_times[name] = run_benchmark([f"./{BIN_DIR}/{name}"], runs, precompile) if not silent: print(" Done.", flush=True) # Move CSV files to output directory @@ -100,7 +103,7 @@ def main(tolerance, runs, silent, no_clean): padded_name = name.ljust(max_name_length) if os.path.exists(f"{JULIA_DIR}/{name}.jl"): if not silent: print(f"Running {name}...", end="", flush=True) - julia_times[name] = run_benchmark(["julia", f"{JULIA_DIR}/{name}.jl"], runs) + julia_times[name] = run_benchmark(["julia", f"{JULIA_DIR}/{name}.jl"], runs, precompile) if os.path.exists(f"./{name}.csv"): are_equal, _, _, _, max_diff = compare_csv_files(f"./{name}.csv", f"{OUTPUT_DIR}/{csv_file}", tolerance) @@ -145,5 +148,6 @@ if __name__ == "__main__": parser.add_argument('--runs', type=int, default=1, help='Number of benchmark runs') parser.add_argument('--silent', action='store_true', help='Run in silent mode without printing details') parser.add_argument('--no-clean', action='store_true', help='Do not clean up temporary files') + parser.add_argument('--precompile', action='store_true', help='Use precompiling for Julia benchmarks and rely on benchmark script for timing') args = parser.parse_args() - main(args.tolerance, args.runs, args.silent, args.no_clean) + main(args.tolerance, args.runs, args.silent, args.no_clean, args.precompile)