Skip to content

Commit

Permalink
Move scoped_tic into profiler
Browse files Browse the repository at this point in the history
This is more convenient now that auto is available.
  • Loading branch information
ddemidov committed Jun 5, 2018
1 parent 9e1ae4f commit 939d792
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 50 deletions.
32 changes: 13 additions & 19 deletions amgcl/profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class profiler {
/**
* Returns delta in the measured value since the corresponding tic().
*/
delta_type toc(const std::string& /*name*/) {
delta_type toc(const std::string& /*name*/ = "") {
profile_unit *top = stack.back();
stack.pop_back();

Expand All @@ -101,6 +101,18 @@ class profiler {
root.begin = counter.current();
}

struct scoped_ticker {
profiler &prof;
scoped_ticker(profiler &prof) : prof(prof) {}
~scoped_ticker() {
prof.toc();
}
};

scoped_ticker scoped_tic(const std::string &name) {
tic(name);
return scoped_ticker(*this);
}
private:
struct profile_unit {
profile_unit() : length(0) {}
Expand Down Expand Up @@ -190,24 +202,6 @@ class profiler {
}
};

/// Scoped ticker.
/** Calls prof.tic(name) on construction, and prof.toc(name) on destruction. */
template <class Profiler>
struct scoped_tic {
Profiler &prof;
std::string name;

scoped_tic(Profiler &prof, const std::string &name)
: prof(prof), name(name)
{
prof.tic(name);
}

~scoped_tic() {
prof.toc(name);
}
};

} // namespace amgcl

#endif
8 changes: 3 additions & 5 deletions examples/cpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > tic;

//---------------------------------------------------------------------------
template <class Matrix>
void solve_cpr(const Matrix &K, const std::vector<double> &rhs, boost::property_tree::ptree &prm)
{
tic t1(prof, "CPR");
auto t1 = prof.scoped_tic("CPR");

typedef amgcl::backend::builtin<double> Backend;

Expand All @@ -46,7 +44,7 @@ void solve_cpr(const Matrix &K, const std::vector<double> &rhs, boost::property_

std::cout << solve.precond() << std::endl;

tic t2(prof, "solve");
auto t2 = prof.scoped_tic("solve");
std::vector<double> x(rhs.size(), 0.0);

size_t iters;
Expand Down Expand Up @@ -136,7 +134,7 @@ int main(int argc, char *argv[]) {
std::vector<char> pm;

{
tic t(prof, "reading");
auto t = prof.scoped_tic("reading");

string Afile = vm["matrix"].as<string>();
bool binary = vm["binary"].as<bool>();
Expand Down
9 changes: 4 additions & 5 deletions examples/cpr_drs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > tic;

//---------------------------------------------------------------------------
template <class Matrix>
void solve_cpr(const Matrix &K, const std::vector<double> &rhs, boost::property_tree::ptree &prm)
{
using amgcl::prof;
Backend::params bprm;

#if defined(SOLVER_BACKEND_VEXCL)
Expand All @@ -67,7 +66,7 @@ void solve_cpr(const Matrix &K, const std::vector<double> &rhs, boost::property_
}
#endif

tic t1(prof, "CPR");
auto t1 = prof.scoped_tic("CPR");

typedef
amgcl::amg<Backend, amgcl::runtime::coarsening::wrapper, amgcl::runtime::relaxation::wrapper>
Expand All @@ -88,7 +87,7 @@ void solve_cpr(const Matrix &K, const std::vector<double> &rhs, boost::property_
auto x = Backend::create_vector(rhs.size(), bprm);
amgcl::backend::clear(*x);

tic t2(prof, "solve");
auto t2 = prof.scoped_tic("solve");
size_t iters;
double error;

Expand Down Expand Up @@ -181,7 +180,7 @@ int main(int argc, char *argv[]) {
std::vector<char> pm;

{
tic t(prof, "reading");
auto t = prof.scoped_tic("reading");

string Afile = vm["matrix"].as<string>();
bool binary = vm["binary"].as<bool>();
Expand Down
5 changes: 2 additions & 3 deletions examples/custom_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,12 @@ profiler<> prof;
} // namespace amgcl

using amgcl::prof;
typedef amgcl::scoped_tic< amgcl::profiler<> > scoped_tic;

int main() {
// Discretize a 1D Poisson problem
const int n = 10000;

scoped_tic(prof, "total");
auto t_total = prof.scoped_tic("total");
sparse_matrix A(n, n);
for(int i = 0; i < n; ++i) {
if (i == 0 || i == n - 1) {
Expand All @@ -138,7 +137,7 @@ int main() {

std::cout << solve.precond() << std::endl;

scoped_tic(prof, "solve");
auto t_solve = prof.scoped_tic("solve");
std::vector<double> f(n, 1.0), x(n, 0.0);
solve(f, x);

Expand Down
4 changes: 1 addition & 3 deletions examples/mpi/runtime_bp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > scoped_tic;

//---------------------------------------------------------------------------
struct renumbering {
const domain_partition<2> &part;
Expand Down Expand Up @@ -68,7 +66,7 @@ std::tuple<size_t, double> solve(
prof.toc("setup");

{
scoped_tic t2(prof, "solve");
auto t2 = prof.scoped_tic("solve");
return solve(rhs, x);
}
}
Expand Down
6 changes: 2 additions & 4 deletions examples/schur_pressure_correction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > tic;

template <class USolver, class PSolver, class Matrix>
typename std::enable_if<
(
Expand Down Expand Up @@ -101,7 +99,7 @@ solve_schur(const Matrix &K, const std::vector<double> &rhs, boost::property_tre
}
#endif

tic t1(prof, "schur_complement");
auto t1 = prof.scoped_tic("schur_complement");

prof.tic("setup");
amgcl::make_solver<
Expand Down Expand Up @@ -284,7 +282,7 @@ int main(int argc, char *argv[]) {
std::vector<char> pm;

{
tic t(prof, "reading");
auto t = prof.scoped_tic("reading");

string Afile = vm["matrix"].as<string>();
bool binary = vm["binary"].as<bool>();
Expand Down
8 changes: 3 additions & 5 deletions examples/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > scoped_tic;

#ifdef SOLVER_BACKEND_BUILTIN
//---------------------------------------------------------------------------
template <int B, template <class> class Precond>
Expand Down Expand Up @@ -482,7 +480,7 @@ int main(int argc, char *argv[]) {
vector<double> val, rhs, null, x;

if (vm.count("matrix")) {
scoped_tic t(prof, "reading");
auto t = prof.scoped_tic("reading");

string Afile = vm["matrix"].as<string>();
bool binary = vm["binary"].as<bool>();
Expand Down Expand Up @@ -529,7 +527,7 @@ int main(int argc, char *argv[]) {
prm.put("precond.coarsening.nullspace.B", &null[0]);
}
} else {
scoped_tic t(prof, "assembling");
auto t = prof.scoped_tic("assembling");
rows = sample_problem(vm["size"].as<int>(), val, col, ptr, rhs, vm["anisotropy"].as<double>());
}

Expand All @@ -548,7 +546,7 @@ int main(int argc, char *argv[]) {
block_size, vm["reorder"].as<bool>());

if (vm.count("output")) {
scoped_tic t(prof, "write");
auto t = prof.scoped_tic("write");
amgcl::io::mm_write(vm["output"].as<string>(), &x[0], x.size());
}

Expand Down
10 changes: 4 additions & 6 deletions examples/solver_complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ namespace amgcl { profiler<> prof; }
using amgcl::prof;
using amgcl::precondition;

typedef amgcl::scoped_tic< amgcl::profiler<> > scoped_tic;

//---------------------------------------------------------------------------
template <template <class> class Precond>
std::tuple<size_t, double> solve(
Expand Down Expand Up @@ -55,7 +53,7 @@ std::tuple<size_t, double> solve(
std::cout << solve.precond() << std::endl;

{
scoped_tic t(prof, "solve");
auto t = prof.scoped_tic("solve");
return solve(rhs, x);
}
}
Expand Down Expand Up @@ -156,7 +154,7 @@ int main(int argc, char *argv[]) {
vector< std::complex<double> > val, rhs, null, x;

if (vm.count("matrix")) {
scoped_tic t(prof, "reading");
auto t = prof.scoped_tic("reading");

string Afile = vm["matrix"].as<string>();

Expand Down Expand Up @@ -188,7 +186,7 @@ int main(int argc, char *argv[]) {
prm.put("precond.coarsening.nullspace.B", &null[0]);
}
} else {
scoped_tic t(prof, "assembling");
auto t = prof.scoped_tic("assembling");
rows = sample_problem(vm["size"].as<int>(), val, col, ptr, rhs);
}

Expand All @@ -204,7 +202,7 @@ int main(int argc, char *argv[]) {
prm, rows, ptr, col, val, rhs, x);

if (vm.count("output")) {
scoped_tic t(prof, "write");
auto t = prof.scoped_tic("write");
amgcl::io::mm_write(vm["output"].as<string>(), &x[0], x.size());
}

Expand Down

0 comments on commit 939d792

Please sign in to comment.