1/*******************************************************************************
2* Copyright 2019-2022 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_REF_REORDER_HPP
18#define GPU_OCL_REF_REORDER_HPP
19
20#include "common/c_types_map.hpp"
21#include "common/memory.hpp"
22#include "common/primitive.hpp"
23#include "common/utils.hpp"
24#include "gpu/gpu_primitive.hpp"
25#include "gpu/gpu_reorder_pd.hpp"
26#include "gpu/gpu_resource.hpp"
27#include "gpu/ocl/ocl_utils.hpp"
28#include "gpu/primitive_conf.hpp"
29
30namespace dnnl {
31namespace impl {
32namespace gpu {
33namespace ocl {
34
35struct ref_reorder_t : public gpu_primitive_t {
36 using gpu_primitive_t::gpu_primitive_t;
37 struct pd_t : public gpu_reorder_pd_t {
38 using gpu_reorder_pd_t::gpu_reorder_pd_t;
39
40 DECLARE_COMMON_PD_T("ocl:ref:any", ref_reorder_t);
41
42 status_t init(
43 engine_t *engine, engine_t *src_engine, engine_t *dst_engine) {
44 bool ok = src_engine == dst_engine
45 && !memory_desc_ndims_ok(src_md(), dst_md())
46 && src_engine->kind() == engine_kind::gpu && attr_ok()
47 && extra_ok();
48 if (!ok) return status::unimplemented;
49
50 if (memory_desc_wrapper(src_md()).has_runtime_dims_or_strides())
51 return status::unimplemented;
52
53 auto *compute_engine = utils::downcast<compute::compute_engine_t *>(
54 dst_engine->kind() == engine_kind::gpu ? dst_engine
55 : src_engine);
56
57 ok = ok
58 && compute_engine->mayiuse(
59 compute::device_ext_t::intel_subgroups)
60 && IMPLICATION(
61 utils::one_of(data_type::f16, src_md()->data_type,
62 dst_md()->data_type),
63 compute_engine->mayiuse(
64 compute::device_ext_t::khr_fp16)
65 && compute_engine->mayiuse(
66 compute::device_ext_t::
67 intel_subgroups_short))
68 && IMPLICATION(
69 utils::one_of(data_type::f64, src_md()->data_type,
70 dst_md()->data_type),
71 compute_engine->mayiuse(
72 compute::device_ext_t::khr_fp64)
73 && attr()->post_ops_.has_default_values());
74
75 if (!ok) return status::unimplemented;
76
77 status_t status = init_conf(engine);
78 if (status != status::success) return status;
79 init_scratchpad();
80
81 return status::success;
82 }
83
84 status_t init_conf(engine_t *engine);
85 void init_scratchpad();
86 status_t init_kernel_ctx(compute::kernel_ctx_t &kernel_ctx) const;
87
88 reorder_conf_t conf;
89
90 private:
91 DECLARE_GPU_REORDER_CREATE();
92 };
93
94 status_t init(engine_t *engine) override {
95 compute::kernel_ctx_t kernel_ctx;
96
97 auto status = pd()->init_kernel_ctx(kernel_ctx);
98 if (status != status::success) return status;
99
100 const auto &conf = pd()->conf;
101 if (conf.nelems == 0) return status::success;
102
103 create_kernel(engine, &kernel_, "ref_reorder", kernel_ctx);
104 if (!kernel_) return status::runtime_error;
105 return status::success;
106 }
107
108 status_t execute(const exec_ctx_t &ctx) const override;
109
110private:
111 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
112 compute::kernel_t kernel_;
113};
114
115} // namespace ocl
116} // namespace gpu
117} // namespace impl
118} // namespace dnnl
119
120#endif
121