Advertisement
Guest User

Haha Hihi

a guest
Apr 18th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1.  
  2. __global__ void odd_even_sort_kernel(int* a, int *nPtr)
  3. {
  4.     int n = *(nPtr);
  5.     // comput tid from blockIdx & threadIdx & blockDim
  6.     j = blockIdx.x; // tid is thread id [0...n/2-1]
  7.     for i := 1 to n
  8.     {
  9.         if (i is odd) {
  10.             compare-exchange(a[2j + 1], a[2j + 2]);
  11.  
  12.         } else {
  13.             // j must be between [1...n/2-1]. So that's why thread-0 sits idle!
  14.             if (j >= 1) {
  15.                 compare-exchange(a[2j], a[2j + 1]);
  16.             }
  17.  
  18.         }
  19.  
  20.         // all the threads must complete before going to next iteration!
  21.         __syncthreads();
  22.     }
  23. }
  24.  
  25. main()
  26. {
  27.     //allocate gpu memories
  28.  
  29.     // copy data(array=a, size=n) from cpu mem to gpu mem
  30.  
  31.  
  32.     dim gridDim(1, 1);
  33.     dim blockDim(1024, 1, 1);
  34.    
  35.     odd_even_sort_kernel<<<gridDim, blockDim>>>(a_gpu, n_ptr_gpu);
  36.  
  37.     // copy results(array) back
  38.  
  39.     //deallocate memories
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement