|
| 1 | +// Copyright (c) 2019, Lawrence Livermore National Security, LLC. |
| 2 | +// See top-level LICENSE file for details. |
| 3 | + |
| 4 | +#include "caliper/caliper-config.h" |
| 5 | + |
| 6 | +#include "caliper/ChannelController.h" |
| 7 | +#include "caliper/ConfigManager.h" |
| 8 | + |
| 9 | +#include "caliper/common/Log.h" |
| 10 | +#include "caliper/common/StringConverter.h" |
| 11 | + |
| 12 | +#include "../../services/Services.h" |
| 13 | + |
| 14 | +#include <algorithm> |
| 15 | +#include <set> |
| 16 | +#include <tuple> |
| 17 | + |
| 18 | +using namespace cali; |
| 19 | + |
| 20 | +namespace |
| 21 | +{ |
| 22 | + |
| 23 | +class RocmActivityProfileController : public cali::ChannelController |
| 24 | +{ |
| 25 | +public: |
| 26 | + |
| 27 | + RocmActivityProfileController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts, const std::string& format) |
| 28 | + : ChannelController(name, 0, initial_cfg) |
| 29 | + { |
| 30 | + std::string output(opts.get("output", "rocm_profile").to_string()); |
| 31 | + |
| 32 | + if (output != "stdout" && output != "stderr") { |
| 33 | + auto pos = output.find_last_of('.'); |
| 34 | + std::string ext = (format == "cali" ? ".cali" : ".json"); |
| 35 | + |
| 36 | + if (pos == std::string::npos || output.substr(pos) != ext) |
| 37 | + output.append(ext); |
| 38 | + } |
| 39 | + |
| 40 | + auto avail_services = services::get_available_services(); |
| 41 | + bool have_mpi = |
| 42 | + std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); |
| 43 | + bool have_adiak = |
| 44 | + std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); |
| 45 | + |
| 46 | + bool use_mpi = have_mpi; |
| 47 | + |
| 48 | + if (opts.is_set("use.mpi")) |
| 49 | + use_mpi = have_mpi && opts.is_enabled("use.mpi"); |
| 50 | + |
| 51 | + if (have_adiak) { |
| 52 | + config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); |
| 53 | + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = |
| 54 | + opts.get("adiak.import_categories", "2,3").to_string(); |
| 55 | + } |
| 56 | + |
| 57 | + if (use_mpi) { |
| 58 | + config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); |
| 59 | + config()["CALI_AGGREGATE_KEY" ] = "*,mpi.rank"; |
| 60 | + config()["CALI_MPIREPORT_FILENAME"] = output; |
| 61 | + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; |
| 62 | + config()["CALI_MPIREPORT_CONFIG" ] = |
| 63 | + opts.build_query("local", { |
| 64 | + { "select", |
| 65 | + "*,scale(rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" |
| 66 | + " ,sum(sum#time.duration) as \"time\" unit sec" |
| 67 | + }, |
| 68 | + { "group by", "prop:nested,rocm.kernel.name,rocm.activity.kind,mpi.rank" }, |
| 69 | + { "format", format } |
| 70 | + }); |
| 71 | + } else { |
| 72 | + config()["CALI_SERVICES_ENABLE" ].append(",report"); |
| 73 | + config()["CALI_REPORT_FILENAME" ] = output; |
| 74 | + config()["CALI_REPORT_CONFIG" ] = |
| 75 | + opts.build_query("local", { |
| 76 | + { "select", |
| 77 | + "*,scale(rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" |
| 78 | + " ,sum(sum#time.duration) as \"time\" unit sec" }, |
| 79 | + { "group by", "prop:nested,rocm.kernel.name,rocm.activity.kind" }, |
| 80 | + { "format", format } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + opts.update_channel_config(config()); |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +std::string |
| 89 | +check_args(const cali::ConfigManager::Options& opts) { |
| 90 | + // Check if output.format is valid |
| 91 | + |
| 92 | + std::string format = opts.get("output.format", "json-split").to_string(); |
| 93 | + std::set<std::string> allowed_formats = { "cali", "json", "json-split", "hatchet" }; |
| 94 | + |
| 95 | + if (allowed_formats.find(format) == allowed_formats.end()) |
| 96 | + return std::string("rocm-activity-profile: Invalid output format \"") + format + "\""; |
| 97 | + |
| 98 | + return ""; |
| 99 | +} |
| 100 | + |
| 101 | +cali::ChannelController* |
| 102 | +make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) |
| 103 | +{ |
| 104 | + std::string format = opts.get("output.format", "json-split").to_string(); |
| 105 | + |
| 106 | + if (format == "hatchet") |
| 107 | + format = "json-split"; |
| 108 | + |
| 109 | + if (!(format == "json-split" || format == "json" || format == "cali")) { |
| 110 | + format = "json-split"; |
| 111 | + Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format |
| 112 | + << "\". Using json-split." |
| 113 | + << std::endl; |
| 114 | + } |
| 115 | + |
| 116 | + return new RocmActivityProfileController(name, initial_cfg, opts, format); |
| 117 | +} |
| 118 | + |
| 119 | +const char* controller_spec = |
| 120 | + "{" |
| 121 | + " \"name\" : \"rocm-activity-profile\"," |
| 122 | + " \"description\" : \"Record AMD ROCm activities and a write profile\"," |
| 123 | + " \"categories\" : [ \"adiak\", \"metric\", \"output\", \"region\" ]," |
| 124 | + " \"services\" : [ \"aggregate\", \"roctracer\", \"event\", \"timestamp\" ]," |
| 125 | + " \"config\" : " |
| 126 | + " { \"CALI_CHANNEL_FLUSH_ON_EXIT\" : \"false\"," |
| 127 | + " \"CALI_EVENT_ENABLE_SNAPSHOT_INFO\" : \"false\"," |
| 128 | + " \"CALI_ROCTRACER_TRACE_ACTIVITIES\" : \"true\"," |
| 129 | + " \"CALI_ROCTRACER_RECORD_KERNEL_NAMES\": \"true\"" |
| 130 | + " }," |
| 131 | + " \"options\": " |
| 132 | + " [" |
| 133 | + " { " |
| 134 | + " \"name\": \"output.format\"," |
| 135 | + " \"type\": \"string\"," |
| 136 | + " \"description\": \"Output format ('hatchet', 'cali', 'json')\"" |
| 137 | + " }," |
| 138 | + " { " |
| 139 | + " \"name\": \"use.mpi\"," |
| 140 | + " \"type\": \"bool\"," |
| 141 | + " \"description\": \"Merge results into a single output stream in MPI programs\"" |
| 142 | + " }" |
| 143 | + " ]" |
| 144 | + "}"; |
| 145 | + |
| 146 | +} // namespace [anonymous] |
| 147 | + |
| 148 | +namespace cali |
| 149 | +{ |
| 150 | + |
| 151 | +ConfigManager::ConfigInfo rocm_activity_profile_controller_info |
| 152 | +{ |
| 153 | + ::controller_spec, ::make_controller, ::check_args |
| 154 | +}; |
| 155 | + |
| 156 | +} |
0 commit comments