Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation and function to make debugging multiple MPI ranks easier #6288

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/DevGuide/DebuggingTips.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ See LICENSE.txt for details.

Learn how to use a debugger such as gdb.

You can debug MPI executables using the `sys::attach_debugger()` function. See
the documentation of that function for details.

# Useful gdb commands

- To break when an exception is thrown `catch throw`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
#include "Evolution/Systems/GeneralizedHarmonic/ConstraintDamping/RegisterDerivedWithCharm.hpp"
#include "Parallel/CharmMain.tpp"
#include "Utilities/Serialization/RegisterDerivedClassesWithCharm.hpp"
#include "Utilities/System/AttachDebugger.hpp"

extern "C" void CkRegisterMainModule() {
Parallel::charmxx::register_main_module<EvolutionMetavars>();
Parallel::charmxx::register_init_node_and_proc(
{&domain::creators::register_derived_with_charm,
{&sys::attach_debugger,
&domain::creators::register_derived_with_charm,
&domain::creators::time_dependence::register_derived_with_charm,
&domain::FunctionsOfTime::register_derived_with_charm,
&gh::BoundaryCorrections::register_derived_with_charm,
Expand Down
34 changes: 34 additions & 0 deletions src/Utilities/System/AttachDebugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Utilities/System/AttachDebugger.hpp"

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <unistd.h>

namespace sys {
void attach_debugger() {
const char* env_enable_parallel_debug =
// NOLINTNEXTLINE(concurrency-mt-unsafe)
std::getenv("SPECTRE_ATTACH_DEBUGGER");
if (env_enable_parallel_debug != nullptr) {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
char hostname[2048];
gethostname(static_cast<char*>(hostname), sizeof(hostname));

const std::string output_info =
std::string{"pid:"} + std::to_string(getpid()) + std::string{":host:"} +
std::string{static_cast<char*>(hostname)} + "\n";
std::cout << output_info << std::flush;
// NOLINTNEXTLINE(misc-const-correctness)
volatile int i = 10;
while (i == 10) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(std::chrono::seconds{i});
}
}
}
} // namespace sys
28 changes: 28 additions & 0 deletions src/Utilities/System/AttachDebugger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

namespace sys {
/*!
* \brief Provide an infinite loop to attach a debugger during startup. Useful
* for debugging MPI runs.
*
* Each MPI rank writes a file name `spectre_pid_#_host_NAME` to the working
* directory. This allows you to attach GDB to the running process using
* `gdb --pid=PID`, once for each MPI rank. You must then halt the program
* using `C-c` and then call `set var i = 7` inside GDB. Once you've done this
* on each MPI rank, you can have each MPI rank `continue`.
*
* To add support for attaching to a debugger in an executable, you must add
* `sys::attach_debugger` to the
* `Parallel::charmxx::register_init_node_and_proc` init node functions. Then,
* when you launch the executable launch it as
* ```shell
* SPECTRE_ATTACH_DEBUGGER=1 mpirun -np N ...
* ```
* The environment variable `SPECTRE_ATTACH_DEBUGGER` being set tells the code
* to allow attaching from a debugger.
*/
void attach_debugger();
} // namespace sys
2 changes: 2 additions & 0 deletions src/Utilities/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spectre_target_sources(
${LIBRARY}
PRIVATE
Abort.cpp
AttachDebugger.cpp
Exit.cpp
ParallelInfo.cpp
Prefetch.cpp
Expand All @@ -19,6 +20,7 @@ spectre_target_headers(
INCLUDE_DIRECTORY ${CMAKE_SOURCE_DIR}/src
HEADERS
Abort.hpp
AttachDebugger.hpp
Exit.hpp
ParallelInfo.hpp
Prefetch.hpp
Expand Down
1 change: 1 addition & 0 deletions tools/CheckFiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ iostream() {
is_c++ "$1" \
&& whitelist "$1" \
'tests/Unit/IO/Exporter/BundledExporter/Test_BundledExporter.cpp$' \
'src/Utilities/System/AttachDebugger.cpp' \
&& grep -q '#include <iostream>' "$1"
}
iostream_report() {
Expand Down
Loading