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_combined_REDUCTION_HPP
18#define GPU_combined_REDUCTION_HPP
19
20#include "common/c_types_map.hpp"
21#include "common/primitive.hpp"
22#include "common/type_helpers.hpp"
23#include "common/utils.hpp"
24#include "gpu/compute/compute.hpp"
25#include "gpu/gpu_primitive.hpp"
26#include "gpu/gpu_reduction_pd.hpp"
27#include "gpu/gpu_resource.hpp"
28#include "gpu/primitive_conf.hpp"
29
30#include <iostream>
31
32namespace dnnl {
33namespace impl {
34namespace gpu {
35namespace ocl {
36struct combined_reduction_t : public gpu_primitive_t {
37 using gpu_primitive_t::gpu_primitive_t;
38 struct pd_t : public gpu_reduction_pd_t {
39 using gpu_reduction_pd_t::gpu_reduction_pd_t;
40
41 DECLARE_COMMON_PD_T("ocl:combined", combined_reduction_t);
42
43 status_t init(engine_t *engine) {
44 using smask_t = primitive_attr_t::skip_mask_t;
45 bool ok = set_default_params() == status::success
46 && attr_.has_default_values(smask_t::gpu_attr)
47 && !memory_desc_ndims_ok(src_md(), dst_md())
48 && post_ops_with_binary_ok(attr(), dst_md()->data_type, 5)
49 && attr_.set_default_formats(dst_md(0)) == status::success;
50 if (!ok) return status::unimplemented;
51
52 CHECK(init_conf(engine));
53 init_scratchpad();
54
55 return status::success;
56 }
57
58 status_t init_conf(engine_t *engine);
59 status_t init_kernel_ctx(compute::kernel_ctx_t &kernel_ctx,
60 const reduction_phase_t &phase) const;
61 void init_scratchpad();
62
63 reduction_conf_t conf;
64 };
65
66 status_t init(engine_t *engine) override {
67 auto &phases = pd()->conf.phases;
68
69 status_t status;
70 for (auto &phase : phases) {
71 compute::kernel_ctx_t kernel_ctx(pd()->attr());
72 status = pd()->init_kernel_ctx(kernel_ctx, phase);
73 CHECK(status);
74 compute::kernel_t kernel;
75 status = create_kernel(
76 engine, &kernel, "combined_reduce", kernel_ctx);
77 CHECK(status);
78 kernels.push_back(kernel);
79 }
80
81 return status::success;
82 }
83
84 virtual status_t execute(const exec_ctx_t &ctx) const override {
85 return execute_combined(ctx);
86 }
87
88private:
89 status_t execute_combined(const exec_ctx_t &ctx) const;
90 const pd_t *pd() const { return (const pd_t *)primitive_t::pd().get(); }
91
92 std::vector<compute::kernel_t> kernels;
93};
94
95} // namespace ocl
96} // namespace gpu
97} // namespace impl
98} // namespace dnnl
99
100#endif
101