1/*******************************************************************************
2* Copyright 2021 Intel Corporation
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*******************************************************************************/
16
17#ifndef GPU_OCL_OCL_BUFFER_MEMORY_STORAGE_HPP
18#define GPU_OCL_OCL_BUFFER_MEMORY_STORAGE_HPP
19
20#include <CL/cl.h>
21
22#include "common/c_types_map.hpp"
23#include "common/utils.hpp"
24#include "gpu/ocl/ocl_memory_storage_base.hpp"
25#include "gpu/ocl/ocl_utils.hpp"
26
27namespace dnnl {
28namespace impl {
29namespace gpu {
30namespace ocl {
31
32class ocl_buffer_memory_storage_t : public ocl_memory_storage_base_t {
33public:
34 ocl_buffer_memory_storage_t(engine_t *engine)
35 : ocl_memory_storage_base_t(engine), mem_object_(nullptr) {}
36
37 ocl_buffer_memory_storage_t(
38 engine_t *engine, const memory_storage_t *parent_storage)
39 : ocl_memory_storage_base_t(engine, parent_storage) {}
40
41 status_t get_data_handle(void **handle) const override {
42 *handle = static_cast<void *>(mem_object_.get());
43 return status::success;
44 }
45
46 status_t set_data_handle(void *handle) override {
47 mem_object_ = ocl_wrapper_t<cl_mem>(static_cast<cl_mem>(handle), true);
48 return status::success;
49 }
50
51 status_t map_data(
52 void **mapped_ptr, stream_t *stream, size_t) const override;
53 status_t unmap_data(void *mapped_ptr, stream_t *stream) const override;
54
55 cl_mem mem_object() const { return mem_object_.get(); }
56
57 bool is_host_accessible() const override { return false; }
58
59 std::unique_ptr<memory_storage_t> get_sub_storage(
60 size_t offset, size_t size) const override;
61
62 std::unique_ptr<memory_storage_t> clone() const override;
63
64 memory_kind_t memory_kind() const override { return memory_kind::buffer; }
65
66protected:
67 status_t init_allocate(size_t size) override;
68
69private:
70 cl_mem parent_mem_object() const;
71
72 ocl_wrapper_t<cl_mem> mem_object_;
73 size_t base_offset_ = 0;
74
75 DNNL_DISALLOW_COPY_AND_ASSIGN(ocl_buffer_memory_storage_t);
76};
77
78} // namespace ocl
79} // namespace gpu
80} // namespace impl
81} // namespace dnnl
82
83#endif
84