ABLATE Source Documentation  0.12.33
serializable.hpp
1 #ifndef ABLATELIBRARY_SERIALIZABLE_HPP
2 #define ABLATELIBRARY_SERIALIZABLE_HPP
3 
4 #include <petsc.h>
5 #include <algorithm>
6 #include <memory>
7 #include <string>
8 
9 namespace ablate::io {
13 class Serializable {
14  public:
18  enum class SerializerType { none, collective, serial };
19 
20  virtual ~Serializable() = default;
25  [[nodiscard]] virtual SerializerType Serialize() const { return SerializerType::collective; }
26 
31  [[nodiscard]] virtual const std::string& GetId() const = 0;
32 
39  virtual PetscErrorCode Save(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) = 0;
40 
47  virtual PetscErrorCode Restore(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) = 0;
48 
49  protected:
56  static PetscErrorCode SaveKeyValue(PetscViewer viewer, const char* name, PetscScalar value);
57 
64  static PetscErrorCode RestoreKeyValue(PetscViewer viewer, const char* name, PetscScalar& value);
65 
73  template <class T>
74  static inline PetscErrorCode SaveKeyValue(PetscViewer viewer, const char* name, T value) {
75  PetscFunctionBeginUser;
76  auto tempValue = (PetscScalar)value;
77  PetscCall(SaveKeyValue(viewer, name, tempValue));
78  PetscFunctionReturn(0);
79  }
80 
88  template <class T>
89  static inline PetscErrorCode RestoreKeyValue(PetscViewer viewer, const char* name, T& value) {
90  PetscFunctionBeginUser;
91  PetscScalar tempValue = {};
92  PetscCall(RestoreKeyValue(viewer, name, tempValue));
93  value = (T)tempValue;
94  PetscFunctionReturn(0);
95  }
96 
100  template <class T>
101  static inline SerializerType DetermineSerializerType(const T& types) {
102  auto collectiveCount = std::count_if(types.begin(), types.end(), [](auto& testProcess) {
103  auto serializable = std::dynamic_pointer_cast<ablate::io::Serializable>(testProcess);
104  return serializable != nullptr && serializable->Serialize() == SerializerType::collective;
105  });
106  auto serialCount = std::count_if(types.begin(), types.end(), [](auto& testProcess) {
107  auto serializable = std::dynamic_pointer_cast<ablate::io::Serializable>(testProcess);
108  return serializable != nullptr && serializable->Serialize() == SerializerType::serial;
109  });
110 
111  if (collectiveCount && serialCount) {
112  throw std::invalid_argument("All objects in DetermineSerializerType must be collective or serial");
113  } else if (collectiveCount) {
114  return SerializerType::collective;
115  } else if (serialCount) {
116  return SerializerType::serial;
117  } else {
118  return SerializerType::none;
119  }
120  }
121 };
122 } // namespace ablate::io
123 
124 #endif // ABLATELIBRARY_SERIALIZABLE_HPP
Definition: serializable.hpp:13
static PetscErrorCode RestoreKeyValue(PetscViewer viewer, const char *name, PetscScalar &value)
Definition: serializable.cpp:26
virtual PetscErrorCode Restore(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time)=0
virtual SerializerType Serialize() const
Definition: serializable.hpp:25
SerializerType
Definition: serializable.hpp:18
virtual const std::string & GetId() const =0
static SerializerType DetermineSerializerType(const T &types)
Definition: serializable.hpp:101
static PetscErrorCode SaveKeyValue(PetscViewer viewer, const char *name, T value)
Definition: serializable.hpp:74
static PetscErrorCode SaveKeyValue(PetscViewer viewer, const char *name, PetscScalar value)
Definition: serializable.cpp:5
virtual PetscErrorCode Save(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time)=0
static PetscErrorCode RestoreKeyValue(PetscViewer viewer, const char *name, T &value)
Definition: serializable.hpp:89