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 #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::array;
24 using Eigen::SyclDevice;
25 using Eigen::Tensor;
26 using Eigen::TensorMap;
27
28 using Eigen::Tensor;
29 using Eigen::RowMajor;
30 template <typename DataType, int DataLayout, typename IndexType>
test_tanh_sycl(const Eigen::SyclDevice & sycl_device)31 static void test_tanh_sycl(const Eigen::SyclDevice &sycl_device)
32 {
33
34 IndexType sizeDim1 = 4;
35 IndexType sizeDim2 = 4;
36 IndexType sizeDim3 = 1;
37 array<IndexType, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}};
38 Tensor<DataType, 3, DataLayout, IndexType> in(tensorRange);
39 Tensor<DataType, 3, DataLayout, IndexType> out(tensorRange);
40 Tensor<DataType, 3, DataLayout, IndexType> out_cpu(tensorRange);
41
42 in = in.random();
43
44 DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(in.size()*sizeof(DataType)));
45 DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(out.size()*sizeof(DataType)));
46
47 TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu1(gpu_data1, tensorRange);
48 TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu2(gpu_data2, tensorRange);
49
50 sycl_device.memcpyHostToDevice(gpu_data1, in.data(),(in.size())*sizeof(DataType));
51 gpu2.device(sycl_device) = gpu1.tanh();
52 sycl_device.memcpyDeviceToHost(out.data(), gpu_data2,(out.size())*sizeof(DataType));
53
54 out_cpu=in.tanh();
55
56 for (int i = 0; i < in.size(); ++i) {
57 VERIFY_IS_APPROX(out(i), out_cpu(i));
58 }
59 }
60 template <typename DataType, int DataLayout, typename IndexType>
test_sigmoid_sycl(const Eigen::SyclDevice & sycl_device)61 static void test_sigmoid_sycl(const Eigen::SyclDevice &sycl_device)
62 {
63
64 IndexType sizeDim1 = 4;
65 IndexType sizeDim2 = 4;
66 IndexType sizeDim3 = 1;
67 array<IndexType, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}};
68 Tensor<DataType, 3, DataLayout, IndexType> in(tensorRange);
69 Tensor<DataType, 3, DataLayout, IndexType> out(tensorRange);
70 Tensor<DataType, 3, DataLayout, IndexType> out_cpu(tensorRange);
71
72 in = in.random();
73
74 DataType* gpu_data1 = static_cast<DataType*>(sycl_device.allocate(in.size()*sizeof(DataType)));
75 DataType* gpu_data2 = static_cast<DataType*>(sycl_device.allocate(out.size()*sizeof(DataType)));
76
77 TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu1(gpu_data1, tensorRange);
78 TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu2(gpu_data2, tensorRange);
79
80 sycl_device.memcpyHostToDevice(gpu_data1, in.data(),(in.size())*sizeof(DataType));
81 gpu2.device(sycl_device) = gpu1.sigmoid();
82 sycl_device.memcpyDeviceToHost(out.data(), gpu_data2,(out.size())*sizeof(DataType));
83
84 out_cpu=in.sigmoid();
85
86 for (int i = 0; i < in.size(); ++i) {
87 VERIFY_IS_APPROX(out(i), out_cpu(i));
88 }
89 }
90
91
sycl_computing_test_per_device(dev_Selector s)92 template<typename DataType, typename dev_Selector> void sycl_computing_test_per_device(dev_Selector s){
93 QueueInterface queueInterface(s);
94 auto sycl_device = Eigen::SyclDevice(&queueInterface);
95 test_tanh_sycl<DataType, RowMajor, int64_t>(sycl_device);
96 test_tanh_sycl<DataType, ColMajor, int64_t>(sycl_device);
97 test_sigmoid_sycl<DataType, RowMajor, int64_t>(sycl_device);
98 test_sigmoid_sycl<DataType, ColMajor, int64_t>(sycl_device);
99 }
100
EIGEN_DECLARE_TEST(cxx11_tensor_math_sycl)101 EIGEN_DECLARE_TEST(cxx11_tensor_math_sycl) {
102 for (const auto& device :Eigen::get_sycl_supported_devices()) {
103 CALL_SUBTEST(sycl_computing_test_per_device<float>(device));
104 }
105 }
106