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 "concat_pd.hpp"
27
28using namespace mkldnn::impl;
29using namespace mkldnn::impl::utils;
30using namespace mkldnn::impl::status;
31
32status_t mkldnn_concat_primitive_desc_create(primitive_desc_t **concat_pd,
33 const memory_desc_t *dst_md, int n, int concat_dim,
34 const memory_desc_t *src_mds,
35 const primitive_attr_t *attr,
36 engine_t *engine) {
37 bool args_ok = !any_null(concat_pd, src_mds) && n > 0;
38 if (!args_ok) return invalid_arguments;
39
40 const primitive_attr_t dummy_attr;
41 if (attr == NULL)
42 attr = &dummy_attr;
43
44 const int ndims = src_mds[0].ndims;
45 const dims_t &dims = src_mds[0].dims;
46 const data_type_t dt = src_mds[0].data_type;
47
48 int concat_dim_sz = dims[concat_dim];
49 for (int i = 1; i < n; ++i) {
50 if (src_mds[i].ndims != ndims) return invalid_arguments;
51 for (int d = 0; d < ndims; ++d) {
52 if (d == concat_dim) continue;
53 if (src_mds[i].dims[d] != dims[d])
54 return invalid_arguments;
55 }
56 if (src_mds[i].data_type != dt) return invalid_arguments;
57 concat_dim_sz += src_mds[i].dims[concat_dim];
58 }
59
60 memory_desc_t dummy_dst_md;
61 if (dst_md) {
62 if (dst_md->ndims != ndims) return invalid_arguments;
63 for (int d = 0; d < ndims; ++d) {
64 if (dst_md->dims[d] !=
65 (d == concat_dim ? concat_dim_sz : dims[d]))
66 return invalid_arguments;
67 }
68 } else {
69 dummy_dst_md = src_mds[0];
70 dummy_dst_md.dims[concat_dim] = concat_dim_sz;
71 dummy_dst_md.format_kind = format_kind::any;
72 dst_md = &dummy_dst_md;
73 }
74
75 auto c_pd = reinterpret_cast<concat_pd_t **>(concat_pd);
76
77 for (auto c = engine->get_concat_implementation_list(); *c; ++c) {
78 if ((*c)(c_pd, engine, attr, dst_md, n, concat_dim, src_mds)
79 == success) {
80 (*c_pd)->init_info();
81 (*c_pd)->init_scratchpad_md();
82 return success;
83 }
84 }
85 return unimplemented;
86}
87