From 0ef1d0dff512c160c2e091bf33fbbaea3aa45d36 Mon Sep 17 00:00:00 2001 From: hmacdope Date: Tue, 31 Dec 2024 16:30:23 +1100 Subject: [PATCH 1/2] angles bench --- libdistopia/test/bench.cpp | 226 +++++++++++++++++++++++++++++++++---- 1 file changed, 207 insertions(+), 19 deletions(-) diff --git a/libdistopia/test/bench.cpp b/libdistopia/test/bench.cpp index 8ba38abe..f200df2a 100644 --- a/libdistopia/test/bench.cpp +++ b/libdistopia/test/bench.cpp @@ -34,30 +34,32 @@ template class CoordinatesBench : public benchmark::Fixture { coords0 = new T[ncoords]; coords1 = new T[ncoords]; + coords2 = new T[ncoords]; + coords3 = new T[ncoords]; ref = new T[nresults]; results = new T[nresults]; idxs = new std::size_t[nindicies]; RandomFloatingPoint(coords0, ncoords, 0 - delta, boxsize + delta); RandomFloatingPoint(coords1, ncoords, 0 - delta, boxsize + delta); + RandomFloatingPoint(coords2, ncoords, 0 - delta, boxsize + delta); + RandomFloatingPoint(coords3, ncoords, 0 - delta, boxsize + delta); + box[0] = boxsize; box[1] = boxsize; box[2] = boxsize; - // triclinic box - // [30, 30, 30, 70, 110, 95] in L ,M, N alpha, beta, gamma format - // in matrix form - triclinic_box[0] = 30; + triclinic_box[0] = boxsize; triclinic_box[1] = 0; triclinic_box[2] = 0; - triclinic_box[3] = -2.6146722; - triclinic_box[4] = 29.885841; + triclinic_box[3] = 0; + triclinic_box[4] = boxsize; triclinic_box[5] = 0; - triclinic_box[6] = -10.260604; - triclinic_box[7] = 9.402112; - triclinic_box[8] = 26.576687; + triclinic_box[6] = 0; + triclinic_box[7] = 0; + triclinic_box[8] = boxsize; RandomInt(idxs, nindicies, 0, nindicies - 1); } @@ -69,6 +71,12 @@ template class CoordinatesBench : public benchmark::Fixture { if (coords1) { delete[] coords1; } + if (coords2) { + delete[] coords2; + } + if (coords3) { + delete[] coords3; + } if (ref) { delete[] ref; } @@ -90,6 +98,8 @@ template class CoordinatesBench : public benchmark::Fixture { T *coords0 = nullptr; T *coords1 = nullptr; + T *coords2 = nullptr; + T *coords3 = nullptr; T *ref = nullptr; T *results = nullptr; T box[3]; @@ -167,6 +177,76 @@ template class CoordinatesBench : public benchmark::Fixture { nresults * state.iterations(), benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } + + void BM_calc_angles_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_angle((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, nresults, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_angles(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcAnglesNoBox(coords0, coords1, coords2, nresults, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_angles_ortho(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcAnglesOrtho(coords0, coords1, coords2, nresults, box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_angles_triclinic(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcAnglesTriclinic(coords0, coords1, coords2, nresults, triclinic_box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + + void BM_calc_angles_ortho_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_angle_ortho((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, nresults, box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_angles_triclinic_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_angle_triclinic((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, nresults, triclinic_box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } }; @@ -188,21 +268,21 @@ BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsDouble, BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); -// calc_bonds_ortho +// // calc_bonds_ortho -BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsOrthoInBoxFloat, - float) -(benchmark::State &state) { BM_calc_bonds_ortho(state); } +// BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsOrthoInBoxFloat, +// float) +// (benchmark::State &state) { BM_calc_bonds_ortho(state); } -BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsOrthoInBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); +// BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsOrthoInBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); -BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsOrthoInBoxDouble, - double) -(benchmark::State &state) { BM_calc_bonds_ortho(state); } +// BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsOrthoInBoxDouble, +// double) +// (benchmark::State &state) { BM_calc_bonds_ortho(state); } -BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsOrthoInBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); +// BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsOrthoInBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsOrthoOutBoxFloat, @@ -308,5 +388,113 @@ BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsMDATriclinicOutBoxDouble, BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsMDATriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); - + + +// ANGLES + + + +// calc_angles + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesFloat, + float) +(benchmark::State &state) { BM_calc_angles(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesDouble, + double) +(benchmark::State &state) { BM_calc_angles(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDAFloat, + float) +(benchmark::State &state) { BM_calc_angles_MDA(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDAFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDADouble, + double) +(benchmark::State &state) { BM_calc_angles_MDA(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDADouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + + +// calc_angles_ortho + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesOrthoOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_angles_ortho(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesOrthoOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesOrthoOutBoxDouble, + double) +(benchmark::State &state) { BM_calc_angles_ortho(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesOrthoOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDAOrthoOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_angles_ortho_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDAOrthoOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDAOrthoOutBoxDouble, + double) + +(benchmark::State &state) { BM_calc_angles_ortho_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDAOrthoOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +// calc_angles_triclinic + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesTriclinicOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_angles_triclinic(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesTriclinicOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesTriclinicOutBoxDouble, + double) + +(benchmark::State &state) { BM_calc_angles_triclinic(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesTriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_angles_triclinic_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxDouble, + double) + +(benchmark::State &state) { BM_calc_angles_triclinic_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + + + + BENCHMARK_MAIN(); \ No newline at end of file From 176acfebdc1badc6a09aa465c753a1f8001eec41 Mon Sep 17 00:00:00 2001 From: hmacdope Date: Tue, 31 Dec 2024 16:58:54 +1100 Subject: [PATCH 2/2] add dihedrals --- libdistopia/CMakeLists.txt | 3 + libdistopia/test/bench.cpp | 163 ++++++++++++++++++++++++++++++++++++- 2 files changed, 163 insertions(+), 3 deletions(-) diff --git a/libdistopia/CMakeLists.txt b/libdistopia/CMakeLists.txt index 421753a9..a715bddd 100644 --- a/libdistopia/CMakeLists.txt +++ b/libdistopia/CMakeLists.txt @@ -29,6 +29,8 @@ else() endif() + + if(DISTOPIA_BUILD_TESTS) set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Enable googlebench tests" FORCE) @@ -41,6 +43,7 @@ if(DISTOPIA_BUILD_TESTS) target_include_directories(bench PUBLIC ${CMAKE_CURRENT_LIST_DIR}) + Include(GoogleTest) add_subdirectory("googletest") enable_testing() diff --git a/libdistopia/test/bench.cpp b/libdistopia/test/bench.cpp index f200df2a..cb2cc5ed 100644 --- a/libdistopia/test/bench.cpp +++ b/libdistopia/test/bench.cpp @@ -247,10 +247,83 @@ template class CoordinatesBench : public benchmark::Fixture { nresults * state.iterations(), benchmark::Counter::kIsRate | benchmark::Counter::kInvert); } + + + void BM_calc_dihedrals_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_dihedral((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, (ctype*)coords3, nresults, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_dihedrals(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcDihedralsNoBox(coords0, coords1, coords2, coords3, nresults, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_dihedrals_ortho(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcDihedralsOrtho(coords0, coords1, coords2, coords3, nresults, box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_dihedrals_triclinic(benchmark::State &state) { + for (auto _ : state) { + distopia::CalcDihedralsTriclinic(coords0, coords1, coords2, coords3, nresults, triclinic_box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_dihedrals_ortho_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_dihedral_ortho((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, (ctype*)coords3, nresults, box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + + void BM_calc_dihedrals_triclinic_MDA(benchmark::State &state) { + using ctype = ScalarToCoordinateT; + + for (auto _ : state) { + _calc_dihedral_triclinic((ctype*)coords0, (ctype*)coords1, + (ctype*)coords2, (ctype*)coords3, nresults, triclinic_box, results); + } + state.SetItemsProcessed(nresults * state.iterations()); + state.counters["Per Result"] = benchmark::Counter( + nresults * state.iterations(), + benchmark::Counter::kIsRate | benchmark::Counter::kInvert); + } + }; -// calc_bonds +// BONDS + +// calc_bonds_ortho BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsFloat, @@ -333,8 +406,6 @@ BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcBondsTriclinicOutBoxDouble, BENCHMARK_REGISTER_F(CoordinatesBench, CalcBondsTriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); -// MDA functions - // calc_bonds @@ -494,6 +565,92 @@ BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxDouble BENCHMARK_REGISTER_F(CoordinatesBench, CalcAnglesMDATriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); +// DIHERALS + + +// calc_dihedrals + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsDouble, + double) +(benchmark::State &state) { BM_calc_dihedrals(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDAFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDAFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDADouble, + double) +(benchmark::State &state) { BM_calc_dihedrals_MDA(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDADouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +// calc_dihedrals_ortho + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsOrthoOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals_ortho(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsOrthoOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsOrthoOutBoxDouble, + double) +(benchmark::State &state) { BM_calc_dihedrals_ortho(state); } + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDAOrthoOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals_ortho_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDAOrthoOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDAOrthoOutBoxDouble, + double) +(benchmark::State &state) { BM_calc_dihedrals_ortho_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDAOrthoOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +// calc_dihedrals_triclinic + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsTriclinicOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals_triclinic(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsTriclinicOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsTriclinicOutBoxDouble, + double) +(benchmark::State &state) { BM_calc_dihedrals_triclinic(state); } + + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsTriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDATriclinicOutBoxFloat, + float) +(benchmark::State &state) { BM_calc_dihedrals_triclinic_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDATriclinicOutBoxFloat)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}}); + + +BENCHMARK_TEMPLATE_DEFINE_F(CoordinatesBench, CalcDihedralsMDATriclinicOutBoxDouble, + double) +(benchmark::State &state) { BM_calc_dihedrals_triclinic_MDA(state); } + +BENCHMARK_REGISTER_F(CoordinatesBench, CalcDihedralsMDATriclinicOutBoxDouble)->RangeMultiplier(10)->Ranges({{10, 10000000}, {0, 0}, {0, 0}});