Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable precision support of print(). #521

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions include/matx/core/tensor_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "matx/kernels/utility.cuh"

static constexpr bool PRINT_ON_DEVICE = false; ///< print() uses printf on device
static unsigned int PRINT_PRECISION = 4; ///< control PrintVal()'s precision

namespace matx
{
Expand Down Expand Up @@ -509,15 +510,23 @@ namespace detail {
{
MATX_NVTX_START("", matx::MATX_NVTX_LOG_API)

using namespace std::literals::string_literals;

if constexpr (is_complex_v<T>) {
printf("%.4e%+.4ej ", static_cast<float>(val.real()),
const auto prec = std::to_string(PRINT_PRECISION);
const auto fmt_s = ("%."s + prec + "e%+." + prec + "ej ").c_str();
printf(fmt_s, static_cast<float>(val.real()),
static_cast<float>(val.imag()));
}
else if constexpr (is_matx_half_v<T> || is_half_v<T>) {
printf("%.4e ", static_cast<float>(val));
const auto prec = std::to_string(PRINT_PRECISION);
const auto fmt_s = ("%."s + prec + "e ").c_str();
printf(fmt_s, static_cast<float>(val));
}
else if constexpr (std::is_floating_point_v<T>) {
printf("%.4e ", val);
const auto prec = std::to_string(PRINT_PRECISION);
const auto fmt_s = ("%."s + prec + "e ").c_str();
printf(fmt_s, val);
}
else if constexpr (std::is_same_v<T, long long int>) {
printf("%lld ", val);
Expand Down Expand Up @@ -906,4 +915,22 @@ auto OpToTensor(Op &&op, [[maybe_unused]] cudaStream_t stream) {
}
}

/**
* @brief Set the print() precision for floating point values
*
* @param precision Number of digits of precision for floating point output (default 4).
*/
__MATX_INLINE__ __MATX_HOST__ void set_print_precision(unsigned int precision) {
PRINT_PRECISION = precision;
}

/**
* @brief Get the print() precision for floating point values
*
* @return Number of digits of precision for floating point output (default 4).
*/
__MATX_INLINE__ __MATX_HOST__ unsigned int get_print_precision() {
return PRINT_PRECISION;
}

}