-
Notifications
You must be signed in to change notification settings - Fork 0
/
Averager.h
205 lines (153 loc) · 5.16 KB
/
Averager.h
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 <iostream>
#include <string>
#include <valarray>
#include <vector>
#include <iterator>
#include <thread>
#include <CCfits/CCfits>
using namespace std;
//using namespace CCfits;
/*
********* Parameters for threads
*/
struct ParamsThread {
unsigned int index; // Index of thread
unsigned int startY; // Global index, from where to start computations and where to write the result
unsigned int endY; // Size of the computations for thread
};
class Averager {
private:
string fileName;
int startX,
endX,
startY,
endY,
startZ,
endZ,
numThreads = 2;
double cdelt;
valarray<float> data, res;
vector<unsigned int> dataDimentions;
public:
Averager(char* fileName) {
readImage(fileName);
this->res.resize(this->dataDimentions[0] * this->dataDimentions[1]);
}
Averager(char* fileName, int startX, int endX, int startY, int endY, int startZ, int endZ, double cdelt) {
this->fileName = fileName;
this->startX = startX;
this->endX = endX;
this->startY = startY;
this->endY = endY;
this->startZ = startZ;
this->endZ = endZ;
this->cdelt = cdelt;
cout << fileName << endl;
readImage(fileName);
this->res.resize(this->dataDimentions[0] * this->dataDimentions[1]);
}
Averager(char* fileName, int startX, int endX, int startY, int endY, int startZ, int endZ, double cdelt, int numThreads) {
this->fileName = fileName;
this->startX = startX;
this->endX = endX;
this->startY = startY;
this->endY = endY;
this->startZ = startZ;
this->endZ = endZ;
this->cdelt = cdelt;
this->numThreads = numThreads;
cout << this->numThreads << endl;
readImage(fileName);
this->res.resize(this->dataDimentions[0] * this->dataDimentions[1]);
}
Averager(int startX, int endX, int startY, int endY, int startZ, int endZ, double cdelt) {
this->startX = startX;
this->endX = endX;
this->startY = startY;
this->endY = endY;
this->startZ = startZ;
this->endZ = endZ;
this->cdelt = cdelt;
cout << cdelt << endl;
}
Averager() {}
void setVariables(int startX, int endX, int startY, int endY, int startZ, int endZ, double cdelt, int numThreads) {
this->startX = startX;
this->endX = endX;
this->startY = startY;
this->endY = endY;
this->startZ = startZ;
this->endZ = endZ;
this->cdelt = cdelt;
this->numThreads = numThreads;
}
void readImage(char* fileName) {
/* if( info )
FITS::setVerboseMode(true);*/
try {
auto_ptr<CCfits::FITS> pInfile( new CCfits::FITS( fileName, CCfits::Read, true ) );
CCfits::PHDU& image = pInfile->pHDU();
// read all user-specifed, coordinate, and checksum keys in the image
// image.readAllKeys();
image.read( this->data );
int size = image.axes();
this->dataDimentions.resize( size );
for(int i = 0; i < size; ++i )
this->dataDimentions[i] = image.axis( i );
} catch (CCfits::FitsException&) {
cerr << " Fits Exception Thrown by FitsImage class \n";
cerr << " Fits file name : " << fileName << endl;
}
catch (std::bad_alloc& ba) {
cerr << "bad_alloc caught: " << ba.what() << endl;
cerr << "Not enough space in RAM to load FITS file" << endl;
exit(1);
}
}
vector<unsigned int> getDataDimentions() {
return this->dataDimentions;
}
void runThreads(unsigned int index, unsigned int startY, unsigned int endY) {
cout << "Thread #" << index << " will process: " << startY << ", " << endY << endl;
unsigned int step = this->dataDimentions[0] * this->dataDimentions[1];
unsigned int count = this->endZ - this->startZ;
unsigned int offset = this->startZ * step;
for(unsigned int i = startY; i < endY; ++i) {
unsigned int realInd = i * this->dataDimentions[0];
for(unsigned int j = 0; j < this->dataDimentions[0] ; ++j) {
unsigned int resIj = realInd + j;
unsigned int start = offset + resIj;
valarray<float> tmp = this->data[slice(start, count, step)];
this->res[resIj] = tmp.sum() * this->cdelt;
}
}
}
vector<float> countAverage() {
cout << "data size: " << this->data.size() << " result size: " << this->res.size() << endl;
vector<thread> threads( this->numThreads );
vector<ParamsThread> paramsThreads( this->numThreads );
unsigned int jobSize = this->dataDimentions[1] / this->numThreads;
unsigned int remainder = this->dataDimentions[1] % this->numThreads;
paramsThreads[0].index = 0;
paramsThreads[0].startY = 0;
paramsThreads[0].endY = paramsThreads[0].startY + (0 < remainder ? jobSize + 1 : jobSize);
for (unsigned int i = 1; i < this->numThreads; ++i) {
paramsThreads[i].index = i;
paramsThreads[i].startY = paramsThreads[i - 1].endY; // + paramsThreads[i - 1].startY;
paramsThreads[i].endY = paramsThreads[i].startY + (i < remainder ? jobSize + 1 : jobSize);
}
for (unsigned int i = 0; i < remainder; ++i) {
++paramsThreads[i].endY;
if( i < ( paramsThreads.size() - 1 ) )
++paramsThreads[i + 1].startY;
}
for (unsigned int i = 0; i < this->numThreads; ++i) {
threads[i] = thread(&Averager::runThreads, this, paramsThreads[i].index, paramsThreads[i].startY, paramsThreads[i].endY);
}
for (unsigned int i = 0; i < this->numThreads; ++i) {
threads[i].join();
}
vector<float> v(begin(this->res), end(this->res));
return v;
}
};