cxx11_tensor_layout_swap_sycl.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016
5 // Mehdi Goli Codeplay Software Ltd.
6 // Ralph Potter Codeplay Software Ltd.
7 // Luke Iwanski Codeplay Software Ltd.
8 // Contact: <eigen@codeplay.com>
9 // Benoit Steiner <benoit.steiner.goog@gmail.com>
10 //
11 // This Source Code Form is subject to the terms of the Mozilla
12 // Public License v. 2.0. If a copy of the MPL was not distributed
13 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 
15 #define EIGEN_TEST_NO_LONGDOUBLE
16 #define EIGEN_TEST_NO_COMPLEX
17 
18 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
19 #define EIGEN_USE_SYCL
20 
21 #include "main.h"
22 
23 #include <Eigen/CXX11/Tensor>
24 
25 using Eigen::Tensor;
26 
27 template <typename DataType, typename IndexType>
28 static void test_simple_swap_sycl(const Eigen::SyclDevice& sycl_device)
29 {
30  IndexType sizeDim1 = 2;
31  IndexType sizeDim2 = 3;
32  IndexType sizeDim3 = 7;
33  array<IndexType, 3> tensorColRange = {{sizeDim1, sizeDim2, sizeDim3}};
34  array<IndexType, 3> tensorRowRange = {{sizeDim3, sizeDim2, sizeDim1}};
35 
36 
37  Tensor<DataType, 3, ColMajor, IndexType> tensor1(tensorColRange);
38  Tensor<DataType, 3, RowMajor, IndexType> tensor2(tensorRowRange);
39  tensor1.setRandom();
40 
41  DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(tensor1.size()*sizeof(DataType)));
42  DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(tensor2.size()*sizeof(DataType)));
43  TensorMap<Tensor<DataType, 3, ColMajor, IndexType>> gpu1(gpu_data1, tensorColRange);
44  TensorMap<Tensor<DataType, 3, RowMajor, IndexType>> gpu2(gpu_data2, tensorRowRange);
45 
46  sycl_device.memcpyHostToDevice(gpu_data1, tensor1.data(),(tensor1.size())*sizeof(DataType));
47  gpu2.device(sycl_device)=gpu1.swap_layout();
48  sycl_device.memcpyDeviceToHost(tensor2.data(), gpu_data2,(tensor2.size())*sizeof(DataType));
49 
50 
51 // Tensor<float, 3, ColMajor> tensor(2,3,7);
52  //tensor.setRandom();
53 
54 // Tensor<float, 3, RowMajor> tensor2 = tensor.swap_layout();
55  VERIFY_IS_EQUAL(tensor1.dimension(0), tensor2.dimension(2));
56  VERIFY_IS_EQUAL(tensor1.dimension(1), tensor2.dimension(1));
57  VERIFY_IS_EQUAL(tensor1.dimension(2), tensor2.dimension(0));
58 
59  for (IndexType i = 0; i < 2; ++i) {
60  for (IndexType j = 0; j < 3; ++j) {
61  for (IndexType k = 0; k < 7; ++k) {
62  VERIFY_IS_EQUAL(tensor1(i,j,k), tensor2(k,j,i));
63  }
64  }
65  }
66  sycl_device.deallocate(gpu_data1);
67  sycl_device.deallocate(gpu_data2);
68 }
69 
70 template <typename DataType, typename IndexType>
71 static void test_swap_as_lvalue_sycl(const Eigen::SyclDevice& sycl_device)
72 {
73 
74  IndexType sizeDim1 = 2;
75  IndexType sizeDim2 = 3;
76  IndexType sizeDim3 = 7;
77  array<IndexType, 3> tensorColRange = {{sizeDim1, sizeDim2, sizeDim3}};
78  array<IndexType, 3> tensorRowRange = {{sizeDim3, sizeDim2, sizeDim1}};
79 
80  Tensor<DataType, 3, ColMajor, IndexType> tensor1(tensorColRange);
81  Tensor<DataType, 3, RowMajor, IndexType> tensor2(tensorRowRange);
82  tensor1.setRandom();
83 
84  DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(tensor1.size()*sizeof(DataType)));
85  DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(tensor2.size()*sizeof(DataType)));
86  TensorMap<Tensor<DataType, 3, ColMajor, IndexType>> gpu1(gpu_data1, tensorColRange);
87  TensorMap<Tensor<DataType, 3, RowMajor, IndexType>> gpu2(gpu_data2, tensorRowRange);
88 
89  sycl_device.memcpyHostToDevice(gpu_data1, tensor1.data(),(tensor1.size())*sizeof(DataType));
90  gpu2.swap_layout().device(sycl_device)=gpu1;
91  sycl_device.memcpyDeviceToHost(tensor2.data(), gpu_data2,(tensor2.size())*sizeof(DataType));
92 
93 
94 // Tensor<float, 3, ColMajor> tensor(2,3,7);
95 // tensor.setRandom();
96 
97  //Tensor<float, 3, RowMajor> tensor2(7,3,2);
98 // tensor2.swap_layout() = tensor;
99  VERIFY_IS_EQUAL(tensor1.dimension(0), tensor2.dimension(2));
100  VERIFY_IS_EQUAL(tensor1.dimension(1), tensor2.dimension(1));
101  VERIFY_IS_EQUAL(tensor1.dimension(2), tensor2.dimension(0));
102 
103  for (IndexType i = 0; i < 2; ++i) {
104  for (IndexType j = 0; j < 3; ++j) {
105  for (IndexType k = 0; k < 7; ++k) {
106  VERIFY_IS_EQUAL(tensor1(i,j,k), tensor2(k,j,i));
107  }
108  }
109  }
110  sycl_device.deallocate(gpu_data1);
111  sycl_device.deallocate(gpu_data2);
112 }
113 
114 
115 template<typename DataType, typename dev_Selector> void sycl_tensor_layout_swap_test_per_device(dev_Selector s){
116  QueueInterface queueInterface(s);
117  auto sycl_device = Eigen::SyclDevice(&queueInterface);
118  test_simple_swap_sycl<DataType, int64_t>(sycl_device);
119  test_swap_as_lvalue_sycl<DataType, int64_t>(sycl_device);
120 }
121 EIGEN_DECLARE_TEST(cxx11_tensor_layout_swap_sycl)
122 {
123  for (const auto& device :Eigen::get_sycl_supported_devices()) {
124  CALL_SUBTEST(sycl_tensor_layout_swap_test_per_device<float>(device));
125  }
126 }
Eigen::Tensor::dimension
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
s
RealScalar s
Definition: level1_cplx_impl.h:126
Eigen::array
Definition: EmulateArray.h:21
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
test_simple_swap_sycl
static void test_simple_swap_sycl(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_layout_swap_sycl.cpp:28
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_layout_swap_sycl)
Definition: cxx11_tensor_layout_swap_sycl.cpp:121
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
test_swap_as_lvalue_sycl
static void test_swap_as_lvalue_sycl(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_layout_swap_sycl.cpp:71
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setRandom
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
main.h
Eigen::Tensor::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
Eigen::Tensor::size
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:103
sycl_tensor_layout_swap_test_per_device
void sycl_tensor_layout_swap_test_per_device(dev_Selector s)
Definition: cxx11_tensor_layout_swap_sycl.cpp:115
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:20