Last active
October 14, 2018 23:37
-
-
Save rharriso/7f50c985f16b2dc66c94575492289075 to your computer and use it in GitHub Desktop.
Looking At Thrust: Thrust Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct initRandomPrg { | |
float minValue, maxValue; | |
__host__ __device__ | |
initRandomPrg(float _mnV=0.f, float _mxV=1.f) : minValue(_mnV), maxValue(_mxV) {}; | |
__host__ __device__ | |
float operator()(const unsigned int n) const { | |
thrust::default_random_engine rng; | |
thrust::uniform_real_distribution<float> dist(minValue, maxValue); | |
rng.discard(n); | |
return dist(rng); | |
} | |
}; | |
int main() { | |
// some stuff | |
auto x = thrust::device_vector<float>(N); | |
auto y = thrust::device_vector<float>(N); | |
auto output = thrust::device_vector<float>(N); | |
// initilize array | |
auto index_sequence_begin = thrust::counting_iterator<unsigned int>(0); | |
// initialize X | |
thrust::transform( | |
index_sequence_begin, | |
index_sequence_begin + N, | |
x.begin(), | |
initRandomPrg() | |
); | |
// initialize Y | |
// add them up | |
for (int i = 0; i < iterations; i++) { | |
thrust::transform( | |
x.begin(), x.end(), | |
y.begin(), | |
output.begin(), | |
thrust::plus<float>() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment