Skip to content

Instantly share code, notes, and snippets.

@rharriso
Last active Oct 14, 2018
Embed
What would you like to do?
Looking At Thrust: Thrust Snippet
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