cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

cxx11_tensor_forced_eval_sycl.cpp (3461B)


      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 template <typename DataType, int DataLayout, typename IndexType>
     25 void test_forced_eval_sycl(const Eigen::SyclDevice &sycl_device) {
     26 
     27   IndexType sizeDim1 = 100;
     28   IndexType sizeDim2 = 20;
     29   IndexType sizeDim3 = 20;
     30   Eigen::array<IndexType, 3> tensorRange = {{sizeDim1, sizeDim2, sizeDim3}};
     31   Eigen::Tensor<DataType, 3, DataLayout, IndexType> in1(tensorRange);
     32   Eigen::Tensor<DataType, 3, DataLayout, IndexType> in2(tensorRange);
     33   Eigen::Tensor<DataType, 3, DataLayout, IndexType> out(tensorRange);
     34 
     35   DataType * gpu_in1_data  = static_cast<DataType*>(sycl_device.allocate(in1.dimensions().TotalSize()*sizeof(DataType)));
     36   DataType * gpu_in2_data  = static_cast<DataType*>(sycl_device.allocate(in2.dimensions().TotalSize()*sizeof(DataType)));
     37   DataType * gpu_out_data =  static_cast<DataType*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(DataType)));
     38 
     39   in1 = in1.random() + in1.constant(static_cast<DataType>(10.0f));
     40   in2 = in2.random() + in2.constant(static_cast<DataType>(10.0f));
     41 
     42   // creating TensorMap from tensor
     43   Eigen::TensorMap<Eigen::Tensor<DataType, 3, DataLayout, IndexType>> gpu_in1(gpu_in1_data, tensorRange);
     44   Eigen::TensorMap<Eigen::Tensor<DataType, 3, DataLayout, IndexType>> gpu_in2(gpu_in2_data, tensorRange);
     45   Eigen::TensorMap<Eigen::Tensor<DataType, 3, DataLayout, IndexType>> gpu_out(gpu_out_data, tensorRange);
     46   sycl_device.memcpyHostToDevice(gpu_in1_data, in1.data(),(in1.dimensions().TotalSize())*sizeof(DataType));
     47   sycl_device.memcpyHostToDevice(gpu_in2_data, in2.data(),(in2.dimensions().TotalSize())*sizeof(DataType));
     48   /// c=(a+b)*b
     49   gpu_out.device(sycl_device) =(gpu_in1 + gpu_in2).eval() * gpu_in2;
     50   sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(DataType));
     51   for (IndexType i = 0; i < sizeDim1; ++i) {
     52     for (IndexType j = 0; j < sizeDim2; ++j) {
     53       for (IndexType k = 0; k < sizeDim3; ++k) {
     54         VERIFY_IS_APPROX(out(i, j, k),
     55                          (in1(i, j, k) + in2(i, j, k)) * in2(i, j, k));
     56       }
     57     }
     58   }
     59   printf("(a+b)*b Test Passed\n");
     60   sycl_device.deallocate(gpu_in1_data);
     61   sycl_device.deallocate(gpu_in2_data);
     62   sycl_device.deallocate(gpu_out_data);
     63 
     64 }
     65 
     66 template <typename DataType, typename Dev_selector> void tensorForced_evalperDevice(Dev_selector s){
     67   QueueInterface queueInterface(s);
     68   auto sycl_device = Eigen::SyclDevice(&queueInterface);
     69   test_forced_eval_sycl<DataType, RowMajor, int64_t>(sycl_device);
     70   test_forced_eval_sycl<DataType, ColMajor, int64_t>(sycl_device);
     71 }
     72 EIGEN_DECLARE_TEST(cxx11_tensor_forced_eval_sycl) {
     73   for (const auto& device :Eigen::get_sycl_supported_devices()) {
     74     CALL_SUBTEST(tensorForced_evalperDevice<float>(device));
     75     CALL_SUBTEST(tensorForced_evalperDevice<half>(device));
     76   }
     77 }