target.hpp 4.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_TARGET_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
#include <cassert>
Paul's avatar
Paul committed
28
29
30
31
32
#include <string>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
Paul's avatar
Paul committed
33
#include <vector>
Paul's avatar
Paul committed
34
35
#include <migraphx/context.hpp>
#include <migraphx/pass.hpp>
Paul's avatar
Paul committed
36
#include <migraphx/config.hpp>
37
#include <migraphx/compile_options.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
38
39
#include <migraphx/argument.hpp>
#include <migraphx/rank.hpp>
Paul's avatar
Paul committed
40

Paul's avatar
Paul committed
41
namespace migraphx {
Paul's avatar
Paul committed
42
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
43

Paul's avatar
Paul committed
44
#ifdef DOXYGEN
Paul's avatar
Paul committed
45

Paul's avatar
Paul committed
46
/// An interface for a compilation target
Paul's avatar
Paul committed
47
48
49
50
51
52
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
53
     *
Paul's avatar
Paul committed
54
     * @param ctx This is the target-dependent context that is created by `get_context`
55
     * @param options Compiling options passed in by the user
Paul's avatar
Paul committed
56
57
     * @return The passes to be ran
     */
58
    std::vector<pass> get_passes(context& ctx, const compile_options& options) const;
Paul's avatar
Paul committed
59
60
61
62
63
    /**
     * @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
64
65
    /**
     * @brief copy an argument to the current target.
Shucai Xiao's avatar
Shucai Xiao committed
66
     *
Shucai Xiao's avatar
Shucai Xiao committed
67
68
69
70
71
72
     * @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
73
     *
Shucai Xiao's avatar
Shucai Xiao committed
74
75
76
77
78
79
     * @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
80
     *
Shucai Xiao's avatar
Shucai Xiao committed
81
82
83
84
     * @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
85
86
87
};

#else
Paul's avatar
Paul committed
88

Shucai Xiao's avatar
Shucai Xiao committed
89
template <class T>
Paul's avatar
Paul committed
90
argument target_allocate(T& x, const shape&)
Shucai Xiao's avatar
Shucai Xiao committed
91
92
93
94
95
96
{
    std::string name = x.name();
    MIGRAPHX_THROW("Not computable: " + name);
}

template <class T>
Paul's avatar
Paul committed
97
argument copy_to_target(T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
98
{
Shucai Xiao's avatar
Shucai Xiao committed
99
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
100
101
102
}

template <class T>
Paul's avatar
Paul committed
103
argument copy_from_target(T&, const argument& arg)
Shucai Xiao's avatar
Shucai Xiao committed
104
{
Shucai Xiao's avatar
Shucai Xiao committed
105
    return arg;
Shucai Xiao's avatar
Shucai Xiao committed
106
107
}

Paul's avatar
Paul committed
108
109
<%
interface('target',
Shucai Xiao's avatar
Shucai Xiao committed
110
     virtual('name', returns='std::string', const=True),
111
     virtual('get_passes', ctx='context&', options='const compile_options&', returns='std::vector<pass>', const=True),
Shucai Xiao's avatar
Shucai Xiao committed
112
     virtual('get_context', returns='context', const=True),
Shucai Xiao's avatar
Shucai Xiao committed
113
114
115
116
117
118
119
120
121
122
123
124
     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
125
126
127
)
%>

Paul's avatar
Paul committed
128
129
#endif

Paul's avatar
Paul committed
130
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
131
} // namespace migraphx
Paul's avatar
Paul committed
132
133

#endif