-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathsapporo.cpp
More file actions
283 lines (230 loc) · 7.61 KB
/
sapporo.cpp
File metadata and controls
283 lines (230 loc) · 7.61 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "sapporo.h"
#include <sys/time.h>
#include <algorithm>
#include <stdio.h>
#include <string.h>
sapporo_multi_struct sapporo_multi_data[MAXCUDADEVICES];
double get_time() {
struct timeval Tvalue;
struct timezone dummy;
gettimeofday(&Tvalue,&dummy);
return ((double) Tvalue.tv_sec +
1.e-6*((double) Tvalue.tv_usec));
}
int sapporo::open(int cluster_id) {
fprintf(stderr, "sapporo::open --- ver 1.5 --- \n");
nCUDAdevices = get_device_count();
if (nCUDAdevices > 0) {
fprintf(stderr, "sapporo::open - found %d CUDA device(s)\n",
nCUDAdevices);
} else {
fprintf(stderr, "sapporo::open - FATAL! No CUDA device found\n");
exit(-1);
}
nj_max = 131072;
if(cluster_id >= nCUDAdevices) {
fprintf(stderr, "sapporo::open - FATAL! Cluster id is too high, no cuda device with given id\n");
return -1;
}
device_id = cluster_id;
cudaSetDevice(device_id);
allocate_cuda_memory(device_id);
return 0;
}
int sapporo::close(int cluster_id) {
printf("sapporo::close --- ver 1.5 --- \n");
free_cuda_memory(device_id);
if(address_j.size() > 0) {
mapping_from_address_j_to_index_in_update_array.clear();
address_j.clear();
t_j.clear();
pos_j.clear();
vel_j.clear();
acc_j.clear();
jrk_j.clear();
}
return 0;
}
int sapporo::set_ti(int cluster_id, double ti) {
t_i = to_DS(ti);
predict = true;
return 0;
}
int sapporo::get_n_pipes() {
return n_pipes;
}
int sapporo::set_j_particle(int cluster_id,
int address,
int id,
double tj, double dtj,
double mass,
double k18[3], double j6[3],
double a2[3], double v[3], double x[3]) {
if (address > nj_max) {
fprintf(stderr, "Fatal! address= %d > nj_max= %d. I am giving up.\n",
address, nj_max);
exit(-1);
}
// Skip test particles (massless particles) as they don't contribute to gravity
if (mass <= SAPPORO_TEST_PARTICLE_MASS) return 0;
DS Dmass = (DS){mass, INT_AS_FLOAT(id)};
map<int,int>::iterator iterator = mapping_from_address_j_to_index_in_update_array.find(address);
map<int,int>::iterator end = mapping_from_address_j_to_index_in_update_array.end();
if(iterator != end)
{
//int index = (*iterator).first;
int index = (*iterator).second; //Jeroen 1-march-2011,
//printf("found index: %d for address: %d\n", index, address);
t_j[index] = (DS2){to_DS(tj), to_DS(dtj)};
pos_j[index] = ( (DS4){to_DS(x[0]), to_DS(x[1]), to_DS(x[2]), Dmass} );
vel_j[index] = ( (float4){v[0], v[1], v[2], 0.0} );
acc_j[index] = ( (float4){a2[0]*2, a2[1]*2, a2[2]*2, 0.0} );
jrk_j[index] = ( (float4){j6[0]*6, j6[1]*6, j6[2]*6, 0.0} );
}
else
{
mapping_from_address_j_to_index_in_update_array[address] = address_j.size();
//printf("set index: %d for address: %d\n", address_j.size(), address);
address_j.push_back(address);
t_j.push_back( (DS2){to_DS(tj), to_DS(dtj)} );
pos_j.push_back( (DS4){to_DS(x[0]), to_DS(x[1]), to_DS(x[2]), Dmass} );
vel_j.push_back( (float4){v[0], v[1], v[2], 0.0} );
acc_j.push_back( (float4){a2[0]*2, a2[1]*2, a2[2]*2, 0.0} );
jrk_j.push_back( (float4){j6[0]*6, j6[1]*6, j6[2]*6, 0.0} );
nj_modified = address_j.size();
}
return 0;
};
void sapporo::calc_firsthalf(int cluster_id,
int nj, int ni,
int id[],
double xi[][3], double vi[][3],
double aold[][3], double j6old[][3],
double phiold[3],
double eps2, double h2[]) {
for (int i = 0; i < ni; i++) {
DS Dmass = (DS){h2[i], INT_AS_FLOAT(id[i])};
pos_i[i] = (DS4) { to_DS(xi[i][0]),
to_DS(xi[i][1]),
to_DS(xi[i][2]),
Dmass };
vel_i[i] = (float4){ vi[i][0], vi[i][1], vi[i][2], eps2};
EPS2 = eps2;
}
if (address_j.size() > 0) {
// Since we skip adding test particles, all particles in address_j are massive
send_j_particles_to_device(device_id);
}
send_i_particles_to_device(device_id, ni);
if(address_j.size() > 0) {
mapping_from_address_j_to_index_in_update_array.clear();
address_j.clear();
t_j.clear();
pos_j.clear();
vel_j.clear();
acc_j.clear();
jrk_j.clear();
}
if (nj > 0) {
evaluate_gravity(ni, nj);
}
}
int sapporo::calc_lasthalf(int cluster_id,
int nj, int ni,
int index[],
double xi[][3], double vi[][3],
double eps2, double h2[],
double acc[][3], double jerk[][3], double pot[]) {
for (int i = 0; i < ni; i++) {
pot[i] = 0;
acc[i][0] = acc[i][1] = acc[i][2] = 0;
jerk[i][0] = jerk[i][1] = jerk[i][2] = 0;
}
fetch_data_from_device(device_id, ni);
for (int i = 0; i < ni; i++) {
float4 acci = acc_i[i];
float4 jrki = jrk_i[i];
pot[i] += acci.w;
acc[i][0] += acci.x;
acc[i][1] += acci.y;
acc[i][2] += acci.z;
jerk[i][0] += jrki.x;
jerk[i][1] += jrki.y;
jerk[i][2] += jrki.z;
}
return 0;
};
int sapporo::calc_lasthalf2(int cluster_id,
int nj, int ni,
int index[],
double xi[][3], double vi[][3],
double eps2, double h2[],
double acc[][3], double jerk[][3], double pot[],
int nnbindex[]) {
float ds_min[NTHREADS];
for (int i = 0; i < ni; i++) {
pot[i] = 0;
acc[i][0] = acc[i][1] = acc[i][2] = 0;
jerk[i][0] = jerk[i][1] = jerk[i][2] = 0;
nnbindex[i] = 0;
ds_min[i] = 1.0e10;
}
double t1 = get_time();
fetch_data_from_device(device_id, ni);
for (int i = 0; i < ni; i++) {
float4 acci = acc_i[i];
float4 jrki = jrk_i[i];
float ds = ds_i[i];
// fprintf(stdout, "device= %d, ni= %d pot = %g\n",
// dev, i, acci.x);
pot[i] += acci.w;
acc[i][0] += acci.x;
acc[i][1] += acci.y;
acc[i][2] += acci.z;
jerk[i][0] += jrki.x;
jerk[i][1] += jrki.y;
jerk[i][2] += jrki.z;
if (ds < ds_min[i]) {
int nnb = (int)(jrki.w);
nnbindex[i] = nnb;
ds_min[i] = ds;
}
}
return 0;
};
int sapporo::read_ngb_list(int cluster_id) {
bool overflow = false;
int ni = fetch_ngb_list_from_device(device_id);
for (int j = 0; j < ni; j++) {
if (ngb_list_i[device_id*NGB_PP*n_pipes + j*NGB_PP] >= NGB_PP) {
overflow = true;
}
}
return overflow;
}
int sapporo::get_ngb_list(int cluster_id,
int ipipe,
int maxlength,
int &nblen,
int nbl[]) {
if (ipipe >= device.ni) {
fprintf(stderr, "Fatal! ipipe= %d >= dev.ni= %d. I give up.\n",
ipipe, device.ni);
exit(-1);
}
bool overflow = false;
nblen = 0;
int offset = device_id*NGB_PP*n_pipes + NGB_PP*ipipe;
int len = ngb_list_i[offset];
memcpy(nbl+nblen, &ngb_list_i[offset+1], sizeof(int)*min(len, maxlength - len));
nblen += len;
if (nblen >= maxlength) {
overflow = true;
}
sort(nbl, nbl + min(nblen, maxlength));
return overflow;
}
int sapporo::get_nj_max() const
{
return nj_max;
}