1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 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_random_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 <Eigen/CXX11/Tensor>
21
22
test_cuda_random_uniform()23 void test_cuda_random_uniform()
24 {
25 Tensor<float, 2> out(72,97);
26 out.setZero();
27
28 std::size_t out_bytes = out.size() * sizeof(float);
29
30 float* d_out;
31 cudaMalloc((void**)(&d_out), out_bytes);
32
33 Eigen::CudaStreamDevice stream;
34 Eigen::GpuDevice gpu_device(&stream);
35
36 Eigen::TensorMap<Eigen::Tensor<float, 2> > gpu_out(d_out, 72,97);
37
38 gpu_out.device(gpu_device) = gpu_out.random();
39
40 assert(cudaMemcpyAsync(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
41 assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
42
43 // For now we just check thes code doesn't crash.
44 // TODO: come up with a valid test of randomness
45 }
46
47
test_cuda_random_normal()48 void test_cuda_random_normal()
49 {
50 Tensor<float, 2> out(72,97);
51 out.setZero();
52
53 std::size_t out_bytes = out.size() * sizeof(float);
54
55 float* d_out;
56 cudaMalloc((void**)(&d_out), out_bytes);
57
58 Eigen::CudaStreamDevice stream;
59 Eigen::GpuDevice gpu_device(&stream);
60
61 Eigen::TensorMap<Eigen::Tensor<float, 2> > gpu_out(d_out, 72,97);
62
63 Eigen::internal::NormalRandomGenerator<float> gen(true);
64 gpu_out.device(gpu_device) = gpu_out.random(gen);
65
66 assert(cudaMemcpyAsync(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
67 assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
68 }
69
test_complex()70 static void test_complex()
71 {
72 Tensor<std::complex<float>, 1> vec(6);
73 vec.setRandom();
74
75 // Fixme: we should check that the generated numbers follow a uniform
76 // distribution instead.
77 for (int i = 1; i < 6; ++i) {
78 VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
79 }
80 }
81
82
test_cxx11_tensor_random_cuda()83 void test_cxx11_tensor_random_cuda()
84 {
85 CALL_SUBTEST(test_cuda_random_uniform());
86 CALL_SUBTEST(test_cuda_random_normal());
87 CALL_SUBTEST(test_complex());
88 }
89