-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccube.cpp
More file actions
525 lines (475 loc) · 18.2 KB
/
ccube.cpp
File metadata and controls
525 lines (475 loc) · 18.2 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "ccube.h"
#include <string.h>
#include <math.h>
#include <vector>
#include <CCfits>
#include <cuda_runtime.h>
#include <cufft.h>
#include "cspslice.h"
#include "ccomplex.h"
#include "cdevice.cuh"
#include "regions.h"
#include "errors.h"
using namespace CCfits;
using std::valarray;
hcube::hcube(std::valarray<double> data, std::vector<long> dim, std::vector<int> wavelengths) {
/*
Construct a cube in host memory using a double valarray [data] with dimensions [dim] and
wavelengths [wavelengths].
*/
hcube::domain = SPATIAL;
long slice_nelements = dim[0] * dim[1];
long offset;
for (int i = 0; i < dim[2]; i++) {
offset = i*slice_nelements;
hspslice* new_slice = new hspslice(this, data[std::slice(offset, slice_nelements, 1)], rectangle(0, 0, dim[0], dim[1]), wavelengths[i]);
hcube::slices.push_back(new_slice);
}
}
hcube::hcube(std::valarray<Complex> data, std::vector<long> dim, std::vector<int> wavelengths, ccube_domains domain) {
/*
Construct a cube in host memory using a double valarray [data] of domain [domain] with dimensions [dim] and
wavelengths [wavelengths].
*/
hcube::domain = domain;
long slice_n_elements = dim[0] * dim[1];
long offset;
for (int i = 0; i < dim[2]; i++) {
offset = i*slice_n_elements;
hspslice* new_slice = new hspslice(this, data[std::slice(offset, slice_n_elements, 1)], rectangle(0, 0, dim[0], dim[1]), wavelengths[i]);
hcube::slices.push_back(new_slice);
}
}
hcube::hcube(dcube* d_datacube) {
/*
Construct a cube in host memory by copying a cube [d_datacube] from device memory.
*/
hcube::domain = d_datacube->domain;
hcube::state = d_datacube->state;
for (std::vector<dspslice*>::iterator it = d_datacube->slices.begin(); it != d_datacube->slices.end(); ++it) {
hspslice* new_slice = new hspslice(this, d_datacube->slices[std::distance(d_datacube->slices.begin(), it)]);
hcube::slices.push_back(new_slice);
}
}
hcube::~hcube() {
// delete slice instances
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
delete (*it);
}
}
void hcube::clear() {
/*
Clear data from all slices in a host cube.
*/
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
(*it)->clear();
}
}
void hcube::crop(rectangle region) {
/*
Crop all slices of a host cube by the region [region].
*/
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
(*it)->crop(region);
}
hcube::state = OK;
}
void hcube::crop(std::vector<rectangle> regions) {
/*
Crop each slice of a host cube by the corresponding region in vector [regions].
*/
std::vector<long> region_size_x, region_size_y;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
(*it)->crop(regions[std::distance(hcube::slices.begin(), it)]);
region_size_x.push_back(regions[std::distance(hcube::slices.begin(), it)].x_size);
region_size_y.push_back(regions[std::distance(hcube::slices.begin(), it)].y_size);
}
// check cube integrity
if (std::equal(region_size_x.begin() + 1, region_size_x.end(), region_size_x.begin()) &&
std::equal(region_size_y.begin() + 1, region_size_y.end(), region_size_y.begin())) {
hcube::state = OK;
} else {
hcube::state = INCONSISTENT;
}
}
hcube* hcube::deepcopy() {
/*
Deep copy an instance of a host cube.
*/
hcube* new_datacube = new hcube();
new_datacube->domain = hcube::domain;
new_datacube->state = hcube::state;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
hspslice* new_slice = (*it)->deepcopy();
new_datacube->slices.push_back(new_slice);
}
return new_datacube;
}
std::valarray<double> hcube::getDataAsValarray(complex_part part) {
/*
Get data from host cube corresponding to complex part [part] as a valarray.
*/
long nelements = 0;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
nelements += (*it)->getNumberOfElements();
}
std::valarray<double> data(nelements);
long data_offset = 0;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
for (int i = 0; i < (*it)->getNumberOfElements(); i++) {
if (part == REAL) {
data[i + data_offset] = (*it)->p_data[i].x;
} else if (part == IMAGINARY) {
data[i + data_offset] = (*it)->p_data[i].y;
} else if (part == AMPLITUDE) {
data[i + data_offset] = cGetAmplitude((*it)->p_data[i]);
} else if (part == PHASE) {
data[i + data_offset] = cGetPhase((*it)->p_data[i]);
}
}
data_offset += (*it)->getNumberOfElements();
}
return data;
}
std::valarray<double> hcube::getDataAsValarray(complex_part part, int slice_index) {
/*
Get data from slice [slice_index] of host cube corresponding to complex part [part] as a valarray.
*/
long nelements = hcube::slices[slice_index]->region.x_size*hcube::slices[slice_index]->region.y_size;
std::valarray<double> data(nelements);
for (int i = 0; i < nelements; i++) {
if (part == REAL) {
data[i] = hcube::slices[slice_index]->p_data[i].x;
} else if (part == IMAGINARY) {
data[i] = hcube::slices[slice_index]->p_data[i].y;
} else if (part == AMPLITUDE) {
data[i] = cGetAmplitude(hcube::slices[slice_index]->p_data[i]);
} else if (part == PHASE) {
data[i] = cGetPhase(hcube::slices[slice_index]->p_data[i]);
}
}
return data;
}
rectangle hcube::getLargestSliceRegion() {
std::vector<rectangle> regions;
std::vector<long> region_sizes;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
regions.push_back((*it)->region);
region_sizes.push_back((*it)->region.x_size*(*it)->region.y_size);
}
std::vector<long>::iterator result = std::max_element(std::begin(region_sizes), std::end(region_sizes));
return regions[std::distance(std::begin(region_sizes), result)];
}
int hcube::getNumberOfSpaxels() {
int n_spaxels = 0;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
n_spaxels += (*it)->getNumberOfElements();
}
return n_spaxels;
}
rectangle hcube::getSmallestSliceRegion() {
std::vector<rectangle> regions;
std::vector<long> region_sizes;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
regions.push_back((*it)->region);
region_sizes.push_back((*it)->region.x_size*(*it)->region.y_size);
}
std::vector<long>::iterator result = std::min_element(std::begin(region_sizes), std::end(region_sizes));
return regions[std::distance(std::begin(region_sizes), result)];
}
void hcube::grow(rectangle region) {
/*
Grow all slices of a host cube by the region [region].
*/
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
(*it)->grow(region);
}
hcube::state = OK;
}
void hcube::grow(std::vector<rectangle> regions) {
/*
Grow each slice of a host cube by the corresponding region in vector [regions].
*/
std::vector<long> region_size_x, region_size_y;
for (std::vector<hspslice*>::iterator it = hcube::slices.begin(); it != hcube::slices.end(); ++it) {
(*it)->grow(regions[std::distance(hcube::slices.begin(), it)]);
region_size_x.push_back(regions[std::distance(hcube::slices.begin(), it)].x_size);
region_size_y.push_back(regions[std::distance(hcube::slices.begin(), it)].y_size);
}
// check cube integrity
if (std::equal(region_size_x.begin() + 1, region_size_x.end(), region_size_x.begin()) &&
std::equal(region_size_y.begin() + 1, region_size_y.end(), region_size_y.begin())) {
hcube::state = OK;
}
else {
hcube::state = INCONSISTENT;
}
}
void hcube::rescale(std::vector<double> scale_factors) {
/*
Rescale slices of a host cube by [scale_factors]. Note that this only makes sense when working on data
in the frequency domain.
*/
if (hcube::domain == FREQUENCY) {
std::vector<double> old_region_size_x, old_region_size_y, new_region_size_x, new_region_size_y;
long x_new_size, y_new_size, x_start, y_start;
for (int i = 0; i < hcube::slices.size(); i++) {
old_region_size_x.push_back((double)hcube::slices[i]->region.x_size);
old_region_size_y.push_back((double)hcube::slices[i]->region.y_size);
x_new_size = round((double)hcube::slices[i]->region.x_size * scale_factors[i]);
y_new_size = round((double)hcube::slices[i]->region.y_size * scale_factors[i]);
x_start = hcube::slices[i]->region.x_start + round((hcube::slices[i]->region.x_size - x_new_size) / 2);
y_start = hcube::slices[i]->region.y_start + round((hcube::slices[i]->region.y_size - y_new_size) / 2);
rectangle this_region = rectangle(x_start, y_start, x_new_size, y_new_size);
if (this_region.x_size < hcube::slices[i]->region.x_size) {
hcube::slices[i]->crop(this_region);
} else if (this_region.x_size > hcube::slices[i]->region.x_size) {
hcube::slices[i]->grow(this_region);
}
new_region_size_x.push_back(this_region.x_size);
new_region_size_y.push_back(this_region.y_size);
}
// check cube integrity (unless all wavelengths are equal, this should fail!)
if (std::equal(new_region_size_x.begin() + 1, new_region_size_x.end(), new_region_size_x.begin()) &&
std::equal(new_region_size_y.begin() + 1, new_region_size_y.end(), new_region_size_y.begin())) {
hcube::state = OK;
}
else {
hcube::state = INCONSISTENT;
}
}
else {
throw_error(CCUBE_BAD_DOMAIN);
}
}
void hcube::write(complex_part part, string out_filename, bool clobber) {
/*
Write hard copy of complex part [part] of host datacube to file [out_filename].
*/
if (hcube::state == OK) {
long naxis = 3;
long naxes[3] = { (*hcube::slices[0]).getDimensions().x, (*hcube::slices[0]).getDimensions().y, long(hcube::slices.size()) };
long n_elements = naxes[0] * naxes[1] * naxes[2];
std::auto_ptr<FITS> pFits(0);
try {
std::string fileName(out_filename);
if (clobber) {
fileName.insert(0, std::string("!"));
}
pFits.reset(new FITS(fileName, DOUBLE_IMG, naxis, naxes));
} catch (FITS::CantCreate) {
throw_error(CCUBE_FAIL_WRITE);
}
pFits->addImage("DATA", DOUBLE_IMG, std::vector<long>({ naxes[0], naxes[1] }));
long fpixel(1);
pFits->pHDU().write(fpixel, n_elements, hcube::getDataAsValarray(part));
} else if (hcube::state == INCONSISTENT) {
throw_error(CCUBE_FAIL_INTEGRITY_CHECK);
}
}
void hcube::write(complex_part part, string out_filename, int slice_index, bool clobber) {
/*
Write hard copy of complex part [part] of slice [slice_index] from host datacube to file [out_filename].
*/
if (hcube::state == OK) {
long naxis = 2;
long naxes[2] = { (*hcube::slices[slice_index]).getDimensions().x, (*hcube::slices[slice_index]).getDimensions().y };
long n_elements = naxes[0] * naxes[1];
std::auto_ptr<FITS> pFits(0);
try {
std::string fileName(out_filename);
if (clobber) {
fileName.insert(0, std::string("!"));
}
pFits.reset(new FITS(fileName, DOUBLE_IMG, naxis, naxes));
} catch (FITS::CantCreate) {
throw_error(CCUBE_FAIL_WRITE);
}
pFits->addImage("DATA", DOUBLE_IMG, std::vector<long>({ naxes[0], naxes[1] }));
long fpixel(1);
pFits->pHDU().write(fpixel, n_elements, hcube::getDataAsValarray(part, slice_index));
}
}
dcube::dcube(hcube* h_datacube) {
/*
Construct a cube in device memory by copying a cube [h_datacube] from host memory.
*/
dcube::domain = h_datacube->domain;
dcube::state = h_datacube->state;
for (std::vector<hspslice*>::iterator it = h_datacube->slices.begin(); it != h_datacube->slices.end(); ++it) {
dspslice* new_slice = new dspslice(this, h_datacube->slices[std::distance(h_datacube->slices.begin(), it)]);
dcube::slices.push_back(new_slice);
}
}
dcube::~dcube() {
// delete slice instances
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
delete (*it);
}
}
void dcube::clear() {
/*
Clear data from all slices in device cube.
*/
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
(*it)->clear();
}
}
void dcube::crop(rectangle region) {
/*
Crop all slices of a device cube by the region [region].
*/
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
(*it)->crop(region);
}
dcube::state = OK;
}
void dcube::crop(std::vector<rectangle> regions) {
/*
Crop each slice of a device cube by the corresponding region in vector [regions].
*/
std::vector<long> region_size_x, region_size_y;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
(*it)->crop(regions[std::distance(dcube::slices.begin(), it)]);
region_size_x.push_back(regions[std::distance(dcube::slices.begin(), it)].x_size);
region_size_y.push_back(regions[std::distance(dcube::slices.begin(), it)].y_size);
}
// check cube integrity
if (std::equal(region_size_x.begin() + 1, region_size_x.end(), region_size_x.begin()) &&
std::equal(region_size_y.begin() + 1, region_size_y.end(), region_size_y.begin())) {
dcube::state = OK;
} else {
dcube::state = INCONSISTENT;
}
}
dcube* dcube::deepcopy() {
/*
Deep copy an instance of a device cube.
*/
dcube* new_datacube = new dcube();
new_datacube->domain = dcube::domain;
new_datacube->state = dcube::state;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
dspslice* new_slice = (*it)->deepcopy();
new_datacube->slices.push_back(new_slice);
}
return new_datacube;
}
void dcube::fft(bool inverse) {
/*
Perform a fast fourier transform on the device data in the direction specified by the [inverse] flag.
*/
int DIRECTION = (inverse == true) ? CUFFT_FORWARD : CUFFT_INVERSE;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
cufftHandle plan;
if (cufftPlan2d(&plan, (*it)->region.x_size, (*it)->region.y_size, CUFFT_Z2Z) != CUFFT_SUCCESS) {
throw_error(CUDA_FFT_FAIL_CREATE_PLAN);
}
if (cufftExecZ2Z(plan, reinterpret_cast<cufftDoubleComplex*>((*it)->p_data),
reinterpret_cast<cufftDoubleComplex*>((*it)->p_data), DIRECTION) != CUFFT_SUCCESS) {
throw_error(CUDA_FFT_FAIL_EXECUTE_PLAN);
}
if (cudaThreadSynchronize() != cudaSuccess){
throw_error(CUDA_FAIL_SYNCHRONIZE);
}
cufftDestroy(plan);
}
switch (dcube::domain) {
case (SPATIAL) :
dcube::domain = FREQUENCY;
break;
case (FREQUENCY) :
dcube::domain = SPATIAL;
break;
}
}
rectangle dcube::getLargestSliceRegion() {
std::vector<rectangle> regions;
std::vector<long> region_sizes;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
regions.push_back((*it)->region);
region_sizes.push_back((*it)->region.x_size*(*it)->region.y_size);
}
std::vector<long>::iterator result = std::max_element(std::begin(region_sizes), std::end(region_sizes));
return regions[std::distance(std::begin(region_sizes), result)];
}
int dcube::getNumberOfSpaxels() {
int n_spaxels = 0;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
n_spaxels += (*it)->getNumberOfElements();
}
return n_spaxels;
}
rectangle dcube::getSmallestSliceRegion() {
std::vector<rectangle> regions;
std::vector<long> region_sizes;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
regions.push_back((*it)->region);
region_sizes.push_back((*it)->region.x_size*(*it)->region.y_size);
}
std::vector<long>::iterator result = std::min_element(std::begin(region_sizes), std::end(region_sizes));
return regions[std::distance(std::begin(region_sizes), result)];
}
void dcube::grow(rectangle region) {
/*
Grow all slices of a device cube by the region [region].
*/
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
(*it)->grow(region);
}
dcube::state = OK;
}
void dcube::grow(std::vector<rectangle> regions) {
/*
Grow each slice of a device cube by the corresponding region in vector [regions].
*/
std::vector<long> region_size_x, region_size_y;
for (std::vector<dspslice*>::iterator it = dcube::slices.begin(); it != dcube::slices.end(); ++it) {
(*it)->grow(regions[std::distance(dcube::slices.begin(), it)]);
region_size_x.push_back(regions[std::distance(dcube::slices.begin(), it)].x_size);
region_size_y.push_back(regions[std::distance(dcube::slices.begin(), it)].y_size);
}
// check cube integrity
if (std::equal(region_size_x.begin() + 1, region_size_x.end(), region_size_x.begin()) &&
std::equal(region_size_y.begin() + 1, region_size_y.end(), region_size_y.begin())) {
dcube::state = OK;
}
else {
dcube::state = INCONSISTENT;
}
}
void dcube::rescale(std::vector<double> scale_factors) {
/*
Rescale slices of a device cube by [scale_factors]. Note that this only makes sense when working on data
in the frequency domain.
*/
if (dcube::domain == FREQUENCY) {
std::vector<double> old_region_size_x, old_region_size_y, new_region_size_x, new_region_size_y;
long x_new_size, y_new_size, x_start, y_start;
for (int i = 0; i < dcube::slices.size(); i++) {
old_region_size_x.push_back((double)dcube::slices[i]->region.x_size);
old_region_size_y.push_back((double)dcube::slices[i]->region.y_size);
x_new_size = round((double)dcube::slices[i]->region.x_size * scale_factors[i]);
y_new_size = round((double)dcube::slices[i]->region.y_size * scale_factors[i]);
x_start = dcube::slices[i]->region.x_start + round((dcube::slices[i]->region.x_size - x_new_size) / 2);
y_start = dcube::slices[i]->region.y_start + round((dcube::slices[i]->region.y_size - y_new_size) / 2);
rectangle this_region = rectangle(x_start, y_start, x_new_size, y_new_size);
if (this_region.x_size < dcube::slices[i]->region.x_size) {
dcube::slices[i]->crop(this_region);
} else if (this_region.x_size > dcube::slices[i]->region.x_size) {
dcube::slices[i]->grow(this_region);
}
new_region_size_x.push_back(this_region.x_size);
new_region_size_y.push_back(this_region.y_size);
}
// check cube integrity (unless all wavelengths are equal, this should fail!)
if (std::equal(new_region_size_x.begin() + 1, new_region_size_x.end(), new_region_size_x.begin()) &&
std::equal(new_region_size_y.begin() + 1, new_region_size_y.end(), new_region_size_y.begin())) {
dcube::state = OK;
} else {
dcube::state = INCONSISTENT;
}
} else {
throw_error(CCUBE_BAD_DOMAIN);
}
}