MCPcopy Create free account
hub / github.com/AnswerDotAI/gpu.cpp / show

Function show

utils/array_utils.hpp:44–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42 */
43template <typename numtype>
44std::string show(const numtype *a, size_t rows, size_t cols,
45 const std::string &name = "") {
46 std::string output = "\n";
47 if (name != "") {
48 output += "\n" + name + " (" + std::to_string(rows) + ", " +
49 std::to_string(cols) + ")\n\n";
50 } else {
51 output +=
52 "\n(" + std::to_string(rows) + ", " + std::to_string(cols) + ")\n\n";
53 }
54 // spacing as log10 of max value
55 int spacing = 1;
56 if constexpr (std::is_same<numtype, int>::value) {
57 int max = *std::max_element(a, a + rows * cols);
58 spacing = std::max(0, (int)log10(max + .01)) + 2;
59 } else if constexpr (std::is_same<numtype, float>::value) {
60 // spacing = std::max(0, (int)log10(max + .01)) + 1;
61 spacing = 8; // scientific notation
62 } else if constexpr (std::is_same<numtype, half>::value) {
63 spacing = 8;
64 } else {
65 throw std::runtime_error("Unsupported number type for show()");
66 }
67 // print to stdout line break for each row
68 for (size_t i = 0; i < rows; i++) {
69 if (i == kShowMaxRows / 2 && rows > kShowMaxRows) {
70 output += "...\n";
71 i = rows - kShowMaxRows / 2;
72 }
73 for (size_t j = 0; j < cols; j++) {
74 if (j == kShowMaxCols / 2 && cols > kShowMaxCols) {
75 output += " ..";
76 j = cols - kShowMaxCols / 2;
77 }
78 char buffer[50];
79 if constexpr (std::is_same<numtype, int>::value) {
80 snprintf(buffer, spacing, "%*d", spacing, a[i * cols + j]);
81 } else if constexpr (std::is_same<numtype, float>::value) {
82 if (std::abs(a[i * cols + j]) < 1000 &&
83 std::abs(a[i * cols + j]) > 0.01 ||
84 a[i * cols + j] == 0.0) {
85 snprintf(buffer, 16, "%9.2f", a[i * cols + j]);
86 } else
87 snprintf(buffer, 16, "%10.2e", a[i * cols + j]);
88 } else if constexpr (std::is_same<numtype, half>::value) {
89 float tmp = halfToFloat(a[i * cols + j]);
90 if (std::abs(tmp) < 1000 &&
91 std::abs(tmp) > 0.01 ||
92 tmp == 0.0) {
93 snprintf(buffer, 16, "%9.2f", tmp);
94 } else
95 snprintf(buffer, 16, "%10.2e", tmp);
96 } else {
97 throw std::runtime_error("Unsupported number type for show()");
98 }
99 output += buffer;
100 }
101 output += "\n";

Callers

nothing calls this directly

Calls 1

halfToFloatFunction · 0.85

Tested by

no test coverage detected