target.hpp 3.72 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <cassert>
Paul's avatar
Paul committed
5
6
7
8
9
#include <string>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
10
#include <vector>
Paul's avatar
Paul committed
11
12
#include <migraphx/context.hpp>
#include <migraphx/pass.hpp>
Paul's avatar
Paul committed
13
#include <migraphx/config.hpp>
14
#include <migraphx/compile_options.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
15
16
#include <migraphx/argument.hpp>
#include <migraphx/rank.hpp>
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
namespace migraphx {
Paul's avatar
Paul committed
19
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
20

Paul's avatar
Paul committed
21
#ifdef DOXYGEN
Paul's avatar
Paul committed
22

Paul's avatar
Paul committed
23
/// An interface for a compilation target
Paul's avatar
Paul committed
24
25
26
27
28
29
struct target
{
    /// A unique name used to identify the target
    std::string name() const;
    /**
     * @brief The transformation pass to be run during compilation.
Paul's avatar
Paul committed
30
     *
Paul's avatar
Paul committed
31
     * @param ctx This is the target-dependent context that is created by `get_context`
32
     * @param options Compiling options passed in by the user
Paul's avatar
Paul committed
33
34
     * @return The passes to be ran
     */
35
    std::vector<pass> get_passes(context& ctx, const compile_options& options) const;
Paul's avatar
Paul committed
36
37
38
39
40
    /**
     * @brief Construct a context for the target.
     * @return The context to be used during compilation and execution.
     */
    context get_context() const;
Shucai Xiao's avatar
Shucai Xiao committed
41
42
    /**
     * @brief copy an argument to the current target.
Shucai Xiao's avatar
Shucai Xiao committed
43
     *
Shucai Xiao's avatar
Shucai Xiao committed
44
45
46
47
48
49
     * @param arg Input argument to be copied to the target
     * @return Argument in the target.
     */
    argument copy_to(const argument& arg) const;
    /**
     * @brief copy an argument from the current target.
Shucai Xiao's avatar
Shucai Xiao committed
50
     *
Shucai Xiao's avatar
Shucai Xiao committed
51
52
53
54
55
56
     * @param arg Input argument to be copied from the target
     * @return Argument in the host.
     */
    argument copy_from(const argument& arg) const;
    /**
     * @brief Allocate an argument based on the input shape
Shucai Xiao's avatar
Shucai Xiao committed
57
     *
Shucai Xiao's avatar
Shucai Xiao committed
58
59
60
61
     * @param s Shape of the argument to be allocated in the target
     * @return Allocated argument in the target.
     */
    argument allocate(const shape& s) const;
Paul's avatar
Paul committed
62
63
64
};

#else
Paul's avatar
Paul committed
65

Shucai Xiao's avatar
Shucai Xiao committed
66
template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
67
auto target_allocate(rank<1>, T& x, const shape& s) -> decltype(x.allocate(s))
Shucai Xiao's avatar
Shucai Xiao committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{
    return x.allocate(s);
}

template <class T>
argument target_allocate(rank<0>, T& x, const shape&)
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T>
argument target_allocate(T& x, const shape& s)
{
    return target_allocate(rank<1>{}, x, s);
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
86
auto copy_to_target(rank<1>, T& x, const argument& arg) -> decltype(x.copy_to(arg))
Shucai Xiao's avatar
Shucai Xiao committed
87
88
89
90
91
{
    return x.copy_to(arg);
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
92
argument copy_to_target(rank<0>, T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
93
{
Shucai Xiao's avatar
Shucai Xiao committed
94
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
95
96
97
98
99
100
101
102
103
}

template <class T>
argument copy_to_target(T& x, const argument& arg)
{
    return copy_to_target(rank<1>{}, x, arg);
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
104
auto copy_from_target(rank<1>, T& x, const argument& arg) -> decltype(x.copy_from(arg))
Shucai Xiao's avatar
Shucai Xiao committed
105
106
107
108
109
{
    return x.copy_from(arg);
}

template <class T>
Shucai Xiao's avatar
Shucai Xiao committed
110
argument copy_from_target(rank<0>, T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
111
{
Shucai Xiao's avatar
Shucai Xiao committed
112
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
113
114
115
116
117
118
119
120
}

template <class T>
argument copy_from_target(T& x, const argument& arg)
{
    return copy_from_target(rank<1>{}, x, arg);
}

Paul's avatar
Paul committed
121
122
<%
interface('target',
Shucai Xiao's avatar
Shucai Xiao committed
123
     virtual('name', returns='std::string', const=True),
124
     virtual('get_passes', ctx='context&', options='const compile_options&', returns='std::vector<pass>', const=True),
Shucai Xiao's avatar
Shucai Xiao committed
125
     virtual('get_context', returns='context', const=True),
Shucai Xiao's avatar
Shucai Xiao committed
126
127
128
129
130
131
132
133
134
135
136
137
     virtual('copy_to',
             returns = 'argument',
             input   = 'const argument&',
             const   = True,
             default = 'copy_to_target'),
     virtual('copy_from',
             returns = 'argument',
             input   = 'const argument&',
             const   = True,
             default = 'copy_from_target'),
    virtual('allocate', s='const shape&', returns='argument', const=True,
             default = 'target_allocate')
Paul's avatar
Paul committed
138
139
140
)
%>

Paul's avatar
Paul committed
141
142
#endif

Paul's avatar
Paul committed
143
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
144
} // namespace migraphx
Paul's avatar
Paul committed
145
146

#endif