From d37b549025459d41c1a0a9aab2a61754e8577110 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 30 Jan 2024 00:52:48 -0500 Subject: [PATCH] trying to remove allocations --- include/extern.h | 20 ++++++- src/extern.cpp | 137 ++++++++++++++++++++++++++++++++++++++++++++++ src/functions.cpp | 2 + src/main.cpp | 4 +- 4 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/extern.cpp diff --git a/include/extern.h b/include/extern.h index 06f658a..2381b64 100644 --- a/include/extern.h +++ b/include/extern.h @@ -20,6 +20,7 @@ #define GP_IMAGE_TEST_EXTERN_H #include +#include struct shapiro_wilk_results { @@ -32,6 +33,8 @@ struct shapiro_wilk_results }; extern "C" shapiro_wilk_results willbert(const double* data, blt::size_t len); +extern "C" shapiro_wilk_results willbert_unsorted(double* data, blt::size_t len); +extern "C" shapiro_wilk_results willbert_unsorted_copy(const double* data, blt::size_t len); extern "C" void test(); @@ -42,13 +45,22 @@ class willbruh public: explicit willbruh(const std::vector& data): results(willbert(data.data(), data.size())) {} + explicit willbruh(const double* data, blt::size_t size): results(willbert(data, size)) {} + /** + * Takes ownership of the results, will free when out of scope. + */ + explicit willbruh(shapiro_wilk_results results): results(results) + {} + willbruh(const willbruh& copy) = delete; + willbruh(const willbruh&& move) = delete; willbruh& operator=(const willbruh& copy) = delete; + willbruh& operator=(const willbruh&& move) = delete; shapiro_wilk_results& get() @@ -56,7 +68,8 @@ class willbruh return results; } - shapiro_wilk_results* operator->(){ + shapiro_wilk_results* operator->() + { return &results; } @@ -66,4 +79,9 @@ class willbruh } }; +void shapiro_test_local(); +void shapiro_test_unsorted(); +void shapiro_test_copy(); +void shapiro_test_run(); + #endif //GP_IMAGE_TEST_EXTERN_H diff --git a/src/extern.cpp b/src/extern.cpp new file mode 100644 index 0000000..b9b9387 --- /dev/null +++ b/src/extern.cpp @@ -0,0 +1,137 @@ +/* + * Created by Brett on 30/01/24. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ +#include "blt/std/assert.h" +#include "blt/std/logging.h" +#include "blt/profiling/profiler_v2.h" +#include +#include +#include + +const std::vector large_c { + 0.139E100, 0.157E100, 0.175E100, 0.256E100, 0.344E100, 0.413E100, 0.503E100, 0.577E100, + 0.614E100, 0.655E100, 0.954E100, 1.392E100, 1.557E100, 1.648E100, 1.690E100, 1.994E100, + 2.174E100, 2.206E100, 3.245E100, 3.510E100, 3.571E100, 4.354E100, 4.980E100, 6.084E100, + 8.351E100}; + +const std::vector normally_c {-0.4417998157703872, + -0.310841224215176, + -0.2544758389229413, + -0.21509882047762266, + -0.18254731741828356, + -0.1541570107663562, + -0.12852819470327187, + -0.1048199468783084, + -0.08247628697708857, + -0.061100278634889656, + -0.040388574421146996, + -0.020093702456713224, + 0.0, + 0.020093702456713224, + 0.040388574421146996, + 0.061100278634889656, + 0.08247628697708857, + 0.1048199468783084, + 0.12852819470327187, + 0.1541570107663562, + 0.18254731741828356, + 0.21509882047762266, + 0.2544758389229413, + 0.310841224215176, + 0.4417998157703872}; + +const std::vector nearly_c { + -0.44, -0.31, -0.25, -0.21, -0.18, -0.15, -0.12, -0.10, -0.08, -0.06, -0.04, -0.02, + 0.0, 0.02, 0.04, 0.06, 0.08, 0.10, 0.12, 0.15, 0.18, 0.21, 0.25, 0.31, 0.44}; + +#undef BLT_DEBUG +#define BLT_DEBUG(...) +void shapiro_test_local() +{ + auto normally = normally_c; + auto nearly = nearly_c; + auto large = large_c; + + std::sort(normally.begin(), normally.end()); + std::sort(nearly.begin(), nearly.end()); + std::sort(large.begin(), large.end()); + + willbruh w1(normally); + BLT_ASSERT(w1->estimate == 0.9999999999999999); + BLT_ASSERT(w1->p_value == 1.0); + BLT_DEBUG("%f // %f", w1->estimate, w1->p_value); + + willbruh w2(nearly); + BLT_ASSERT(w2->estimate == 0.9997987717271388); + BLT_ASSERT(w2->p_value == 1.0); + BLT_DEBUG("%f // %f", w2->estimate, w2->p_value); + + willbruh w3(large); + BLT_ASSERT(w3->estimate == 0.8346662753181684); + BLT_ASSERT(w3->p_value == 0.0009134904817755807); + BLT_DEBUG("%f // %f", w3->estimate, w3->p_value); +} + +void shapiro_test_unsorted() +{ + auto normally = normally_c; + auto nearly = nearly_c; + auto large = large_c; + + willbruh w1(willbert_unsorted(normally.data(), normally.size())); + BLT_ASSERT(w1->estimate == 0.9999999999999999); + BLT_ASSERT(w1->p_value == 1.0); + BLT_DEBUG("%f // %f", w1->estimate, w1->p_value); + + willbruh w2(willbert_unsorted(nearly.data(), nearly.size())); + BLT_ASSERT(w2->estimate == 0.9997987717271388); + BLT_ASSERT(w2->p_value == 1.0); + BLT_DEBUG("%f // %f", w2->estimate, w2->p_value); + + willbruh w3(willbert_unsorted(large.data(), large.size())); + BLT_ASSERT(w3->estimate == 0.8346662753181684); + BLT_ASSERT(w3->p_value == 0.0009134904817755807); + BLT_DEBUG("%f // %f", w3->estimate, w3->p_value); +} + +void shapiro_test_copy() +{ + auto normally = normally_c; + auto nearly = nearly_c; + auto large = large_c; + + willbruh w1(willbert_unsorted_copy(normally.data(), normally.size())); + BLT_ASSERT(w1->estimate == 0.9999999999999999); + BLT_ASSERT(w1->p_value == 1.0); + BLT_DEBUG("%f // %f", w1->estimate, w1->p_value); + + willbruh w2(willbert_unsorted_copy(nearly.data(), nearly.size())); + BLT_ASSERT(w2->estimate == 0.9997987717271388); + BLT_ASSERT(w2->p_value == 1.0); + BLT_DEBUG("%f // %f", w2->estimate, w2->p_value); + + willbruh w3(willbert_unsorted_copy(large.data(), large.size())); + BLT_ASSERT(w3->estimate == 0.8346662753181684); + BLT_ASSERT(w3->p_value == 0.0009134904817755807); + BLT_DEBUG("%f // %f", w3->estimate, w3->p_value); +} + +void shapiro_test_run() +{ + const auto runs = 500000; + BLT_START_INTERVAL("shapiro", "copy"); + for (size_t i = 0; i < runs; i++) + shapiro_test_copy(); + BLT_END_INTERVAL("shapiro", "copy"); + BLT_START_INTERVAL("shapiro", "unsorted_mut"); + for (size_t i = 0; i < runs; i++) + shapiro_test_unsorted(); + BLT_END_INTERVAL("shapiro", "unsorted_mut"); + BLT_START_INTERVAL("shapiro", "local_sorted"); + for (size_t i = 0; i < runs; i++) + shapiro_test_local(); + BLT_END_INTERVAL("shapiro", "local_sorted"); + BLT_PRINT_PROFILE("shapiro"); +} diff --git a/src/functions.cpp b/src/functions.cpp index a82ee85..385e2fc 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -261,6 +261,8 @@ float eval_DNF_SW(const image& img) for (const auto& v : img.getData()) order.push_back(v.magnitude()); + std::sort(order.begin(), order.end()); + blt::size_t len = 5000; blt::size_t current_pos = 0; double total = 0; diff --git a/src/main.cpp b/src/main.cpp index 3aa0863..60eff60 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "blt/gfx/renderer/batch_2d_renderer.h" #include "blt/std/assert.h" #include "imgui.h" +#include "extern.h" #include #include #include @@ -113,7 +114,7 @@ class tree static crossover_result_t crossover(tree* p1, tree* p2) { - + return {}; } void evaluate() @@ -215,6 +216,7 @@ void update(std::int32_t w, std::int32_t h) int main() { + shapiro_test_run(); blt::gfx::init(blt::gfx::window_data{"Window of GP test", init, update}.setSyncInterval(1)); global_matrices.cleanup(); resources.cleanup();