DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
SerializerJSON.hpp
Go to the documentation of this file.
1#pragma once
2/// @file SerializerJSON.hpp
3/// @brief Per-rank JSON serializer implementing the SerializerBase interface.
4/// @par Unit Test Coverage (test_Serializer.cpp, MPI np=1,2,4)
5/// - Scalar round-trip: WriteInt/ReadInt, WriteIndex/ReadIndex,
6/// WriteReal/ReadReal, WriteString/ReadString
7/// - Vector round-trip: WriteRealVector, WriteIndexVector, WriteRowsizeVector
8/// - uint8 array: with and without codec (base64 + zlib)
9/// - Path operations: CreatePath, GoToPath, GetCurrentPath, ListCurrentPath
10/// - Shared pointer deduplication: WriteSharedIndexVector / ReadSharedIndexVector
11/// @par Not Yet Tested
12/// - WriteSharedRowsizeVector / ReadSharedRowsizeVector
13/// - WriteIndexVectorPerRank
14/// - SetDeflateLevel impact verification
15/// - Error handling (nonexistent file, duplicate paths)
16#include "SerializerBase.hpp"
17
19#include <fstream>
20#include <map>
21
22namespace DNDS::Serializer
23{
24 /// @brief Per-rank JSON file serializer; each MPI rank writes its own .json file.
26 {
27 std::fstream fileStream;
28 nlohmann::json jObj;
29 bool reading = true;
30 std::vector<std::string> cPathSplit;
31 std::string cP; // current path
32 // ptr_2_pth and pth_2_ssp are inherited from SerializerBase
33
34 bool useCodecOnUint8{false};
35 int deflateLevel{5};
36
37 MPIInfo mpi; // NULL
38
39 public:
40 // Rule-of-five closure. Owns `fstream` + JSON DOM; copy / move
41 // are inherited-deleted from SerializerBase but must be re-declared
42 // explicitly because the non-trivial dtor suppresses implicit defaults.
43 SerializerJSON() = default;
44 SerializerJSON(const SerializerJSON &) = delete;
48
49 void SetUseCodecOnUint8(bool v) { useCodecOnUint8 = v; }
50 void SetDeflateLevel(int v) { deflateLevel = v; }
51
52 void OpenFile(const std::string &fName, bool read) override;
53 void CloseFile() override;
55 void CreatePath(const std::string &p) override;
56 void GoToPath(const std::string &p) override;
57 bool IsPerRank() override { return true; }
58 std::string GetCurrentPath() override;
59 std::set<std::string> ListCurrentPath() override;
60 int GetMPIRank() override { return -1; }
61 int GetMPISize() override { return -1; }
62 const MPIInfo &getMPI() override { return mpi; }
63
64 void WriteInt(const std::string &name, int v) override;
65 void WriteIndex(const std::string &name, index v) override;
66 void WriteReal(const std::string &name, real v) override;
67 void WriteString(const std::string &name, const std::string &v) override;
68
69 void WriteIndexVector(const std::string &name, const std::vector<index> &v, ArrayGlobalOffset offset) override;
70 void WriteRowsizeVector(const std::string &name, const std::vector<rowsize> &v, ArrayGlobalOffset offset) override;
71 void WriteRealVector(const std::string &name, const std::vector<real> &v, ArrayGlobalOffset offset) override;
72 void WriteSharedIndexVector(const std::string &name, const ssp<host_device_vector<index>> &v, ArrayGlobalOffset offset) override;
73 void WriteSharedRowsizeVector(const std::string &name, const ssp<host_device_vector<rowsize>> &v, ArrayGlobalOffset offset) override;
74
75 void WriteUint8Array(const std::string &name, const uint8_t *data, index size, ArrayGlobalOffset offset) override;
76
77 void WriteIndexVectorPerRank(const std::string &name, const std::vector<index> &v) override
78 {
79 this->WriteIndexVector(name, v, ArrayGlobalOffset_Unknown);
80 }
81
82 void ReadInt(const std::string &name, int &v) override;
83 void ReadIndex(const std::string &name, index &v) override;
84 void ReadReal(const std::string &name, real &v) override;
85 void ReadString(const std::string &name, std::string &v) override;
86
87 void ReadIndexVector(const std::string &name, std::vector<index> &v, ArrayGlobalOffset &offset) override;
88 void ReadRowsizeVector(const std::string &name, std::vector<rowsize> &v, ArrayGlobalOffset &offset) override;
89 void ReadRealVector(const std::string &name, std::vector<real> &v, ArrayGlobalOffset &offset) override;
90 void ReadSharedIndexVector(const std::string &name, ssp<host_device_vector<index>> &v, ArrayGlobalOffset &offset) override;
91 void ReadSharedRowsizeVector(const std::string &name, ssp<host_device_vector<rowsize>> &v, ArrayGlobalOffset &offset) override;
92
93 void ReadUint8Array(const std::string &name, uint8_t *data, index &size, ArrayGlobalOffset &offset) override;
94
95 ~SerializerJSON() override
96 {
97 // Destructors must not throw; swallow any exception from the
98 // close path (fstream flush, bad-path cleanup). A failure here
99 // is reported when the user invoked CloseFile() explicitly.
100 // NOLINTBEGIN(bugprone-empty-catch)
101 try
102 {
104 }
105 catch (...)
106 {
107 }
108 // NOLINTEND(bugprone-empty-catch)
109 }
110 };
111}
JSON-to-Eigen conversion utilities and nlohmann_json helper macros.
Base types and abstract interface for array serialization.
Describes one rank's window into a globally-distributed dataset.
Abstract interface for reading/writing scalars, vectors, and byte arrays.
Per-rank JSON file serializer; each MPI rank writes its own .json file.
void ReadIndexVector(const std::string &name, std::vector< index > &v, ArrayGlobalOffset &offset) override
void ReadSharedIndexVector(const std::string &name, ssp< host_device_vector< index > > &v, ArrayGlobalOffset &offset) override
void ReadString(const std::string &name, std::string &v) override
Read a UTF-8 string into v.
void ReadRealVector(const std::string &name, std::vector< real > &v, ArrayGlobalOffset &offset) override
std::set< std::string > ListCurrentPath() override
Names of direct children of the current path.
std::string GetCurrentPath() override
String form of the current path.
SerializerJSON & operator=(SerializerJSON &&)=delete
void CloseFile() override
Close the backing file, flushing buffers.
SerializerJSON(SerializerJSON &&)=delete
void WriteSharedRowsizeVector(const std::string &name, const ssp< host_device_vector< rowsize > > &v, ArrayGlobalOffset offset) override
Write a shared rowsize vector; deduplicated across multiple writes.
void WriteString(const std::string &name, const std::string &v) override
Write a UTF-8 string under name.
void WriteIndexVectorPerRank(const std::string &name, const std::vector< index > &v) override
Write a per-rank index vector (replicated name, independent values).
void OpenFile(const std::string &fName, bool read) override
Open a backing file (H5 file or JSON file depending on subclass).
SerializerJSON(const SerializerJSON &)=delete
void WriteSharedIndexVector(const std::string &name, const ssp< host_device_vector< index > > &v, ArrayGlobalOffset offset) override
Write a shared index vector; deduplicated across multiple writes that share the same shared_ptr.
void ReadInt(const std::string &name, int &v) override
Read a scalar int into v.
void ReadUint8Array(const std::string &name, uint8_t *data, index &size, ArrayGlobalOffset &offset) override
Two-pass byte array read.
void CreatePath(const std::string &p) override
Create a sub-path (H5 group / JSON object) at the current location.
void WriteRowsizeVector(const std::string &name, const std::vector< rowsize > &v, ArrayGlobalOffset offset) override
Write a rowsize vector (collective for H5).
void WriteInt(const std::string &name, int v) override
Write a scalar int under name at the current path.
SerializerJSON & operator=(const SerializerJSON &)=delete
void WriteIndexVector(const std::string &name, const std::vector< index > &v, ArrayGlobalOffset offset) override
Write an index vector (collective for H5). offset carries the distribution mode (ArrayGlobalOffset_Pa...
void WriteUint8Array(const std::string &name, const uint8_t *data, index size, ArrayGlobalOffset offset) override
Write a raw byte buffer under name. offset.isDist() = true means the caller provides the exact per-ra...
int GetMPIRank() override
Rank index cached by the serializer (relevant for collective I/O).
void ReadSharedRowsizeVector(const std::string &name, ssp< host_device_vector< rowsize > > &v, ArrayGlobalOffset &offset) override
bool IsPerRank() override
Whether this serializer is per-rank (JSON-file-per-rank) or collective (shared H5 file)....
void ReadIndex(const std::string &name, index &v) override
Read a scalar index into v.
void WriteRealVector(const std::string &name, const std::vector< real > &v, ArrayGlobalOffset offset) override
Write a real vector (collective for H5).
void WriteReal(const std::string &name, real v) override
Write a scalar real under name.
void ReadRowsizeVector(const std::string &name, std::vector< rowsize > &v, ArrayGlobalOffset &offset) override
void WriteIndex(const std::string &name, index v) override
Write a scalar index under name.
const MPIInfo & getMPI() override
MPI context the serializer was opened with.
int GetMPISize() override
Rank count cached by the serializer.
void GoToPath(const std::string &p) override
Navigate to an existing path. Supports / -separated segments.
void ReadReal(const std::string &name, real &v) override
Read a scalar real into v.
Host + optional device vector of trivially copyable T.
Definition Vector.hpp:217
int64_t index
Global row / DOF index type (signed 64-bit; handles multi-billion-cell meshes).
Definition Defines.hpp:112
std::shared_ptr< T > ssp
Shortened alias for std::shared_ptr used pervasively in DNDSR.
Definition Defines.hpp:143
double real
Canonical floating-point scalar used throughout DNDSR (double precision).
Definition Defines.hpp:110
Lightweight bundle of an MPI communicator and the calling rank's coordinates.
Definition MPI.hpp:231
Eigen::Matrix< real, 5, 1 > v
const tPoint const tPoint const tPoint & p