diff --git a/optimization/include/optimization/common.h b/optimization/include/optimization/common.h index 588f8c69..86ce0d20 100644 --- a/optimization/include/optimization/common.h +++ b/optimization/include/optimization/common.h @@ -1,3 +1,22 @@ #pragma once #include "../../src/mutation.h" +#include "../../src/pass/single_operator.h" +#include + +namespace optimization { + +/// @brief Calculates the memory usage of a graph. +/// @param arg0 The graph. +/// @return The reciprocal of the total memory usage of the graph in bytes. +inline float memory_usage(Unigraph const &g) { + std::unordered_set mark; + uintptr_t memory; + for (const auto &op : g.operators) + for (const auto &t : op.outputs) + if (mark.insert(reinterpret_cast(t.get())).second) + memory += t->size(); + return 1e6f / static_cast(memory); +} + +} // namespace optimization diff --git a/optimization/src/mutation.cc b/optimization/src/mutation.cc deleted file mode 100644 index d2f2fc3e..00000000 --- a/optimization/src/mutation.cc +++ /dev/null @@ -1,24 +0,0 @@ -#include "mutation.h" -#include - -using namespace optimization; - -Vec> -optimization::split_each(Unigraph &&g) { - Vec> ans; - for (auto &op : g.operators) { - auto &[g, t] = ans.emplace_back(); - g.push_operator(op.op_type, op.inputs, op.outputs); - } - return ans; -} - -float optimization::memory_usage(Unigraph const &g) { - std::unordered_set mark; - uintptr_t memory; - for (const auto &op : g.operators) - for (const auto &t : op.outputs) - if (mark.insert(reinterpret_cast(t.get())).second) - memory += t->size(); - return 1e6f / static_cast(memory); -} diff --git a/optimization/src/mutation.h b/optimization/src/mutation.h index 93bbc935..5334f2a1 100644 --- a/optimization/src/mutation.h +++ b/optimization/src/mutation.h @@ -147,20 +147,6 @@ template class Rating { } }; -/// @brief Partition every operator as a `Unigraph`. -struct SingleOperator {}; - -/// @brief Splits a graph into subgraphs, where each subgraph contains -/// only one operator. -/// @param arg0 An unpartitioned graph. -/// @return A vector of individual subgraphs. -Vec> split_each(Unigraph &&); - -/// @brief Calculates the memory usage of a graph. -/// @param arg0 The graph. -/// @return The reciprocal of the total memory usage of the graph in bytes. -float memory_usage(Unigraph const &); - template Vec list_size(Vec> const &list) { Vec ans(list.size()); std::transform(list.begin(), list.end(), ans.begin(), diff --git a/optimization/src/pass/single_operator.cc b/optimization/src/pass/single_operator.cc new file mode 100644 index 00000000..cd471ad5 --- /dev/null +++ b/optimization/src/pass/single_operator.cc @@ -0,0 +1,14 @@ +#include "single_operator.h" + +using namespace optimization; +using namespace pass; + +Vec> +optimization::pass::split_each(Unigraph &&g) { + Vec> ans; + for (auto &op : g.operators) { + auto &[g, t] = ans.emplace_back(); + g.push_operator(op.op_type, op.inputs, op.outputs); + } + return ans; +} diff --git a/optimization/src/pass/single_operator.h b/optimization/src/pass/single_operator.h new file mode 100644 index 00000000..0a88dd45 --- /dev/null +++ b/optimization/src/pass/single_operator.h @@ -0,0 +1,16 @@ +#pragma once + +#include "../mutation.h" + +namespace optimization::pass { + +/// @brief Partition every operator as a `Unigraph`. +struct SingleOperator {}; + +/// @brief Splits a graph into subgraphs, where each subgraph contains +/// only one operator. +/// @param arg0 An unpartitioned graph. +/// @return A vector of individual subgraphs. +Vec> split_each(Unigraph &&); + +} // namespace optimization::pass diff --git a/optimization/test/test.cpp b/optimization/test/test.cpp index baa7186e..b6a0f8d9 100644 --- a/optimization/test/test.cpp +++ b/optimization/test/test.cpp @@ -1,4 +1,4 @@ -#include "../src/mutation.h" +#include "../include/optimization/common.h" #include #include @@ -28,11 +28,12 @@ int main() { {c} // outputs ); - auto p = Partition(std::move(g), split_each); - auto m = Mutation( + auto p = + Partition(std::move(g), pass::split_each); + auto m = Mutation( std::move(p), [](const auto &g, const auto &t) { return Vec{}; }); - auto r = Rating(std::move(m), memory_usage); + auto r = Rating(std::move(m), memory_usage); auto ans = r.build(Vec(r.size().size(), 0)); return 0;