1#include "ParticleSystemHelper.h"
3#include "Algorithm/Reduction.h"
5#include <thrust/sort.h>
9 __device__ OcKey K_CalculateMortonCode(OcIndex x, OcIndex y, OcIndex z)
13 for (int i = 0; i < MAX_LEVEL; i++)
15 key |= (x & 1U << i) << 2 * i | (y & 1U << i) << (2 * i + 1) | (z & 1U << i) << (2 * i + 2);
20 template<typename Real, typename Coord>
21 __global__ void PSH_CalculateMortonCode(
28 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
29 if (pId >= pos.size()) return;
32 OcIndex x = OcIndex((p_i.x - lo.x) / d);
33 OcIndex y = OcIndex((p_i.y - lo.y) / d);
34 OcIndex z = OcIndex((p_i.z - lo.z) / d);
36 morton[pId] = K_CalculateMortonCode(x, y, z);
39 template<typename TDataType>
40 void ParticleSystemHelper<TDataType>::calculateMortonCode(
41 DArray<OcKey>& morton,
45 Reduction<Coord> reduce;
46 Coord lo = reduce.minimum(pos.begin(), pos.size());
47 Coord hi = reduce.maximum(pos.begin(), pos.size());
50 PSH_CalculateMortonCode,
58 __global__ void PSH_InitParticleIds(
61 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
62 if (pId >= ids.size()) return;
67 template<typename Coord>
68 __global__ void PSH_ReorderParticles(
71 DArray<uint> idsInOrder)
73 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
74 if (pId >= idsInOrder.size()) return;
76 target[pId] = source[idsInOrder[pId]];
79 template<typename TDataType>
80 void ParticleSystemHelper<TDataType>::reorderParticles(
83 DArray<OcKey>& morton)
85 DArray<uint> idsInOrder(pos.size());
91 thrust::sort_by_key(thrust::device, morton.begin(), morton.begin() + morton.size(), idsInOrder.begin());
93 DArray<Coord> buffer(pos.size());
104 cuExecute(vel.size(),
105 PSH_ReorderParticles,
114 template class ParticleSystemHelper<DataType3f>;