Alamo
WriteMetaData.cpp
Go to the documentation of this file.
1#include "WriteMetaData.H"
2#include <cstdlib>
3#include <sstream>
4#include <functional>
5#include "Util/Util.H"
6
7/// \todo We need to update ParmParse so that the FORMATTED input file gets recorded permanently.
8
9namespace
10{
11
12const char* const slurm_environment_variables[] = {
13 "SLURM_JOB_ID",
14 "SLURM_JOB_NAME",
15 "SLURM_CLUSTER_NAME",
16 "SLURM_JOB_ACCOUNT",
17 "SLURM_JOB_PARTITION",
18 "SLURM_JOB_QOS",
19 "SLURM_JOB_RESERVATION",
20 "SLURM_ARRAY_JOB_ID",
21 "SLURM_ARRAY_TASK_ID",
22 "SLURM_ARRAY_TASK_COUNT",
23 "SLURM_SUBMIT_HOST",
24 "SLURM_SUBMIT_DIR",
25 "SLURM_JOB_NODELIST",
26 "SLURM_JOB_NUM_NODES",
27 "SLURM_NTASKS",
28 "SLURM_NTASKS_PER_NODE",
29 "SLURM_TASKS_PER_NODE",
30 "SLURM_DISTRIBUTION",
31 "SLURM_JOB_CPUS_PER_NODE",
32 "SLURM_CPUS_PER_TASK",
33 "SLURM_CPUS_ON_NODE",
34 "SLURM_MEM_PER_NODE",
35 "SLURM_MEM_PER_CPU",
36 "SLURM_GPUS",
37 "SLURM_GPUS_PER_NODE",
38 "SLURM_GPUS_PER_TASK",
39 "SLURM_JOB_GPUS",
40 "SLURM_TRES_PER_TASK",
41 "SLURM_STEP_ID",
42 "SLURM_STEP_NUM_TASKS",
43 "SLURM_RESTART_COUNT",
44 "SLURM_JOB_START_TIME",
45 "SLURM_JOB_END_TIME",
46 "SLURM_HET_SIZE"
47};
48
49void WriteSlurmVariables(std::ostream &metadatafile)
50{
51 for (const char *variable : slurm_environment_variables)
52 {
53 const char *value = std::getenv(variable);
54 if (value != nullptr && value[0] != '\0')
55 metadatafile << variable << " = " << value << std::endl;
56 }
57}
58
59}
60
61namespace IO
62{
63
64// GLOBAL VARIABLES
65unsigned long hash = 0;
66std::chrono::time_point<std::chrono::system_clock> starttime_cr;
67std::time_t starttime = 0;
68int percent = -1;
69
70void WriteMetaData(std::string plot_file, Status status, int per)
71{
72 if (amrex::ParallelDescriptor::IOProcessor())
73 {
74 amrex::ParmParse pp;
75
76 std::chrono::time_point<std::chrono::system_clock> now_cr = std::chrono::system_clock::now();
77 std::time_t now = std::chrono::system_clock::to_time_t(now_cr);
78
79 if (!starttime)
80 {
81 starttime_cr = now_cr;
82 starttime = now;
83 }
84
85 if (!hash)
86 {
87 std::stringstream ss;
88 pp.dumpTable(ss);
89 std::string dumptable = ss.str();
90 hash = std::hash<std::string>{}(dumptable + std::ctime(&starttime));
91 }
92
93 if (per > percent) percent = per;
94
95 std::ofstream metadatafile;
96 metadatafile.open(plot_file+"/metadata",std::ios_base::out);
97 metadatafile << "# COMPILATION DETAILS" << std::endl;
98 metadatafile << "# ===================" << std::endl;
99 metadatafile << "Git_commit_hash = " << METADATA_GITHASH << std::endl;
100 metadatafile << "AMReX_version = " << amrex::Version() << std::endl;
101 metadatafile << "Dimension = " << AMREX_SPACEDIM << std::endl;
102 metadatafile << "User = " << METADATA_USER << std::endl;
103 metadatafile << "Platform = " << METADATA_PLATFORM << std::endl;
104 metadatafile << "Compiler = " << METADATA_COMPILER << std::endl;
105 metadatafile << "Compilation_Date = " << METADATA_DATE << std::endl;
106 metadatafile << "Compilation_Time = " << METADATA_TIME << std::endl;
107 metadatafile << std::endl;
108
109 metadatafile << "# PARAMETERS" << std::endl;
110 metadatafile << "# ==========" << std::endl;
111
112 pp.dumpTable(metadatafile,true);
113
114 metadatafile << "# RUN DETAILS" << std::endl;
115 metadatafile << "# ===========" << std::endl;
116 metadatafile << "HASH = " << hash << std::endl;
117 {
118 char buffer [80];
119 std::strftime(buffer,80,"%Y-%m-%d %H:%M:%S",std::localtime(&starttime));
120 std::string timefmt(buffer);
121 metadatafile << "Simulation_start_time = " << timefmt << std::endl;
122 }
123 metadatafile << "Number_of_processors = " << amrex::ParallelDescriptor::NProcs() << std::endl;
124 if (status == Status::Running) metadatafile << "Status = Running";
125 else if (status == Status::Complete) metadatafile << "Status = Complete";
126 else if (status == Status::Abort) metadatafile << "Status = Abort";
127 else if (status == Status::Segfault) metadatafile << "Status = Segfault";
128 else if (status == Status::Interrupt) metadatafile << "Status = Interrupt";
129
130 if (percent>0) metadatafile << " (" << percent << "%)";
131 metadatafile << std::endl;
132
133 if (status != Status::Running)
134 {
135 char buffer[80];
136 std::strftime(buffer,80,"%Y-%m-%d %H:%M:%S",std::localtime(&now));
137 std::string timefmt(buffer);
138 metadatafile << "Simulation_end_time = " << timefmt << std::endl;
139 }
140
141 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now_cr - starttime_cr);
142 metadatafile << "Simulation_run_time = " << (float)milliseconds.count()/1000.0 << " " << std::endl;
143
144 WriteSlurmVariables(metadatafile);
145
146 #ifdef GIT_DIFF_OUTPUT
147 {
148 std::ifstream src(GIT_DIFF_OUTPUT,std::ios::binary);
149 std::ofstream dst(plot_file+"/diff.html",std::ios::binary);
150 dst << src.rdbuf();
151 src.close();
152 dst.close();
153 }
154 #endif
155 {
156 std::ifstream src(GIT_DIFF_PATCH_OUTPUT,std::ios::binary);
157 if (src.is_open())
158 {
159 std::ofstream dst(plot_file+"/diff.patch",std::ios::binary);
160 dst << src.rdbuf();
161 src.close();
162 dst.close();
163 }
164 else
165 {
166 Util::Warning(INFO,"Could not open ",GIT_DIFF_PATCH_OUTPUT);
167 }
168
169 }
170
171 metadatafile.close();
172 }
173}
174
175}
#define INFO
Definition Util.H:24
void WriteMetaData(std::string plot_file, Status status, int per)
unsigned long hash
int percent
@ Segfault
@ Complete
@ Running
@ Interrupt
@ Abort
std::chrono::time_point< std::chrono::system_clock > starttime_cr
std::time_t starttime
void Warning(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:213