Commit 058470c8 authored by Paul's avatar Paul
Browse files

Add driver to build

parent eb0d8fee
......@@ -40,6 +40,7 @@ target_include_directories(migraphx SYSTEM PUBLIC $<BUILD_INTERFACE:${HALF_INCLU
set(PACKAGE_DEPENDS)
add_subdirectory(driver)
add_subdirectory(onnx)
add_subdirectory(tf)
......
add_executable(driver main.cpp)
target_link_libraries(driver migraphx_cpu)
#ifndef MIGRAPHX_GUARD_RTGLIB_ARGUMENT_PARSER_HPP
#define MIGRAPHX_GUARD_RTGLIB_ARGUMENT_PARSER_HPP
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include <migraphx/config.hpp>
#include <migraphx/requires.hpp>
#include <migraphx/type_name.hpp>
#include <migraphx/each_args.hpp>
#include <migraphx/functional.hpp>
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {
template <class T>
struct value_parser
......@@ -43,7 +54,7 @@ struct argument_parser
struct argument
{
std::vector<std::string> flags;
std::function<bool(const std::vector<std::string>&)> action;
std::function<bool(argument_parser&, const std::vector<std::string>&)> action{};
std::string type = "";
std::string help = "";
};
......@@ -63,11 +74,21 @@ struct argument_parser
migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
}
template <class... Fs>
void add(std::nullptr_t x, std::vector<std::string> flags, Fs... fs)
{
arguments.push_back({flags});
argument& arg = arguments.back();
arg.type = "";
migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
}
template <class F>
static auto write_action(F f)
{
return [=](auto& x, auto& arg) {
arg.action = [ f, &](auto& self, const std::vector<std::string>& params)
arg.action = [&, f](auto& self, const std::vector<std::string>& params)
{
f(self, x, params);
return false;
......@@ -79,7 +100,7 @@ struct argument_parser
static auto do_action(F f)
{
return [=](auto&, auto& arg) {
arg.action = [ f, &](auto& self, const std::vector<std::string>&)
arg.action = [&, f](auto& self, const std::vector<std::string>&)
{
f(self);
return true;
......@@ -89,17 +110,18 @@ struct argument_parser
static auto write_range()
{
return write_action([](auto& self, auto& x, auto& params) {
std::transform(param.begin(),
return write_action([](auto&, auto& x, auto& params) {
using type = typename decltype(params)::value_type;
std::transform(params.begin(),
params.end(),
std::inserter(x, x.end()),
[](std::string y) { return value_parser<T>::apply(params.back()); });
})
[](std::string y) { return value_parser<type>::apply(y); });
});
}
static auto show_help()
static auto show_help(std::string msg = "")
{
return do_action([](auto& self) {
return do_action([=](auto& self) {
for(auto&& arg : self.arguments)
{
std::cout << std::endl;
......@@ -116,6 +138,8 @@ struct argument_parser
std::cout << " " << arg.help << std::endl;
}
std::cout << std::endl;
if (not msg.empty())
std::cout << msg << std::endl;
});
}
......@@ -129,7 +153,7 @@ struct argument_parser
{
return [=](auto& x, auto& arg) {
arg.type = "";
arg.action = [ value, &](auto& self, const std::vector<std::string>& params)
arg.action = [&, value](auto&, const std::vector<std::string>&)
{
x = value;
return false;
......@@ -144,7 +168,7 @@ struct argument_parser
{
keywords.insert(arg.flags.begin(), arg.flags.end());
}
auto arg_map = generic_parse(as, [&](std::string x) { return (keywords.count(x) > 0); });
auto arg_map = generic_parse(args, [&](std::string x) { return (keywords.count(x) > 0); });
for(auto&& arg : arguments)
{
for(auto&& flag : arg.flags)
......@@ -184,4 +208,8 @@ struct argument_parser
std::vector<argument> arguments;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx
#endif
#ifndef MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP
#define MIGRAPHX_GUARD_RTGLIB_COMMAND_HPP
#include "argument_parser.hpp"
#include <migraphx/config.hpp>
#include <migraphx/type_name.hpp>
#include <unordered_map>
#include <vector>
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {
inline auto& get_commands()
{
static std::unordered_map<std::string, std::function<void(std::vector<std::string> args)>> m;
return m;
}
template<class T>
std::string command_name()
{
static const std::string& name = get_type_name<T>();
return name.substr(name.rfind("::") + 2);
}
template <class T>
int auto_register_command()
{
auto& m = get_commands();
m[command_name<T>()] = [](std::vector<std::string> args) {
T x;
argument_parser ap;
x.parse(ap);
ap.parse(args);
x.run();
};
return 0;
}
template <class T>
struct command
{
static int static_register;
// This typedef ensures that the static member will be instantiated if
// the class itself is instantiated
using static_register_type =
std::integral_constant<decltype(&static_register), &static_register>;
};
template <class T>
int command<T>::static_register = auto_register_command<T>(); // NOLINT
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx
#endif
#include "argument_parser.hpp"
#include "command.hpp"
struct main_command
{
static std::string get_command_help()
{
std::string result = "Commands:\n";
for(const auto& p:migraphx::driver::get_commands())
result += " " + p.first + "\n";
return result;
}
void parse(migraphx::driver::argument_parser& ap)
{
ap.add(nullptr, {"-h", "--help"}, ap.help("Show help"), ap.show_help(get_command_help()));
}
void run() {}
};
int main(int argc, const char* argv[]) {
std::vector<std::string> args(argv + 1, argv + argc);
if (args.empty())
return 0;
auto&& m = migraphx::driver::get_commands();
auto cmd = args.front();
if (m.count(cmd) > 0)
{
m.at(cmd)({args.begin()+1, args.end()});
}
else
{
migraphx::driver::argument_parser ap;
main_command mc;
mc.parse(ap);
ap.parse(args);
mc.run();
}
return 0;
}
......@@ -42,7 +42,7 @@ struct requires_enum
#define MIGRAPHX_REQUIRES(...) \
typename migraphx::requires_enum<__LINE__>::e MIGRAPHX_REQUIRES_CAT( \
PrivateRequires, __LINE__) = migraphx::requires_enum<__LINE__>::a, \
class = typename std::enable_if<and_<__VA_ARGS__>{}>::type
class = typename std::enable_if<migraphx::and_<__VA_ARGS__>{}>::type
#endif
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment