Skip to content

Commit caf05f8

Browse files
authored
Merge pull request #362 from jacobmerson/pcms-reduce-FieldData
Use Field with serializer
2 parents 3a45c2d + f642092 commit caf05f8

10 files changed

Lines changed: 252 additions & 217 deletions

‎src/pcms/coupler/field_communicator.hpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class FieldCommunicator
5151
PCMS_FUNCTION_TIMER;
5252
PCMS_ALWAYS_ASSERT(layout_comm_.GetChannel().InSendCommunicationPhase());
5353
auto buffer = make_array_view(comm_buffer_);
54-
serializer_->Serialize(field_.GetData(), field_.GetLayout(), buffer,
55-
layout_comm_.GetPermutationArray());
54+
serializer_->Serialize(field_, buffer, layout_comm_.GetPermutationArray());
5655
comm_.Send(buffer.data_handle(), mode);
5756
}
5857

@@ -64,8 +63,7 @@ class FieldCommunicator
6463
// mode because we make an immediate call to deserialize after a call to
6564
// receive.
6665
auto data = comm_.Recv(redev::Mode::Synchronous);
67-
serializer_->Deserialize(field_.GetData(), field_.GetLayout(),
68-
make_const_array_view(data),
66+
serializer_->Deserialize(field_, make_const_array_view(data),
6967
layout_comm_.GetPermutationArray());
7068
}
7169

‎src/pcms/coupler/field_serializer.h‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@ template <typename T>
1414
class FieldSerializer
1515
{
1616
public:
17-
virtual int Serialize(const FieldData<T>& field, const FieldLayout& layout,
17+
virtual int Serialize(const Field<T>& field,
1818
Rank1View<T, HostMemorySpace> buffer,
1919
Rank1View<const LO, HostMemorySpace> permutation) const
2020
{
2121
auto data = field.GetDOFHolderDataHost();
22-
auto owned = layout.GetOwnedHost();
23-
// The exchange plan is per DOF holder: owned[i] and permutation[i] are
24-
// indexed by holder. All num_components components of a holder share its
25-
// location, so they occupy one contiguous block permutation[i]*num_comp in
26-
// the wire buffer.
2722
if (buffer.size() > 0) {
2823
const LO num_dof = static_cast<LO>(data.extent(0));
2924
const LO num_comp = static_cast<LO>(data.extent(1));
3025
for (LO i = 0; i < num_dof; ++i) {
31-
// A negative permutation entry marks a holder outside the exchange
32-
// (non-owned, or owned but outside the overlap region); it has no slot.
3326
if (permutation[i] >= 0) {
3427
for (LO c = 0; c < num_comp; ++c) {
3528
buffer[permutation[i] * num_comp + c] = data(i, c);
@@ -41,10 +34,10 @@ class FieldSerializer
4134
}
4235

4336
virtual void Deserialize(
44-
FieldData<T>& field, const FieldLayout& layout,
45-
Rank1View<const T, HostMemorySpace> buffer,
37+
Field<T>& field, Rank1View<const T, HostMemorySpace> buffer,
4638
Rank1View<const LO, HostMemorySpace> permutation) const
4739
{
40+
const auto& layout = field.GetLayout();
4841
const LO num_dof = layout.GetNumOwnedDofHolder();
4942
const LO num_comp = layout.GetNumComponents();
5043
Kokkos::View<T*, HostMemorySpace> sorted("sorted", layout.OwnedSize());

‎src/pcms/coupler/serializer/xgc.h‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,20 @@ class XGCFieldSerializer : public FieldSerializer<T>
2020
{
2121
}
2222

23-
int Serialize(const FieldData<T>& field, const FieldLayout& layout,
24-
Rank1View<T, HostMemorySpace> buffer,
23+
int Serialize(const Field<T>& field, Rank1View<T, HostMemorySpace> buffer,
2524
Rank1View<const LO, HostMemorySpace> permutation) const override
2625
{
2726
if (!rank_participates_) {
2827
return 0;
2928
}
3029

31-
auto const* xgc_field = dynamic_cast<const XGCFieldData<T>*>(&field);
30+
auto const* xgc_field =
31+
dynamic_cast<const XGCFieldData<T>*>(&field.GetData());
3232
if (!xgc_field) {
3333
throw pcms_error("XGCFieldSerializer::Serialize: incompatible FieldData");
3434
}
3535

3636
auto data = xgc_field->GetDOFHolderDataHost();
37-
auto owned = layout.GetOwnedHost();
38-
// Per-holder plan: owned[i]/permutation[i] index holders; a holder's
39-
// num_components values form one contiguous block in the wire buffer.
4037
if (buffer.size() > 0) {
4138
const LO num_dof = static_cast<LO>(data.extent(0));
4239
const LO num_comp = static_cast<LO>(data.extent(1));
@@ -54,17 +51,17 @@ class XGCFieldSerializer : public FieldSerializer<T>
5451
}
5552

5653
void Deserialize(
57-
FieldData<T>& field, const FieldLayout& layout,
58-
Rank1View<const T, HostMemorySpace> buffer,
54+
Field<T>& field, Rank1View<const T, HostMemorySpace> buffer,
5955
Rank1View<const LO, HostMemorySpace> permutation) const override
6056
{
61-
auto* xgc_field = dynamic_cast<XGCFieldData<T>*>(&field);
57+
auto* xgc_field = dynamic_cast<XGCFieldData<T>*>(&field.GetData());
6258
if (!xgc_field) {
6359
throw pcms_error(
6460
"XGCFieldSerializer::Deserialize: incompatible FieldData");
6561
}
6662

6763
auto current = xgc_field->GetDOFHolderDataHost();
64+
const auto& layout = field.GetLayout();
6865
const LO num_dof = static_cast<LO>(current.extent(0));
6966
const LO num_comp = static_cast<LO>(current.extent(1));
7067
std::vector<T> full_data(current.size());

‎test/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ if(Catch2_FOUND)
400400
message(
401401
STATUS "Found Catch2: ${Catch2_DIR} (found version ${Catch2_VERSION})")
402402
set(PCMS_UNIT_TEST_SOURCES unit_test_main.cpp test_coordinate_transform.cpp
403-
test_coordinate.cpp test_bounding_box.cpp)
403+
test_coordinate.cpp test_bounding_box.cpp
404+
test_field_serializer.cpp)
404405
if(PCMS_ENABLE_XGC)
405406
list(APPEND PCMS_UNIT_TEST_SOURCES test_xgc_reverse_classification.cpp
406407
test_xgc_field_data.cpp)

‎test/field_test_utils.h‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "pcms/field/evaluation_request.h"
1111
#include "pcms/field/point_evaluator.h"
1212
#include "pcms/field/out_of_bounds_policy.h"
13-
#include "pcms/coupler/field_serializer.h"
1413
#include "pcms/field/coordinate_system.h"
1514
#include "pcms/utility/arrays.h"
1615
#include "pcms/utility/memory_spaces.h"
@@ -366,39 +365,6 @@ inline void SetField(FieldData<Real>& field, const FieldLayout& layout,
366365
SetField<ExecutionSpace>(layout, field, func);
367366
}
368367

369-
// Check that serialize followed by deserialize round-trips the data.
370-
// Uses an identity permutation so permutation[i] = i.
371-
inline void CheckSerializeDeserialize(const FieldLayout& layout,
372-
FieldData<Real>& field)
373-
{
374-
auto data_before = FlattenToRank1View(field.GetDOFHolderDataHost());
375-
int n = static_cast<int>(data_before.size());
376-
377-
std::vector<Real> buffer(n);
378-
std::vector<LO> perm(n);
379-
for (int i = 0; i < n; ++i)
380-
perm[i] = i;
381-
382-
Rank1View<Real, HostMemorySpace> buf_view(buffer.data(), n);
383-
Rank1View<const LO, HostMemorySpace> perm_view(perm.data(), n);
384-
385-
FieldSerializer<Real> serializer;
386-
serializer.Serialize(field, layout, buf_view, perm_view);
387-
serializer.Deserialize(
388-
field, layout, Rank1View<const Real, HostMemorySpace>(buf_view), perm_view);
389-
390-
auto data_after = FlattenToRank1View(field.GetDOFHolderDataHost());
391-
REQUIRE(data_after.size() == data_before.size());
392-
for (int i = 0; i < n; ++i) {
393-
REQUIRE(data_after[i] == Catch::Approx(data_before[i]));
394-
}
395-
}
396-
397-
inline void CheckSerializeDeserialize(Field<Real>& field)
398-
{
399-
CheckSerializeDeserialize(field.GetLayout(), field.GetData());
400-
}
401-
402368
// Helper structure to hold device coordinates with proper lifetime management
403369
struct DeviceCoordinates
404370
{

0 commit comments

Comments
 (0)