cxx11_tensor_padding_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 
16 #define EIGEN_TEST_NO_LONGDOUBLE
17 #define EIGEN_TEST_NO_COMPLEX
18 
19 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
20 #define EIGEN_USE_SYCL
21 
22 
23 #include "main.h"
24 #include <unsupported/Eigen/CXX11/Tensor>
25 
26 using Eigen::array;
27 using Eigen::SyclDevice;
28 using Eigen::Tensor;
29 using Eigen::TensorMap;
30 
31 
32 template<typename DataType, int DataLayout, typename IndexType>
33 static void test_simple_padding(const Eigen::SyclDevice& sycl_device)
34 {
35 
36  IndexType sizeDim1 = 2;
37  IndexType sizeDim2 = 3;
38  IndexType sizeDim3 = 5;
39  IndexType sizeDim4 = 7;
40  array<IndexType, 4> tensorRange = {{sizeDim1, sizeDim2, sizeDim3, sizeDim4}};
41 
43  tensor.setRandom();
44 
46  paddings[0] = std::make_pair(0, 0);
47  paddings[1] = std::make_pair(2, 1);
48  paddings[2] = std::make_pair(3, 4);
49  paddings[3] = std::make_pair(0, 0);
50 
51  IndexType padedSizeDim1 = 2;
52  IndexType padedSizeDim2 = 6;
53  IndexType padedSizeDim3 = 12;
54  IndexType padedSizeDim4 = 7;
55  array<IndexType, 4> padedtensorRange = {{padedSizeDim1, padedSizeDim2, padedSizeDim3, padedSizeDim4}};
56 
57  Tensor<DataType, 4, DataLayout, IndexType> padded(padedtensorRange);
58 
59 
60  DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(tensor.size()*sizeof(DataType)));
61  DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(padded.size()*sizeof(DataType)));
62  TensorMap<Tensor<DataType, 4,DataLayout,IndexType>> gpu1(gpu_data1, tensorRange);
63  TensorMap<Tensor<DataType, 4,DataLayout,IndexType>> gpu2(gpu_data2, padedtensorRange);
64 
65  VERIFY_IS_EQUAL(padded.dimension(0), 2+0);
66  VERIFY_IS_EQUAL(padded.dimension(1), 3+3);
67  VERIFY_IS_EQUAL(padded.dimension(2), 5+7);
68  VERIFY_IS_EQUAL(padded.dimension(3), 7+0);
69  sycl_device.memcpyHostToDevice(gpu_data1, tensor.data(),(tensor.size())*sizeof(DataType));
70  gpu2.device(sycl_device)=gpu1.pad(paddings);
71  sycl_device.memcpyDeviceToHost(padded.data(), gpu_data2,(padded.size())*sizeof(DataType));
72  for (IndexType i = 0; i < padedSizeDim1; ++i) {
73  for (IndexType j = 0; j < padedSizeDim2; ++j) {
74  for (IndexType k = 0; k < padedSizeDim3; ++k) {
75  for (IndexType l = 0; l < padedSizeDim4; ++l) {
76  if (j >= 2 && j < 5 && k >= 3 && k < 8) {
77  VERIFY_IS_EQUAL(padded(i,j,k,l), tensor(i,j-2,k-3,l));
78  } else {
79  VERIFY_IS_EQUAL(padded(i,j,k,l), 0.0f);
80  }
81  }
82  }
83  }
84  }
85  sycl_device.deallocate(gpu_data1);
86  sycl_device.deallocate(gpu_data2);
87 }
88 
89 template<typename DataType, int DataLayout, typename IndexType>
90 static void test_padded_expr(const Eigen::SyclDevice& sycl_device)
91 {
92  IndexType sizeDim1 = 2;
93  IndexType sizeDim2 = 3;
94  IndexType sizeDim3 = 5;
95  IndexType sizeDim4 = 7;
96  array<IndexType, 4> tensorRange = {{sizeDim1, sizeDim2, sizeDim3, sizeDim4}};
97 
99  tensor.setRandom();
100 
102  paddings[0] = std::make_pair(0, 0);
103  paddings[1] = std::make_pair(2, 1);
104  paddings[2] = std::make_pair(3, 4);
105  paddings[3] = std::make_pair(0, 0);
106 
107  Eigen::DSizes<IndexType, 2> reshape_dims;
108  reshape_dims[0] = 12;
109  reshape_dims[1] = 84;
110 
111 
113 
114  DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(tensor.size()*sizeof(DataType)));
115  DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(result.size()*sizeof(DataType)));
116  TensorMap<Tensor<DataType, 4,DataLayout,IndexType>> gpu1(gpu_data1, tensorRange);
117  TensorMap<Tensor<DataType, 2,DataLayout,IndexType>> gpu2(gpu_data2, reshape_dims);
118 
119 
120  sycl_device.memcpyHostToDevice(gpu_data1, tensor.data(),(tensor.size())*sizeof(DataType));
121  gpu2.device(sycl_device)=gpu1.pad(paddings).reshape(reshape_dims);
122  sycl_device.memcpyDeviceToHost(result.data(), gpu_data2,(result.size())*sizeof(DataType));
123 
124  for (IndexType i = 0; i < 2; ++i) {
125  for (IndexType j = 0; j < 6; ++j) {
126  for (IndexType k = 0; k < 12; ++k) {
127  for (IndexType l = 0; l < 7; ++l) {
128  const float result_value = DataLayout == ColMajor ?
129  result(i+2*j,k+12*l) : result(j+6*i,l+7*k);
130  if (j >= 2 && j < 5 && k >= 3 && k < 8) {
131  VERIFY_IS_EQUAL(result_value, tensor(i,j-2,k-3,l));
132  } else {
133  VERIFY_IS_EQUAL(result_value, 0.0f);
134  }
135  }
136  }
137  }
138  }
139  sycl_device.deallocate(gpu_data1);
140  sycl_device.deallocate(gpu_data2);
141 }
142 
143 template<typename DataType, typename dev_Selector> void sycl_padding_test_per_device(dev_Selector s){
144  QueueInterface queueInterface(s);
145  auto sycl_device = Eigen::SyclDevice(&queueInterface);
146  test_simple_padding<DataType, RowMajor, int64_t>(sycl_device);
147  test_simple_padding<DataType, ColMajor, int64_t>(sycl_device);
148  test_padded_expr<DataType, RowMajor, int64_t>(sycl_device);
149  test_padded_expr<DataType, ColMajor, int64_t>(sycl_device);
150 
151 }
152 EIGEN_DECLARE_TEST(cxx11_tensor_padding_sycl)
153 {
154  for (const auto& device :Eigen::get_sycl_supported_devices()) {
155  CALL_SUBTEST(sycl_padding_test_per_device<float>(device));
156  }
157 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:103
int array[24]
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
EIGEN_DECLARE_TEST(cxx11_tensor_padding_sycl)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorReshapingOp< const NewDimensions, const TensorMap< PlainObjectType, Options_, MakePointer_ > > reshape(const NewDimensions &newDimensions) const
Definition: TensorBase.h:1055
static const Line3 l(Rot3(), 1, 1)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Values result
A tensor expression mapping an existing array of data.
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
RealScalar s
TensorDevice< TensorMap< PlainObjectType, Options_, MakePointer_ >, DeviceType > device(const DeviceType &dev)
Definition: TensorBase.h:1145
void sycl_padding_test_per_device(dev_Selector s)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
static void test_padded_expr(const Eigen::SyclDevice &sycl_device)
static void test_simple_padding(const Eigen::SyclDevice &sycl_device)
std::ptrdiff_t j
static const int DataLayout
The tensor class.
Definition: Tensor.h:63


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:08