-
Notifications
You must be signed in to change notification settings - Fork 0
/
culsp.cu
359 lines (241 loc) · 8.51 KB
/
culsp.cu
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2010 Rich Townsend <[email protected]>
//
// This file is part of CULSP.
//
// CULSP is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CULSP is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CULSP. If not, see <http://www.gnu.org/licenses/>.
// Includes
#include <stdlib.h>
#include <string.h>
#include <argtable2.h>
#include "periodogram.h"
#include "culsp.h"
#include "culsp_kernel.cu"
#include "minmax.cu"
// Wrapper macros
#define CUDA_CALL(call) { \
cudaError err = call; \
if(err != cudaSuccess) { \
fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
__FILE__, __LINE__, cudaGetErrorString(err)); \
exit(EXIT_FAILURE); \
}}
#define CUDA_ERR_CHECK() { \
err = cudaGetLastError(); \
if(err != cudaSuccess) { \
fprintf(stderr, "Cuda error: kernel launch failed in file '%s' in line %i : %s.\n", \
__FILE__, __LINE__, cudaGetErrorString(err)); \
exit(EXIT_FAILURE); \
}}
// Forward declarations
//void initialize (int, char **, char **, char **, float *, float *, int *);
//void initialize_cuda (int, int);
//void eval_LS_periodogram (int, int, float, float *, float *, float *);
// Main program
int
main( int argc, char** argv)
{
char *filename_in;
char *filename_out;
float F_over;
float F_high;
int device;
int N_t;
int N_f;
float *t;
float *X;
float df;
float *P;
float minf = 0.0;
// Initialize
initialize(argc, argv, &filename_in, &filename_out, &F_over, &F_high, &device);
// Read the light curve
read_light_curve(filename_in, &N_t, &t, &X);
// Set up the frequency parameters
set_frequency_params(N_t, t, F_over, F_high, &N_f, &df);
// Allocate space for the periodogram
P = (float *) malloc(N_f*sizeof(float));
// Initialize CUDA
initialize_cuda(device);
// Start the timer
double time_a = get_time();
// Evaluate the Lomb-Scargle periodogram
// set minf to 0 here (simplicity; I'm interacting with this
// through python anyway.
eval_LS_periodogram(N_t, N_f, df, minf, t, X, P);
// Stop the timer
double time_b = get_time();
printf( "Processing time: %16.3f (ms)\n", (time_b-time_a)*1000);
// Write the data to file
write_periodogram(filename_out, N_f, df, P);
// Free up space
free(P);
free(X);
free(t);
// Finish
return 0;
}
////
// Initialization
////
void
initialize (int argc, char **argv, char **filename_in, char **filename_out,
float *F_over, float *F_high, int *device)
{
// Set up the argtable structs
struct arg_file *in = arg_file1(NULL, "in", "<filename_in>", "input file");
struct arg_file *out = arg_file1(NULL, "out", "<filename_out>", "output file");
struct arg_dbl *over = arg_dbl0(NULL, "over", "<F_over>", "oversample factor");
struct arg_dbl *high = arg_dbl0(NULL, "high", "<F_high>", "highest-frequency factor");
struct arg_int *dev = arg_int0(NULL, "device", "<device>", "device number");
struct arg_end *end = arg_end(20);
void *argtable[] = {in,out,over,high,dev,end};
// Parse the command line
int n_error = arg_parse(argc, argv, argtable);
if (n_error == 0) {
*filename_in = (char *) malloc(strlen(in->filename[0])+1);
strcpy(*filename_in, in->filename[0]);
*filename_out = (char *) malloc(strlen(out->filename[0])+1);
strcpy(*filename_out, out->filename[0]);
*F_over = over->count == 1 ? (float) over->dval[0] : 1.f;
*F_high = high->count == 1 ? (float) high->dval[0] : 1.f;
*device = dev->count == 1 ? dev->ival[0] : 0;
}
else {
printf("Syntax: %s", argv[0]);
arg_print_syntax(stdout, argtable, "\n");
exit(EXIT_FAILURE);
}
// Finish
}
////
// CUDA Initialization
////
void
initialize_cuda (int device)
{
// Select the device
CUDA_CALL(cudaSetDevice(device));
// Dummy call to initialize the CUDA runtime
CUDA_CALL(cudaThreadSynchronize());
// Finish
}
////
// Periodogram evaluation
////
void
eval_LS_periodogram (int N_t, int N_f, float df, float minf,
float *t, float *X, float *P)
{
// Allocate device memory and copy data over
float *d_t;
float *d_X;
float *d_P;
CUDA_CALL(cudaMalloc((void**) &d_t, N_t*sizeof(float)));
CUDA_CALL(cudaMalloc((void**) &d_X, N_t*sizeof(float)));
CUDA_CALL(cudaMalloc((void**) &d_P, N_f*sizeof(float)));
CUDA_CALL(cudaMemcpy(d_t, t, N_t*sizeof(float), cudaMemcpyHostToDevice));
CUDA_CALL(cudaMemcpy(d_X, X, N_t*sizeof(float), cudaMemcpyHostToDevice));
// Set up run parameters
dim3 grid_dim(N_f/BLOCK_SIZE, 1, 1);
dim3 block_dim(BLOCK_SIZE, 1, 1);
//printf("Grid of %d frequency blocks of size %d threads\n", N_f/BLOCK_SIZE, BLOCK_SIZE);
// Launch the kernel
//printf("Launching kernel...\n");
culsp_kernel<<<grid_dim, block_dim>>>(d_t, d_X, d_P, df, N_t, N_f, minf);
cudaError_t err = cudaGetLastError();
if(err != cudaSuccess) {
fprintf(stderr, "Cuda error: kernel launch failed in file '%s' in line %i : %s.\n",
__FILE__, __LINE__, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
CUDA_CALL(cudaThreadSynchronize());
//printf("Completed!\n");
// Copy data from the device
CUDA_CALL(cudaMemcpy(P, d_P, N_f*sizeof(float), cudaMemcpyDeviceToHost));
CUDA_CALL(cudaFree(d_P));
CUDA_CALL(cudaFree(d_X));
CUDA_CALL(cudaFree(d_t));
// Finish
}
void
bootstrap_LS_periodogram(int N_t, int N_f, float df, float minf,
float *t, float *X, float *max_heights, int N_bootstrap, int use_gpu_to_get_max){
// Allocate device memory and copy data over
float *d_t, *d_X, *d_P;
float *P, *gmax;
int i, gd, gdm, gdm0;
float val;
curandState *state;
cudaError_t err;
CUDA_CALL(cudaMalloc((void**) &d_t, N_t*sizeof(float)));
CUDA_CALL(cudaMalloc((void**) &d_X, N_t*sizeof(float)));
CUDA_CALL(cudaMalloc((void**) &d_P, N_f*sizeof(float)));
CUDA_CALL(cudaMemcpy(d_t, t, N_t*sizeof(float), cudaMemcpyHostToDevice));
CUDA_CALL(cudaMemcpy(d_X, X, N_t*sizeof(float), cudaMemcpyHostToDevice));
// Get N_bootstraps LSP; then get max_height of each of these.
// This can be made faster by altering the bootstrap code to get
// the max within the function, though this is much more complicated
// should be small speed increase:
// <LC> ~ 0.4 MB; transfer time wasted = 2 * (0.4/1000 GB) / (~15 GB/s PCIe3x16)
// ~ 8E-4 seconds...maaaaaybe significant...
// timing the results on
gd = N_f/BLOCK_SIZE;
if (gd * BLOCK_SIZE < N_f) gd += 1; // ensure we have enough blocks
dim3 grid_dim(gd, 1, 1);
dim3 block_dim(BLOCK_SIZE, 1, 1);
// setup the random generator
CUDA_CALL(cudaMalloc((void **) &state, gd*BLOCK_SIZE * sizeof(curandState)));
setup_curand_kernel<<<grid_dim, block_dim>>>(state, time(NULL));
if (use_gpu_to_get_max){
// allocate memory for the maximums array
CUDA_CALL(cudaMalloc((void **) &gmax, gd * sizeof(float)));
} else {
//printf("USING CPU TO FIND MAX(P_LS)\n");
P = (float *) malloc(N_f * sizeof(float));
}
for(i=0; i<N_bootstrap; i++){
bootstrap_kernel<<<grid_dim, block_dim>>>(d_t, d_X, d_P, df, N_t, N_f, minf, state);
//CUDA_ERR_CHECK();
if (use_gpu_to_get_max){
// calculate the maximum.
max_reduce<<<grid_dim, block_dim, BLOCK_SIZE * sizeof(float)>>>(d_P, gmax, N_f);
// Now reduce until only one block is needed.
gdm = gd;
while (gdm > 1){
gdm0 = gdm;
gdm /= BLOCK_SIZE;
if( gdm * BLOCK_SIZE < gdm0 ) gdm += 1;
dim3 grid_dim_max(gdm, 1, 1);
max_reduce<<<grid_dim_max, block_dim, BLOCK_SIZE*sizeof(float)>>>(gmax, gmax, gdm0);
}
//copy max(P) to the host
CUDA_CALL(cudaMemcpy(&val, gmax, sizeof(float), cudaMemcpyDeviceToHost));
} else {
CUDA_CALL(cudaMemcpy(P, d_P, N_f*sizeof(float), cudaMemcpyDeviceToHost));
//printf("CPUMAX");
val = cpu_maxf(P, N_f);
}
max_heights[i] = val;
}
//CUDA_ERR_CHECK();
CUDA_CALL(cudaThreadSynchronize());
CUDA_CALL(cudaFree(d_P));
CUDA_CALL(cudaFree(d_X));
CUDA_CALL(cudaFree(d_t));
if (use_gpu_to_get_max) {
CUDA_CALL(cudaFree(state));
CUDA_CALL(cudaFree(gmax));
}
// Finish
}