-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudist.cpp
202 lines (156 loc) · 6.34 KB
/
udist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "udist.h"
#include "random.h"
/**
* @brief Sets none(0 temperature, 0 fluid) u distribution
*
* @param part Particle data
*/
void UDistribution::None::set( Particles & part, unsigned int seed ) const {
sycl::range<1> local{ 8 };
sycl::range<1> global{ part.ntiles.x * part.ntiles.y };
part.queue.submit([&](sycl::handler &h) {
auto part_offset = part.offset;
auto part_np = part.np;
auto part_u = part.u;
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<1> it) {
const auto tile_id = it.get_group_linear_id();
const int offset = part_offset[tile_id];
const int np = part_np[tile_id];
float3 * __restrict__ const u = &part_u[ offset ];
for( auto i = it.get_local_id(0); i < np; i += it.get_local_range(0) )
u[i] = make_float3(0,0,0);
});
});
}
/**
* @brief Sets cold(0 temperatures) u distribution
*
* @param part Particle data
*/
void UDistribution::Cold::set( Particles & part, unsigned int seed ) const {
sycl::range<1> local{ 8 };
sycl::range<1> global{ part.ntiles.x * part.ntiles.y };
part.queue.submit([&](sycl::handler &h) {
auto part_offset = part.offset;
auto part_np = part.np;
auto part_u = part.u;
auto ufl = this -> ufl;
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<1> it) {
const auto tile_id = it.get_group_linear_id();
const int offset = part_offset[tile_id];
const int np = part_np[tile_id];
float3 * __restrict__ const u = &part_u[ offset ];
for( auto i = it.get_local_id(0); i < np; i += it.get_local_range(0) )
u[i] = ufl;
});
});
}
/**
* @brief Sets momentum of all particles in object using uth / ufl
*
*/
void UDistribution::Thermal::set( Particles & part, unsigned int seed ) const {
uint2 rnd_seed { 12345 + seed, 67890 };
sycl::range<1> local{ 8 };
sycl::range<1> global{ part.ntiles.x * part.ntiles.y };
part.queue.submit([&](sycl::handler &h) {
auto part_offset = part.offset;
auto part_np = part.np;
auto part_u = part.u;
auto ufl = this -> ufl;
auto uth = this -> uth;
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<1> it) {
const auto tile_id = it.get_group_linear_id();
// Initialize random state variables
uint2 state;
double norm;
zrandom::rand_init( it.get_global_linear_id(), rnd_seed, state, norm );
const int offset = part_offset[tile_id];
const int np = part_np[tile_id];
float3 * __restrict__ const u = &part_u[ offset ];
for( int i = 0; i < np; i++ ) {
u[i] = make_float3(
ufl.x + uth.x * zrandom::rand_norm( state, norm ),
ufl.y + uth.y * zrandom::rand_norm( state, norm ),
ufl.z + uth.z * zrandom::rand_norm( state, norm )
);
}
});
});
}
/**
* @brief Sets particle momentum correcting local ufl fluctuations
*
*/
void UDistribution::ThermalCorr::set( Particles & part, unsigned int seed ) const {
uint2 rnd_seed{ 12345 + seed, 67890 };
const auto bsize = part.nx.x * part.nx.y;
const int ystride = part.nx.x;
sycl::range<1> local{ 8 };
sycl::range<1> global{ part.ntiles.x * part.ntiles.y };
part.queue.submit([&](sycl::handler &h) {
auto part_offset = part.offset;
auto part_np = part.np;
auto part_u = part.u;
auto part_ix = part.ix;
auto ufl = this -> ufl;
auto uth = this -> uth;
auto npmin = this -> npmin;
/// @brief [shared] Local copy of E-field
auto fluid = sycl::local_accessor< float3, 1 > ( bsize, h );
/// @brief [shared] Local copy of B-field
auto npcell = sycl::local_accessor< int, 1 > ( bsize, h );
h.parallel_for(
sycl::nd_range{ global * local, local },
[=](sycl::nd_item<1> it) {
const auto tile_id = it.get_group_linear_id();
const int offset = part_offset[tile_id];
const int np = part_np[tile_id];
float3 * __restrict__ const u = &part_u[ offset ];
int2 const * const __restrict__ ix = &part_ix[offset];
// Initialize random state variables
uint2 state;
double norm;
zrandom::rand_init( it.get_global_linear_id(), rnd_seed, state, norm );
for( auto idx = it.get_local_id(0); idx < bsize; idx += it.get_local_range(0) ) {
fluid[idx] = make_float3(0,0,0);
npcell[idx] = 0;
}
it.barrier();
for( auto i = it.get_local_id(0); i < np; i += it.get_local_range(0) ) {
float3 upart = make_float3 (
uth.x * zrandom::rand_norm( state, norm ),
uth.y * zrandom::rand_norm( state, norm ),
uth.z * zrandom::rand_norm( state, norm )
);
u[i] = upart;
int const idx = ix[i].x + ystride * ix[i].y;
device::local::atomicAdd( & npcell[idx], 1 );
device::local::atomicAdd( & fluid[idx].x, upart.x );
device::local::atomicAdd( & fluid[idx].y, upart.y );
device::local::atomicAdd( & fluid[idx].z, upart.z );
}
it.barrier();
for( auto idx = it.get_local_id(0); idx < bsize; idx += it.get_local_range(0) ) {
if ( npcell[idx] > npmin ) {
fluid[idx].x /= npcell[idx];
fluid[idx].y /= npcell[idx];
fluid[idx].z /= npcell[idx];
} else {
fluid[idx] = make_float3(0,0,0);
}
}
it.barrier();
for( auto i = it.get_local_id(0); i < np; i += it.get_local_range(0) ) {
int const idx = ix[i].x + ystride * ix[i].y;
u[i] += ufl - fluid[idx];
}
});
});
}