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: cuda snippet
// add two arrays
template<typename T>
__global__ void add(T *output, T *inputA, T *inputB) {
int idx = (blockIdx.x * blockDim.x) + threadIdx.x;
output[idx] = inputA[idx] + inputB[idx];
}
template<typename T>
__global__ void initRandom(T *arr, float minValue, float maxValue) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
thrust::default_random_engine rng;
thrust::uniform_real_distribution<float> dist(minValue, maxValue);
rng.discard(idx);
arr[idx] = dist(rng);
}
int main() {
// some stuff
// initialize arrays
initRandom<<<numBlocks, blockSize>>>(x, 0., 1.);
initRandom<<<numBlocks, blockSize>>>(y, 0., 1.);
cudaDeviceSynchronize();
// perform additions
for (int blerp = 0; blerp < iterations; blerp++) {
add<<<numBlocks, blockSize>>>(output, x, y);
cudaDeviceSynchronize();
}
// other stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment