Last active
October 14, 2018 23:13
-
-
Save rharriso/6fc1bd0caeaf72c36987dfdc0c645c1f to your computer and use it in GitHub Desktop.
Looking at Thrust: cuda snippet
This file contains hidden or 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
// 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