.. _program_listing_file_src_pcms_coupler_field_serializer.h: Program Listing for File field_serializer.h =========================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/pcms/coupler/field_serializer.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef PCMS_FIELD_SERIALIZER_H #define PCMS_FIELD_SERIALIZER_H #include "pcms/field/field.h" #include "pcms/utility/arrays.h" #include "pcms/utility/memory_spaces.h" #include "pcms/utility/types.h" #include namespace pcms { // Packs a field's owned values into, and unpacks them from, an exchange // buffer, through the field's DOFAccess. Any field with DOFAccess is // exchanged this way. Backends that need extra work on receive (e.g., // synchronizing replicas) do it in DOFAccess::SetOwnedValues. template class FieldSerializer { public: int Serialize(const Field& field, Rank1View buffer, Rank1View permutation) const { const auto data = field.GetDOFHolderDataHost().GetValues(); if (buffer.size() > 0) { const LO num_dof = static_cast(data.extent(0)); const LO num_comp = static_cast(data.extent(1)); for (LO i = 0; i < num_dof; ++i) { if (permutation[i] >= 0) { for (LO c = 0; c < num_comp; ++c) { buffer[permutation[i] * num_comp + c] = data(i, c); } } } } return static_cast(buffer.size()); } void Deserialize(Field& field, Rank1View buffer, Rank1View permutation) const { const auto& layout = field.GetLayout(); const LO num_dof = layout.GetNumOwnedDofHolder(); const LO num_comp = layout.GetNumComponents(); // We initialize with local data so that // data that doesn't get received from deserialized // doesn't get zeroed out. Long term FIXME // is to make Get/SetData operate over only part of the // arrays const auto current = field.GetDOFHolderDataHost().GetValues(); Kokkos::View sorted("sorted", layout.OwnedSize()); for (LO i = 0; i < num_dof; ++i) { for (LO c = 0; c < num_comp; ++c) { sorted[i * num_comp + c] = current(i, c); } } for (LO i = 0; i < num_dof; ++i) { if (permutation[i] >= 0) { for (LO c = 0; c < num_comp; ++c) { sorted[i * num_comp + c] = buffer[permutation[i] * num_comp + c]; } } } field.SetDOFHolderDataUncheckedHost( Rank2View(sorted.data(), num_dof, num_comp)); } }; } // namespace pcms #endif // PCMS_FIELD_SERIALIZER_H