ATLAS Offline Software
Loading...
Searching...
No Matches
MPIClusterSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "MPIClusterSvc.h"
5
6#include <mpi.h>
7
8#include <bit>
9#include <boost/serialization/variant.hpp>
10
11#include "CxxUtils/XXH.h"
12#include "GaudiKernel/FileIncident.h"
13
15 ATH_MSG_DEBUG("Initializing MPI");
16 m_env = std::make_unique<mpi3::environment>(mpi3::thread_level::multiple);
17 // Suggestion from Codex (GPT 5.6-sol)
18 if (m_env->thread_support() != mpi3::thread_level::multiple) {
19 ATH_MSG_ERROR("MPI_THREAD_MULTIPLE is required but unavailable");
20 return StatusCode::FAILURE;
21 }
22 ATH_MSG_DEBUG("Created MPI environment");
23
24 // Print version information
25 char version_string[MPI_MAX_LIBRARY_VERSION_STRING];
26 int version_string_len;
27 MPI_Get_library_version(version_string, &version_string_len);
28 ATH_MSG_INFO("Running on " << version_string);
29 m_world = m_env->world();
30
31 if (m_world.size() < 2) {
32 ATH_MSG_ERROR("Only have " << m_world.size()
33 << " ranks! This is insufficient!");
34 return StatusCode::FAILURE;
35 }
36 m_datacom =
37 m_world.duplicate(); // make a duplicate communicator for event data
38 ATH_MSG_DEBUG("Got MPI_COMM_WORLD");
39 m_rank = m_world.rank();
40 ATH_MSG_INFO("On MPI rank " << m_rank);
41 if (std::getenv("RANK") != std::to_string(m_rank)) {
42 const char* env_rank = std::getenv("RANK");
43 ATH_MSG_WARNING("MPI rank (" << m_rank
44 << ") does not match $RANK = " << env_rank);
45 }
46
47 ATH_CHECK(m_mpiLog.retrieve());
48 m_mpiLog->createStatement("PRAGMA foreign_keys = ON").run();
49
51 ->createStatement(
52 "CREATE TABLE ranks (rank INTEGER PRIMARY KEY, "
53 "node TEXT, start_time FLOAT, end_time FLOAT)")
54 .run();
56 ->createStatement(
57 "INSERT INTO ranks (rank, node, start_time) "
58 "VALUES(?1, ?2, julianday('now'))")
59 .run(m_rank, m_env->processor_name());
61 ->createStatement(
62 "CREATE TABLE files (fileId INTEGER PRIMARY KEY, fileName TEXT)")
63 .run();
65 ->createStatement(
66 "CREATE TABLE event_log (rank INTEGER, id INTEGER UNIQUE,"
67 "inputFileId INTEGER,"
68 "runNumber INTEGER, eventNumber INTEGER, complete INTEGER,"
69 "status INTEGER, request_time_ns INTEGER, start_time FLOAT,"
70 "end_time FLOAT, PRIMARY KEY (runNumber, eventNumber, id), "
71 "FOREIGN KEY (rank) REFERENCES ranks(rank),"
72 "FOREIGN KEY (inputFileId) REFERENCES files(fileId))")
73 .run();
74 m_mpiLog_addEvent = m_mpiLog->createStatement(
75 "INSERT INTO event_log(id, rank, inputFileId, runNumber, eventNumber, "
76 "complete, "
77 "start_time, request_time_ns) "
78 "VALUES(?1, ?4, ?6, ?2, ?3, 0, julianday('now'), ?5)");
79 m_mpiLog_completeEvent = m_mpiLog->createStatement(
80 "UPDATE event_log SET complete = 1, status = ?4, end_time = "
81 "julianday('now') WHERE runNumber = ?2 "
82 "AND eventNumber = ?3 AND id = ?1");
83 m_mpiLog_addFile = m_mpiLog->createStatement(
84 "INSERT INTO files (fileId, fileName) VALUES(?1, ?2)");
85
86 // Set up incident listener
87 ServiceHandle<IIncidentSvc> incsvc("IncidentSvc", this->name());
88 if (!incsvc.retrieve().isSuccess()) {
89 ATH_MSG_FATAL("Cannot get IncidentSvc.");
90 return (StatusCode::FAILURE);
91 }
92 incsvc->addListener(this, IncidentType::BeginInputFile, 100);
93 incsvc->addListener(this, IncidentType::BeginProcessing, 100);
94
95 return StatusCode::SUCCESS;
96}
97
100 ->createStatement(
101 "UPDATE ranks SET end_time = julianday('now') WHERE rank = ?1")
102 .run(m_rank);
103 return StatusCode::SUCCESS;
104}
105
107void MPIClusterSvc::handle(const Incident& inc) {
108 // Fill in slot map at start of every event
109 if (inc.type() == IncidentType::BeginProcessing) {
110 const std::size_t slot = Gaudi::Hive::currentContext().slot();
112 }
113
114 // Cache new input filename on start of every file
115 if (inc.type() == IncidentType::BeginInputFile) {
116 const FileIncident* fileInc = dynamic_cast<const FileIncident*>(&inc);
117 if (fileInc == nullptr) {
118 ATH_MSG_ERROR("BeginInputFile does not have a file name attached");
119 return;
120 }
121
122 const std::string fileName = fileInc->fileName();
123 // Convert the hash into a signed int64. Just a hash so this doesn't matter.
124 m_lastInputFileHash = static_cast<std::int64_t>(xxh3::hash64(fileName));
125 m_mpiLog_addFile.run(m_lastInputFileHash, std::move(fileName));
126 }
127 return;
128}
129
131 return m_world.size();
132}
133
135 return m_rank;
136}
137
139 ATH_MSG_DEBUG("Barrier on rank " << rank() << " of " << numRanks());
140 m_world.barrier();
141}
142
144 m_world.abort();
145}
146
148 ClusterComm communicator) {
149 ATH_MSG_DEBUG("Sending message from rank " << rank() << " to " << destRank);
150 // Don't send event request message if we're not the master *and* we have a
151 // message waiting.
152 // Probably an emergency stop message
153 if (m_rank != 0 && message.messageType == ClusterMessageType::RequestEvent &&
154 m_world.iprobe().has_value()) {
155 return;
156 }
157
158 // Select correct communicator
159 mpi3::communicator& comm =
160 (communicator == ClusterComm::EventData) ? m_datacom : m_world;
161 if (message.messageType == ClusterMessageType::Data &&
162 communicator != ClusterComm::EventData) {
164 "Event data should be sent with EventData communicator. "
165 "Dropping message");
166 return;
167 }
168
169 message.source = m_rank;
170 const auto& [header, body] = message.wire_msg();
171 comm.send_n(header.begin(), header.size(), destRank, 0);
172 if (body.has_value()) {
173 comm.send_n(body->begin(), body->size(), destRank, header[2]);
174 if (message.messageType == ClusterMessageType::Data) {
175 const ClusterMessage::WireMsgBody& bdy = *body;
176 // Decode the body to figure out what to send
177 char* ptr = reinterpret_cast<char*>((std::uint64_t(bdy[0]) << 32) +
178 std::uint64_t(bdy[1]));
179 std::size_t len = (std::uint64_t(bdy[2]) << 32) + std::uint64_t(bdy[3]);
180
181 // Offset the tag by 16384 to minimize chance of conflict
182 // (max tag in MPI spec is 32767)
183 constexpr int tag_offset = 16384;
184 comm.send_n(ptr, len, destRank, header[2] + tag_offset);
185 }
186 }
187}
188
190 // Same offset as line 114
191 constexpr int tag_offset = 16384;
192 constexpr std::uint64_t thirtytwo_ones = 0xFFFFFFFF;
193
194 // Select correct communicator
195 mpi3::communicator& comm =
196 (communicator == ClusterComm::EventData) ? m_datacom : m_world;
198 auto&& [head, body] = msg;
199 comm.receive_n(head.begin(), head.size());
200 // Only time we need to figure out ourselves whether there's a body
201 if (head[0] == int(ClusterMessageType::FinalWorkerStatus) ||
202 head[0] == int(ClusterMessageType::WorkerError) ||
203 head[0] == int(ClusterMessageType::Data)) {
205 comm.receive_n(body->begin(), body->size(), head[1], head[2]);
206 if (head[0] == int(ClusterMessageType::Data)) {
207 ClusterMessage::WireMsgBody& bdy = *body;
208 // Decode the body to figure out what to recieve
209 std::size_t len = (std::uint64_t(bdy[2]) << 32) + std::uint64_t(bdy[3]);
210 std::size_t align = (std::uint64_t(bdy[4]) << 32) + std::uint64_t(bdy[5]);
211 std::size_t alloc_size = len;
212 if (!std::has_single_bit(align)) {
213 ATH_MSG_WARNING("Alignment " << align << " is not a power of two!");
214 align = std::bit_ceil(align);
215 }
216 if (len % align != 0) {
217 ATH_MSG_WARNING("Length " << len << " is not a multiple of alignment "
218 << align << "!");
219 // Convert to next multiple by adding align - 1, then zeroing out those
220 // final bits
221 alloc_size = (len + align - 1) & ~(align - 1);
222 }
223
224 char* ptr = static_cast<char*>(std::aligned_alloc(align, alloc_size));
225 comm.receive_n(ptr, len, head[1], head[2] + tag_offset);
226
227 // update the pointer in the WireMsgBody
228 bdy[0] = int(std::uint64_t(ptr) >> 32);
229 bdy[1] = int(std::uint64_t(ptr) & thirtytwo_ones);
230 }
231 }
232 ClusterMessage message(msg);
233 ATH_MSG_DEBUG("Rank " << rank() << " received message from "
234 << message.source);
235 return message;
236}
237
238void MPIClusterSvc::log_addEvent(int eventIdx, std::int64_t run_number,
239 std::int64_t event_number,
240 std::int64_t request_time_ns,
241 std::size_t slot) {
242 m_mpiLog_addEvent.run(eventIdx, run_number, event_number, m_rank,
243 request_time_ns, m_inputFileSlotMap[slot]);
244}
245
246void MPIClusterSvc::log_completeEvent(int eventIdx, std::int64_t run_number,
247 std::int64_t event_number,
248 std::int64_t status) {
249 m_mpiLog_completeEvent.run(eventIdx, run_number, event_number, status);
250}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
ClusterComm
C++ native wrapper for the C xxhash API.
std::int64_t m_lastInputFileHash
virtual ClusterMessage waitReceiveMessage(ClusterComm communicator=ClusterComm::Default) override final
Block until we receive an MPI message.
virtual void barrier() override final
Insert a barrier No rank will continue until all ranks reach this point.
virtual void log_addEvent(int eventIdx, std::int64_t run_number, std::int64_t event_number, std::int64_t request_time_ns, std::size_t slot) override final
Add (begin) an event in the log.
SQLite::Statement m_mpiLog_addFile
std::unique_ptr< mpi3::environment > m_env
SQLite::Statement m_mpiLog_addEvent
virtual void handle(const Incident &inc) override
IIncidentListener handle.
virtual void abort() override final
Abort the MPI run.
virtual int rank() const override final
Return our rank.
mpi3::communicator m_world
virtual StatusCode initialize() override final
Initialize.
mpi3::communicator m_datacom
virtual StatusCode finalize() override final
Finalize.
virtual void log_completeEvent(int eventIdx, std::int64_t run_number, std::int64_t event_number, std::int64_t status) override final
Complete an event in the log.
SQLite::Statement m_mpiLog_completeEvent
virtual void sendMessage(int destRank, ClusterMessage message, ClusterComm communicator=ClusterComm::Default) override final
Send an MPI message.
ServiceHandle< ISQLiteDBSvc > m_mpiLog
virtual int numRanks() const override final
Return number of ranks.
std::map< std::size_t, std::int64_t > m_inputFileSlotMap
std::string head(std::string s, const std::string &pattern)
head of a string
std::uint64_t hash64(const void *data, std::size_t size)
Passthrough to XXH3_64bits.
Definition XXH.cxx:9
A class describing a message sent between nodes in a cluster.
std::tuple< WireMsgHdr, std::optional< WireMsgBody > > WireMsg
std::array< std::uint32_t, 10 > WireMsgBody
MsgStream & msg
Definition testRead.cxx:32