libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
mpiDemoBuilder.cpp
Go to the documentation of this file.
1/* Copyright 2025 NTESS. See the top-level LICENSE.txt file for details.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5//
6// This demo shows how one might send messages from a parallel program.
7// Via ldms only.
8//
9#if ADC_BOOST_JSON_PUBLIC
10#include "boost/json/src.hpp"
11#endif
12#include "adc/factory.hpp"
13#include <cstring>
14#include <cerrno>
15#include <limits>
16#ifdef ADC_HAVE_MPI
17#include <mpi.h>
18#endif
19
20/*! \file mpiDemoBuilder.cpp
21 */
22
23
24/** \addtogroup examples
25 * @{
26 */
27/** \addtogroup mpi
28 * @{
29 */
30namespace adc_examples {
31namespace mpiDemoBuider {
32
33std::map<std::string, std::string> file_config =
34 {{ "DIRECTORY", "./test.outputs"},
35 { "FILE", "out.file.mpi.log" },
36 { "APPEND", "true" }
37 };
38
39std::map<std::string, std::string> ldmsd_stream_publish_config =
40 {
41 { "STREAM", "adc_publish_api" }
42 };
43
44int test_publisher(std::shared_ptr<adc::publisher_api> pi, std::shared_ptr< adc::builder_api> b, std::map<std::string, std::string>& pconfig ) {
45 std::cout << "------------------- begin --------------------" << std::endl;
46 int err = 0;
47 int e = 0;
48 err = pi->config(pconfig);
49 if (err) {
50 std::cout << "config failed " <<
51 std::strerror(err) << std::endl;
52 e += 1;
53 err = 0;
54 }
55 err = pi->initialize();
56 if (err) {
57 std::cout << "initialize failed " <<
58 std::strerror(err) << std::endl;
59 e += 1;
60 err = 0;
61 }
62 err = pi->publish(b); // there should be 1 b in the output
63 if (err) {
64 std::cout << "publish 1 failed " <<
65 std::strerror(err) << std::endl;
66 e += 1;
67 err = 0;
68 }
69 pi->pause();
70 err = pi->publish(b); // this publication should be silent in output
71 if (err) {
72 std::cout << "publish 2 failed " <<
73 std::strerror(err) << std::endl;
74 e += 1;
75 err = 0;
76 }
77 pi->resume();
78 err = pi->publish(b); // there should be 2 b in the output
79 if (err) {
80 std::cout << "publish 3 failed " <<
81 std::strerror(err) << std::endl;
82 e += 1;
83 err = 0;
84 }
85 pi->finalize();
86 std::cout << "------------------- end --------------------" << std::endl;
87 return e;
88}
89
90
91void populate_builder(std::shared_ptr<adc::builder_api> b, adc::factory & f) {
92
93 std::shared_ptr< adc::builder_api > app_data = f.get_builder();
94#if ADC_BOOST_JSON_PUBLIC
95 boost::json::object jo = {{"a","b"},{"C","d"},{"n",1}};
96#endif
97#ifdef ADC_HAVE_MPI
98 MPI_Comm comm = MPI_COMM_WORLD;
99 void *commptr = &comm;
100#else
101 void *commptr = NULL;
102#endif
103 b->add_mpi_section("mpi_comm_world", commptr, ADC_MPI_RANK | ADC_MPI_SIZE);
104
105 std::vector<std::string> children = { "uuid1", "uuid2", "uuid3"};
106 b->add_workflow_section();
107 b->add_workflow_children(children);
108
109 app_data->add("bool0", false);
110 app_data->add("bool1", true);
111 app_data->add("char1", 'A');
112 app_data->add("c16", u'¢');
113 // there are intentionally non-ascii characters in the next line.
114 app_data->add("c32", U'猫');
115 const std::string ccppstr("ccppstr");
116 app_data->add("ccppstr", ccppstr);
117 std::string cppstr("cppstr");
118 app_data->add("cppstr", cppstr);
119 // add null-terminated string
120 const char *cstr = "cstr_nul";
121 app_data->add("cstr1", cstr);
122 // add string which is serialized json.
123 app_data->add_json_string("jstr1", std::string ("{\"a\":\"b\", \"c\":[1,2, 3]}"));
124#if ADC_BOOST_JSON_PUBLIC
125 app_data->add("jsonobj", jo);
126#endif
127 uint8_t u8 = std::numeric_limits<uint8_t>::max();
128 uint16_t u16 = std::numeric_limits<uint16_t>::max();
129 uint32_t u32 = std::numeric_limits<uint32_t>::max();
130 uint64_t u64 = std::numeric_limits<uint64_t>::max();
131 app_data->add("u8", u8);
132 app_data->add("u16", u16);
133 app_data->add("u32", u32);
134 app_data->add("u64", u64);
135 int8_t i8 = std::numeric_limits<int8_t>::max();
136 int16_t i16 = std::numeric_limits<int16_t>::max();
137 int32_t i32 = std::numeric_limits<int32_t>::max();
138 int64_t i64 = std::numeric_limits<int64_t>::max();
139 app_data->add("i8", i8);
140 app_data->add("i16", i16);
141 app_data->add("i32", i32);
142 app_data->add("i64", i64);
143 app_data->add("flt", std::numeric_limits<float>::max());
144 app_data->add("dbl", std::numeric_limits<double>::max() );
145 app_data->add("fcplx", std::complex<float>(
146 std::numeric_limits<float>::max(),std::numeric_limits<float>::max()));
147 app_data->add("dcplx", std::complex<double>(
148 std::numeric_limits<double>::max(),std::numeric_limits<double>::max()));
149 const char *cstrings[] = {"a", "B", "c2"};
150 char *vcstrings[4];
151 int ia[4];
152 float fa[4];
153 double da[4];
154 unsigned ua[4];
155 uint64_t ua64[4];
156 for (int i = 0; i < 4; i++) {
157 vcstrings[i] = new char[2];
158 snprintf(vcstrings[i], 2, "%d", i);
159 ua[i] = i;
160 ua64[i] = std::numeric_limits<uint64_t>::max() - i;
161 ia[i] = -i;
162 da[i] = 3.14*i;
163 fa[i] = 3.14*i *2;
164 }
165 std::string cppstrings[] = {"ap", "Bp", "c2p"};
166 app_data->add_array("nulembed", "a\0b", 3);
167 app_data->add_array("cstrs", cstrings, 3);
168 app_data->add_array("cppstrs", cppstrings, 3);
169 app_data->add_array("vcstrs", vcstrings, 4);
170 for (int i = 0; i < 4; i++) {
171 delete [] vcstrings[i];
172 }
173 app_data->add_array("ia", ia, 4);
174 app_data->add_array("ua", ua, 4);
175 app_data->add_array("ua64", ua64, 4);
176 app_data->add_array("fa", fa, 4);
177 app_data->add_array("da", da, 4);
178
179 b->add_header_section("cxx_demo_1");
180
181 b->add_host_section(ADC_HS_ALL);
182
183 b->add_app_data_section(app_data);
184
185 b->add_memory_usage_section();
186
187 std::shared_ptr< adc::builder_api > code_details = f.get_builder();
188 // more here
189
190 std::shared_ptr< adc::builder_api > version = f.get_builder();
191 version->add("version", "1.1.3");
192 const char* tags[] = { "boca_raton", "saronida_2", "mpi"};
193 version->add_array("tags", tags ,2);
194 version->add("mesh_version", "5.0.0");
195
196 b->add_code_section("repartitioner", version, code_details);
197
198 std::shared_ptr< adc::builder_api > build_details = f.get_builder();
199 b->add_code_configuration_section(build_details);
200
201 std::shared_ptr< adc::builder_api > model_data = f.get_builder();
202 model_data->add("nx", 3);
203 model_data->add("ny", 10);
204 model_data->add("step", 0);
205 b->add_model_data_section(model_data);
206
207 std::shared_ptr< adc::builder_api > status_details = f.get_builder();
208 status_details->add("tmax", 15000.25);
209 status_details->add("tmax_loc", 10325);
210 status_details->add("step", 234);
211 b->add_exit_data_section(1,"we didn't succeed due to high temperatures", status_details);
212
213#if 0
214 std::string ss = b->serialize();
215 std::cout << "-------------------------------" << std::endl;
216 std::cout << ss << std::endl;
217 std::cout << "-------------------------------" << std::endl;
218#endif
219}
220
221/*! \brief test mpi, all data types, and several publishers
222 */
223#ifdef ADC_HAVE_MPI
224int main(int argc, char **argv)
225{
226 MPI_Init(&argc, &argv);
227#else
228int main(int , char **)
229{
230#endif
231 std::cout << "adc pub version: " << adc::publisher_api_version.name << std::endl;
232 std::cout << "adc builder version: " << adc::builder_api_version.name << std::endl;
233 std::cout << "adc enum version: " << adc::enum_version.name << std::endl;
234
235 adc::factory f;
236 std::shared_ptr< adc::builder_api > b = f.get_builder();
237
238 populate_builder(b, f);
239
240 // get publisher from the factory class.
241 std::shared_ptr< adc::publisher_api > p1 = f.get_publisher("file");
242 std::cout << test_publisher(p1, b, file_config) << std::endl;
243
244 std::shared_ptr< adc::publisher_api > p2 = f.get_publisher("stdout");
245 std::cout << test_publisher(p2, b, file_config) << std::endl;
246
247 std::shared_ptr< adc::publisher_api > p4 = f.get_publisher("ldmsd_stream_publish");
248 std::cout << test_publisher(p4, b, ldmsd_stream_publish_config) << std::endl;
249
250#ifdef ADC_HAVE_MPI
251 MPI_Finalize();
252#endif
253 return 0;
254}
255
256
257} // end mpiDemoBuider
258} // end adc_examples
259
260/** @} mpi*/
261/** @} examples*/
262
263int main(int argc, char **argv)
264{
265 return adc_examples::mpiDemoBuider::main(argc, argv);
266}
267
int main(int argc, char **argv)
provides publishers and builders of application metadata.
Definition factory.hpp:34
std::shared_ptr< publisher_api > get_publisher(const std::string &name)
Definition factory.ipp:178
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
version publisher_api_version("1.0.0", {"none"})
version builder_api_version("1.0.0", {"none"})
#define ADC_HS_ALL
all ADC_HS_* optional data included
Definition builder.hpp:73
#define ADC_MPI_RANK
include "mpi_rank" field from mpi_comm_rank
Definition builder.hpp:93
#define ADC_MPI_SIZE
include "mpi_size" field from mpi_comm_size
Definition builder.hpp:96
std::map< std::string, std::string > file_config
int main(int, char **)
test mpi, all data types, and several publishers
std::map< std::string, std::string > ldmsd_stream_publish_config
int test_publisher(std::shared_ptr< adc::publisher_api > pi, std::shared_ptr< adc::builder_api > b, std::map< std::string, std::string > &pconfig)
void populate_builder(std::shared_ptr< adc::builder_api > b, adc::factory &f)
const std::string name
Definition types.hpp:29