cart-elc

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

cxx11_tensor_broadcast_sycl.cpp (5708B)


      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::array;
     24 using Eigen::SyclDevice;
     25 using Eigen::Tensor;
     26 using Eigen::TensorMap;
     27 
     28 template <typename DataType, int DataLayout, typename IndexType>
     29 static void test_broadcast_sycl_fixed(const Eigen::SyclDevice &sycl_device){
     30 
     31   // BROADCAST test:
     32   IndexType inDim1=2;
     33   IndexType inDim2=3;
     34   IndexType inDim3=5;
     35   IndexType inDim4=7;
     36   IndexType bDim1=2;
     37   IndexType bDim2=3;
     38   IndexType bDim3=1;
     39   IndexType bDim4=4;
     40   array<IndexType, 4> in_range   = {{inDim1, inDim2, inDim3, inDim4}};
     41   array<IndexType, 4> broadcasts = {{bDim1, bDim2, bDim3, bDim4}};
     42   array<IndexType, 4> out_range;  // = in_range * broadcasts
     43   for (size_t i = 0; i < out_range.size(); ++i)
     44     out_range[i] = in_range[i] * broadcasts[i];
     45 
     46   Tensor<DataType, 4, DataLayout, IndexType>  input(in_range);
     47   Tensor<DataType, 4, DataLayout, IndexType> out(out_range);
     48 
     49   for (size_t i = 0; i < in_range.size(); ++i)
     50     VERIFY_IS_EQUAL(out.dimension(i), out_range[i]);
     51 
     52 
     53   for (IndexType i = 0; i < input.size(); ++i)
     54     input(i) = static_cast<DataType>(i);
     55 
     56   DataType * gpu_in_data  = static_cast<DataType*>(sycl_device.allocate(input.dimensions().TotalSize()*sizeof(DataType)));
     57   DataType * gpu_out_data  = static_cast<DataType*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(DataType)));
     58 
     59   TensorMap<TensorFixedSize<DataType, Sizes<2, 3, 5, 7>, DataLayout, IndexType>> gpu_in(gpu_in_data, in_range);
     60   TensorMap<Tensor<DataType, 4, DataLayout, IndexType>> gpu_out(gpu_out_data, out_range);
     61   sycl_device.memcpyHostToDevice(gpu_in_data, input.data(),(input.dimensions().TotalSize())*sizeof(DataType));
     62   gpu_out.device(sycl_device) = gpu_in.broadcast(broadcasts);
     63   sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(DataType));
     64 
     65   for (IndexType i = 0; i < inDim1*bDim1; ++i) {
     66     for (IndexType j = 0; j < inDim2*bDim2; ++j) {
     67       for (IndexType k = 0; k < inDim3*bDim3; ++k) {
     68         for (IndexType l = 0; l < inDim4*bDim4; ++l) {
     69           VERIFY_IS_APPROX(input(i%2,j%3,k%5,l%7), out(i,j,k,l));
     70         }
     71       }
     72     }
     73   }
     74   printf("Broadcast Test with fixed size Passed\n");
     75   sycl_device.deallocate(gpu_in_data);
     76   sycl_device.deallocate(gpu_out_data);
     77 }
     78 
     79 template <typename DataType, int DataLayout, typename IndexType>
     80 static void test_broadcast_sycl(const Eigen::SyclDevice &sycl_device){
     81 
     82   // BROADCAST test:
     83   IndexType inDim1=2;
     84   IndexType inDim2=3;
     85   IndexType inDim3=5;
     86   IndexType inDim4=7;
     87   IndexType bDim1=2;
     88   IndexType bDim2=3;
     89   IndexType bDim3=1;
     90   IndexType bDim4=4;
     91   array<IndexType, 4> in_range   = {{inDim1, inDim2, inDim3, inDim4}};
     92   array<IndexType, 4> broadcasts = {{bDim1, bDim2, bDim3, bDim4}};
     93   array<IndexType, 4> out_range;  // = in_range * broadcasts
     94   for (size_t i = 0; i < out_range.size(); ++i)
     95     out_range[i] = in_range[i] * broadcasts[i];
     96 
     97   Tensor<DataType, 4, DataLayout, IndexType>  input(in_range);
     98   Tensor<DataType, 4, DataLayout, IndexType> out(out_range);
     99 
    100   for (size_t i = 0; i < in_range.size(); ++i)
    101     VERIFY_IS_EQUAL(out.dimension(i), out_range[i]);
    102 
    103 
    104   for (IndexType i = 0; i < input.size(); ++i)
    105     input(i) = static_cast<DataType>(i);
    106 
    107   DataType * gpu_in_data  = static_cast<DataType*>(sycl_device.allocate(input.dimensions().TotalSize()*sizeof(DataType)));
    108   DataType * gpu_out_data  = static_cast<DataType*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(DataType)));
    109 
    110   TensorMap<Tensor<DataType, 4, DataLayout, IndexType>>  gpu_in(gpu_in_data, in_range);
    111   TensorMap<Tensor<DataType, 4, DataLayout, IndexType>> gpu_out(gpu_out_data, out_range);
    112   sycl_device.memcpyHostToDevice(gpu_in_data, input.data(),(input.dimensions().TotalSize())*sizeof(DataType));
    113   gpu_out.device(sycl_device) = gpu_in.broadcast(broadcasts);
    114   sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(DataType));
    115 
    116   for (IndexType i = 0; i < inDim1*bDim1; ++i) {
    117     for (IndexType j = 0; j < inDim2*bDim2; ++j) {
    118       for (IndexType k = 0; k < inDim3*bDim3; ++k) {
    119         for (IndexType l = 0; l < inDim4*bDim4; ++l) {
    120           VERIFY_IS_APPROX(input(i%inDim1,j%inDim2,k%inDim3,l%inDim4), out(i,j,k,l));
    121         }
    122       }
    123     }
    124   }
    125   printf("Broadcast Test Passed\n");
    126   sycl_device.deallocate(gpu_in_data);
    127   sycl_device.deallocate(gpu_out_data);
    128 }
    129 
    130 template<typename DataType> void sycl_broadcast_test_per_device(const cl::sycl::device& d){
    131   std::cout << "Running on " << d.template get_info<cl::sycl::info::device::name>() << std::endl;
    132   QueueInterface queueInterface(d);
    133   auto sycl_device = Eigen::SyclDevice(&queueInterface);
    134   test_broadcast_sycl<DataType, RowMajor, int64_t>(sycl_device);
    135   test_broadcast_sycl<DataType, ColMajor, int64_t>(sycl_device);
    136   test_broadcast_sycl_fixed<DataType, RowMajor, int64_t>(sycl_device);
    137   test_broadcast_sycl_fixed<DataType, ColMajor, int64_t>(sycl_device);
    138 }
    139 
    140 EIGEN_DECLARE_TEST(cxx11_tensor_broadcast_sycl) {
    141   for (const auto& device :Eigen::get_sycl_supported_devices()) {
    142     CALL_SUBTEST(sycl_broadcast_test_per_device<float>(device));
    143   }
    144 }