cart-elc

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

cxx11_tensor_image_op_sycl.cpp (3890B)


      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>
     31 static void test_image_op_sycl(const Eigen::SyclDevice &sycl_device)
     32 {
     33   IndexType sizeDim1 = 245;
     34   IndexType sizeDim2 = 343;
     35   IndexType sizeDim3 = 577;
     36 
     37   array<IndexType, 3> input_range ={{sizeDim1, sizeDim2, sizeDim3}};
     38   array<IndexType, 3> slice_range ={{sizeDim1-1, sizeDim2, sizeDim3}};
     39 
     40   Tensor<DataType, 3,DataLayout, IndexType> tensor1(input_range);
     41   Tensor<DataType, 3,DataLayout, IndexType> tensor2(input_range);
     42   Tensor<DataType, 3, DataLayout, IndexType> tensor3(slice_range);
     43   Tensor<DataType, 3, DataLayout, IndexType> tensor3_cpu(slice_range);
     44 
     45 
     46 
     47   typedef Eigen::DSizes<IndexType, 3> Index3;
     48   Index3 strides1(1L,1L, 1L);
     49   Index3 indicesStart1(1L, 0L, 0L);
     50   Index3 indicesStop1(sizeDim1, sizeDim2, sizeDim3);
     51 
     52   Index3 strides2(1L,1L, 1L);
     53   Index3 indicesStart2(0L, 0L, 0L);
     54   Index3 indicesStop2(sizeDim1-1, sizeDim2, sizeDim3);
     55   Eigen::DSizes<IndexType, 3> sizes(sizeDim1-1,sizeDim2,sizeDim3);
     56 
     57   tensor1.setRandom();
     58   tensor2.setRandom();
     59 
     60 
     61   DataType* gpu_data1  = static_cast<DataType*>(sycl_device.allocate(tensor1.size()*sizeof(DataType)));
     62   DataType* gpu_data2  = static_cast<DataType*>(sycl_device.allocate(tensor2.size()*sizeof(DataType)));
     63   DataType* gpu_data3  = static_cast<DataType*>(sycl_device.allocate(tensor3.size()*sizeof(DataType)));
     64 
     65   TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu1(gpu_data1, input_range);
     66   TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu2(gpu_data2, input_range);
     67   TensorMap<Tensor<DataType, 3, DataLayout, IndexType>> gpu3(gpu_data3, slice_range);
     68 
     69   sycl_device.memcpyHostToDevice(gpu_data1, tensor1.data(),(tensor1.size())*sizeof(DataType));
     70   sycl_device.memcpyHostToDevice(gpu_data2, tensor2.data(),(tensor2.size())*sizeof(DataType));
     71   gpu3.device(sycl_device)= gpu1.slice(indicesStart1, sizes) - gpu2.slice(indicesStart2, sizes);
     72   sycl_device.memcpyDeviceToHost(tensor3.data(), gpu_data3,(tensor3.size())*sizeof(DataType));
     73 
     74   tensor3_cpu = tensor1.stridedSlice(indicesStart1,indicesStop1,strides1) - tensor2.stridedSlice(indicesStart2,indicesStop2,strides2);
     75 
     76 
     77   for (IndexType i = 0; i <slice_range[0] ; ++i) {
     78     for (IndexType j = 0; j < slice_range[1]; ++j) {
     79       for (IndexType k = 0; k < slice_range[2]; ++k) {
     80         VERIFY_IS_EQUAL(tensor3_cpu(i,j,k), tensor3(i,j,k));
     81       }
     82     }
     83   }
     84   sycl_device.deallocate(gpu_data1);
     85   sycl_device.deallocate(gpu_data2);
     86   sycl_device.deallocate(gpu_data3);
     87 }
     88 
     89 
     90 template<typename DataType, typename dev_Selector> void sycl_computing_test_per_device(dev_Selector s){
     91   QueueInterface queueInterface(s);
     92   auto sycl_device = Eigen::SyclDevice(&queueInterface);
     93   test_image_op_sycl<DataType, RowMajor, int64_t>(sycl_device);
     94 }
     95 
     96 EIGEN_DECLARE_TEST(cxx11_tensor_image_op_sycl) {
     97   for (const auto& device :Eigen::get_sycl_supported_devices()) { 
     98    CALL_SUBTEST(sycl_computing_test_per_device<float>(device));
     99 #ifdef EIGEN_SYCL_DOUBLE_SUPPORT
    100    CALL_SUBTEST(sycl_computing_test_per_device<double>(device));
    101 #endif
    102   }
    103 }