Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiling #968

Merged
merged 17 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cmdstan/arguments/arg_output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cmdstan/arguments/arg_diagnostic_file.hpp>
#include <cmdstan/arguments/arg_output_file.hpp>
#include <cmdstan/arguments/arg_output_sig_figs.hpp>
#include <cmdstan/arguments/arg_profile_file.hpp>
#include <cmdstan/arguments/arg_refresh.hpp>
#include <cmdstan/arguments/categorical_argument.hpp>

Expand All @@ -19,6 +20,7 @@ class arg_output : public categorical_argument {
_subarguments.push_back(new arg_diagnostic_file());
_subarguments.push_back(new arg_refresh());
_subarguments.push_back(new arg_output_sig_figs());
_subarguments.push_back(new arg_profile_file());
}
};

Expand Down
23 changes: 23 additions & 0 deletions src/cmdstan/arguments/arg_profile_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CMDSTAN_ARGUMENTS_ARG_PROFILE_FILE_HPP
#define CMDSTAN_ARGUMENTS_ARG_PROFILE_FILE_HPP

#include <cmdstan/arguments/singleton_argument.hpp>

namespace cmdstan {

class arg_profile_file : public string_argument {
public:
arg_profile_file() : string_argument() {
_name = "profile_file";
_description = "File to store profiling information";
_validity = "Valid path and write acces to the folder";
_default = "\"\"";
_default_value = "profile.csv";
_constrained = false;
_good_value = "profile.csv";
_value = _default_value;
}
};

} // namespace cmdstan
#endif
17 changes: 16 additions & 1 deletion src/cmdstan/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include <cmdstan/arguments/arg_output.hpp>
#include <cmdstan/arguments/arg_random.hpp>
#include <cmdstan/arguments/arg_opencl.hpp>
#include <cmdstan/arguments/arg_profile_file.hpp>
#include <cmdstan/arguments/argument_parser.hpp>
#include <cmdstan/io/json/json_data.hpp>
#include <cmdstan/write_model_compile_info.hpp>
#include <cmdstan/write_model.hpp>
#include <cmdstan/write_opencl_device.hpp>
#include <cmdstan/write_parallel_info.hpp>
#include <cmdstan/write_profiling.hpp>
#include <cmdstan/write_stan.hpp>
#include <stan/callbacks/interrupt.hpp>
#include <stan/callbacks/logger.hpp>
Expand Down Expand Up @@ -62,6 +64,7 @@
// forward declaration for function defined in another translation unit
stan::model::model_base &new_model(stan::io::var_context &data_context,
unsigned int seed, std::ostream *msg_stream);
stan::math::profile_map &get_stan_profile_data();
bbbales2 marked this conversation as resolved.
Show resolved Hide resolved

namespace cmdstan {

Expand Down Expand Up @@ -871,7 +874,19 @@ int command(int argc, const char *argv[]) {
init_writer, sample_writer, diagnostic_writer);
}
}

stan::math::profile_map &profile_data = get_stan_profile_data();
if (profile_data.size() > 0) {
std::string profile_file_name
= dynamic_cast<string_argument *>(
parser.arg("output")->arg("profile_file"))
->value();
std::fstream profile_stream(profile_file_name.c_str(), std::fstream::out);
if (!sig_figs_arg->is_default()) {
profile_stream << std::setprecision(sig_figs_arg->value());
}
write_profiling(profile_stream, profile_data);
profile_stream.close();
}
output_stream.close();
diagnostic_stream.close();
for (size_t i = 0; i < valid_arguments.size(); ++i)
Expand Down
36 changes: 36 additions & 0 deletions src/cmdstan/write_profiling.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef CMDSTAN_WRITE_PROFILING_HPP
#define CMDSTAN_WRITE_PROFILING_HPP

#include <stan/math/rev/core/profiling.hpp>
#include <stan/callbacks/writer.hpp>
#include <stan/version.hpp>
#include <string>
#include <vector>

namespace cmdstan {

/**
* Writes the data from the map of profiles in a CSV format
* to the output.
*
* @param output stream to write output to
* @param p reference to the map of profiles
*/
void write_profiling(std::ostream& output, stan::math::profile_map& p) {
stan::math::profile_map::iterator it;

output << "name,thread_id,total_time,forward_time,reverse_time,chain_"
"stack,no_chain_stack,autodiff_calls,no_autodiff_calls"
<< std::endl;
for (it = p.begin(); it != p.end(); it++) {
output << it->first.first << "," << it->first.second << ","
<< (it->second.get_fwd_time() + it->second.get_rev_time()) << ","
<< it->second.get_fwd_time() << "," << it->second.get_rev_time()
<< "," << it->second.get_chain_stack_used() << ","
<< it->second.get_nochain_stack_used() << ","
<< it->second.get_num_rev_passes() << ","
<< it->second.get_num_no_AD_fwd_passes() << std::endl;
}
}
} // namespace cmdstan
#endif