Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <iomanip>
8#include <iostream>
9#include <memory>
10#include <sstream>
11#include <type_traits> // std::is_same
12#include <utility> // std::swap, std::move
13
14#include "../global/global.h"
15#include "../grid/grid3D.h"
16#include "../io/AttrRecorderInterface.h"
17#include "../io/FieldWriter.h"
18#include "../io/FnameTemplate.h"
19#include "../io/WriterManager.h"
20#include "../utils/error_handling.h"
21
25inline bool Is_Root_Proc() { return procID == root; }
26
27/* Compute stats for a grid. */
28void Print_Stats(Grid3D& G);
29
37void Write_Data(Grid3D& G, struct Parameters P, int nfile, const io::WriterManager& write_manager);
38
39/* MPI-safe printf routine */
40int chprintf(const char* __restrict sdata, ...);
41
50template <typename T>
51std::string to_string_exact(T const& input)
52{
53 std::stringstream output;
54 output << std::setprecision(std::numeric_limits<T>::max_digits10);
55 output << input;
56 return output.str();
57}
58
59void Create_Log_File(struct Parameters P);
60
61void Write_Message_To_Log_File(const char* message);
62
63void Write_Debug(Real* Value, const char* fname, int nValues, int iProc);
64
65/* Checks whether the directories referred to within outdir exist. Creates them
66 * if they don't. It gracefully handles cases where outdir contains a prefix
67 * for the output files.
68 */
69void Ensure_Dir_Exists(std::string dir_path);
70
71#ifdef HDF5
72// From io/io.cpp
73
80class H5Space1D
81{
82 std::unique_ptr<hsize_t> dim_;
83 hid_t id_;
84
85 public:
86 H5Space1D() : dim_(nullptr), id_{H5I_INVALID_HID} {}
87 H5Space1D(hsize_t dim) : H5Space1D() { this->ensure_dim(dim); }
88 H5Space1D(H5Space1D&& other) noexcept : H5Space1D() { *this = std::move(other); }
89
91 H5Space1D& operator=(H5Space1D&& other) noexcept;
92
93 ~H5Space1D()
94 {
95 if (this->id_ != H5I_INVALID_HID) H5Sclose(this->id_);
96 }
97
99 hid_t id() const { return this->id_; }
100
102 H5Space1D& ensure_dim(hsize_t dim);
103};
104
108class H5AttrRecorder : public AttrRecorderInterface
109{
110 hid_t file_id_;
111 hid_t stringType_;
112 H5Space1D cached_dataspace_;
113
114 hid_t make_attr_1d_(const char* name, hid_t type_id, hsize_t n_elem);
115
116 public:
117 H5AttrRecorder() = delete;
118
119 explicit H5AttrRecorder(hid_t file_id)
120 {
121 this->file_id_ = file_id;
122 this->stringType_ = H5Tcopy(H5T_C_S1);
123 CHOLLA_ASSERT(H5Tset_size(this->stringType_, H5T_VARIABLE) >= 0, "error creating the string type");
124 }
125
126 ~H5AttrRecorder() override { H5Tclose(this->stringType_); }
127
128 void record_arr(const char* name, const double* arr, int length) override;
129 void record_arr(const char* name, const int* arr, int length) override;
130 void record_arr(const char* name, const long* arr, int length) override;
131 void record(const char* name, const char* val) override;
132
133 // for historical consistency scalar arithmetic values are saved as 1-element arrays
134 void record(const char* name, double val) override { this->record_arr(name, &val, 1); }
135 void record(const char* name, int val) override { this->record_arr(name, &val, 1); }
136 void record(const char* name, long val) override { this->record_arr(name, &val, 1); }
137};
138
139herr_t Read_HDF5_Dataset(hid_t file_id, double* dataset_buffer, const char* name);
140herr_t Read_HDF5_Dataset(hid_t file_id, float* dataset_buffer, const char* name);
141
142herr_t Write_HDF5_Dataset(hid_t file_id, hid_t dataspace_id, double* dataset_buffer, const char* name);
143herr_t Write_HDF5_Dataset(hid_t file_id, hid_t dataspace_id, float* dataset_buffer, const char* name);
144
145/* \brief After HDF5 reads data into a buffer, remap and write to grid buffer. */
146void Fill_Grid_From_HDF5_Buffer(int nx, int ny, int nz, int nx_real, int ny_real, int nz_real, int n_ghost,
147 Real* hdf5_buffer, Real* grid_buffer);
148
150void Write_Grid_HDF5_Field_CPU(Header H, hid_t file_id, Real* dataset_buffer, Real* grid_buffer, const char* name);
151
153void Write_Grid_HDF5_Field_GPU(Header H, hid_t file_id, Real* dataset_buffer, Real* device_hdf5_buffer,
154 Real* device_grid_buffer, const char* name);
155
156// From io/io_gpu.cu
157// Use GPU to pack source -> device_buffer, then copy device_buffer -> buffer,
158// then write HDF5 field
159void Write_HDF5_Field_3D(int nx, int ny, int nx_real, int ny_real, int nz_real, int n_ghost, hid_t file_id,
160 float* buffer, float* device_buffer, Real* source, const char* name, int mhd_direction = -1);
161void Write_HDF5_Field_3D(int nx, int ny, int nx_real, int ny_real, int nz_real, int n_ghost, hid_t file_id,
162 double* buffer, double* device_buffer, Real* source, const char* name, int mhd_direction = -1);
163#endif
Definition AttrRecorderInterface.h:36
virtual void record(const char *name, const char *val)=0
virtual void record_arr(const char *name, const double *arr, int length)=0
Class to create a the gravity object.
Definition grid3D.h:229
Manages each configured file-writer.
Definition WriterManager.h:45
void Write_Data(Grid3D &G, struct Parameters P, int nfile, const io::WriterManager &write_manager)
Definition io.cpp:82
std::string to_string_exact(T const &input)
Convert a floating point number to a string such that it can be exactly deserialized back from a stri...
Definition io.h:51
bool Is_Root_Proc()
Definition io.h:25
Definition grid3D.h:55
Definition global.h:217