1/*******************************************************************************
2* Copyright 2021-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_GENERIC_REORDER_HPP
18#define GPU_OCL_GENERIC_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 generic_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:generic:any", generic_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 && src_engine->kind() == engine_kind::gpu && attr_ok()
46 && extra_ok();
47 if (!ok) return status::unimplemented;
48
49 if (memory_desc_wrapper(src_md()).has_runtime_dims_or_strides())
50 return status::unimplemented;
51
52 auto *compute_engine = utils::downcast<compute::compute_engine_t *>(
53 dst_engine->kind() == engine_kind::gpu ? dst_engine
54 : src_engine);
55
56 ok = ok
57 && compute_engine->mayiuse(
58 compute::device_ext_t::intel_subgroups)
59 && !memory_desc_ndims_ok(src_md(), dst_md())
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
69 if (!ok) return status::unimplemented;
70
71 status_t status = init_conf(engine);
72 if (status != status::success) return status;
73 init_scratchpad();
74
75 return status::success;
76 }
77
78 status_t init_conf(engine_t *engine);
79 void init_scratchpad();
80 status_t init_kernel_ctx(compute::kernel_ctx_t &kernel_ctx) const;
81
82 reorder_conf_t conf;
83
84 private:
85 DECLARE_GPU_REORDER_CREATE();
86 };
87
88 status_t init(engine_t *engine) override {
89 compute::kernel_ctx_t kernel_ctx;
90
91 auto status = pd()->init_kernel_ctx(kernel_ctx);
92 if (status != status::success) return status;
93
94 const auto &conf = pd()->conf;
95 if (conf.nelems == 0) return status::success;
96
97 create_kernel(engine, &kernel_, "generic_reorder", kernel_ctx);
98 if (!kernel_) return status::runtime_error;
99 return status::success;
100 }
101
102 status_t execute(const exec_ctx_t &ctx) const override;
103
104private:
105 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
106 compute::kernel_t kernel_;
107};
108
109} // namespace ocl
110} // namespace gpu
111} // namespace impl
112} // namespace dnnl
113
114#endif
115