31#include <unordered_map>
55 logger::raw_ln(
"tag =", tag,
"size =", vals.size());
73 std::vector<std::string> tags;
79 for (
u32 i = 0; i < nvars; i++) {
80 tmp.
entries.push_back({tags[i], vals[i]});
90 int nvars = entries.size();
97 std::vector<std::string> tags;
99 for (
u32 i = 0; i < nvars; i++) {
100 auto [a, b] = entries[i];
112 for (
auto [key, val] : entries) {
113 logger::raw_ln(key, val);
119 logger::raw_ln(
"--blocks_fort_int --");
123 logger::raw_ln(
"--blocks_i8 --");
127 logger::raw_ln(
"--blocks_i16 --");
131 logger::raw_ln(
"--blocks_i32 --");
135 logger::raw_ln(
"--blocks_i64 --");
139 logger::raw_ln(
"--blocks_fort_real--");
143 logger::raw_ln(
"--blocks_f32 --");
147 logger::raw_ln(
"--blocks_f64 --");
159 for (
u32 j = 0; j < numarray[0]; j++) {
163 for (
u32 j = 0; j < numarray[1]; j++) {
166 for (
u32 j = 0; j < numarray[2]; j++) {
167 block.
blocks_i16.push_back(PhantomDumpBlockArray<i16>::from_file(phfile, block.
tot_count));
169 for (
u32 j = 0; j < numarray[3]; j++) {
170 block.
blocks_i32.push_back(PhantomDumpBlockArray<i32>::from_file(phfile, block.
tot_count));
172 for (
u32 j = 0; j < numarray[4]; j++) {
173 block.
blocks_i64.push_back(PhantomDumpBlockArray<i64>::from_file(phfile, block.
tot_count));
175 for (
u32 j = 0; j < numarray[5]; j++) {
179 for (
u32 j = 0; j < numarray[6]; j++) {
182 for (
u32 j = 0; j < numarray[7]; j++) {
183 block.
blocks_f64.push_back(PhantomDumpBlockArray<f64>::from_file(phfile, block.
tot_count));
193 for (
u32 j = 0; j < numarray[0]; j++) {
194 blocks_fort_int[j].write(phfile, tot_count);
196 for (
u32 j = 0; j < numarray[1]; j++) {
197 blocks_i8[j].write(phfile, tot_count);
199 for (
u32 j = 0; j < numarray[2]; j++) {
200 blocks_i16[j].write(phfile, tot_count);
202 for (
u32 j = 0; j < numarray[3]; j++) {
203 blocks_i32[j].write(phfile, tot_count);
205 for (
u32 j = 0; j < numarray[4]; j++) {
206 blocks_i64[j].write(phfile, tot_count);
208 for (
u32 j = 0; j < numarray[5]; j++) {
209 blocks_fort_real[j].write(phfile, tot_count);
211 for (
u32 j = 0; j < numarray[6]; j++) {
212 blocks_f32[j].write(phfile, tot_count);
214 for (
u32 j = 0; j < numarray[7]; j++) {
215 blocks_f64[j].write(phfile, tot_count);
221 s = shambase::format(
"{:16s}", s);
222 auto &blocks = blocks_fort_real;
224 for (
u32 i = 0; i < blocks_fort_real.size(); i++) {
225 if (blocks_fort_real[i].tag == s) {
232 blocks_fort_real.push_back(std::move(tmp));
234 for (
u32 i = 0; i < blocks_fort_real.size(); i++) {
235 if (blocks_fort_real[i].tag == s) {
245 s = shambase::format(
"{:16s}", s);
247 auto &blocks = blocks_f32;
249 for (
u32 i = 0; i < blocks_f32.size(); i++) {
250 if (blocks_f32[i].tag == s) {
257 blocks_f32.push_back(std::move(tmp));
259 for (
u32 i = 0; i < blocks_f32.size(); i++) {
260 if (blocks_f32[i].tag == s) {
272 phfile.
write(i1, r1, i2, iversion, i3);
276 table_header_fort_int.write(phfile);
277 table_header_i8.write(phfile);
278 table_header_i16.write(phfile);
279 table_header_i32.write(phfile);
280 table_header_i64.write(phfile);
281 table_header_fort_real.write(phfile);
282 table_header_f32.write(phfile);
283 table_header_f64.write(phfile);
285 int nblocks = blocks.size();
286 phfile.
write(nblocks);
288 std::vector<i64> block_tot_counts;
289 std::vector<std::array<i32, 8>> block_numarray;
290 for (
u32 i = 0; i < nblocks; i++) {
292 i64 tot_count = blocks[i].tot_count;
293 std::array<i32, 8> counts
294 = {
i32(blocks[i].blocks_fort_int.size()),
295 i32(blocks[i].blocks_i8.size()),
296 i32(blocks[i].blocks_i16.size()),
297 i32(blocks[i].blocks_i32.size()),
298 i32(blocks[i].blocks_i64.size()),
299 i32(blocks[i].blocks_fort_real.size()),
300 i32(blocks[i].blocks_f32.size()),
301 i32(blocks[i].blocks_f64.size())};
303 phfile.
write(tot_count, counts);
304 block_tot_counts.push_back(tot_count);
305 block_numarray.push_back(counts);
308 for (
u32 i = 0; i < nblocks; i++) {
309 blocks[i].write(phfile, block_tot_counts[i], block_numarray[i]);
343 phfile.
read(nblocks);
345 std::vector<i64> block_tot_counts;
346 std::vector<std::array<i32, 8>> block_numarray;
348 for (
u32 i = 0; i < nblocks; i++) {
351 std::array<i32, 8> counts;
353 phfile.
read(tot_count, counts);
355 block_tot_counts.push_back(tot_count);
356 block_numarray.push_back(counts);
358 for (
u32 i = 0; i < nblocks; i++) {
364 logger::warn_ln(
"[PhantomReader]",
"some data was not read");
371 logger::raw_ln(
"--- dump state ---");
373 logger::raw_ln(
"table_header_fort_int len =", table_header_fort_int.entries.size());
374 table_header_fort_int.print_state();
375 logger::raw_ln(
"table_header_i8 len =", table_header_i8.entries.size());
376 table_header_i8.print_state();
377 logger::raw_ln(
"table_header_i16 len =", table_header_i16.entries.size());
378 table_header_i16.print_state();
379 logger::raw_ln(
"table_header_i32 len =", table_header_i32.entries.size());
380 table_header_i32.print_state();
381 logger::raw_ln(
"table_header_i64 len =", table_header_i64.entries.size());
382 table_header_i64.print_state();
383 logger::raw_ln(
"table_header_fort_real len =", table_header_fort_real.entries.size());
384 table_header_fort_real.print_state();
385 logger::raw_ln(
"table_header_f32 len =", table_header_f32.entries.size());
386 table_header_f32.print_state();
387 logger::raw_ln(
"table_header_f64 len =", table_header_f64.entries.size());
388 table_header_f64.print_state();
390 for (
u32 i = 0; i < blocks.size(); i++) {
391 logger::raw_ln(
"block ", i,
":");
392 blocks[i].print_state();
394 logger::raw_ln(
"------------------");
402 std::unordered_map<std::string, f64> header;
404 dump.table_header_fort_int.add_to_map(header);
405 dump.table_header_i8.add_to_map(header);
406 dump.table_header_i16.add_to_map(header);
407 dump.table_header_i32.add_to_map(header);
408 dump.table_header_i64.add_to_map(header);
409 dump.table_header_fort_real.add_to_map(header);
410 dump.table_header_f32.add_to_map(header);
411 dump.table_header_f64.add_to_map(header);
415 std::unordered_map<std::string, f64> header_ref = load_header(dump_ref);
416 std::unordered_map<std::string, f64> header_comp = load_header(dump_comp);
418 auto get_keys = [](std::unordered_map<std::string, f64> &map) {
419 std::set<std::string> ret;
420 for (
auto [key, val] : map) {
426 std::set<std::string> header_key_ref = get_keys(header_ref);
427 std::set<std::string> header_key_comp = get_keys(header_comp);
429 std::vector<std::string> missing = {};
430 std::vector<std::string> matching = {};
431 std::vector<std::string> extra = {};
435 for (std::string &missing_key : missing) {
438 "The dump we are comparing against is missing the key",
443 for (std::string &matching_key : matching) {
444 if (header_ref[matching_key] != header_comp[matching_key]) {
448 "Mismatch in the header key {}, ref={}, comp={}",
450 header_ref[matching_key],
451 header_comp[matching_key]));
455 for (std::string &extra_key : extra) {
458 "The dump we are comparing against has the extra key",
464 logger::info_ln(
"PhantomDump",
"This comparison reported", offenses,
"offenses");
466 return offenses == 0;
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int64_t i64
64 bit integer
std::int32_t i32
32 bit integer
Class for reading and writing Fortran-style binary files.
void write(Args &...args)
Write a list of arguments to the internal buffer.
void write_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Write a fixed-length string array to the buffer.
void write_val_array(std::vector< T > &vec, u32 val_count)
Write an array of values to the buffer.
void read_fixed_string(std::string &s, u32 len)
Read a fixed-length string from the buffer.
void read(Args &...args)
Read a list of arguments from the internal buffer.
void read_string_array(std::vector< std::string > &svec, u32 strlen, u32 str_count)
Read a fixed-length string array from the buffer.
void read_val_array(std::vector< T > &vec, u32 val_count)
Read an array of values from the buffer.
void write_fixed_string(std::string &s, u32 len)
Write a fixed-length string to the buffer.
bool finished_read()
Check if the end of the file has been reached.
This header file contains utility functions related to exception handling in the code.
void set_diff(Container1 &c1, Container2 &ref, std::vector< T > &missing, std::vector< T > &matching, std::vector< T > &extra)
Compute the difference between two containers as three separate vectors.
bool compare_phantom_dumps(PhantomDump &dump1, PhantomDump &dump2)
Compare two phantom dumps and report offenses.
This file contains the definition for the stacktrace related functionality.
A helper class to represent a single block of data in a Phantom dump.
void write(shambase::FortranIOFile &phfile, i64 tot_count)
Writes a block to a file.
std::string tag
The tag of the block.
void print_state()
Prints the state of the block.
std::vector< T > vals
The values of the block.
static PhantomDumpBlockArray from_file(shambase::FortranIOFile &phfile, i64 tot_count)
Reads a block from a file.
A class to represent a single block of data in a Phantom dump.
std::vector< PhantomDumpBlockArray< fort_int > > blocks_fort_int
The blocks of values of type fort_int.
static PhantomDumpBlock from_file(shambase::FortranIOFile &phfile, i64 tot_count, std::array< i32, 8 > numarray)
Reads a block from a file.
u64 get_ref_f32(std::string s)
Gets the index of a block of type f32 with the given name.
std::vector< PhantomDumpBlockArray< i16 > > blocks_i16
The blocks of values of type i16.
std::vector< PhantomDumpBlockArray< i8 > > blocks_i8
The blocks of values of type i8.
std::vector< PhantomDumpBlockArray< f64 > > blocks_f64
The blocks of values of type f64.
std::vector< PhantomDumpBlockArray< i64 > > blocks_i64
The blocks of values of type i64.
void write(shambase::FortranIOFile &phfile, i64 tot_count, std::array< i32, 8 > numarray)
Writes a block to a file.
u64 get_ref_fort_real(std::string s)
Gets the index of a block of type fort_real with the given name.
std::vector< PhantomDumpBlockArray< i32 > > blocks_i32
The blocks of values of type i32.
i64 tot_count
The total number of values in the block.
std::vector< PhantomDumpBlockArray< fort_real > > blocks_fort_real
The blocks of values of type fort_real.
std::vector< PhantomDumpBlockArray< f32 > > blocks_f32
The blocks of values of type f32.
void print_state()
Prints the state of the block.
Class representing a Phantom dump file.
PhantomDumpTableHeader< i16 > table_header_i16
Table header for signed 16-bit integer data.
fort_int i3
Magic number used in the phantom dump format.
void check_magic_numbers()
Checks if the magic numbers in the PhantomDump struct match the expected values.
PhantomDumpTableHeader< fort_int > table_header_fort_int
Table header for integer data.
std::string fileid
Magic number used in the phantom dump format.
fort_int i1
Magic number used in the phantom dump format.
PhantomDumpTableHeader< f32 > table_header_f32
Table header for 32-bit floating-point data.
PhantomDumpTableHeader< i64 > table_header_i64
Table header for signed 64-bit integer data.
PhantomDumpTableHeader< i32 > table_header_i32
Table header for signed 32-bit integer data.
static PhantomDump from_file(shambase::FortranIOFile &phfile)
Reads a Phantom dump file and returns a PhantomDump object.
shambase::FortranIOFile gen_file()
Generates a Phantom dump file from the current state of the object.
fort_int i2
Magic number used in the phantom dump format.
std::vector< PhantomDumpBlock > blocks
List of blocks in the Phantom dump file.
PhantomDumpTableHeader< fort_real > table_header_fort_real
Table header for floating-point data.
PhantomDumpTableHeader< f64 > table_header_f64
Table header for 64-bit floating-point data.
fort_int iversion
Magic number used in the phantom dump format.
void print_state()
Print current state of the data stored in the class.
PhantomDumpTableHeader< i8 > table_header_i8
Table header for signed 8-bit integer data.
fort_real r1
Magic number used in the phantom dump format.