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