Skip to content

Commit 4a70be9

Browse files
authored
Adapt to new serialbox2 convention (Serialbox v2.2.1) (#20)
1 parent 337927d commit 4a70be9

File tree

5 files changed

+88
-27
lines changed

5 files changed

+88
-27
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
/.cproject
66
/serialbox
77
/external
8+
/.settings
9+

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ project(gridtools-verification CXX C)
99

1010
include(ExternalProject)
1111

12-
set(GRIDTOOLS_VERIFICATION_VERSION_STRING "0.2")
13-
set(SERIALBOX_VERSION_REQUIRED "2.1.0")
12+
set(GRIDTOOLS_VERIFICATION_VERSION_STRING "0.3")
13+
set(SERIALBOX_VERSION_REQUIRED "2.2.1")
1414

1515
#----------------- CMake options
1616
# C++11 compiler support
@@ -45,6 +45,12 @@ find_package( Serialbox "${SERIALBOX_VERSION_REQUIRED}" REQUIRED HINTS
4545
"$ENV{SERIALBOX_ROOT}/cmake")
4646
include_directories(${SERIALBOX_INCLUDE_DIRS})
4747

48+
#----------------- find boost
49+
set( Boost_NO_SYSTEM_PATHS ON )
50+
set( BOOST_INCLUDEDIR "${SERIALBOX_BOOST_INCLUDE_DIRS}" )
51+
set( REQUIRED_BOOST_COMPONENTS ${SERIALBOX_REQUIRED_BOOST_COMPONENTS} program_options system)
52+
find_package( Boost ${SERIALBOX_BOOST_VERSION} REQUIRED ${REQUIRED_BOOST_COMPONENTS})
53+
4854
#----------------- build bundled libs
4955
add_subdirectory( libs )
5056
include_directories(${GTEST_INCLUDE_DIRS})

cmake/gridtools_verification-config.cmake.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ set_and_check(GRIDTOOLS_ROOT "@GRIDTOOLS_ROOT@")
1919
set_and_check(GRIDTOOLS_VERIFICATION_INCLUDE_DIR "@PACKAGE_GRIDTOOLS_VERIFICATION_INCLUDE_DIR@")
2020
set_and_check(GRIDTOOLS_VERIFICATION_LIBRARY_DIR "@PACKAGE_GRIDTOOLS_VERIFICATION_LIBRARY_DIR@")
2121

22-
23-
set(GRIDTOOLS_VERIFICATION_BOOST_LIBRARYDIR "@SERIALBOX_BOOST_LIBRARY_DIRS@")
24-
set(GRIDTOOLS_VERIFICATION_BOOST_INCLUDEDIR "@SERIALBOX_BOOST_INCLUDE_DIRS@")
25-
set(GRIDTOOLS_VERIFICATION_BOOST_VERSION "@SERIALBOX_BOOST_VERSION@")
26-
set(GRIDTOOLS_VERIFICATION_REQUIRED_BOOST_COMPONENTS "@SERIALBOX_REQUIRED_BOOST_COMPONENTS@")
22+
set(GRIDTOOLS_VERIFICATION_BOOST_LIB_VERSION "@Boost_LIB_VERSION@")
23+
STRING(REGEX REPLACE "_" "." GRIDTOOLS_VERIFICATION_BOOST_VERSION ${GRIDTOOLS_VERIFICATION_BOOST_LIB_VERSION})
24+
set(GRIDTOOLS_VERIFICATION_BOOST_LIBRARYDIR "@Boost_LIBRARY_DIRS@")
25+
set(GRIDTOOLS_VERIFICATION_BOOST_INCLUDEDIR "@Boost_INCLUDE_DIRS@")
26+
set(GRIDTOOLS_VERIFICATION_REQUIRED_BOOST_COMPONENTS "@REQUIRED_BOOST_COMPONENTS@")
2727

2828
list(APPEND GRIDTOOLS_VERIFICATION_INCLUDE_DIRS "${GRIDTOOLS_ROOT}/include")
2929
list(APPEND GRIDTOOLS_VERIFICATION_INCLUDE_DIRS ${SERIALBOX_INCLUDE_DIRS})

src/core/serialization.h

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "../verification_exception.h"
4444
#include "error.h"
4545
#include "logger.h"
46+
#include <numeric>
47+
#include <string>
4648

4749
namespace gt_verification {
4850

@@ -86,38 +88,32 @@ namespace gt_verification {
8688
// Get info of serialized field
8789
const ser::field_meta_info &info = serializer_->get_field_meta_info(name);
8890

89-
int iSizeHalo = field.i_size();
90-
int jSizeHalo = field.j_size();
91-
int kSize = field.k_size();
91+
std::vector< int > field_sizes{field.i_size(), field.j_size(), field.k_size()};
92+
93+
auto mask = mask_for_killed_dimensions(info.dims(), field_sizes);
94+
field_sizes = apply_mask(mask, field_sizes);
9295

93-
VERIFICATION_LOG() << boost::format(" - loading %-15s (%2i, %2i, %2i)") % name % iSizeHalo % jSizeHalo %
94-
kSize
96+
VERIFICATION_LOG() << boost::format(" - loading %-15s (%2i, %2i, %2i)") % name % field_sizes[0] %
97+
field_sizes[1] % field_sizes[2]
9598
<< logger_action::endl;
9699

97100
// Check dimensions
98-
if ((info.dims()[0] != iSizeHalo) || (info.dims()[1] != jSizeHalo) || (info.dims()[2] != kSize))
101+
if (!sizes_compatible(info.dims(), field_sizes))
99102
throw verification_exception("the requested field '%s' has a different size than the provided field.\n"
100-
"Registered as: (%i, %i, %i)\n"
101-
"Given as: (%i, %i, %i)",
103+
"Registered as: (%s)\n"
104+
"Given as: (%s)",
102105
name,
103-
info.dims()[0],
104-
info.dims()[1],
105-
info.dims()[2],
106-
iSizeHalo,
107-
jSizeHalo,
108-
kSize);
106+
to_string(info.dims()),
107+
to_string(field_sizes));
109108

110109
// Check types
111110
if (info.type() != serialbox::ToTypeID< T >::value)
112111
throw verification_exception(
113112
"the requested field '%s' has a different type than the provided field.", name);
114113

115114
// Deserialize field
116-
int iStride = field.i_stride();
117-
int jStride = field.j_stride();
118-
int kStride = field.k_stride();
119-
120-
std::vector< int > strides{iStride, jStride, kStride};
115+
std::vector< int > strides{field.i_stride(), field.j_stride(), field.k_stride()};
116+
strides = apply_mask(mask, strides);
121117
serializer_->read(name, savepoint, field.data(), strides);
122118

123119
field.sync();
@@ -189,5 +185,62 @@ namespace gt_verification {
189185

190186
private:
191187
std::shared_ptr< ser::serializer > serializer_;
188+
189+
bool can_transform_dimension(int serialized_size, int verifier_size) {
190+
// We allow automatic transformation of D-1-dim fields to D-dim fields if the length of the dimension is 1
191+
if (serialized_size == 0 && verifier_size == 1)
192+
return true;
193+
else
194+
return false;
195+
}
196+
197+
bool sizes_compatible(const std::vector< int > &serialized_sizes, const std::vector< int > &verifier_sizes) {
198+
for (size_t i = 0; i < serialized_sizes.size(); ++i) {
199+
if (serialized_sizes[i] == 0)
200+
return true;
201+
else if (serialized_sizes[i] != verifier_sizes[i] &&
202+
!can_transform_dimension(serialized_sizes[i], verifier_sizes[i]))
203+
return false;
204+
}
205+
return true;
206+
}
207+
208+
std::string to_string(const std::vector< int > &v) {
209+
if (v.size() == 0)
210+
return std::string("<empty-vector>");
211+
else
212+
return std::accumulate(std::next(v.begin()),
213+
v.end(),
214+
std::string(std::to_string(v[0])),
215+
[](std::string s, int i) { return s + ", " + std::to_string(i); });
216+
}
217+
218+
// FIXME: hack the mapping of killed dimension
219+
std::vector< bool > mask_for_killed_dimensions(
220+
const std::vector< int > &serialized_sizes, const std::vector< int > &verifier_sizes) const {
221+
std::vector< bool > mask;
222+
size_t i_serialized = 0;
223+
for (size_t i = 0; i < verifier_sizes.size(); ++i) {
224+
if (verifier_sizes[i] == serialized_sizes[i_serialized]) {
225+
mask.push_back(true);
226+
i_serialized++;
227+
} else if (verifier_sizes[i] == 1)
228+
mask.push_back(false);
229+
else
230+
throw verification_exception("Failed to mask killed dimensions.");
231+
}
232+
return mask;
233+
}
234+
235+
std::vector< int > apply_mask(const std::vector< bool > &mask, const std::vector< int > &v) const {
236+
if (mask.size() != v.size())
237+
throw verification_exception("Size of mask does not match size of vector.");
238+
std::vector< int > result;
239+
for (size_t i = 0; i < mask.size(); ++i) {
240+
if (mask[i])
241+
result.push_back(v[i]);
242+
}
243+
return result;
244+
}
192245
};
193246
}

unittest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function(add_unittest_executable name sources)
22
add_executable(${name} ${sources} ${GT_VERIFICATION_HEADERS})
3-
43
target_link_libraries(${name}
54
gridtools_verification
65
${GTEST_BOTH_LIBRARIES}
6+
${SERIALBOX_CXX_LIBRARIES}
77
)
88
endfunction(add_unittest_executable)
99

0 commit comments

Comments
 (0)