Skip to content

Commit bc91b5e

Browse files
committed
move tui code to tui.h
1 parent 63d6ea6 commit bc91b5e

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ endif()
7272
# Build the library target (libgpu)
7373

7474
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
75-
set(SRC_LIB gpu.h utils/shaders.h utils/array_utils.h utils/logging.h)
75+
set(SRC_LIB gpu.h utils/shaders.h utils/array_utils.h utils/logging.h utils/tui.h)
7676
add_library(gpu SHARED ${SRC_LIB})
7777
set_target_properties(gpu PROPERTIES LINKER_LANGUAGE CXX)
7878

examples/physics/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ FASTBUILD_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=ON
77
DEBUG_FLAGS = $(FLAGS) -DDEBUG:BOOL=ON
88

99
run:
10-
mkdir -p build && cd build && cmake .. $(FASTBUILD_FLAGS) && make -j$(NUM_JOBS) $(TARGET) && ./$(TARGET)
10+
mkdir -p build && cd build && cmake .. $(FLAGS) && make -j$(NUM_JOBS) $(TARGET) && ./$(TARGET)
1111

1212
debug :
1313
mkdir -p build && cd build && cmake .. $(DEBUG_FLAGS) && make -j$(NUM_JOBS) $(TARGET) && ./$(TARGET)

examples/physics/run.cpp

+4-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#include "gpu.h"
21
#include <array>
32
#include <chrono>
43
#include <cstdio>
54
#include <future>
65

6+
#include "gpu.h"
7+
#include "utils/tui.h" // rasterize
8+
79
using namespace gpu; // CreateContext, CreateTensor, CreateKernel,
810
// CreateShader, DispatchKernel, Wait, ToCPU
911
// Tensor, TensorList Kernel, Context, Shape, kf32
@@ -47,36 +49,6 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
4749
}
4850
)";
4951

50-
void rasterize(float *pos, size_t n, float maxX, float maxY, std::string &screen,
51-
size_t screenWidth, size_t screenHeight) {
52-
static const char intensity[] = " .`'^-+=*x17X$8#%@";
53-
const size_t eps = 1;
54-
// iterate over screen
55-
for (size_t i = 0; i < screenHeight; ++i) {
56-
for (size_t j = 0; j < screenWidth - 2; ++j) {
57-
int count = 0;
58-
for (size_t k = 0; k < 2 * n; k += 2) {
59-
float nx =
60-
(1.0 + pos[k] / maxX) / 2.0 * static_cast<float>(screenWidth);
61-
// negate y since it extends from top to bottom
62-
float ny = (1.0 - (pos[k + 1] / maxY)) / 2.0 *
63-
static_cast<float>(screenHeight);
64-
// printf("x: %.2f, y: %.2f\n", nx, ny);
65-
float length = std::sqrt((nx - j) * (nx - j) + (ny - i) * (ny - i));
66-
if (length < eps) {
67-
count++;
68-
}
69-
}
70-
count = std::min(count / 2, 17); // Need to adjust this for N
71-
screen[i * screenWidth + j] = intensity[count];
72-
}
73-
screen[i * screenWidth + screenWidth - 1] = '\n';
74-
}
75-
// clear screen
76-
printf("\033[2J\033[1;1H");
77-
printf("# simulations: %d\n%s", n / 2, screen.c_str());
78-
}
79-
8052
int main() {
8153
Context ctx = CreateContext();
8254

@@ -123,6 +95,7 @@ int main() {
12395
std::chrono::duration<double> elapsed = end - start;
12496
// N * 2 because there's two objects per pendulum
12597
rasterize(posArr.data(), N * 2, 2.0, 2.0, screen, 80, 40);
98+
printf("\033[2J\033[1;1H""# simulations: %lu\n%s", N, screen.c_str());
12699
std::this_thread::sleep_for(std::chrono::milliseconds(8) - elapsed);
127100
}
128101
}

utils/tui.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef TUI_H
2+
#define TUI_H
3+
4+
#include <array>
5+
#include <cstdio>
6+
7+
// Work-in-progress - various terminal UI visualization functions
8+
9+
namespace gpu {
10+
11+
static const char kIntensity[] = "@%#8$X71x*+=-:^~'.` ";
12+
13+
void cls() { printf("\033[2J\033[H"); }
14+
15+
template <size_t NROWS, size_t NCOLS>
16+
void canvas(const std::array<char, NROWS * NCOLS> &raster) {
17+
printf("+");
18+
for (size_t col = 0; col < NCOLS; ++col) {
19+
printf("-");
20+
}
21+
printf("+\n");
22+
for (size_t row = 0; row < NROWS; ++row) {
23+
printf("|");
24+
for (size_t col = 0; col < NCOLS; ++col) {
25+
printf("%c", raster[row * NCOLS + col]);
26+
}
27+
printf("|\n");
28+
}
29+
printf("+");
30+
for (size_t col = 0; col < NCOLS; ++col) {
31+
printf("-");
32+
}
33+
printf("+\n");
34+
}
35+
36+
void rasterize(float *pos, size_t n, float maxX, float maxY, std::string &screen,
37+
size_t screenWidth, size_t screenHeight) {
38+
static const char intensity[] = " .`'^-+=*x17X$8#%@";
39+
const size_t eps = 1;
40+
// maximum number of simulations to display on the screen
41+
const size_t nShow = std::min(static_cast<int>(n), 2000);
42+
for (size_t i = 0; i < screenHeight; ++i) {
43+
for (size_t j = 0; j < screenWidth - 2; ++j) {
44+
int count = 0;
45+
for (size_t k = 0; k < 2 * nShow; k += 2) {
46+
float nx =
47+
(1.0 + pos[k] / maxX) / 2.0 * static_cast<float>(screenWidth);
48+
// negate y since it extends from top to bottom
49+
float ny = (1.0 - (pos[k + 1] / maxY)) / 2.0 *
50+
static_cast<float>(screenHeight);
51+
float length = std::sqrt((nx - j) * (nx - j) + (ny - i) * (ny - i));
52+
if (length < eps) {
53+
count++;
54+
}
55+
}
56+
count = std::min(count / 2, 17); // Need to adjust this for N
57+
screen[i * screenWidth + j] = intensity[count];
58+
}
59+
screen[i * screenWidth + screenWidth - 1] = '\n';
60+
}
61+
}
62+
63+
} // namespace gpu
64+
65+
#endif

0 commit comments

Comments
 (0)