Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __global__ void odd_even_sort_kernel(int* a, int *nPtr)
- {
- int n = *(nPtr);
- // comput tid from blockIdx & threadIdx & blockDim
- j = blockIdx.x; // tid is thread id [0...n/2-1]
- for i := 1 to n
- {
- if (i is odd) {
- compare-exchange(a[2j + 1], a[2j + 2]);
- } else {
- // j must be between [1...n/2-1]. So that's why thread-0 sits idle!
- if (j >= 1) {
- compare-exchange(a[2j], a[2j + 1]);
- }
- }
- // all the threads must complete before going to next iteration!
- __syncthreads();
- }
- }
- main()
- {
- //allocate gpu memories
- // copy data(array=a, size=n) from cpu mem to gpu mem
- dim gridDim(1, 1);
- dim blockDim(1024, 1, 1);
- odd_even_sort_kernel<<<gridDim, blockDim>>>(a_gpu, n_ptr_gpu);
- // copy results(array) back
- //deallocate memories
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement