Home | History | Annotate | Download | only in test
      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 (at) 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_TEST_FUNC cxx11_tensor_broadcast_sycl
     17 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
     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 static void test_broadcast_sycl(const Eigen::SyclDevice &sycl_device){
     29 
     30   // BROADCAST test:
     31   array<int, 4> in_range   = {{2, 3, 5, 7}};
     32   array<int, 4> broadcasts = {{2, 3, 1, 4}};
     33   array<int, 4> out_range;  // = in_range * broadcasts
     34   for (size_t i = 0; i < out_range.size(); ++i)
     35     out_range[i] = in_range[i] * broadcasts[i];
     36 
     37   Tensor<float, 4>  input(in_range);
     38   Tensor<float, 4> out(out_range);
     39 
     40   for (size_t i = 0; i < in_range.size(); ++i)
     41     VERIFY_IS_EQUAL(out.dimension(i), out_range[i]);
     42 
     43 
     44   for (int i = 0; i < input.size(); ++i)
     45     input(i) = static_cast<float>(i);
     46 
     47   float * gpu_in_data  = static_cast<float*>(sycl_device.allocate(input.dimensions().TotalSize()*sizeof(float)));
     48   float * gpu_out_data  = static_cast<float*>(sycl_device.allocate(out.dimensions().TotalSize()*sizeof(float)));
     49 
     50   TensorMap<Tensor<float, 4>>  gpu_in(gpu_in_data, in_range);
     51   TensorMap<Tensor<float, 4>> gpu_out(gpu_out_data, out_range);
     52   sycl_device.memcpyHostToDevice(gpu_in_data, input.data(),(input.dimensions().TotalSize())*sizeof(float));
     53   gpu_out.device(sycl_device) = gpu_in.broadcast(broadcasts);
     54   sycl_device.memcpyDeviceToHost(out.data(), gpu_out_data,(out.dimensions().TotalSize())*sizeof(float));
     55 
     56   for (int i = 0; i < 4; ++i) {
     57     for (int j = 0; j < 9; ++j) {
     58       for (int k = 0; k < 5; ++k) {
     59         for (int l = 0; l < 28; ++l) {
     60           VERIFY_IS_APPROX(input(i%2,j%3,k%5,l%7), out(i,j,k,l));
     61         }
     62       }
     63     }
     64   }
     65   printf("Broadcast Test Passed\n");
     66   sycl_device.deallocate(gpu_in_data);
     67   sycl_device.deallocate(gpu_out_data);
     68 }
     69 
     70 void test_cxx11_tensor_broadcast_sycl() {
     71   cl::sycl::gpu_selector s;
     72   Eigen::SyclDevice sycl_device(s);
     73   CALL_SUBTEST(test_broadcast_sycl(sycl_device));
     74 }
     75