libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
adcHelloWorldMPI.cpp
Go to the documentation of this file.
1/* Copyright 2026 NTESS. See the top-level LICENSE.txt file for details.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "adc/adc.hpp"
6#include <cstdlib>
7#include <cstring>
8#include <cerrno>
9#include <limits>
10#include <mpi.h>
11
12/*! \file adcHelloWorldMPI.cpp
13 * This demonstrates using the adc::factory API to build and publish a message from
14 * all ranks of an mpi program.
15 * The message sent includes the bare minimum, plus hello world.
16 */
17
18/** \addtogroup examples
19 * @{
20 */
21
22namespace adc_examples {
23namespace adcHelloWorldMPI {
24
25/**
26 * \brief adc c++ hello world without hard-coded publisher choices.
27 */
28int main(int argc, char ** argv) {
29
30
31
32 MPI_Init(&argc, &argv);
33 MPI_Comm comm = MPI_COMM_WORLD;
34
35 // create a factory
37
38 // create a message and add header
39 std::shared_ptr< adc::builder_api > b = f.get_builder();
40 b->add_header_section("cxx_demo_1");
41
42 // add an application-defined payload to the message
43 auto app_data = f.get_builder();
44 app_data->add("hello", "world");
45 b->add_app_data_section(app_data);
46
47 // add environment chunks of interest on at least the first message in parallel production
48 b->add_host_section(ADC_HS_ALL);
49 b->add_slurm_section(); // or add_flux_section
50 b->add_mpi_section("world", &comm, ADC_MPI_LOCAL);
51
52 // could add lots of other sections, as needed.
53
54
55 // export rank from mpi to ADC_MULTIFILE_PLUGIN_RANK ?
56 // or create multipublisher manually?
57 int rank;
58 (void)MPI_Comm_rank(comm, &rank);
59 if (!rank) {
60 std::cout << "adc pub version: " << adc::publisher_api_version.name << std::endl;
61 std::cout << "adc builder version: " << adc::builder_api_version.name << std::endl;
62 std::cout << "adc enum version: " << adc::enum_version.name << std::endl;
63 }
64
65 std::string rank_str = std::to_string(rank);
66 setenv("ADC_MULTIFILE_PLUGIN_RANK", rank_str.c_str(), 1);
67
68 // create publishers following runtime environment variables and defaults
69 auto mp = f.get_multi_publisher_from_env("");
70
71 // send built message b to all publishers
72 int err = mp->publish(b);
73 if (err) {
74 std::cout << "got " << err << " publication errors." << std::endl;
75 }
76
77 // do some work
78
79 // send the final status; updating the header to get new timestamp and uuid
80 b->add_exit_data_section(0, "all good", nullptr);
81 b->add_header_section("cxx_demo_1");
82 err = mp->publish(b);
83 if (err) {
84 std::cout << "got " << err << " publication errors." << std::endl;
85 }
86
87 // clean up all publishers
88 mp->terminate();
89
90 // consolidate files; this could be done with a separate utility script
91 // if you don't like how the provided function does it.
92 MPI_Barrier(comm);
93 if (rank == 0) {
94 // may need to sleep here to give local fs a chance to catch up
95 // dir/user/[wfid.].host.Ppid.Tstarttime.pptr/application.Rrank.XXXXXX
96 // -->
97 // dir/user/consolidated.[wfid].adct-json.multi.xml
98 auto path = std::getenv("ADC_MULTIFILE_PLUGIN_DIRECTORY");
99 auto wfid = std::getenv("ADC_WFID");
100 auto pattern = adc::get_multifile_log_path(path, wfid);
101
102 std::vector< std::string > old_paths;
103 auto new_files = adc::consolidate_multifile_logs(pattern.c_str(), old_paths);
104 if (old_paths.size()) {
105 for (auto i : old_paths) {
106 std::cout << "consolidating from:" << i << std::endl;
107 if (false) {
108 std::remove(i.c_str()); // we could delete the merged files.
109 }
110 }
111 for (auto i : new_files) {
112 std::cout << "consolidated to:" << i << std::endl;
113 }
114 } else {
115 std::cout << "no consolidation done." << std::endl;
116 }
117 }
118
119 MPI_Finalize();
120
121 return 0;
122}
123
124} // adcHelloWorldMPI
125} // adc_examples
126
127/** @}*/
128/** @}*/
129
130int main(int argc, char **argv)
131{
132 return adc_examples::adcHelloWorldMPI::main(argc, argv);
133}
int main(int argc, char **argv)
provides publishers and builders of application metadata.
Definition factory.hpp:34
std::shared_ptr< multi_publisher_api > get_multi_publisher_from_env(const std::string &env_name)
Definition factory.ipp:121
std::shared_ptr< builder_api > get_builder()
Definition factory.ipp:300
version enum_version("1.0.0", {"none"})
the version number of enum scalar_type and object_type
ADC_VISIBLE std::string get_multifile_log_path(string_view dir, string_view wfid)
Definition utility.ipp:37
version publisher_api_version("1.0.0", {"none"})
version builder_api_version("1.0.0", {"none"})
ADC_VISIBLE std::vector< std::string > consolidate_multifile_logs(const std::string &match, std::vector< std::string > &old_paths, bool debug=false)
Definition utility.ipp:29
#define ADC_HS_ALL
all ADC_HS_* optional data included
Definition builder.hpp:73
#define ADC_MPI_LOCAL
include all mpi options that do not require collective work.
Definition builder.hpp:124
int main(int argc, char **argv)
adc c++ hello world without hard-coded publisher choices.
const std::string name
Definition types.hpp:29