1/*******************************************************************************
2* Copyright 2020-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#include "dnnl_test_common.hpp"
18#include "gtest/gtest.h"
19
20#include "oneapi/dnnl/dnnl.hpp"
21
22namespace dnnl {
23
24using dt = memory::data_type;
25using tag = memory::format_tag;
26
27// This test checks that globally defined primitive_t object will be
28// successfully destroyed after finishing the program despite the order of
29// internal objects destruction.
30// The cause was thread-local non-trivially-constructed object in
31// global_scratchpad_t object which got destroyed before global_scratchpad_t
32// causing a crash.
33class global_scratchpad_t : public ::testing::Test {};
34
35struct conv_ctx_t {
36 conv_ctx_t() : eng_(engine::kind::cpu, 0) {}
37
38 struct conv_t {
39 conv_t() = default;
40
41 memory::desc src_md;
42 memory::desc wei_md;
43 memory::desc dst_md;
44 convolution_forward::primitive_desc pd;
45 memory src_mem;
46 memory wei_mem;
47 memory dst_mem;
48 primitive prim;
49 };
50
51 void Setup(const memory::dims &src_dims, const memory::dims &wei_dims,
52 const memory::dims &dst_dims, const memory::dims &strides_dims,
53 const memory::dims &dilations_dims,
54 const memory::dims &padding_left,
55 const memory::dims &padding_right) {
56 c_.src_md = memory::desc(src_dims, dt::f32, tag::any);
57 c_.wei_md = memory::desc(wei_dims, dt::f32, tag::any);
58 c_.dst_md = memory::desc(dst_dims, dt::f32, tag::any);
59
60 c_.pd = convolution_forward::primitive_desc(eng_, prop_kind::forward,
61 algorithm::convolution_direct, c_.src_md, c_.wei_md, c_.dst_md,
62 strides_dims, dilations_dims, padding_left, padding_right);
63
64 c_.src_mem = test::make_memory(c_.pd.src_desc(), eng_);
65 c_.wei_mem = test::make_memory(c_.pd.weights_desc(), eng_);
66 c_.dst_mem = test::make_memory(c_.pd.dst_desc(), eng_);
67
68 c_.prim = convolution_forward(c_.pd);
69 }
70
71 engine eng_;
72 struct conv_t c_;
73};
74
75conv_ctx_t global_conv_ctx1;
76conv_ctx_t global_conv_ctx2;
77
78HANDLE_EXCEPTIONS_FOR_TEST(global_scratchpad_t, TestGlobalScratchpad) {
79#if defined(DNNL_WITH_SYCL) && defined(TEST_DNNL_DPCPP_BUFFER)
80 // It seems static USM data doesn't get along with OpenCL runtime.
81 // TODO: investigate.
82 if (get_test_engine_kind() == engine::kind::gpu) return;
83#endif
84
85 memory::dims src1 = {1, 1, 3, 4};
86 memory::dims wei1 = {1, 1, 3, 3};
87 memory::dims dst1 = {1, 1, 8, 5};
88 memory::dims str1 = {1, 1};
89 memory::dims dil1 = {0, 0};
90 memory::dims pad_l1 = {3, 1};
91 memory::dims pad_r1 = {4, 2};
92 global_conv_ctx1.Setup(src1, wei1, dst1, str1, dil1, pad_l1, pad_r1);
93
94 memory::dims src2 = {256, 3, 227, 227};
95 memory::dims wei2 = {96, 3, 11, 11};
96 memory::dims dst2 = {256, 96, 55, 55};
97 memory::dims str2 = {4, 4};
98 memory::dims dil2 = {0, 0};
99 memory::dims pad_l2 = {0, 0};
100 memory::dims pad_r2 = {0, 0};
101 global_conv_ctx2.Setup(src2, wei2, dst2, str2, dil2, pad_l2, pad_r2);
102
103 // if something goes wrong, test should return 139 on Linux.
104};
105
106} // namespace dnnl
107