summaryrefslogtreecommitdiffhomepage
path: root/eigen/unsupported/test/cxx11_tensor_striding_sycl.cpp
blob: 603c3746f488d8e50a818798edf52f88e46a0104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2016
// Mehdi Goli    Codeplay Software Ltd.
// Ralph Potter  Codeplay Software Ltd.
// Luke Iwanski  Codeplay Software Ltd.
// Contact: <eigen@codeplay.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#define EIGEN_TEST_NO_LONGDOUBLE
#define EIGEN_TEST_NO_COMPLEX
#define EIGEN_TEST_FUNC cxx11_tensor_striding_sycl
#define EIGEN_DEFAULT_DENSE_INDEX_TYPE int64_t
#define EIGEN_USE_SYCL

#include <iostream>
#include <chrono>
#include <ctime>

#include "main.h"
#include <unsupported/Eigen/CXX11/Tensor>

using Eigen::array;
using Eigen::SyclDevice;
using Eigen::Tensor;
using Eigen::TensorMap;


template <typename DataType, int DataLayout, typename IndexType>
static void test_simple_striding(const Eigen::SyclDevice& sycl_device)
{

  Eigen::array<IndexType, 4> tensor_dims = {{2,3,5,7}};
  Eigen::array<IndexType, 4> stride_dims = {{1,1,3,3}};


  Tensor<DataType, 4, DataLayout, IndexType> tensor(tensor_dims);
  Tensor<DataType, 4, DataLayout,IndexType> no_stride(tensor_dims);
  Tensor<DataType, 4, DataLayout,IndexType> stride(stride_dims);


  std::size_t tensor_bytes = tensor.size()  * sizeof(DataType);
  std::size_t no_stride_bytes = no_stride.size() * sizeof(DataType);
  std::size_t stride_bytes = stride.size() * sizeof(DataType);
  DataType * d_tensor = static_cast<DataType*>(sycl_device.allocate(tensor_bytes));
  DataType * d_no_stride = static_cast<DataType*>(sycl_device.allocate(no_stride_bytes));
  DataType * d_stride = static_cast<DataType*>(sycl_device.allocate(stride_bytes));

  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_tensor(d_tensor, tensor_dims);
  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_no_stride(d_no_stride, tensor_dims);
  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_stride(d_stride, stride_dims);


  tensor.setRandom();
  array<IndexType, 4> strides;
  strides[0] = 1;
  strides[1] = 1;
  strides[2] = 1;
  strides[3] = 1;
  sycl_device.memcpyHostToDevice(d_tensor, tensor.data(), tensor_bytes);
  gpu_no_stride.device(sycl_device)=gpu_tensor.stride(strides);
  sycl_device.memcpyDeviceToHost(no_stride.data(), d_no_stride, no_stride_bytes);

  //no_stride = tensor.stride(strides);

  VERIFY_IS_EQUAL(no_stride.dimension(0), 2);
  VERIFY_IS_EQUAL(no_stride.dimension(1), 3);
  VERIFY_IS_EQUAL(no_stride.dimension(2), 5);
  VERIFY_IS_EQUAL(no_stride.dimension(3), 7);

  for (IndexType i = 0; i < 2; ++i) {
    for (IndexType j = 0; j < 3; ++j) {
      for (IndexType k = 0; k < 5; ++k) {
        for (IndexType l = 0; l < 7; ++l) {
          VERIFY_IS_EQUAL(tensor(i,j,k,l), no_stride(i,j,k,l));
        }
      }
    }
  }

  strides[0] = 2;
  strides[1] = 4;
  strides[2] = 2;
  strides[3] = 3;
//Tensor<float, 4, DataLayout> stride;
//  stride = tensor.stride(strides);

  gpu_stride.device(sycl_device)=gpu_tensor.stride(strides);
  sycl_device.memcpyDeviceToHost(stride.data(), d_stride, stride_bytes);

  VERIFY_IS_EQUAL(stride.dimension(0), 1);
  VERIFY_IS_EQUAL(stride.dimension(1), 1);
  VERIFY_IS_EQUAL(stride.dimension(2), 3);
  VERIFY_IS_EQUAL(stride.dimension(3), 3);

  for (IndexType i = 0; i < 1; ++i) {
    for (IndexType j = 0; j < 1; ++j) {
      for (IndexType k = 0; k < 3; ++k) {
        for (IndexType l = 0; l < 3; ++l) {
          VERIFY_IS_EQUAL(tensor(2*i,4*j,2*k,3*l), stride(i,j,k,l));
        }
      }
    }
  }

  sycl_device.deallocate(d_tensor);
  sycl_device.deallocate(d_no_stride);
  sycl_device.deallocate(d_stride);
}

template <typename DataType, int DataLayout, typename IndexType>
static void test_striding_as_lvalue(const Eigen::SyclDevice& sycl_device)
{

  Eigen::array<IndexType, 4> tensor_dims = {{2,3,5,7}};
  Eigen::array<IndexType, 4> stride_dims = {{3,12,10,21}};


  Tensor<DataType, 4, DataLayout, IndexType> tensor(tensor_dims);
  Tensor<DataType, 4, DataLayout,IndexType> no_stride(stride_dims);
  Tensor<DataType, 4, DataLayout,IndexType> stride(stride_dims);


  std::size_t tensor_bytes = tensor.size()  * sizeof(DataType);
  std::size_t no_stride_bytes = no_stride.size() * sizeof(DataType);
  std::size_t stride_bytes = stride.size() * sizeof(DataType);

  DataType * d_tensor = static_cast<DataType*>(sycl_device.allocate(tensor_bytes));
  DataType * d_no_stride = static_cast<DataType*>(sycl_device.allocate(no_stride_bytes));
  DataType * d_stride = static_cast<DataType*>(sycl_device.allocate(stride_bytes));

  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_tensor(d_tensor, tensor_dims);
  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_no_stride(d_no_stride, stride_dims);
  Eigen::TensorMap<Eigen::Tensor<DataType, 4, DataLayout, IndexType> > gpu_stride(d_stride, stride_dims);

  //Tensor<float, 4, DataLayout> tensor(2,3,5,7);
  tensor.setRandom();
  array<IndexType, 4> strides;
  strides[0] = 2;
  strides[1] = 4;
  strides[2] = 2;
  strides[3] = 3;

//  Tensor<float, 4, DataLayout> result(3, 12, 10, 21);
//  result.stride(strides) = tensor;
  sycl_device.memcpyHostToDevice(d_tensor, tensor.data(), tensor_bytes);
  gpu_stride.stride(strides).device(sycl_device)=gpu_tensor;
  sycl_device.memcpyDeviceToHost(stride.data(), d_stride, stride_bytes);

  for (IndexType i = 0; i < 2; ++i) {
    for (IndexType j = 0; j < 3; ++j) {
      for (IndexType k = 0; k < 5; ++k) {
        for (IndexType l = 0; l < 7; ++l) {
          VERIFY_IS_EQUAL(tensor(i,j,k,l), stride(2*i,4*j,2*k,3*l));
        }
      }
    }
  }

  array<IndexType, 4> no_strides;
  no_strides[0] = 1;
  no_strides[1] = 1;
  no_strides[2] = 1;
  no_strides[3] = 1;
//  Tensor<float, 4, DataLayout> result2(3, 12, 10, 21);
//  result2.stride(strides) = tensor.stride(no_strides);

  gpu_no_stride.stride(strides).device(sycl_device)=gpu_tensor.stride(no_strides);
  sycl_device.memcpyDeviceToHost(no_stride.data(), d_no_stride, no_stride_bytes);

  for (IndexType i = 0; i < 2; ++i) {
    for (IndexType j = 0; j < 3; ++j) {
      for (IndexType k = 0; k < 5; ++k) {
        for (IndexType l = 0; l < 7; ++l) {
          VERIFY_IS_EQUAL(tensor(i,j,k,l), no_stride(2*i,4*j,2*k,3*l));
        }
      }
    }
  }
  sycl_device.deallocate(d_tensor);
  sycl_device.deallocate(d_no_stride);
  sycl_device.deallocate(d_stride);
}


template <typename Dev_selector> void tensorStridingPerDevice(Dev_selector& s){
  QueueInterface queueInterface(s);
  auto sycl_device=Eigen::SyclDevice(&queueInterface);
  test_simple_striding<float, ColMajor, int64_t>(sycl_device);
  test_simple_striding<float, RowMajor, int64_t>(sycl_device);
  test_striding_as_lvalue<float, ColMajor, int64_t>(sycl_device);
  test_striding_as_lvalue<float, RowMajor, int64_t>(sycl_device);
}

void test_cxx11_tensor_striding_sycl() {
  for (const auto& device :Eigen::get_sycl_supported_devices()) {
    CALL_SUBTEST(tensorStridingPerDevice(device));
  }
}