Skip to content

Instantly share code, notes, and snippets.

@mchakravarty
Created April 17, 2011 12:45
Show Gist options
  • Save mchakravarty/924009 to your computer and use it in GitHub Desktop.
Save mchakravarty/924009 to your computer and use it in GitHub Desktop.
/* -----------------------------------------------------------------------------
*
* Module : Permute
* Copyright : (c) [2009..2010] Trevor L. McDonell
* License : BSD
*
* Forward permutation, characterised by a function that determines for each
* element in the source array where it should go in the target. The output
* array should be initialised with a default value, as the permutation may be
* between arrays of different sizes and some positions may never be touched.
*
* Elements from the source array are dropped for which the permutation function
* yields the magic index `ignore`.
*
* ---------------------------------------------------------------------------*/
extern "C"
__global__ void
permute
(
ArrOut d_out,
const ArrIn0 d_in0,
const Ix shape
)
{
Ix dst;
Ix idx;
const Ix gridSize = __umul24(blockDim.x, gridDim.x);
for (idx = __umul24(blockDim.x, blockIdx.x) + threadIdx.x; idx < shape; idx += gridSize)
{
dst = project(idx);
if (dst != ignore)
{
TyIn0 x0 = get0(d_in0, idx);
TyOut x1_ = get0(d_out, dst);
TyOut x1;
do
{
x1 = x1_;
x1_ = atomicCAS(&d_out[dst], x1, apply(x1, x0));
}
while (x1 != x1_);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment