From 03d7e4e8cc9a48e1f311c8d6b908b82f21212048 Mon Sep 17 00:00:00 2001 From: Samuel Ayala Date: Wed, 18 Sep 2024 22:55:54 -0400 Subject: [PATCH] fixed windows random silent crash on pbar --- headeronly/tinypbar.hpp | 44 ++++++++++++++++++++--------------------- vlm/src/vlm.cpp | 23 +-------------------- 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/headeronly/tinypbar.hpp b/headeronly/tinypbar.hpp index 0415190..7c83da9 100644 --- a/headeronly/tinypbar.hpp +++ b/headeronly/tinypbar.hpp @@ -26,20 +26,19 @@ inline int get_terminal_width() { // } // #endif // Return a default value if unable to get the terminal size - return 80; + return 100; } -template class pbar { private: - T start_, end_, step_; // idx range + int start_, end_, step_; // idx range bool disable; // disable pbar double rate; // update freq in Hz std::chrono::steady_clock::time_point start_time; - T i; // current idx value - T n; // current iteration - long long skip; // Skip displaying pbar - size_t total; // total iterations + int i; // current idx value + int n; // current iteration + int skip; // Skip displaying pbar + int total; // total iterations const char* desc_; // pbar description // must have 9 bytes of space @@ -66,18 +65,18 @@ class pbar { std::snprintf(out, 8, "0.00"); return; } - const int g = static_cast(std::log10(x) / 3); + const int g = (x==0) ? 0 : static_cast(std::log10(x) / 3); const double scaled = x / std::pow(1000, g); std::snprintf(out, 8, "%.2f%c", scaled, " kMGTPEZY"[g]); } public: - pbar(T start, T end, T step = 1, const char* desc = "", - bool disable = false, double rate = 100.0) + pbar(int start, int end, int step = 1, const char* desc = "", + bool disable = false, double rate = 60.0) : start_(start), end_(end), step_(step), disable(disable), rate(rate), i(start-step), n(0), skip(1ull), desc_(desc) { - start_time = std::chrono::steady_clock::now(); - total = static_cast((end_ - start_ + step_ - 1) / step_); + start_time = std::chrono::high_resolution_clock::now(); + total = (end_ - start_ + step_ - 1) / step_; update(0); } @@ -85,18 +84,19 @@ class pbar { update(0, true); } - void update(T increment = 0, bool close = false) { + void update(int increment = 0, bool close = false) { n += increment; i += step_; if (disable || (!close && static_cast(i - start_) % skip != 0)) return; - auto now = std::chrono::steady_clock::now(); - double elapsed = std::chrono::duration(now - start_time).count(); + auto now = std::chrono::high_resolution_clock::now(); + double elapsed = std::chrono::duration(now - start_time).count(); // cast to seconds + elapsed += 1e-6; // add 1 microsecond to avoid rounding errors double progress = static_cast(n) / total; - if (n / elapsed > rate && n != start_) { - skip = static_cast(n / elapsed / rate); + if (n / elapsed > rate && n != 0) { + skip = static_cast(n / elapsed / rate); } const int terminal_width = get_terminal_width(); @@ -116,8 +116,8 @@ class pbar { HMS(time_remaining, hms_time_remaining); SI(static_cast(n) / elapsed, si_speed); - std::printf("\r%s%3.0f%% [%s] %zu/%zu [%s<%s, %s it/s]", - desc_, progress * 100.0, prog_bar, static_cast(n), total, + std::printf("\r%s%3.0f%% [%s] %d/%d [%s<%s, %s it/s]", + desc_, progress * 100.0, prog_bar, n, total, hms_time_elapsed, hms_time_remaining, si_speed); if (close) std::printf("\n"); @@ -126,9 +126,9 @@ class pbar { class iterator { private: pbar& bar; - T current; + int current; public: - iterator(pbar& t, T start) : bar(t), current(start) {} + iterator(pbar& t, int start) : bar(t), current(start) {} iterator& operator++() { current += bar.step_; bar.update(1); @@ -137,7 +137,7 @@ class pbar { bool operator!=(const iterator& other) const { return current < other.current; } - T operator*() const { return current; } + int operator*() const { return current; } }; iterator begin() { return iterator(*this, start_); } diff --git a/vlm/src/vlm.cpp b/vlm/src/vlm.cpp index 8c2d703..53237f8 100644 --- a/vlm/src/vlm.cpp +++ b/vlm/src/vlm.cpp @@ -110,21 +110,6 @@ AeroCoefficients VLM::run(const FlowData& flow) { backend->gamma_shed(gamma_wing.d_view(), gamma_wing_prev.d_view(), gamma_wake.d_view(), 0); backend->gamma_delta(gamma_wing_delta.d_view(), gamma_wing.d_view()); - // mesh.verts_wing_init.to_host(); - // mesh.verts_wing.to_host(); - // mesh.verts_wake.to_host(); - // mesh.normals.to_host(); - // mesh.colloc.to_host(); - // mesh.area.to_host(); - // lhs.to_host(); - // rhs.to_host(); - // gamma_wing.to_host(); - // gamma_wake.to_host(); - // gamma_wing_prev.to_host(); - // gamma_wing_delta.to_host(); - // local_velocities.to_host(); - // transforms.to_host(); - return AeroCoefficients{ backend->coeff_steady_cl_multi(mesh.verts_wing.d_view(), gamma_wing_delta.d_view(), flow, mesh.area.d_view()), backend->coeff_steady_cd_multi(mesh.verts_wake.d_view(), gamma_wake.d_view(), flow, mesh.area.d_view()), @@ -357,7 +342,7 @@ void UVLM::run(const std::vector& kinematics, const std::vector(0, vec_t.size()-1)) { + for (const i32 i : tiny::pbar(0, (i32)vec_t.size()-1)) { const f32 t = vec_t[i]; const f32 dt = vec_t[i+1] - t; @@ -388,12 +373,6 @@ void UVLM::run(const std::vector& kinematics, const std::vectormemory->fill_f32(MemoryLocation::Device, rhs.d_view().ptr, 0.f, rhs.d_view().size()); backend->rhs_assemble_velocities(rhs.d_view(), mesh.normals.d_view(), velocities.d_view()); - - rhs.to_host(); - gamma_wake.to_host(); - mesh.colloc.to_host(); - mesh.normals.to_host(); - mesh.verts_wake.to_host(); backend->rhs_assemble_wake_influence(rhs.d_view(), gamma_wake.d_view(), mesh.colloc.d_view(), mesh.normals.d_view(), mesh.verts_wake.d_view(), i); backend->lu_solve(lhs.d_view(), rhs.d_view(), gamma_wing.d_view()); backend->gamma_delta(gamma_wing_delta.d_view(), gamma_wing.d_view());