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 //
10 // This Source Code Form is subject to the terms of the Mozilla
11 // Public License v. 2.0. If a copy of the MPL was not distributed
12 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13
14 #define EIGEN_TEST_NO_LONGDOUBLE
15 #define EIGEN_TEST_NO_COMPLEX
16
17 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
18 #define EIGEN_USE_SYCL
19
20 #include "main.h"
21 #include <unsupported/Eigen/CXX11/Tensor>
22
23 using Eigen::Tensor;
24
25 // Inflation Definition for each dimension the inflated val would be
26 //((dim-1)*strid[dim] +1)
27
28 // for 1 dimension vector of size 3 with value (4,4,4) with the inflated stride value of 3 would be changed to
29 // tensor of size (2*3) +1 = 7 with the value of
30 // (4, 0, 0, 4, 0, 0, 4).
31
32 template <typename DataType, int DataLayout, typename IndexType>
test_simple_inflation_sycl(const Eigen::SyclDevice & sycl_device)33 void test_simple_inflation_sycl(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 Tensor<DataType, 4, DataLayout,IndexType> tensor(tensorRange);
42 Tensor<DataType, 4, DataLayout,IndexType> no_stride(tensorRange);
43 tensor.setRandom();
44
45 array<IndexType, 4> strides;
46 strides[0] = 1;
47 strides[1] = 1;
48 strides[2] = 1;
49 strides[3] = 1;
50
51
52 const size_t tensorBuffSize =tensor.size()*sizeof(DataType);
53 DataType* gpu_data_tensor = static_cast<DataType*>(sycl_device.allocate(tensorBuffSize));
54 DataType* gpu_data_no_stride = static_cast<DataType*>(sycl_device.allocate(tensorBuffSize));
55
56 TensorMap<Tensor<DataType, 4, DataLayout,IndexType>> gpu_tensor(gpu_data_tensor, tensorRange);
57 TensorMap<Tensor<DataType, 4, DataLayout,IndexType>> gpu_no_stride(gpu_data_no_stride, tensorRange);
58
59 sycl_device.memcpyHostToDevice(gpu_data_tensor, tensor.data(), tensorBuffSize);
60 gpu_no_stride.device(sycl_device)=gpu_tensor.inflate(strides);
61 sycl_device.memcpyDeviceToHost(no_stride.data(), gpu_data_no_stride, tensorBuffSize);
62
63 VERIFY_IS_EQUAL(no_stride.dimension(0), sizeDim1);
64 VERIFY_IS_EQUAL(no_stride.dimension(1), sizeDim2);
65 VERIFY_IS_EQUAL(no_stride.dimension(2), sizeDim3);
66 VERIFY_IS_EQUAL(no_stride.dimension(3), sizeDim4);
67
68 for (IndexType i = 0; i < 2; ++i) {
69 for (IndexType j = 0; j < 3; ++j) {
70 for (IndexType k = 0; k < 5; ++k) {
71 for (IndexType l = 0; l < 7; ++l) {
72 VERIFY_IS_EQUAL(tensor(i,j,k,l), no_stride(i,j,k,l));
73 }
74 }
75 }
76 }
77
78
79 strides[0] = 2;
80 strides[1] = 4;
81 strides[2] = 2;
82 strides[3] = 3;
83
84 IndexType inflatedSizeDim1 = 3;
85 IndexType inflatedSizeDim2 = 9;
86 IndexType inflatedSizeDim3 = 9;
87 IndexType inflatedSizeDim4 = 19;
88 array<IndexType, 4> inflatedTensorRange = {{inflatedSizeDim1, inflatedSizeDim2, inflatedSizeDim3, inflatedSizeDim4}};
89
90 Tensor<DataType, 4, DataLayout, IndexType> inflated(inflatedTensorRange);
91
92 const size_t inflatedTensorBuffSize =inflated.size()*sizeof(DataType);
93 DataType* gpu_data_inflated = static_cast<DataType*>(sycl_device.allocate(inflatedTensorBuffSize));
94 TensorMap<Tensor<DataType, 4, DataLayout, IndexType>> gpu_inflated(gpu_data_inflated, inflatedTensorRange);
95 gpu_inflated.device(sycl_device)=gpu_tensor.inflate(strides);
96 sycl_device.memcpyDeviceToHost(inflated.data(), gpu_data_inflated, inflatedTensorBuffSize);
97
98 VERIFY_IS_EQUAL(inflated.dimension(0), inflatedSizeDim1);
99 VERIFY_IS_EQUAL(inflated.dimension(1), inflatedSizeDim2);
100 VERIFY_IS_EQUAL(inflated.dimension(2), inflatedSizeDim3);
101 VERIFY_IS_EQUAL(inflated.dimension(3), inflatedSizeDim4);
102
103 for (IndexType i = 0; i < inflatedSizeDim1; ++i) {
104 for (IndexType j = 0; j < inflatedSizeDim2; ++j) {
105 for (IndexType k = 0; k < inflatedSizeDim3; ++k) {
106 for (IndexType l = 0; l < inflatedSizeDim4; ++l) {
107 if (i % strides[0] == 0 &&
108 j % strides[1] == 0 &&
109 k % strides[2] == 0 &&
110 l % strides[3] == 0) {
111 VERIFY_IS_EQUAL(inflated(i,j,k,l),
112 tensor(i/strides[0], j/strides[1], k/strides[2], l/strides[3]));
113 } else {
114 VERIFY_IS_EQUAL(0, inflated(i,j,k,l));
115 }
116 }
117 }
118 }
119 }
120 sycl_device.deallocate(gpu_data_tensor);
121 sycl_device.deallocate(gpu_data_no_stride);
122 sycl_device.deallocate(gpu_data_inflated);
123 }
124
sycl_inflation_test_per_device(dev_Selector s)125 template<typename DataType, typename dev_Selector> void sycl_inflation_test_per_device(dev_Selector s){
126 QueueInterface queueInterface(s);
127 auto sycl_device = Eigen::SyclDevice(&queueInterface);
128 test_simple_inflation_sycl<DataType, RowMajor, int64_t>(sycl_device);
129 test_simple_inflation_sycl<DataType, ColMajor, int64_t>(sycl_device);
130 }
EIGEN_DECLARE_TEST(cxx11_tensor_inflation_sycl)131 EIGEN_DECLARE_TEST(cxx11_tensor_inflation_sycl)
132 {
133 for (const auto& device :Eigen::get_sycl_supported_devices()) {
134 CALL_SUBTEST(sycl_inflation_test_per_device<float>(device));
135 }
136 }
137