Spaces:
Runtime error
Runtime error
File size: 2,303 Bytes
be11144 |
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 |
#pragma once
#include <thrust/iterator/counting_iterator.h>
#include <thrust/transform.h>
struct hash32
{
__host__ __device__
unsigned int operator()(unsigned int h) const
{
h = ~h + (h << 15);
h = h ^ (h >> 12);
h = h + (h << 2);
h = h ^ (h >> 4);
h = h + (h << 3) + (h << 11);
h = h ^ (h >> 16);
return h;
}
};
struct hash64
{
__host__ __device__
unsigned long long operator()(unsigned long long h) const
{
h = ~h + (h << 21);
h = h ^ (h >> 24);
h = (h + (h << 3)) + (h << 8);
h = h ^ (h >> 14);
h = (h + (h << 2)) + (h << 4);
h = h ^ (h >> 28);
h = h + (h << 31);
return h;
}
};
struct hashtofloat
{
__host__ __device__
float operator()(unsigned int h) const
{
return static_cast<float>(hash32()(h)) / 4294967296.0f;
}
};
struct hashtodouble
{
__host__ __device__
double operator()(unsigned long long h) const
{
return static_cast<double>(hash64()(h)) / 18446744073709551616.0;
}
};
template <typename Vector, typename T>
void _randomize(Vector& v, T)
{
thrust::transform(thrust::counting_iterator<unsigned int>(0),
thrust::counting_iterator<unsigned int>(0) + v.size(),
v.begin(),
hash32());
}
template <typename Vector>
void _randomize(Vector& v, long long)
{
thrust::transform(thrust::counting_iterator<unsigned long long>(0),
thrust::counting_iterator<unsigned long long>(0) + v.size(),
v.begin(),
hash64());
}
template <typename Vector>
void _randomize(Vector& v, float)
{
thrust::transform(thrust::counting_iterator<unsigned int>(0),
thrust::counting_iterator<unsigned int>(0) + v.size(),
v.begin(),
hashtofloat());
}
template <typename Vector>
void _randomize(Vector& v, double)
{
thrust::transform(thrust::counting_iterator<unsigned long long>(0),
thrust::counting_iterator<unsigned long long>(0) + v.size(),
v.begin(),
hashtodouble());
}
// fill Vector with random values
template <typename Vector>
void randomize(Vector& v)
{
_randomize(v, typename Vector::value_type());
}
|