Skip to content

Instantly share code, notes, and snippets.

@rharriso
Last active October 14, 2018 23:13
Show Gist options
  • Save rharriso/6fc1bd0caeaf72c36987dfdc0c645c1f to your computer and use it in GitHub Desktop.
Save rharriso/6fc1bd0caeaf72c36987dfdc0c645c1f to your computer and use it in GitHub Desktop.
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