• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define EIGEN_TEST_NO_LONGDOUBLE
11 #define EIGEN_TEST_NO_COMPLEX
12 
13 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
14 #define EIGEN_USE_GPU
15 
16 #include "main.h"
17 #include <unsupported/Eigen/CXX11/Tensor>
18 
19 using Eigen::Tensor;
20 
test_gpu_conversion()21 void test_gpu_conversion() {
22   Eigen::GpuStreamDevice stream;
23   Eigen::GpuDevice gpu_device(&stream);
24   int num_elem = 101;
25 
26   Tensor<float, 1> floats(num_elem);
27   floats.setRandom();
28 
29   float* d_float = (float*)gpu_device.allocate(num_elem * sizeof(float));
30   Eigen::half* d_half = (Eigen::half*)gpu_device.allocate(num_elem * sizeof(Eigen::half));
31   float* d_conv = (float*)gpu_device.allocate(num_elem * sizeof(float));
32 
33   Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_float(
34       d_float, num_elem);
35   Eigen::TensorMap<Eigen::Tensor<Eigen::half, 1>, Eigen::Aligned> gpu_half(
36       d_half, num_elem);
37   Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_conv(
38       d_conv, num_elem);
39 
40   gpu_device.memcpyHostToDevice(d_float, floats.data(), num_elem*sizeof(float));
41 
42   gpu_half.device(gpu_device) = gpu_float.cast<Eigen::half>();
43   gpu_conv.device(gpu_device) = gpu_half.cast<float>();
44 
45   Tensor<float, 1> initial(num_elem);
46   Tensor<float, 1> final(num_elem);
47   gpu_device.memcpyDeviceToHost(initial.data(), d_float, num_elem*sizeof(float));
48   gpu_device.memcpyDeviceToHost(final.data(), d_conv, num_elem*sizeof(float));
49   gpu_device.synchronize();
50 
51   for (int i = 0; i < num_elem; ++i) {
52     VERIFY_IS_APPROX(initial(i), final(i));
53   }
54 
55   gpu_device.deallocate(d_float);
56   gpu_device.deallocate(d_half);
57   gpu_device.deallocate(d_conv);
58 }
59 
60 
test_fallback_conversion()61 void test_fallback_conversion() {
62   int num_elem = 101;
63   Tensor<float, 1> floats(num_elem);
64   floats.setRandom();
65 
66   Eigen::Tensor<Eigen::half, 1> halfs = floats.cast<Eigen::half>();
67   Eigen::Tensor<float, 1> conv = halfs.cast<float>();
68 
69   for (int i = 0; i < num_elem; ++i) {
70     VERIFY_IS_APPROX(floats(i), conv(i));
71   }
72 }
73 
74 
EIGEN_DECLARE_TEST(cxx11_tensor_cast_float16_gpu)75 EIGEN_DECLARE_TEST(cxx11_tensor_cast_float16_gpu)
76 {
77   CALL_SUBTEST(test_gpu_conversion());
78   CALL_SUBTEST(test_fallback_conversion());
79 }
80