1/*******************************************************************************
2* Copyright 2019-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.h"
21
22#include <algorithm>
23#include <memory>
24#include <sstream>
25#include <vector>
26
27namespace dnnl {
28
29class submemory_test_cpp_t
30 : public ::testing::TestWithParam<dnnl_engine_kind_t> {};
31
32HANDLE_EXCEPTIONS_FOR_TEST_P(submemory_test_cpp_t, SubmemoryMemoryInteraction) {
33
34#ifdef DNNL_SYCL_HIP
35 SKIP_IF(true,
36 "AllEngineKinds/submemory_test_cpp_t.SubmemoryMemoryInteraction/"
37 "gpu is skipped for HIP because of unimplemented Reorder");
38#endif
39 auto engine_kind = static_cast<engine::kind>(GetParam());
40
41 SKIP_IF(engine::get_count(engine_kind) == 0,
42 "Engine kind is not supported");
43
44 engine eng(engine_kind, 0);
45
46 const memory::dim dst_offset = 1;
47 const memory::dim copy_size = 1;
48
49 float src_buf[1] = {35};
50 auto src = test::make_memory(
51 {{1}, memory::data_type::f32, memory::format_tag::a}, eng);
52 {
53 auto mapped_src_ptr = map_memory<float>(src);
54 GTEST_EXPECT_NE(mapped_src_ptr, nullptr);
55 std::copy(src_buf, src_buf + sizeof(src_buf) / sizeof(src_buf[0]),
56 static_cast<float *>(mapped_src_ptr));
57 }
58
59 float dst_buf[2] = {1, 0};
60 auto dst = test::make_memory(
61 {{2}, memory::data_type::f32, memory::format_tag::a}, eng);
62 {
63 auto mapped_dst_ptr = map_memory<float>(dst);
64 GTEST_EXPECT_NE(mapped_dst_ptr, nullptr);
65 std::copy(dst_buf, dst_buf + sizeof(dst_buf) / sizeof(dst_buf[0]),
66 static_cast<float *>(mapped_dst_ptr));
67 }
68
69 memory::desc dst_submemory
70 = dst.get_desc().submemory_desc({copy_size}, {dst_offset});
71
72 reorder::primitive_desc reorder_pd(
73 eng, src.get_desc(), eng, dst_submemory, primitive_attr());
74 reorder reorder_prim(reorder_pd);
75
76 stream strm(eng);
77 reorder_prim.execute(strm, src, dst);
78 strm.wait();
79
80 dst_buf[dst_offset] = src_buf[0];
81 {
82 auto mapped_dst_ptr = map_memory<float>(dst);
83 GTEST_EXPECT_NE(mapped_dst_ptr, nullptr);
84 for (size_t i = 0; i < sizeof(dst_buf) / sizeof(dst_buf[0]); ++i)
85 ASSERT_EQ(mapped_dst_ptr[i], dst_buf[i]) << "at position " << i;
86 }
87}
88
89namespace {
90struct print_to_string_param_name_t {
91 template <class ParamType>
92 std::string operator()(
93 const ::testing::TestParamInfo<ParamType> &info) const {
94 return to_string(info.param);
95 }
96};
97
98auto all_engine_kinds = ::testing::Values(dnnl_cpu, dnnl_gpu);
99
100} // namespace
101
102INSTANTIATE_TEST_SUITE_P(AllEngineKinds, submemory_test_cpp_t, all_engine_kinds,
103 print_to_string_param_name_t());
104
105} // namespace dnnl
106