1/*******************************************************************************
2* Copyright 2018 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#include <assert.h>
18
19#include "mkldnn.h"
20
21#include "c_types_map.hpp"
22#include "engine.hpp"
23#include "type_helpers.hpp"
24#include "utils.hpp"
25
26#include "sum_pd.hpp"
27
28using namespace mkldnn::impl;
29using namespace mkldnn::impl::utils;
30using namespace mkldnn::impl::status;
31
32status_t mkldnn_sum_primitive_desc_create(primitive_desc_t **sum_pd,
33 const memory_desc_t *dst_md, int n, const float *scales,
34 const memory_desc_t *src_mds, const primitive_attr_t *attr,
35 engine_t *engine) {
36 bool args_ok = !any_null(sum_pd, src_mds, scales) && n > 0;
37 if (!args_ok) return invalid_arguments;
38
39 const primitive_attr_t dummy_attr;
40 if (attr == NULL)
41 attr = &dummy_attr;
42
43 const int ndims = src_mds[0].ndims;
44 const dims_t &dims = src_mds[0].dims;
45 const data_type_t dt = src_mds[0].data_type;
46
47 for (int i = 1; i < n; ++i) {
48 if (src_mds[i].ndims != ndims) return invalid_arguments;
49 for (int d = 0; d < ndims; ++d) {
50 if (src_mds[i].dims[d] != dims[d])
51 return invalid_arguments;
52 }
53 if (src_mds[i].data_type != dt) return invalid_arguments;
54 }
55
56 memory_desc_t dummy_dst_md;
57 if (dst_md) {
58 if (dst_md->ndims != ndims) return invalid_arguments;
59 for (int d = 0; d < ndims; ++d) {
60 if (dst_md->dims[d] != dims[d])
61 return invalid_arguments;
62 }
63 } else {
64 dummy_dst_md = src_mds[0];
65 dummy_dst_md.format_kind = format_kind::any;
66 dst_md = &dummy_dst_md;
67 }
68
69 auto s_pd = reinterpret_cast<sum_pd_t **>(sum_pd);
70
71 for (auto s = engine->get_sum_implementation_list(); *s; ++s) {
72 if ((*s)(s_pd, engine, attr, dst_md, n, scales, src_mds) == success) {
73 (*s_pd)->init_info();
74 (*s_pd)->init_scratchpad_md();
75 return success;
76 }
77 }
78 return unimplemented;
79}
80