Skip to content

Commit

Permalink
fixed windows random silent crash on pbar
Browse files Browse the repository at this point in the history
  • Loading branch information
samayala22 committed Sep 19, 2024
1 parent d9cd23e commit 03d7e4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
44 changes: 22 additions & 22 deletions headeronly/tinypbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
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
Expand All @@ -66,37 +65,38 @@ class pbar {
std::snprintf(out, 8, "0.00");
return;
}
const int g = static_cast<int>(std::log10(x) / 3);
const int g = (x==0) ? 0 : static_cast<int>(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<size_t>((end_ - start_ + step_ - 1) / step_);
start_time = std::chrono::high_resolution_clock::now();
total = (end_ - start_ + step_ - 1) / step_;
update(0);
}

~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<int>(i - start_) % skip != 0)) return;

auto now = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration<double>(now - start_time).count();
auto now = std::chrono::high_resolution_clock::now();
double elapsed = std::chrono::duration<double>(now - start_time).count(); // cast to seconds
elapsed += 1e-6; // add 1 microsecond to avoid rounding errors
double progress = static_cast<double>(n) / total;

if (n / elapsed > rate && n != start_) {
skip = static_cast<long long>(n / elapsed / rate);
if (n / elapsed > rate && n != 0) {
skip = static_cast<int>(n / elapsed / rate);
}

const int terminal_width = get_terminal_width();
Expand All @@ -116,8 +116,8 @@ class pbar {
HMS(time_remaining, hms_time_remaining);
SI(static_cast<double>(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<size_t>(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");
Expand All @@ -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);
Expand All @@ -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_); }
Expand Down
23 changes: 1 addition & 22 deletions vlm/src/vlm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -357,7 +342,7 @@ void UVLM::run(const std::vector<Kinematics>& kinematics, const std::vector<lina

// 4. Transient simulation loop
// for (u32 i = 0; i < vec_t.size()-1; i++) {
for (const i32 i : tiny::pbar<i32>(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;

Expand Down Expand Up @@ -388,12 +373,6 @@ void UVLM::run(const std::vector<Kinematics>& kinematics, const std::vector<lina
velocities.to_device();
backend->memory->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());
Expand Down

0 comments on commit 03d7e4e

Please sign in to comment.