Skip to content

Commit

Permalink
Move profiler to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Snektron committed Jul 10, 2021
1 parent b99e373 commit a319492
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 122 deletions.
4 changes: 2 additions & 2 deletions include/pareas/compiler/frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "futhark_generated.h"

#include "pareas/compiler/ast.hpp"
#include "pareas/compiler/profiler.hpp"
#include "pareas/profiler/profiler.hpp"

#include <chrono>
#include <stdexcept>
Expand Down Expand Up @@ -33,7 +33,7 @@ namespace frontend {
std::runtime_error(error_name(e)) {}
};

DeviceAst compile(futhark_context* ctx, const std::string& input, Profiler& p);
DeviceAst compile(futhark_context* ctx, const std::string& input, pareas::Profiler& p);
}

#endif
46 changes: 0 additions & 46 deletions include/pareas/compiler/profiler.hpp

This file was deleted.

48 changes: 48 additions & 0 deletions include/pareas/profiler/profiler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef _PAREAS_PROFILER_PROFILER_HPP
#define _PAREAS_PROFILER_PROFILER_HPP

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

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

using Clock = std::chrono::high_resolution_clock;

struct HistoryEntry {
unsigned level;
const char* name;
Clock::duration elapsed;
};

unsigned max_level;
unsigned level;

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

Profiler(unsigned max_level);

void set_sync_callback(SyncCallback sync_callback = null_callback);

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

void dump(std::ostream& os);

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

static void null_callback() {}
};
}

#endif
10 changes: 8 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ pareas_lpg_exe = executable(
include_directories: inc,
)

# Profiling library
pareas_prof_dep = declare_dependency(
include_directories: inc,
sources: files('src/profiler/profiler.cpp'),
dependencies: fmt_dep,
)

# Compiler
dependencies = [fmt_dep, dependency('threads')]
dependencies = [pareas_prof_dep, fmt_dep, dependency('threads')]

# Build futhark library
futhark_backend = get_option('futhark-backend')
Expand Down Expand Up @@ -143,7 +150,6 @@ sources = files(
'src/compiler/main.cpp',
'src/compiler/frontend.cpp',
'src/compiler/ast.cpp',
'src/compiler/profiler.cpp',
)

pareas_exe = executable(
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace frontend {
}
}

DeviceAst compile(futhark_context* ctx, const std::string& input, Profiler& p) {
DeviceAst compile(futhark_context* ctx, const std::string& input, pareas::Profiler& p) {
p.begin();
p.begin();
auto lex_table = upload_lex_table(ctx);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "pareas/compiler/futhark_interop.hpp"
#include "pareas/compiler/ast.hpp"
#include "pareas/compiler/frontend.hpp"
#include "pareas/compiler/profiler.hpp"
#include "pareas/profiler/profiler.hpp"

#include <fmt/format.h>
#include <fmt/ostream.h>
Expand Down Expand Up @@ -199,7 +199,7 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS;
}

auto p = Profiler(opts.profile);
auto p = pareas::Profiler(opts.profile);

auto in = std::ifstream(opts.input_path, std::ios::binary);
if (!in) {
Expand Down
69 changes: 0 additions & 69 deletions src/compiler/profiler.cpp

This file was deleted.

71 changes: 71 additions & 0 deletions src/profiler/profiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "pareas/profiler/profiler.hpp"

#include <fmt/ostream.h>
#include <fmt/chrono.h>

#include <cstddef>
#include <cassert>

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

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;

this->sync_callback();

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

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

this->sync_callback();

auto end = Clock::now();
auto start = this->starts.back();
this->starts.pop_back();
auto diff = end - start;
this->history.push_back(HistoryEntry{this->level, name, diff});
}

void Profiler::dump(std::ostream& os) {
assert(this->level == 0);

auto ordered_history = std::vector<HistoryEntry>(this->history.size());
auto level_index_stack = std::vector<size_t>();

size_t j = 0;
for (const auto& entry : this->history) {
for (unsigned i = level_index_stack.size(); i <= entry.level; ++i) {
level_index_stack.push_back(j++);
}

size_t index = level_index_stack.back();
ordered_history[index] = entry;
level_index_stack.pop_back();
}

auto name_stack = std::vector<const char*>();
for (auto [level, name, elapsed] : ordered_history) {
name_stack.resize(level);
name_stack.push_back(name);

auto us = std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
fmt::print("{}: {}\n", fmt::join(name_stack, "."), us);
}
}
}

0 comments on commit a319492

Please sign in to comment.