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#include <thread>
18
19#include "dnnl_test_common.hpp"
20#include "gtest/gtest.h"
21
22#include "oneapi/dnnl/dnnl.hpp"
23
24namespace dnnl {
25
26class engine_test_t : public ::testing::TestWithParam<engine::kind> {
27protected:
28 void SetUp() override {}
29 engine::kind eng_kind;
30};
31
32HANDLE_EXCEPTIONS_FOR_TEST_P(engine_test_t, TestMultithreading) {
33
34#if DNNL_CPU_RUNTIME == DNNL_RUNTIME_NONE
35 if (eng_kind == engine::kind::cpu) {
36 EXPECT_EQ((int)engine::get_count(eng_kind), 0);
37 EXPECT_ANY_THROW(engine eng(eng_kind, 0));
38 return;
39 }
40#endif
41
42 engine::kind eng_kind = GetParam();
43 SKIP_IF(engine::get_count(eng_kind) == 0, "Engine is not found.");
44 engine eng {eng_kind, 0};
45
46 memory::dims tz = {1};
47 memory::desc mem_d(tz, memory::data_type::f32, memory::format_tag::x);
48 auto mem = test::make_memory(mem_d, eng);
49
50 {
51 auto *ptr = mem.map_data<float>();
52 GTEST_EXPECT_NE(ptr, nullptr);
53 for (size_t i = 0; i < mem_d.get_size() / sizeof(float); ++i)
54 ptr[i] = float(i) * (i % 2 == 0 ? 1 : -1);
55 mem.unmap_data(ptr);
56 }
57
58 auto eltwise_pd = eltwise_forward::primitive_desc(eng, prop_kind::forward,
59 algorithm::eltwise_relu, mem_d, mem_d, 0.0f);
60
61 std::unique_ptr<eltwise_forward> eltwise;
62 std::thread create(
63 [&]() { eltwise.reset(new eltwise_forward(eltwise_pd)); });
64
65 create.join();
66
67 stream s(eng);
68 std::thread exe([&]() {
69 eltwise->execute(s, {{DNNL_ARG_SRC, mem}, {DNNL_ARG_DST, mem}});
70 s.wait();
71 });
72
73 exe.join();
74}
75
76INSTANTIATE_TEST_SUITE_P(AllEngineKinds, engine_test_t,
77 ::testing::Values(engine::kind::cpu, engine::kind::gpu));
78
79} // namespace dnnl
80