Skip to content

Commit

Permalink
Remove futhark from profiler; use callbacks instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Snektron committed Jul 10, 2021
1 parent bc31b94 commit b99e373
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 55 deletions.
19 changes: 9 additions & 10 deletions include/pareas/compiler/profiler.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef _PAREAS_COMPILER_PROFILER_HPP
#define _PAREAS_COMPILER_PROFILER_HPP

#include "futhark_generated.h"

#include <iosfwd>
#include <chrono>
#include <vector>
#include <functional>

struct Profiler {
using SyncCallback = std::function<void()>;

using Clock = std::chrono::high_resolution_clock;

struct HistoryEntry {
Expand All @@ -19,13 +20,16 @@ struct Profiler {
unsigned max_level;
unsigned level;

SyncCallback sync_callback;
std::vector<Clock::time_point> starts;
std::vector<HistoryEntry> history;

Profiler(unsigned max_level);

void begin(futhark_context* ctx = nullptr);
void end(const char* name, futhark_context* ctx = nullptr);
void set_sync_callback(SyncCallback sync_callback = null_callback);

void begin();
void end(const char* name);

void dump(std::ostream& os);

Expand All @@ -36,12 +40,7 @@ struct Profiler {
this->end(name);
}

template <typename F>
void measure(const char* name, futhark_context* ctx, F f) {
this->begin(ctx);
f();
this->end(name, ctx);
}
static void null_callback() {}
};

#endif
68 changes: 34 additions & 34 deletions src/compiler/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ namespace frontend {
}

DeviceAst compile(futhark_context* ctx, const std::string& input, Profiler& p) {
p.begin(ctx);
p.begin(ctx);
p.begin();
p.begin();
auto lex_table = upload_lex_table(ctx);
auto sct = upload_strtab<futhark::UniqueStackChangeTable>(
ctx,
Expand All @@ -101,24 +101,24 @@ namespace frontend {
);

auto arity_array = futhark::UniqueArray<int32_t, 1>(ctx, grammar::arities, grammar::NUM_PRODUCTIONS);
p.end("table", ctx);
p.begin(ctx);
p.end("table");
p.begin();

auto input_array = futhark::UniqueArray<uint8_t, 1>(ctx, reinterpret_cast<const uint8_t*>(input.data()), input.size());
p.end("input", ctx);
p.end("upload", ctx);
p.end("input");
p.end("upload");

p.begin(ctx);
p.begin();

auto tokens = futhark::UniqueTokenArray(ctx);
p.measure("tokenize", ctx, [&]{
p.measure("tokenize", [&]{
int err = futhark_entry_frontend_tokenize(ctx, &tokens, input_array, lex_table);
if (err)
throw futhark::Error(ctx);
});

auto node_types = futhark::UniqueArray<uint8_t, 1>(ctx);
p.measure("parse", ctx, [&]{
p.measure("parse", [&]{
bool valid = false;
int err = futhark_entry_frontend_parse(ctx, &valid, &node_types, tokens, sct, pt);
if (err)
Expand All @@ -128,22 +128,22 @@ namespace frontend {
});

auto parents = futhark::UniqueArray<int32_t, 1>(ctx);
p.measure("build parse tree", ctx, [&]{
p.measure("build parse tree", [&]{
int err = futhark_entry_frontend_build_parse_tree(ctx, &parents, node_types, arity_array);
if (err)
throw futhark::Error(ctx);
});

p.begin(ctx);
p.measure("fix bin ops", ctx, [&]{
p.begin();
p.measure("fix bin ops", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
int err = futhark_entry_frontend_fix_bin_ops(ctx, &node_types, &parents, old_node_types, old_parents);
if (err)
throw futhark::Error(ctx);
});

p.measure("fix conditionals", ctx, [&]{
p.measure("fix conditionals", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
bool valid;
Expand All @@ -154,15 +154,15 @@ namespace frontend {
throw CompileError(Error::STRAY_ELSE_ERROR);
});

p.measure("flatten lists", ctx, [&]{
p.measure("flatten lists", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
int err = futhark_entry_frontend_flatten_lists(ctx, &node_types, &parents, old_node_types, old_parents);
if (err)
throw futhark::Error(ctx);
});

p.measure("fix names", ctx, [&]{
p.measure("fix names", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
bool valid;
Expand All @@ -173,14 +173,14 @@ namespace frontend {
throw CompileError(Error::INVALID_DECL);
});

p.measure("fix ascriptions", ctx, [&]{
p.measure("fix ascriptions", [&]{
auto old_parents = std::move(parents);
int err = futhark_entry_frontend_fix_ascriptions(ctx, &parents, node_types, old_parents);
if (err)
throw futhark::Error(ctx);
});

p.measure("fix fn decls", ctx, [&]{
p.measure("fix fn decls", [&]{
auto old_parents = std::move(parents);
bool valid;
int err = futhark_entry_frontend_fix_fn_decls(ctx, &valid, &parents, node_types, old_parents);
Expand All @@ -190,14 +190,14 @@ namespace frontend {
throw CompileError(Error::INVALID_FN_PROTO);
});

p.measure("fix args and params", ctx, [&]{
p.measure("fix args and params", [&]{
auto old_node_types = std::move(node_types);
int err = futhark_entry_frontend_fix_args_and_params(ctx, &node_types, old_node_types, parents);
if (err)
throw futhark::Error(ctx);
});

p.measure("fix decls", ctx, [&]{
p.measure("fix decls", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
bool valid;
Expand All @@ -208,23 +208,23 @@ namespace frontend {
throw CompileError(Error::INVALID_DECL);
});

p.measure("remove marker nodes", ctx, [&]{
p.measure("remove marker nodes", [&]{
auto old_parents = std::move(parents);
int err = futhark_entry_frontend_remove_marker_nodes(ctx, &parents, node_types, old_parents);
if (err)
throw futhark::Error(ctx);
});

auto prev_siblings = futhark::UniqueArray<int32_t, 1>(ctx);
p.measure("compute prev siblings", ctx, [&]{
p.measure("compute prev siblings", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
int err = futhark_entry_frontend_compute_prev_sibling(ctx, &node_types, &parents, &prev_siblings, old_node_types, old_parents);
if (err)
throw futhark::Error(ctx);
});

p.measure("check assignments", ctx, [&]{
p.measure("check assignments", [&]{
bool valid;
int err = futhark_entry_frontend_check_assignments(ctx, &valid, node_types, parents, prev_siblings);
if (err)
Expand All @@ -234,8 +234,8 @@ namespace frontend {
});
p.end("syntax");

p.begin(ctx);
p.measure("insert derefs", ctx, [&]{
p.begin();
p.measure("insert derefs", [&]{
auto old_node_types = std::move(node_types);
auto old_parents = std::move(parents);
auto old_prev_siblings = std::move(prev_siblings);
Expand All @@ -245,14 +245,14 @@ namespace frontend {
});

auto node_data = futhark::UniqueArray<uint32_t, 1>(ctx);
p.measure("extract lexemes", ctx, [&]{
p.measure("extract lexemes", [&]{
int err = futhark_entry_frontend_extract_lexemes(ctx, &node_data, input_array, tokens, node_types);
if (err)
throw futhark::Error(ctx);
});

auto resolution = futhark::UniqueArray<int32_t, 1>(ctx);
p.measure("resolve vars", ctx, [&]{
p.measure("resolve vars", [&]{
bool valid;
int err = futhark_entry_frontend_resolve_vars(ctx, &valid, &resolution, node_types, parents, prev_siblings, node_data);
if (err)
Expand All @@ -261,7 +261,7 @@ namespace frontend {
throw CompileError(Error::INVALID_VARIABLE);
});

p.measure("resolve fns", ctx, [&]{
p.measure("resolve fns", [&]{
auto old_resolution = std::move(resolution);
bool valid;
int err = futhark_entry_frontend_resolve_fns(ctx, &valid, &resolution, node_types, old_resolution, node_data);
Expand All @@ -271,7 +271,7 @@ namespace frontend {
throw CompileError(Error::DUPLICATE_FN_OR_INVALID_CALL);
});

p.measure("resolve args", ctx, [&]{
p.measure("resolve args", [&]{
auto old_resolution = std::move(resolution);
bool valid;
int err = futhark_entry_frontend_resolve_args(ctx, &valid, &resolution, node_types, parents, prev_siblings, old_resolution);
Expand All @@ -282,7 +282,7 @@ namespace frontend {
});

auto data_types = futhark::UniqueArray<uint8_t, 1>(ctx);
p.measure("resolve dtypes", ctx, [&]{
p.measure("resolve dtypes", [&]{
bool valid;
int err = futhark_entry_frontend_resolve_data_types(ctx, &valid, &data_types, node_types, parents, prev_siblings, resolution.get());
if (err)
Expand All @@ -291,7 +291,7 @@ namespace frontend {
throw CompileError(Error::TYPE_ERROR);
});

p.measure("check return dtypes", ctx, [&]{
p.measure("check return dtypes", [&]{
bool valid;
int err = futhark_entry_frontend_check_return_types(ctx, &valid, node_types, parents, data_types);
if (err)
Expand All @@ -300,7 +300,7 @@ namespace frontend {
throw CompileError(Error::INVALID_RETURN);
});

p.measure("check convergence", ctx, [&]{
p.measure("check convergence", [&]{
bool valid;
int err = futhark_entry_frontend_check_convergence(ctx, &valid, node_types, parents, prev_siblings, data_types);
if (err)
Expand All @@ -310,7 +310,7 @@ namespace frontend {
});

auto ast = DeviceAst(ctx);
p.measure("build ast", ctx, [&]{
p.measure("build ast", [&]{
// Other arrays are destructed at the end of the function.
int err = futhark_entry_frontend_build_ast(
ctx,
Expand All @@ -332,8 +332,8 @@ namespace frontend {
throw futhark::Error(ctx);
});

p.end("sema", ctx);
p.end("compile", ctx);
p.end("sema");
p.end("compile");

return ast;
}
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,16 @@ int main(int argc, char* argv[]) {
#endif

auto ctx = futhark::Context(futhark_context_new(config.get()));
p.end("context init", ctx.get());
p.set_sync_callback([ctx = ctx.get()]{
if (futhark_context_sync(ctx))
throw futhark::Error(ctx);
});
p.end("context init");

try {
p.begin(ctx.get());
p.begin();
auto ast = frontend::compile(ctx.get(), input, p);
p.end("frontend", ctx.get());
p.end("frontend");

if (opts.profile > 0)
p.dump(std::cout);
Expand Down
18 changes: 10 additions & 8 deletions src/compiler/profiler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "pareas/compiler/profiler.hpp"
#include "pareas/compiler/futhark_interop.hpp"

#include <fmt/ostream.h>
#include <fmt/chrono.h>
Expand All @@ -9,28 +8,31 @@

Profiler::Profiler(unsigned max_level):
max_level(max_level),
level(0) {
level(0),
sync_callback(null_callback) {
}

void Profiler::begin(futhark_context* ctx) {
void Profiler::set_sync_callback(SyncCallback sync_callback) {
this->sync_callback = sync_callback;
}

void Profiler::begin() {
++this->level;
if (this->level > this->max_level)
return;

if (ctx && futhark_context_sync(ctx))
throw futhark::Error(ctx);
this->sync_callback();

auto start = Clock::now();
this->starts.push_back(start);
}

void Profiler::end(const char* name, futhark_context* ctx) {
void Profiler::end(const char* name) {
--this->level;
if (this->level >= this->max_level)
return;

if (ctx && futhark_context_sync(ctx))
throw futhark::Error(ctx);
this->sync_callback();

auto end = Clock::now();
auto start = this->starts.back();
Expand Down

0 comments on commit b99e373

Please sign in to comment.