Skip to content

Commit 4a608e8

Browse files
committed
Merge branch 'bartgol/eamxx/fix-physics-baselines-writing' into next (PR #7524)
Fix a template overload resolution. Also, add possibility to generate ASCII baselines (for debug purposes only). [BFB]
2 parents 250e373 + 5a34ad9 commit 4a608e8

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

components/eamxx/src/physics/p3/tests/p3_run_and_cmp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ struct Baseline {
203203
EKAT_REQUIRE_MSG(dim == f.dim,
204204
"For field " << f.name << " read expected dim " <<
205205
f.dim << " but got " << dim);
206-
std::vector<int> ds(dim);
207-
impl::read_scalars(ifile,ds);
206+
int extents[3];
207+
impl::read_scalars(ifile,extents);
208208
for (int i = 0; i < dim; ++i)
209-
EKAT_REQUIRE_MSG(ds[i] == f.extent[i],
209+
EKAT_REQUIRE_MSG(extents[i] == f.extent[i],
210210
"For field " << f.name << " read expected dim "
211211
<< i << " to have extent " << f.extent[i] << " but got "
212-
<< ds[i]);
212+
<< extents[i]);
213213
impl::read_scalars(ifile,f.data, f.size);
214214
// The code below is to force a result difference. This is used by the
215215
// scream/scripts internal testing to verify that various DIFFs are detected.

components/eamxx/src/physics/share/physics_test_read_write_helpers.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
#include <vector>
66
#include <type_traits>
77

8+
#ifdef EAMXX_ASCII_BASELINES
9+
#include <iomanip>
10+
#include <limits>
11+
#endif
12+
813
namespace scream {
914
namespace impl {
1015

1116
template <typename T>
1217
void read_scalars(std::ifstream& ifile, T& data)
1318
{
19+
#ifdef EAMXX_ASCII_BASELINES
20+
ifile >> data;
21+
#else
1422
ifile.read(reinterpret_cast<char*>(&data),sizeof(data));
23+
#endif
1524
}
1625

1726
template <typename T>
@@ -47,7 +56,15 @@ void read_scalars(std::ifstream& ifile, T& data, S&... tail)
4756
template <typename T>
4857
void write_scalars(std::ofstream& ofile, const T& data)
4958
{
59+
#ifdef EAMXX_ASCII_BASELINES
60+
if constexpr (std::is_floating_point_v<T>) {
61+
ofile << std::fixed << std::setprecision(std::numeric_limits<T>::digits10+1) << data << " ";
62+
} else {
63+
ofile << data << " ";
64+
}
65+
#else
5066
ofile.write(reinterpret_cast<const char*>(&data),sizeof(data));
67+
#endif
5168
}
5269

5370
template <typename T>
@@ -67,7 +84,7 @@ void write_scalars(std::ofstream& ofile, const T (&data)[N]) {
6784

6885
template <typename T, typename I>
6986
std::enable_if_t<std::is_integral<I>::value>
70-
write_scalars(std::ofstream& ofile, const T* const data, I N) {
87+
write_scalars(std::ofstream& ofile, T* const data, I N) {
7188
for (I i=0; i<N; ++i) {
7289
write_scalars(ofile,data[i]);
7390
}

components/eamxx/src/share/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ target_compile_options(scream_share PUBLIC
150150
if (SCREAM_CIME_BUILD)
151151
target_compile_definitions (scream_share PUBLIC SCREAM_CIME_BUILD)
152152
endif()
153+
154+
# When dealing with subtle bugs in read/write of unit tests baselines,
155+
# using ascii baselines can help inspect that the file indeed looks as expected
156+
option (EAMXX_ASCII_BASELINES "Whether physics units tests should assume ascii (instead of binary) format for baselines" OFF)
157+
if (EAMXX_ASCII_BASELINES)
158+
target_compile_definitions (scream_share PUBLIC EAMXX_ASCII_BASELINES)
159+
endif()
160+
153161
# We have some issues with RDC and repeated libraries in the link line.
154162
# It's a known issue, and nvcc_wrapper has a flag for handling this.
155163
if (Kokkos_ENABLE_CUDA_RELOCATABLE_DEVICE_CODE)

0 commit comments

Comments
 (0)