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
13 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
14 #define EIGEN_USE_GPU
15
16 #include "main.h"
17 #include <Eigen/CXX11/Tensor>
18
19 #include <Eigen/CXX11/src/Tensor/TensorGpuHipCudaDefines.h>
20
test_gpu_random_uniform()21 void test_gpu_random_uniform()
22 {
23 Tensor<float, 2> out(72,97);
24 out.setZero();
25
26 std::size_t out_bytes = out.size() * sizeof(float);
27
28 float* d_out;
29 gpuMalloc((void**)(&d_out), out_bytes);
30
31 Eigen::GpuStreamDevice stream;
32 Eigen::GpuDevice gpu_device(&stream);
33
34 Eigen::TensorMap<Eigen::Tensor<float, 2> > gpu_out(d_out, 72,97);
35
36 gpu_out.device(gpu_device) = gpu_out.random();
37
38 assert(gpuMemcpyAsync(out.data(), d_out, out_bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
39 assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
40
41 // For now we just check this code doesn't crash.
42 // TODO: come up with a valid test of randomness
43 }
44
45
test_gpu_random_normal()46 void test_gpu_random_normal()
47 {
48 Tensor<float, 2> out(72,97);
49 out.setZero();
50
51 std::size_t out_bytes = out.size() * sizeof(float);
52
53 float* d_out;
54 gpuMalloc((void**)(&d_out), out_bytes);
55
56 Eigen::GpuStreamDevice stream;
57 Eigen::GpuDevice gpu_device(&stream);
58
59 Eigen::TensorMap<Eigen::Tensor<float, 2> > gpu_out(d_out, 72,97);
60
61 Eigen::internal::NormalRandomGenerator<float> gen(true);
62 gpu_out.device(gpu_device) = gpu_out.random(gen);
63
64 assert(gpuMemcpyAsync(out.data(), d_out, out_bytes, gpuMemcpyDeviceToHost, gpu_device.stream()) == gpuSuccess);
65 assert(gpuStreamSynchronize(gpu_device.stream()) == gpuSuccess);
66 }
67
test_complex()68 static void test_complex()
69 {
70 Tensor<std::complex<float>, 1> vec(6);
71 vec.setRandom();
72
73 // Fixme: we should check that the generated numbers follow a uniform
74 // distribution instead.
75 for (int i = 1; i < 6; ++i) {
76 VERIFY_IS_NOT_EQUAL(vec(i), vec(i-1));
77 }
78 }
79
80
EIGEN_DECLARE_TEST(cxx11_tensor_random_gpu)81 EIGEN_DECLARE_TEST(cxx11_tensor_random_gpu)
82 {
83 CALL_SUBTEST(test_gpu_random_uniform());
84 CALL_SUBTEST(test_gpu_random_normal());
85 CALL_SUBTEST(test_complex());
86 }
87