PeriDyno 1.0.0
Loading...
Searching...
No Matches
ParticleSystemHelper.cu
Go to the documentation of this file.
1#include "ParticleSystemHelper.h"
2
3#include "Algorithm/Reduction.h"
4
5#include <thrust/sort.h>
6
7namespace dyno
8{
9 __device__ OcKey K_CalculateMortonCode(OcIndex x, OcIndex y, OcIndex z)
10 {
11 OcKey key = 0;
12
13 for (int i = 0; i < MAX_LEVEL; i++)
14 {
15 key |= (x & 1U << i) << 2 * i | (y & 1U << i) << (2 * i + 1) | (z & 1U << i) << (2 * i + 2);
16 }
17 return key;
18 }
19
20 template<typename Real, typename Coord>
21 __global__ void PSH_CalculateMortonCode(
22 DArray<OcKey> morton,
23 DArray<Coord> pos,
24 Coord lo,
25 Coord hi,
26 Real d)
27 {
28 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
29 if (pId >= pos.size()) return;
30
31 Coord p_i = pos[pId];
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);
35
36 morton[pId] = K_CalculateMortonCode(x, y, z);
37 }
38
39 template<typename TDataType>
40 void ParticleSystemHelper<TDataType>::calculateMortonCode(
41 DArray<OcKey>& morton,
42 DArray<Coord>& pos,
43 Real d)
44 {
45 Reduction<Coord> reduce;
46 Coord lo = reduce.minimum(pos.begin(), pos.size());
47 Coord hi = reduce.maximum(pos.begin(), pos.size());
48
49 cuExecute(pos.size(),
50 PSH_CalculateMortonCode,
51 morton,
52 pos,
53 lo,
54 hi,
55 d);
56 }
57
58 __global__ void PSH_InitParticleIds(
59 DArray<uint> ids)
60 {
61 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
62 if (pId >= ids.size()) return;
63
64 ids[pId] = pId;
65 }
66
67 template<typename Coord>
68 __global__ void PSH_ReorderParticles(
69 DArray<Coord> target,
70 DArray<Coord> source,
71 DArray<uint> idsInOrder)
72 {
73 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
74 if (pId >= idsInOrder.size()) return;
75
76 target[pId] = source[idsInOrder[pId]];
77 }
78
79 template<typename TDataType>
80 void ParticleSystemHelper<TDataType>::reorderParticles(
81 DArray<Coord>& pos,
82 DArray<Coord>& vel,
83 DArray<OcKey>& morton)
84 {
85 DArray<uint> idsInOrder(pos.size());
86
87 cuExecute(pos.size(),
88 PSH_InitParticleIds,
89 idsInOrder);
90
91 thrust::sort_by_key(thrust::device, morton.begin(), morton.begin() + morton.size(), idsInOrder.begin());
92
93 DArray<Coord> buffer(pos.size());
94 buffer.assign(pos);
95
96 cuExecute(pos.size(),
97 PSH_ReorderParticles,
98 pos,
99 buffer,
100 idsInOrder);
101
102 buffer.assign(vel);
103
104 cuExecute(vel.size(),
105 PSH_ReorderParticles,
106 vel,
107 buffer,
108 idsInOrder);
109
110 idsInOrder.clear();
111 buffer.clear();
112 }
113
114 template class ParticleSystemHelper<DataType3f>;
115}