1/*******************************************************************************
2* Copyright 2019-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_CROSS_ENGINE_REORDER_HPP
18#define GPU_OCL_CROSS_ENGINE_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/ocl/ocl_utils.hpp"
27
28namespace dnnl {
29namespace impl {
30namespace gpu {
31namespace ocl {
32
33// Cross-engine reorder manages all reorders between GPU and CPU engines.
34//
35// For CPU -> GPU reorder, it includes 2 steps:
36// 1. CPU -> GPU copying
37// 2. GPU reorder
38//
39// For GPU -> CPU reorder, it includes 2 steps:
40// 1. GPU reorder
41// 2. GPU -> CPU copying
42struct cross_engine_reorder_t : public gpu_primitive_t {
43 using gpu_primitive_t::gpu_primitive_t;
44 struct pd_t : public gpu_reorder_pd_t {
45 using gpu_reorder_pd_t::gpu_reorder_pd_t;
46
47 DECLARE_COMMON_PD_T("ocl:cross_engine::any", cross_engine_reorder_t);
48
49 status_t init(
50 engine_t *engine, engine_t *src_engine, engine_t *dst_engine);
51
52 std::shared_ptr<primitive_desc_t> reorder_pd_;
53 engine_kind_t reorder_engine_kind_ = engine_kind::gpu;
54 bool do_reorder_ = true;
55
56 private:
57 void init_scratchpad();
58 DECLARE_GPU_REORDER_CREATE();
59 };
60
61 status_t init(engine_t *engine) override {
62 if (!pd()->do_reorder_) return status::success;
63 return create_nested_primitive(reorder_, pd()->reorder_pd_, engine);
64 }
65
66 status_t execute(const exec_ctx_t &ctx) const override;
67
68private:
69 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
70 std::shared_ptr<primitive_t> reorder_;
71};
72
73} // namespace ocl
74} // namespace gpu
75} // namespace impl
76} // namespace dnnl
77
78#endif
79