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 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
17 #define EIGEN_USE_SYCL
18
19 #include "main.h"
20 #include <unsupported/Eigen/CXX11/Tensor>
21
22 template <typename DataType, int DataLayout, typename IndexType>
test_sycl_random_uniform(const Eigen::SyclDevice & sycl_device)23 static void test_sycl_random_uniform(const Eigen::SyclDevice& sycl_device)
24 {
25 Tensor<DataType, 2,DataLayout, IndexType> out(72,97);
26 out.setZero();
27
28 std::size_t out_bytes = out.size() * sizeof(DataType);
29
30 IndexType sizeDim0 = 72;
31 IndexType sizeDim1 = 97;
32
33 array<IndexType, 2> tensorRange = {{sizeDim0, sizeDim1}};
34
35 DataType* d_out = static_cast<DataType*>(sycl_device.allocate(out_bytes));
36 TensorMap<Tensor<DataType, 2, DataLayout, IndexType>> gpu_out(d_out, tensorRange);
37
38 gpu_out.device(sycl_device)=gpu_out.random();
39 sycl_device.memcpyDeviceToHost(out.data(), d_out,out_bytes);
40 for(IndexType i=1; i<sizeDim0; i++)
41 for(IndexType j=1; j<sizeDim1; j++)
42 {
43 VERIFY_IS_NOT_EQUAL(out(i,j), out(i-1,j));
44 VERIFY_IS_NOT_EQUAL(out(i,j), out(i,j-1));
45 VERIFY_IS_NOT_EQUAL(out(i,j), out(i-1,j-1)); }
46
47 // For now we just check thes code doesn't crash.
48 // TODO: come up with a valid test of randomness
49 sycl_device.deallocate(d_out);
50 }
51
52 template <typename DataType, int DataLayout, typename IndexType>
test_sycl_random_normal(const Eigen::SyclDevice & sycl_device)53 void test_sycl_random_normal(const Eigen::SyclDevice& sycl_device)
54 {
55 Tensor<DataType, 2,DataLayout,IndexType> out(72,97);
56 out.setZero();
57 std::size_t out_bytes = out.size() * sizeof(DataType);
58
59 IndexType sizeDim0 = 72;
60 IndexType sizeDim1 = 97;
61
62 array<IndexType, 2> tensorRange = {{sizeDim0, sizeDim1}};
63
64 DataType* d_out = static_cast<DataType*>(sycl_device.allocate(out_bytes));
65 TensorMap<Tensor<DataType, 2, DataLayout, IndexType>> gpu_out(d_out, tensorRange);
66 Eigen::internal::NormalRandomGenerator<DataType> gen(true);
67 gpu_out.device(sycl_device)=gpu_out.random(gen);
68 sycl_device.memcpyDeviceToHost(out.data(), d_out,out_bytes);
69 for(IndexType i=1; i<sizeDim0; i++)
70 for(IndexType j=1; j<sizeDim1; j++)
71 {
72 VERIFY_IS_NOT_EQUAL(out(i,j), out(i-1,j));
73 VERIFY_IS_NOT_EQUAL(out(i,j), out(i,j-1));
74 VERIFY_IS_NOT_EQUAL(out(i,j), out(i-1,j-1));
75
76 }
77
78 // For now we just check thes code doesn't crash.
79 // TODO: come up with a valid test of randomness
80 sycl_device.deallocate(d_out);
81 }
82
sycl_random_test_per_device(dev_Selector s)83 template<typename DataType, typename dev_Selector> void sycl_random_test_per_device(dev_Selector s){
84 QueueInterface queueInterface(s);
85 auto sycl_device = Eigen::SyclDevice(&queueInterface);
86 test_sycl_random_uniform<DataType, RowMajor, int64_t>(sycl_device);
87 test_sycl_random_uniform<DataType, ColMajor, int64_t>(sycl_device);
88 test_sycl_random_normal<DataType, RowMajor, int64_t>(sycl_device);
89 test_sycl_random_normal<DataType, ColMajor, int64_t>(sycl_device);
90
91 }
EIGEN_DECLARE_TEST(cxx11_tensor_random_sycl)92 EIGEN_DECLARE_TEST(cxx11_tensor_random_sycl)
93 {
94 for (const auto& device :Eigen::get_sycl_supported_devices()) {
95 CALL_SUBTEST(sycl_random_test_per_device<float>(device));
96 #ifdef EIGEN_SYCL_DOUBLE_SUPPORT
97 CALL_SUBTEST(sycl_random_test_per_device<double>(device));
98 #endif
99 }
100 }
101