← Documentation
ADS-IDAC Engine 5.0.0
ParallelGlobal.h
Go to the documentation of this file.
1
4
5#ifndef PARALLELGLOBAL_H
6#define PARALLELGLOBAL_H
7
8#include "../Global/SimulationContext.h"
9#include <fstream>
10#include <sstream>
11#include <filesystem>
12#include <iostream>
13#include <cstdlib>
14#ifdef __APPLE__
15#include <mach/mach.h>
16#endif
17#include <lz4.h>
18#include <cstring>
19#include <boost/serialization/vector.hpp>
20#include <boost/serialization/string.hpp>
21
22// ---- Fortran callback declarations (used by both Server and Client) ----
23extern "C" void interfc_();
24extern "C" void actonce_();
25extern "C" void secini_();
26extern "C" void actte_();
27extern "C" void restartforanewsequence_();
28extern "C" void initiationforrestart_();
29extern "C" void mutiproactonce_();
30extern "C" void setrnode_();
31extern "C" { void relap5_(); }
32
33namespace fs = std::filesystem;
34
36inline std::string restart_path(const std::string& name) {
37 return g_sim.config.memory_restart ? (g_sim.config.shm_prefix + name) : name;
38}
39
41inline long get_rss_mb() {
42#if defined(__linux__)
43 long rss = 0;
44 std::ifstream stat("/proc/self/status");
45 std::string line;
46 while (std::getline(stat, line)) {
47 if (line.rfind("VmRSS:", 0) == 0) {
48 std::istringstream iss(line.substr(6));
49 iss >> rss; // in kB
50 return rss / 1024;
51 }
52 }
53 return 0;
54#elif defined(__APPLE__)
55 struct mach_task_basic_info info;
56 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
57 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
58 (task_info_t)&info, &count) == KERN_SUCCESS)
59 return static_cast<long>(info.resident_size / (1024 * 1024));
60 return 0;
61#else
62 return 0;
63#endif
64}
65
69 if (!g_sim.config.memory_restart) return false;
70 if (g_sim.config.memory_limit_mb == 0) return true;
72}
73
75inline std::vector<char> lz4_compress(const char* data, int size) {
76 int max_dst = LZ4_compressBound(size);
77 std::vector<char> out(sizeof(int32_t) + max_dst);
78 int32_t orig = size;
79 std::memcpy(out.data(), &orig, sizeof(int32_t));
80 int comp_sz = LZ4_compress_default(data, out.data() + sizeof(int32_t), size, max_dst);
81 out.resize(sizeof(int32_t) + comp_sz);
82 // resize() never shrinks capacity: without shrink_to_fit() a compressed
83 // buffer would keep its full LZ4_compressBound allocation and forfeit the
84 // RSS benefit of --compress-queue.
85 out.shrink_to_fit();
86 return out;
87}
88
90inline std::vector<char> lz4_decompress(const char* data, int size) {
91 int32_t orig;
92 std::memcpy(&orig, data, sizeof(int32_t));
93 std::vector<char> out(orig);
94 int n = LZ4_decompress_safe(data + sizeof(int32_t), out.data(), size - (int)sizeof(int32_t), orig);
95 if (n < 0 || n != orig) {
96 // Feeding a corrupt buffer into the RELAP5 membuf would restart the
97 // solver from garbage state, so exit instead.
98 std::cerr << "FATAL: LZ4 decompression failed (got " << n << ", expected "
99 << orig << " bytes) — restart buffer is corrupt or was written "
100 << "with a different --compress-queue setting." << std::endl;
101 std::exit(1);
102 }
103 return out;
104}
105
107inline void cleanup_fort_temps() {
108 std::error_code ec;
109 for (auto& entry : fs::directory_iterator(".", ec)) {
110 auto fname = entry.path().filename().string();
111 if (fname.size() > 4 && fname.substr(0, 4) == "fort" && fname != "fort.13")
112 fs::remove(entry.path(), ec);
113 }
114}
115
120 double branch_time;
121 double end_time;
124 template<class Archive>
125 void serialize(Archive& ar, unsigned int) {
128 }
129};
130
133
136 double time = 0.0;
137 std::string statement;
138 int pe_id = -999;
139 template<class Archive>
140 void serialize(Archive& ar, unsigned int) { ar & time & statement & pe_id; }
141};
142
145 int sequence_id = -1;
149 double time = 0.0;
150 double probability = 0.0;
151 template<class Archive>
152 void serialize(Archive& ar, unsigned int) {
155 }
156};
157
160 int sequence_id = -1;
161 std::vector<SequenceOutputEvent> events;
163 template<class Archive>
164 void serialize(Archive& ar, unsigned int) {
166 }
167};
168
171namespace PG {
172 inline int rank;
173 inline int size;
174 inline bool log = true;
175
177 enum {
193 };
194
195}
196
197#endif //PARALLELGLOBAL_H
void secini_()
Security initialization callback.
Definition: ADSIDAC.cpp:586
std::vector< char > lz4_compress(const char *data, int size)
LZ4-compress a buffer with a 4-byte original-size header.
void initiationforrestart_()
Initialize restart mode for next sequence.
Definition: ADSIDAC.cpp:610
void interfc_()
Called every RELAP5 timestep from dtstep.f. Triggers crew/diagnosis decisions.
Definition: ADSIDAC.cpp:574
bool should_use_memory_restart()
Decide whether to use memory mode for this restart operation. memory_limit_mb == 0 → unlimited (alway...
bool viz_outputs_any()
True if any viz output streaming is enabled.
std::string restart_path(const std::string &name)
Get restart file path (shm or local).
void setrnode_()
Set restart node information in r5pars_.
Definition: ADSIDAC.cpp:618
std::vector< char > lz4_decompress(const char *data, int size)
LZ4-decompress a buffer that has a 4-byte original-size header.
long get_rss_mb()
Get current process RSS in MB (Linux + macOS, returns 0 elsewhere).
void restartforanewsequence_()
Find next unexplored branch, generate indeck.i restart input.
Definition: ADSIDAC.cpp:606
void actonce_()
Execute crew actions once per transient.
Definition: ADSIDAC.cpp:592
void relap5_()
void mutiproactonce_()
Definition: ADSIDAC.cpp:614
void actte_()
Execute crew actions at each timestep.
Definition: ADSIDAC.cpp:580
void cleanup_fort_temps()
Clean up Fortran temporary files (fort*) left by RELAP5.
SimulationContext g_sim
Global simulation context: the single instance, defined in SimulationContext.cpp.
Parallel globals namespace — MPI rank/size and message tag definitions.
bool log
Enable/disable rank-specific logging.
@ client_send_to_peer
Server -> Client A: send held data to another client.
@ server_branch_meta
Client A -> Server: branch metadata only (rank-local branch id + num_branches)
@ server_branch_this
Unused placeholder (occupies tag value 0)
@ client_run_held
Server -> Client A: run task using own held data.
@ client_peer_assign
Server -> Client A: peer assignment list for direct sends.
@ client_shutdown
Server -> Client: no more work, shut down.
@ server_done_simulating
Client -> Server: empty completion signal, client is free. The server handles it; Client....
@ server_done_with_report
Client -> Server: sequence completed with SequenceReport payload.
@ client_debug_resume
Unused (no sender or handler)
@ server_send_complete
Client -> Server: data send finished, client is free again.
@ client_simulate_this
Server -> Client: run the initial (first) sequence.
@ client_restart_run
Server -> Client: run a pending task of a resumed checkpoint with server-provided data.
@ client_peer_restart
Client A -> Client B: direct peer-to-peer restart data.
@ client_checkpoint
Server -> Client: send held_data for checkpoint gather.
@ server_output_data
Client -> Server: SequenceOutputData (before report)
int size
Total number of MPI processes.
int rank
MPI rank of this process.
Per-sequence output payload sent from client to server via MPI.
void serialize(Archive &ar, unsigned int)
std::vector< SequenceOutputEvent > events
SequenceOutputEndState end_state
Lightweight end state record (extracted from BranchEndState).
std::string consequence_description
void serialize(Archive &ar, unsigned int)
Lightweight PE event record (extracted from AnalysisInfoContainerUnit).
void serialize(Archive &ar, unsigned int)
int pe_id
associated_branch_ID → PE_N
Report sent by client when a sequence completes (for DET visualization).
double branch_time
Sim time when parent branched to create this sequence.
int parent_sequence_id
-1 for root (sequence 0)
int num_branches_generated
Children spawned during this sequence.
void serialize(Archive &ar, unsigned int)
double end_time
Sim time at completion.
int worker_rank
Which MPI rank ran it.
std::string shm_prefix
Path prefix for memory-restart files: "/dev/shm/adsidac_<pid>_" when /dev/shm exists,...
Definition: SimConfig.h:26
long memory_limit_mb
–memory-limit [MB]: 0 = unlimited; >0 = keep a restart buffer in memory only while process RSS is bel...
Definition: SimConfig.h:21
bool viz_output_end_states
Stream end states to viz server (–viz-outputs=es or all)
Definition: SimConfig.h:35
bool viz_output_events
Stream PE events to viz server (–viz-outputs=events or all)
Definition: SimConfig.h:34
bool memory_restart
In-memory restart. parseOptions() sets it to !–no-memory-restart, so it is TRUE in every run unless –...
Definition: SimConfig.h:20