Skip to content

Commit fa6f859

Browse files
authored
Simplify ConfigManager option specs (#638)
1 parent 97ab08d commit fa6f859

16 files changed

+497
-744
lines changed

include/caliper/ConfigManager.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,19 @@ class ConfigManager
116116
/// according to the selected options
117117
void update_channel_metadata(info_map_t& metadata) const;
118118

119-
/// \brief Returns a CalQL query based on the fields in \a input
120-
/// and option list.
119+
/// \brief Returns a CalQL query based on the query \a in
120+
/// and the selected options.
121121
///
122-
/// Construct a CalQL query for the option list. Example:
122+
/// Construct a CalQL query for the selected options. Example:
123123
/// \code
124-
/// auto query = opts.build_query("cross", {
125-
/// { "select", "sum(inclusive#sum#time.duration) as Total unit sec" },
126-
/// { "group by", "prop:nested" },
127-
/// { "format", "tree" }
128-
/// });
124+
/// auto query = opts.build_query("cross",
125+
/// "select sum(inclusive#sum#time.duration.ns) as Time unit sec group by path format tree");
129126
/// \endcode
130127
///
131128
/// \param level The aggreatation level ("local" or "cross")
132129
/// \param in Base CalQL clauses as needed by the controller
133130
/// \return Complete CalQL query statement
134-
std::string build_query(const char* level, const std::map<std::string, std::string>& in) const;
131+
std::string build_query(const char* level, const std::string& in) const;
135132

136133
friend class ConfigManager;
137134
};

src/caliper/ConfigManager.cpp

+50-69
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,18 @@ std::string join_stringlist(const std::vector<std::string>& list)
141141
return ret;
142142
}
143143

144-
void join_stringlist(std::string& in, const std::vector<std::string>& list)
144+
std::string join_stringlist(const std::vector<StringConverter>& list)
145145
{
146-
for (const auto& s : list) {
147-
if (!in.empty())
148-
in.append(",");
149-
in.append(s);
146+
std::string ret;
147+
int c = 0;
148+
149+
for (const StringConverter& sc : list) {
150+
if (c++ > 0)
151+
ret.append(",");
152+
ret.append(sc.to_string());
150153
}
151-
}
152154

153-
std::string find_or(const std::map<std::string, std::string>& m, const std::string& k, const std::string v = "")
154-
{
155-
auto it = m.find(k);
156-
return it != m.end() ? it->second : v;
155+
return ret;
157156
}
158157

159158
std::string expand_variables(const std::string& in, const std::string& val)
@@ -340,15 +339,6 @@ void add_metadata(const std::string& args, info_map_t& info)
340339

341340
class ConfigManager::OptionSpec
342341
{
343-
struct query_arg_t {
344-
std::vector<std::string> select;
345-
std::vector<std::string> groupby;
346-
std::vector<std::string> let;
347-
std::vector<std::string> where;
348-
std::vector<std::string> aggregate;
349-
std::vector<std::string> orderby;
350-
};
351-
352342
struct option_spec_t {
353343
std::string type;
354344
std::string description;
@@ -357,7 +347,7 @@ class ConfigManager::OptionSpec
357347
std::vector<std::string> services;
358348
std::vector<std::string> inherited_specs;
359349

360-
std::map<std::string, query_arg_t> query_args;
350+
std::map<std::string, std::string> query;
361351
std::map<std::string, std::string> config;
362352
};
363353

@@ -372,8 +362,10 @@ class ConfigManager::OptionSpec
372362
m_error_msg = msg;
373363
}
374364

375-
void parse_select(const std::vector<StringConverter>& list, query_arg_t& qarg)
365+
std::string parse_select(const std::vector<StringConverter>& list)
376366
{
367+
std::string ret;
368+
377369
for (const StringConverter& sc : list) {
378370
bool is_a_dict = false;
379371
std::map<std::string, StringConverter> dict = sc.rec_dict(&is_a_dict);
@@ -397,50 +389,58 @@ class ConfigManager::OptionSpec
397389
str.append(it->second.to_string());
398390
str.append("\"");
399391
}
400-
qarg.select.push_back(str);
392+
if (!ret.empty())
393+
ret.append(",");
394+
ret.append(str);
401395
} else {
402-
qarg.select.push_back(sc.to_string());
403-
}
396+
if (!ret.empty())
397+
ret.append(",");
398+
ret.append(sc.to_string());
399+
}
404400
}
401+
402+
return ret.empty() ? ret : std::string(" select ") + ret;
405403
}
406404

407405
void parse_query_args(const std::vector<StringConverter>& list, option_spec_t& opt)
408406
{
407+
// parses the deprecated list-of-dicts form of the query args
408+
409409
for (const StringConverter& sc : list) {
410410
std::map<std::string, StringConverter> dict = sc.rec_dict();
411-
query_arg_t qarg;
411+
std::string query;
412412

413413
auto it = dict.find("group by");
414414
if (it != dict.end())
415-
qarg.groupby = ::to_stringlist(it->second.rec_list());
415+
query.append(" group by ").append(::join_stringlist(it->second.rec_list()));
416416

417417
it = dict.find("let");
418418
if (it != dict.end())
419-
qarg.let = ::to_stringlist(it->second.rec_list());
419+
query.append(" let ").append(::join_stringlist(it->second.rec_list()));
420420

421421
it = dict.find("where");
422422
if (it != dict.end())
423-
qarg.where = ::to_stringlist(it->second.rec_list());
423+
query.append(" where ").append(::join_stringlist(it->second.rec_list()));
424424

425425
it = dict.find("aggregate");
426426
if (it != dict.end())
427-
qarg.aggregate = ::to_stringlist(it->second.rec_list());
427+
query.append(" aggregate ").append(::join_stringlist(it->second.rec_list()));
428428

429429
it = dict.find("order by");
430430
if (it != dict.end())
431-
qarg.orderby = ::to_stringlist(it->second.rec_list());
431+
query.append(" order by ").append(::join_stringlist(it->second.rec_list()));
432432

433433
it = dict.find("select");
434434
if (it != dict.end())
435-
parse_select(it->second.rec_list(), qarg);
435+
query.append(parse_select(it->second.rec_list()));
436436

437437
it = dict.find("level");
438438
if (it == dict.end()) {
439439
set_error(": query arg: missing \"level\"");
440440
continue;
441441
}
442442

443-
opt.query_args[it->second.to_string()] = qarg;
443+
opt.query[it->second.to_string()] = std::move(query);
444444
}
445445
}
446446

@@ -468,8 +468,17 @@ class ConfigManager::OptionSpec
468468
if (ok && !m_error && it != dict.end())
469469
parse_config(it->second.rec_dict(&ok), opt);
470470
it = dict.find("query");
471-
if (ok && !m_error && it != dict.end())
472-
parse_query_args(it->second.rec_list(&ok), opt);
471+
if (ok && !m_error && it != dict.end()) {
472+
bool is_dict = false;
473+
auto query_dict = it->second.rec_dict(&is_dict);
474+
if (is_dict) {
475+
for (const auto &query_entry : query_dict)
476+
opt.query[query_entry.first] = query_entry.second.to_string();
477+
} else {
478+
// try parsing deprecated form of query args
479+
parse_query_args(it->second.rec_list(&ok), opt);
480+
}
481+
}
473482
it = dict.find("type");
474483
if (ok && !m_error && it != dict.end())
475484
opt.type = it->second.to_string();
@@ -691,50 +700,22 @@ struct ConfigManager::Options::OptionsImpl {
691700
}
692701
}
693702

694-
std::string build_query(const char* level, const std::map<std::string, std::string>& in) const
703+
std::string build_query(const char* level, const std::string& in) const
695704
{
696-
std::string q_let = ::find_or(in, "let");
697-
std::string q_select = ::find_or(in, "select");
698-
std::string q_groupby = ::find_or(in, "group by");
699-
std::string q_where = ::find_or(in, "where");
700-
std::string q_aggregate = ::find_or(in, "aggregate");
701-
std::string q_orderby = ::find_or(in, "order by");
702-
std::string q_format = ::find_or(in, "format");
705+
std::string ret = in;
703706

704707
for (const std::string& opt : enabled_options) {
705708
auto s_it = spec.data.find(opt);
706709
if (s_it == spec.data.end())
707710
continue;
708711

709-
auto l_it = s_it->second.query_args.find(level);
710-
if (l_it != s_it->second.query_args.end()) {
711-
const auto& q = l_it->second;
712-
::join_stringlist(q_let, q.let);
713-
::join_stringlist(q_select, q.select);
714-
::join_stringlist(q_groupby, q.groupby);
715-
::join_stringlist(q_where, q.where);
716-
::join_stringlist(q_aggregate, q.aggregate);
717-
::join_stringlist(q_orderby, q.orderby);
712+
auto l_it = s_it->second.query.find(level);
713+
if (l_it != s_it->second.query.end()) {
714+
ret.append(" ");
715+
ret.append(l_it->second);
718716
}
719717
}
720718

721-
std::string ret;
722-
723-
if (!q_let.empty())
724-
ret.append(" let ").append(q_let);
725-
if (!q_select.empty())
726-
ret.append(" select ").append(q_select);
727-
if (!q_groupby.empty())
728-
ret.append(" group by ").append(q_groupby);
729-
if (!q_where.empty())
730-
ret.append(" where ").append(q_where);
731-
if (!q_aggregate.empty())
732-
ret.append(" aggregate ").append(q_aggregate);
733-
if (!q_orderby.empty())
734-
ret.append(" order by ").append(q_orderby);
735-
if (!q_format.empty())
736-
ret.append(" format ").append(q_format);
737-
738719
return ret;
739720
}
740721

@@ -858,7 +839,7 @@ void ConfigManager::Options::update_channel_metadata(info_map_t& metadata) const
858839
mP->update_channel_metadata(metadata);
859840
}
860841

861-
std::string ConfigManager::Options::build_query(const char* level, const std::map<std::string, std::string>& in) const
842+
std::string ConfigManager::Options::build_query(const char* level, const std::string& in) const
862843
{
863844
return mP->build_query(level, in);
864845
}

src/caliper/controllers/CudaActivityProfileController.cpp

+12-18
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class CudaActivityProfileController : public cali::ChannelController
2727
const char* name,
2828
const config_map_t& initial_cfg,
2929
const ConfigManager::Options& opts,
30-
const std::string& format
30+
const std::string& format_spec
3131
)
3232
: ChannelController(name, 0, initial_cfg)
3333
{
3434
std::string output(opts.get("output", "cuda_profile"));
3535

3636
if (output != "stdout" && output != "stderr") {
3737
auto pos = output.find_last_of('.');
38-
std::string ext = (format == "cali" ? ".cali" : ".json");
38+
std::string ext = (format_spec == "cali" ? ".cali" : ".json");
3939

4040
if (pos == std::string::npos || output.substr(pos) != ext)
4141
output.append(ext);
@@ -56,30 +56,24 @@ class CudaActivityProfileController : public cali::ChannelController
5656
config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3");
5757
}
5858

59+
std::string query =
60+
" select *,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec"
61+
",scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec"
62+
" group by path,cupti.kernel.name,cupti.activity.kind,mpi.rank "
63+
" format ";
64+
65+
query.append(format_spec);
66+
5967
if (use_mpi) {
6068
config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport");
6169
config()["CALI_AGGREGATE_KEY"] = "*,mpi.rank";
6270
config()["CALI_MPIREPORT_FILENAME"] = output;
6371
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
64-
config()["CALI_MPIREPORT_CONFIG"] = opts.build_query(
65-
"local",
66-
{ { "select",
67-
"*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec"
68-
" ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" },
69-
{ "group by", "path,cupti.kernel.name,cupti.activity.kind,mpi.rank" },
70-
{ "format", format } }
71-
);
72+
config()["CALI_MPIREPORT_CONFIG"] = opts.build_query("local", query);
7273
} else {
7374
config()["CALI_SERVICES_ENABLE"].append(",report");
7475
config()["CALI_REPORT_FILENAME"] = output;
75-
config()["CALI_REPORT_CONFIG"] = opts.build_query(
76-
"local",
77-
{ { "select",
78-
"*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec"
79-
" ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" },
80-
{ "group by", "path,cupti.kernel.name,cupti.activity.kind" },
81-
{ "format", format } }
82-
);
76+
config()["CALI_REPORT_CONFIG"] = opts.build_query("local", query);
8377
}
8478

8579
opts.update_channel_config(config());

src/caliper/controllers/CudaActivityReportController.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ class CudaActivityReportController : public cali::ChannelController
3232
)
3333
: ChannelController(name, 0, initial_cfg)
3434
{
35-
// Config for first aggregation step in MPI mode (process-local aggregation)
36-
std::string local_select =
37-
" inclusive_scale(sum#cupti.host.duration,1e-9)"
38-
",inclusive_scale(cupti.activity.duration,1e-9)";
3935
// Config for serial-mode aggregation
40-
std::string serial_select =
36+
std::string local_select =
4137
" inclusive_scale(sum#cupti.host.duration,1e-9) as \"Host Time\""
4238
",inclusive_scale(cupti.activity.duration,1e-9) as \"GPU Time\""
4339
",inclusive_ratio(cupti.activity.duration,sum#cupti.host.duration,100.0) as \"GPU %\"";
@@ -54,31 +50,34 @@ class CudaActivityReportController : public cali::ChannelController
5450

5551
if (opts.is_enabled("show_kernels")) {
5652
groupby += ",cupti.kernel.name";
57-
serial_select = std::string("cupti.kernel.name as Kernel,") + serial_select;
58-
cross_select = std::string("cupti.kernel.name as Kernel,") + cross_select;
53+
local_select = std::string("cupti.kernel.name as Kernel,") + local_select;
54+
cross_select = std::string("cupti.kernel.name as Kernel,") + cross_select;
5955
}
6056

6157
std::string format = util::build_tree_format_spec(config(), opts);
6258

59+
std::string local_query = "select ";
60+
local_query.append(local_select);
61+
local_query.append(" group by ").append(groupby);
62+
local_query.append(" format ").append(format);
63+
64+
std::string cross_query = "select ";
65+
cross_query.append(cross_select);
66+
cross_query.append(" group by ").append(groupby);
67+
cross_query.append(" format ").append(format);
68+
6369
if (use_mpi) {
6470
config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport");
6571
config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr");
6672
config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append");
6773
config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false";
68-
config()["CALI_MPIREPORT_LOCAL_CONFIG"] =
69-
opts.build_query("local", { { "select", local_select }, { "group by", groupby } });
70-
config()["CALI_MPIREPORT_CONFIG"] = opts.build_query(
71-
"cross",
72-
{ { "select", cross_select }, { "group by", groupby }, { "format", format } }
73-
);
74+
config()["CALI_MPIREPORT_LOCAL_CONFIG"] = opts.build_query("local", local_query);
75+
config()["CALI_MPIREPORT_CONFIG"] = opts.build_query("cross", cross_query);
7476
} else {
7577
config()["CALI_SERVICES_ENABLE"].append(",report");
7678
config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr");
7779
config()["CALI_REPORT_APPEND"] = opts.get("output.append");
78-
config()["CALI_REPORT_CONFIG"] = opts.build_query(
79-
"local",
80-
{ { "select", serial_select }, { "group by", groupby }, { "format", format } }
81-
);
80+
config()["CALI_REPORT_CONFIG"] = opts.build_query("local", local_query);
8281
}
8382

8483
opts.update_channel_config(config());

0 commit comments

Comments
 (0)