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 #include <stdint.h>
23 #include <iostream>
24
25 template <typename DataType, int DataLayout, typename IndexType>
test_device_memory(const Eigen::SyclDevice & sycl_device)26 void test_device_memory(const Eigen::SyclDevice &sycl_device) {
27 std::cout << "Running on : "
28 << sycl_device.sycl_queue().get_device(). template get_info<cl::sycl::info::device::name>()
29 <<std::endl;
30 IndexType sizeDim1 = 100;
31 array<IndexType, 1> tensorRange = {{sizeDim1}};
32 Tensor<DataType, 1, DataLayout,IndexType> in(tensorRange);
33 Tensor<DataType, 1, DataLayout,IndexType> in1(tensorRange);
34 memset(in1.data(), 1, in1.size() * sizeof(DataType));
35 DataType* gpu_in_data = static_cast<DataType*>(sycl_device.allocate(in.size()*sizeof(DataType)));
36 sycl_device.memset(gpu_in_data, 1, in.size()*sizeof(DataType));
37 sycl_device.memcpyDeviceToHost(in.data(), gpu_in_data, in.size()*sizeof(DataType));
38 for (IndexType i=0; i<in.size(); i++) {
39 VERIFY_IS_EQUAL(in(i), in1(i));
40 }
41 sycl_device.deallocate(gpu_in_data);
42 }
43
44 template <typename DataType, int DataLayout, typename IndexType>
test_device_exceptions(const Eigen::SyclDevice & sycl_device)45 void test_device_exceptions(const Eigen::SyclDevice &sycl_device) {
46 VERIFY(sycl_device.ok());
47 IndexType sizeDim1 = 100;
48 array<IndexType, 1> tensorDims = {{sizeDim1}};
49 DataType* gpu_data = static_cast<DataType*>(sycl_device.allocate(sizeDim1*sizeof(DataType)));
50 sycl_device.memset(gpu_data, 1, sizeDim1*sizeof(DataType));
51
52 TensorMap<Tensor<DataType, 1, DataLayout,IndexType>> in(gpu_data, tensorDims);
53 TensorMap<Tensor<DataType, 1, DataLayout,IndexType>> out(gpu_data, tensorDims);
54 out.device(sycl_device) = in / in.constant(0);
55
56 sycl_device.synchronize();
57 VERIFY(!sycl_device.ok());
58 sycl_device.deallocate(gpu_data);
59 }
60
sycl_device_test_per_device(const cl::sycl::device & d)61 template<typename DataType> void sycl_device_test_per_device(const cl::sycl::device& d){
62 std::cout << "Running on " << d.template get_info<cl::sycl::info::device::name>() << std::endl;
63 QueueInterface queueInterface(d);
64 auto sycl_device = Eigen::SyclDevice(&queueInterface);
65 test_device_memory<DataType, RowMajor, int64_t>(sycl_device);
66 test_device_memory<DataType, ColMajor, int64_t>(sycl_device);
67 /// this test throw an exception. enable it if you want to see the exception
68 //test_device_exceptions<DataType, RowMajor>(sycl_device);
69 /// this test throw an exception. enable it if you want to see the exception
70 //test_device_exceptions<DataType, ColMajor>(sycl_device);
71 }
72
EIGEN_DECLARE_TEST(cxx11_tensor_device_sycl)73 EIGEN_DECLARE_TEST(cxx11_tensor_device_sycl) {
74 for (const auto& device :Eigen::get_sycl_supported_devices()) {
75 CALL_SUBTEST(sycl_device_test_per_device<float>(device));
76 }
77 }
78