From 373ff0c5f0e70f7d24bfa860b2d986865beadf96 Mon Sep 17 00:00:00 2001 From: Folkert Nobels <nobels@strw.leidenuniv.nl> Date: Fri, 26 Oct 2018 14:48:21 +0100 Subject: [PATCH] Check if unit conversion is valid --- src/parallel_io.c | 32 +++++++++++++++++++++++++++++++- src/serial_io.c | 32 +++++++++++++++++++++++++++++++- src/single_io.c | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/parallel_io.c b/src/parallel_io.c index cf705eed57..e1a9d2c068 100644 --- a/src/parallel_io.c +++ b/src/parallel_io.c @@ -138,7 +138,37 @@ void readArray_chunk(hid_t h_data, hid_t h_plist_id, for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor; } else { float* temp_f = (float*)temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precission. */ + if (factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } } diff --git a/src/serial_io.c b/src/serial_io.c index 147a5e3628..8e63f9b72e 100644 --- a/src/serial_io.c +++ b/src/serial_io.c @@ -153,7 +153,37 @@ void readArray(hid_t grp, const struct io_props props, size_t N, for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= factor; } else { float* temp_f = temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.f; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precision. */ + if (factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } } diff --git a/src/single_io.c b/src/single_io.c index 17318d2803..d0bc850251 100644 --- a/src/single_io.c +++ b/src/single_io.c @@ -127,9 +127,40 @@ void readArray(hid_t h_grp, const struct io_props props, size_t N, if (io_is_double_precision(props.type)) { double* temp_d = (double*)temp; for (size_t i = 0; i < num_elements; ++i) temp_d[i] *= unit_factor; + } else { float* temp_f = (float*)temp; - for (size_t i = 0; i < num_elements; ++i) temp_f[i] *= unit_factor; + +#ifdef SWIFT_DEBUG_CHECKS + float maximum = 0.f; + float minimum = FLT_MAX; +#endif + + /* Loop that converts the Units */ + for (size_t i = 0; i < num_elements; ++i) { + +#ifdef SWIFT_DEBUG_CHECKS + /* Find the absolute minimum and maximum values */ + const float abstemp_f = fabsf(temp_f[i]); + if (abstemp_f != 0.f) { + maximum = max(maximum, abstemp_f); + minimum = min(minimum, abstemp_f); + } +#endif + + /* Convert the float units */ + temp_f[i] *= unit_factor; + } + +#ifdef SWIFT_DEBUG_CHECKS + /* The two possible errors: larger than float or smaller + * than float precision. */ + if (unit_factor * maximum > FLT_MAX) { + error("Unit conversion results in numbers larger than floats"); + } else if (unit_factor * minimum < FLT_MIN) { + error("Numbers smaller than float precision"); + } +#endif } } -- GitLab