context.hpp 2.89 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_CONTEXT_HPP
#define MIGRAPHX_GUARD_CONTEXT_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 <migraphx/config.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
34
#include <migraphx/value.hpp>
35
#include <migraphx/any_ptr.hpp>
Paul's avatar
Paul committed
36

Paul's avatar
Paul committed
37
namespace migraphx {
Paul's avatar
Paul committed
38
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
39

Paul's avatar
Paul committed
40
41
42
43
44
45
#ifdef DOXYGEN

/// A context is used to store internal data for a `target`. A context is
/// constructed by a target during compilation and passed to the operations
/// during `eval`.
struct context
Paul's avatar
Paul committed
46
{
Paul's avatar
Paul committed
47
48
    /// Wait for any tasks in the context to complete
    void finish() const;
Paul's avatar
Paul committed
49
};
Paul's avatar
Paul committed
50
51
52

#else

Shucai Xiao's avatar
Shucai Xiao committed
53
54
55
56
57
58
59
template <class T>
value to_value_context(const T&)
{
    return value{};
}

template <class T>
60
61
62
63
64
65
66
67
68
void from_value_context(T&, const value&)
{
}

template <class T>
any_ptr get_queue_context(T&)
{
    return {};
}
69

70
71
72
73
74
75
76
template <class T>
void wait_for_context(T&, any_ptr)
{
}

template <class T>
void finish_on_context(T&, any_ptr){}
Shucai Xiao's avatar
Shucai Xiao committed
77

Paul's avatar
Paul committed
78
<%
Shucai Xiao's avatar
Shucai Xiao committed
79
80
81
 interface('context',
           virtual('to_value', returns = 'value', const = True, default = 'to_value_context'),
           virtual('from_value', v = 'const value&', default = 'from_value_context'),
82
           virtual('get_queue', returns = 'any_ptr', default = 'get_queue_context'),
83
84
           virtual('wait_for', queue = 'any_ptr', returns = 'void', default = 'wait_for_context'),
           virtual('finish_on', queue = 'any_ptr', returns = 'void', default = 'finish_on_context'),
Shucai Xiao's avatar
Shucai Xiao committed
85
86
87
88
89
90
           virtual('finish', returns = 'void', const = True)) %>

    inline void migraphx_to_value(value& v, const context& ctx)
{
    v = ctx.to_value();
}
91

Shucai Xiao's avatar
Shucai Xiao committed
92
inline void migraphx_from_value(const value& v, context& ctx) { ctx.from_value(v); }
Paul's avatar
Paul committed
93

Paul's avatar
Paul committed
94
95
#endif

Paul's avatar
Paul committed
96
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
97
} // namespace migraphx
Paul's avatar
Paul committed
98
99

#endif