cxx11_tensor_random_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 //
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>
23 static void test_sycl_random_uniform(const Eigen::SyclDevice& sycl_device)
24 {
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  {
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>
53 void test_sycl_random_normal(const Eigen::SyclDevice& sycl_device)
54 {
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);
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  {
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 
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 }
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 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:103
void test_sycl_random_normal(const Eigen::SyclDevice &sycl_device)
#define VERIFY_IS_NOT_EQUAL(a, b)
Definition: main.h:387
std::ofstream out("Result.txt")
void sycl_random_test_per_device(dev_Selector s)
A tensor expression mapping an existing array of data.
RealScalar s
TensorDevice< TensorMap< PlainObjectType, Options_, MakePointer_ >, DeviceType > device(const DeviceType &dev)
Definition: TensorBase.h:1145
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
EIGEN_DECLARE_TEST(cxx11_tensor_random_sycl)
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
static void test_sycl_random_uniform(const Eigen::SyclDevice &sycl_device)
std::ptrdiff_t j
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setZero()
Definition: TensorBase.h:988
The tensor class.
Definition: Tensor.h:63


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