aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/runtime/CL/CLMemoryRegion.h
blob: 6f7c3cd9a8cfca0a967168b5dea3ac99666e4aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2018-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * 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.
 */
#ifndef __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__
#define __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__

#include "arm_compute/core/CL/OpenCL.h"
#include "arm_compute/runtime/IMemoryRegion.h"

#include <cstddef>

namespace arm_compute
{
class CLCoreRuntimeContext;
/** OpenCL memory region interface */
class ICLMemoryRegion : public IMemoryRegion
{
public:
    /** Constructor
     *
     * @param[in] ctx  Runtime context
     * @param[in] size Region size
     */
    ICLMemoryRegion(CLCoreRuntimeContext *ctx, size_t size);
    /** Default Destructor */
    virtual ~ICLMemoryRegion() = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    ICLMemoryRegion(const ICLMemoryRegion &) = delete;
    /** Default move constructor */
    ICLMemoryRegion(ICLMemoryRegion &&) = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
    /** Default move assignment operator */
    ICLMemoryRegion &operator=(ICLMemoryRegion &&) = default;
    /** Returns the underlying CL buffer
     *
     * @return CL memory buffer object
     */
    const cl::Buffer &cl_data() const;
    /** Host/SVM pointer accessor
     *
     * @return Host/SVM pointer base
     */
    virtual void *ptr() = 0;
    /** Enqueue a map operation of the allocated buffer on the given queue.
     *
     * @param[in,out] q        The CL command queue to use for the mapping operation.
     * @param[in]     blocking If true, then the mapping will be ready to use by the time
     *                         this method returns, else it is the caller's responsibility
     *                         to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
     *
     * @return The mapping address.
     */
    virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
    /** Enqueue an unmap operation of the allocated buffer on the given queue.
     *
     * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
     *       the memory is accessed by the device.
     *
     * @param[in,out] q The CL command queue to use for the mapping operation.
     */
    virtual void unmap(cl::CommandQueue &q) = 0;

    // Inherited methods overridden :
    void                          *buffer() override;
    void                          *buffer() const override;
    std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;

protected:
    cl::CommandQueue _queue;
    cl::Context      _ctx;
    void            *_mapping;
    cl::Buffer       _mem;
};

/** OpenCL buffer memory region implementation */
class CLBufferMemoryRegion final : public ICLMemoryRegion
{
public:
    /** Constructor
     *
     * @param[in] ctx   Runtime context
     * @param[in] flags Memory flags
     * @param[in] size  Region size
     */
    CLBufferMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size);
    /** Constructor
     *
     * @param[in] buffer Buffer to be used as a memory region
     * @param[in] ctx    Runtime context
     */
    CLBufferMemoryRegion(const cl::Buffer &buffer, CLCoreRuntimeContext *ctx);

    // Inherited methods overridden :
    void *ptr() final;
    void *map(cl::CommandQueue &q, bool blocking) final;
    void unmap(cl::CommandQueue &q) final;
};

/** OpenCL SVM memory region interface */
class ICLSVMMemoryRegion : public ICLMemoryRegion
{
protected:
    /** Constructor
     *
     * @param[in] ctx       Runtime context
     * @param[in] flags     Memory flags
     * @param[in] size      Region size
     * @param[in] alignment Alignment
     */
    ICLSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);
    /** Destructor */
    virtual ~ICLSVMMemoryRegion();
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
    /** Default move constructor */
    ICLSVMMemoryRegion(ICLSVMMemoryRegion &&) = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
    /** Default move assignment operator */
    ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;

    // Inherited methods overridden :
    void *ptr() override;

protected:
    void *_ptr;
};

/** OpenCL coarse-grain SVM memory region implementation */
class CLCoarseSVMMemoryRegion final : public ICLSVMMemoryRegion
{
public:
    /** Constructor
     *
     * @param[in] ctx       Runtime context
     * @param[in] flags     Memory flags
     * @param[in] size      Region size
     * @param[in] alignment Alignment
     */
    CLCoarseSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);

    // Inherited methods overridden :
    void *map(cl::CommandQueue &q, bool blocking) final;
    void unmap(cl::CommandQueue &q) final;
};

/** OpenCL fine-grain SVM memory region implementation */
class CLFineSVMMemoryRegion final : public ICLSVMMemoryRegion
{
public:
    /** Constructor
     *
     * @param[in] ctx       Runtime context
     * @param[in] flags     Memory flags
     * @param[in] size      Region size
     * @param[in] alignment Alignment
     */
    CLFineSVMMemoryRegion(CLCoreRuntimeContext *ctx, cl_mem_flags flags, size_t size, size_t alignment);

    // Inherited methods overridden :
    void *map(cl::CommandQueue &q, bool blocking) final;
    void unmap(cl::CommandQueue &q) final;
};
} // namespace arm_compute
#endif /* __ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H__ */