File: random.cu

package info (click to toggle)
python-escript 5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 87,772 kB
  • ctags: 49,550
  • sloc: python: 585,488; cpp: 133,173; ansic: 18,675; xml: 3,283; sh: 690; makefile: 215
file content (116 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (4)
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
#include <unittest/unittest.h>

#include <cusp/detail/random.h>

#include <cusp/array1d.h>
#include <cusp/array2d.h>
#include <thrust/sort.h>
#include <thrust/unique.h>
#include <thrust/extrema.h>

#include <limits>

#include <cusp/print.h>

template <typename T>
struct TestRandomIntegersDistribution
{
    void operator()(void)
    {
        size_t n = 123456;
        cusp::detail::random_integers<T> random(n);

        cusp::array2d<size_t, cusp::host_memory> counts(2 * sizeof(T), 16, 0);

        for (size_t i = 0; i < n; i++)
        {
            unsigned long long raw = random[i] - std::numeric_limits<T>::min();
            for (size_t nibble = 0; nibble < 2 * sizeof(T); nibble++)
            {
                counts(nibble, (raw >> (4 * nibble)) % 16)++;
            }
        }
        
        //std::cout << "min " << *thrust::min_element(counts.values.begin(), counts.values.end()) << std::endl;
        //std::cout << "max " << *thrust::max_element(counts.values.begin(), counts.values.end()) << std::endl;
        //cusp::print_matrix(counts);

        size_t expected = n / 16;
        size_t min_bin = *thrust::min_element(counts.values.begin(), counts.values.end());
        size_t max_bin = *thrust::max_element(counts.values.begin(), counts.values.end());
        
        ASSERT_GEQUAL(min_bin, (size_t) (0.95 * expected));
        ASSERT_LEQUAL(max_bin, (size_t) (1.05 * expected));
    }
};
SimpleUnitTest<TestRandomIntegersDistribution, IntegralTypes> TestRandomIntegersDistributionInstance;


template <typename T>
struct TestRandomRealsDistribution
{
    void operator()(void)
    {
        size_t n = 123456;
        cusp::detail::random_reals<T> random(n);

        cusp::array1d<size_t, cusp::host_memory> buckets(32, 0);

        for (size_t i = 0; i < n; i++)
        {
            const T val = random[i];
            ASSERT_EQUAL(T(0) <= val, true);
            ASSERT_EQUAL(val < T(1), true);

            buckets[ size_t(val * T(buckets.size())) ]++;
        }
        
//        std::cout << "min " << *thrust::min_element(buckets.begin(), buckets.end()) << std::endl;
//        std::cout << "max " << *thrust::max_element(buckets.begin(), buckets.end()) << std::endl;
//        cusp::print_matrix(buckets);

        size_t expected = n / buckets.size();
        size_t min_bin = *thrust::min_element(buckets.begin(), buckets.end());
        size_t max_bin = *thrust::max_element(buckets.begin(), buckets.end());
        
        ASSERT_GEQUAL(min_bin, (size_t) (0.95 * expected));
        ASSERT_LEQUAL(max_bin, (size_t) (1.05 * expected));
    }
};
SimpleUnitTest<TestRandomRealsDistribution, FloatingPointTypes> TestRandomRealsDistributionInstance;


template <typename T>
struct TestRandomIntegers
{
    void operator()(void)
    {
        size_t n = 123456;
        cusp::detail::random_integers<T> random(n);

        cusp::array1d<T, cusp::host_memory>   h(random);
        cusp::array1d<T, cusp::device_memory> d(random);

        ASSERT_EQUAL(h, d);
    }
};
SimpleUnitTest<TestRandomIntegers, IntegralTypes> TestRandomIntegersInstance;


// TODO test double on supported devices
template <typename T>
struct TestRandomReals
{
    void operator()(void)
    {
        size_t n = 123456;
        cusp::detail::random_reals<T> random(n);

        cusp::array1d<T, cusp::host_memory>   h(random);
        cusp::array1d<T, cusp::device_memory> d(random);

        ASSERT_ALMOST_EQUAL(h, d);
    }
};
SimpleUnitTest<TestRandomReals, unittest::type_list<float> > TestRandomRealsInstance;