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 #define EIGEN_TEST_FUNC cxx11_tensor_scan_cuda
13 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
14 #define EIGEN_USE_GPU
15
16 #if defined __CUDACC_VER__ && __CUDACC_VER__ >= 70500
17 #include <cuda_fp16.h>
18 #endif
19 #include "main.h"
20 #include <unsupported/Eigen/CXX11/Tensor>
21
22 using Eigen::Tensor;
23 typedef Tensor<float, 1>::DimensionPair DimPair;
24
25 template<int DataLayout>
test_cuda_cumsum(int m_size,int k_size,int n_size)26 void test_cuda_cumsum(int m_size, int k_size, int n_size)
27 {
28 std::cout << "Testing for (" << m_size << "," << k_size << "," << n_size << ")" << std::endl;
29 Tensor<float, 3, DataLayout> t_input(m_size, k_size, n_size);
30 Tensor<float, 3, DataLayout> t_result(m_size, k_size, n_size);
31 Tensor<float, 3, DataLayout> t_result_gpu(m_size, k_size, n_size);
32
33 t_input.setRandom();
34
35 std::size_t t_input_bytes = t_input.size() * sizeof(float);
36 std::size_t t_result_bytes = t_result.size() * sizeof(float);
37
38 float* d_t_input;
39 float* d_t_result;
40
41 cudaMalloc((void**)(&d_t_input), t_input_bytes);
42 cudaMalloc((void**)(&d_t_result), t_result_bytes);
43
44 cudaMemcpy(d_t_input, t_input.data(), t_input_bytes, cudaMemcpyHostToDevice);
45
46 Eigen::CudaStreamDevice stream;
47 Eigen::GpuDevice gpu_device(&stream);
48
49 Eigen::TensorMap<Eigen::Tensor<float, 3, DataLayout> >
50 gpu_t_input(d_t_input, Eigen::array<int, 3>(m_size, k_size, n_size));
51 Eigen::TensorMap<Eigen::Tensor<float, 3, DataLayout> >
52 gpu_t_result(d_t_result, Eigen::array<int, 3>(m_size, k_size, n_size));
53
54 gpu_t_result.device(gpu_device) = gpu_t_input.cumsum(1);
55 t_result = t_input.cumsum(1);
56
57 cudaMemcpy(t_result_gpu.data(), d_t_result, t_result_bytes, cudaMemcpyDeviceToHost);
58 for (DenseIndex i = 0; i < t_result.size(); i++) {
59 if (fabs(t_result(i) - t_result_gpu(i)) < 1e-4f) {
60 continue;
61 }
62 if (Eigen::internal::isApprox(t_result(i), t_result_gpu(i), 1e-4f)) {
63 continue;
64 }
65 std::cout << "mismatch detected at index " << i << ": " << t_result(i)
66 << " vs " << t_result_gpu(i) << std::endl;
67 assert(false);
68 }
69
70 cudaFree((void*)d_t_input);
71 cudaFree((void*)d_t_result);
72 }
73
74
test_cxx11_tensor_scan_cuda()75 void test_cxx11_tensor_scan_cuda()
76 {
77 CALL_SUBTEST_1(test_cuda_cumsum<ColMajor>(128, 128, 128));
78 CALL_SUBTEST_2(test_cuda_cumsum<RowMajor>(128, 128, 128));
79 }
80