style: 调整结构

Signed-off-by: YdrMaster <ydrml@hotmail.com>
This commit is contained in:
YdrMaster 2023-04-23 16:02:38 +08:00
parent 17033fad97
commit 2a147c235d
6 changed files with 54 additions and 42 deletions

View File

@ -1,3 +1,22 @@
#pragma once #pragma once
#include "../../src/mutation.h" #include "../../src/mutation.h"
#include "../../src/pass/single_operator.h"
#include <unordered_set>
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<size_t> mark;
uintptr_t memory;
for (const auto &op : g.operators)
for (const auto &t : op.outputs)
if (mark.insert(reinterpret_cast<uintptr_t>(t.get())).second)
memory += t->size();
return 1e6f / static_cast<float>(memory);
}
} // namespace optimization

View File

@ -1,24 +0,0 @@
#include "mutation.h"
#include <unordered_set>
using namespace optimization;
Vec<std::pair<Unigraph, SingleOperator>>
optimization::split_each(Unigraph &&g) {
Vec<std::pair<Unigraph, SingleOperator>> 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<size_t> mark;
uintptr_t memory;
for (const auto &op : g.operators)
for (const auto &t : op.outputs)
if (mark.insert(reinterpret_cast<uintptr_t>(t.get())).second)
memory += t->size();
return 1e6f / static_cast<float>(memory);
}

View File

@ -147,20 +147,6 @@ template <class PartitionType> 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<std::pair<Unigraph, SingleOperator>> 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 <class t> Vec<size_t> list_size(Vec<SubGraph<t>> const &list) { template <class t> Vec<size_t> list_size(Vec<SubGraph<t>> const &list) {
Vec<size_t> ans(list.size()); Vec<size_t> ans(list.size());
std::transform(list.begin(), list.end(), ans.begin(), std::transform(list.begin(), list.end(), ans.begin(),

View File

@ -0,0 +1,14 @@
#include "single_operator.h"
using namespace optimization;
using namespace pass;
Vec<std::pair<Unigraph, SingleOperator>>
optimization::pass::split_each(Unigraph &&g) {
Vec<std::pair<Unigraph, SingleOperator>> ans;
for (auto &op : g.operators) {
auto &[g, t] = ans.emplace_back();
g.push_operator(op.op_type, op.inputs, op.outputs);
}
return ans;
}

View File

@ -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<std::pair<Unigraph, SingleOperator>> split_each(Unigraph &&);
} // namespace optimization::pass

View File

@ -1,4 +1,4 @@
#include "../src/mutation.h" #include "../include/optimization/common.h"
#include <iostream> #include <iostream>
#include <unordered_set> #include <unordered_set>
@ -28,11 +28,12 @@ int main() {
{c} // outputs {c} // outputs
); );
auto p = Partition<SingleOperator>(std::move(g), split_each); auto p =
auto m = Mutation<SingleOperator>( Partition<pass::SingleOperator>(std::move(g), pass::split_each);
auto m = Mutation<pass::SingleOperator>(
std::move(p), std::move(p),
[](const auto &g, const auto &t) { return Vec<Unigraph>{}; }); [](const auto &g, const auto &t) { return Vec<Unigraph>{}; });
auto r = Rating<SingleOperator>(std::move(m), memory_usage); auto r = Rating<pass::SingleOperator>(std::move(m), memory_usage);
auto ans = r.build(Vec<size_t>(r.size().size(), 0)); auto ans = r.build(Vec<size_t>(r.size().size(), 0));
return 0; return 0;