-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
269 lines (183 loc) · 7.02 KB
/
main.cpp
File metadata and controls
269 lines (183 loc) · 7.02 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
#include <thrust/device_vector.h>
#include "cudaTimer.h"
#include "windowsCpuTimer.h"
#include "cuda.h"
#include <time.h>
#include <cstdlib>
#include <vector>
#include <iomanip>
#include <iostream>
#include <fstream>
//#include <vtkExecutive.h>
#include <vtkStructuredPointsReader.h>
#include <vtkSmartPointer.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkStructuredPoints.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkFloatArray.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
#include <vtkChartHistogram2D.h>
//#include <vtkRenderer.h>
//#include <vtkRenderWindow.h>
//#include <vtkRenderWindowInteractor.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <vtkColorTransferFunction.h>
#include <vtkAxis.h>
#include <vtkInformation.h>
using namespace std;
//bool loadTextFile(FILE *infile, int xSize, int ySize, int zSize, int numvars, thrust::host_vector<float> & h_data, int bufferSize, int & xPos, int & yPos, int & zPos );
///bool loadTextFile(FILE *infile, int xSize, int ySize, int zSize, int numvars, thrust::host_vector<float> & h_data, int bufferSize, int & xPos, int & yPos, int & zPos )
int main(int argc, char *argv[])
{
const int NUM_BINS = 10;
const int XSIZE = 600;
const int YSIZE = 248;
const int ZSIZE = 248;
//const int XSIZE = 10;
//const int XSIZE = 4;
//const int XSIZE = 600 * 248 * 248;
//const int YSIZE = 1;
//const int ZSIZE = 1;
const int NUMVARS = 10;
//const int NUMVARS = 10;
int rowCount = XSIZE * YSIZE * ZSIZE;
int bufferSize = rowCount / 10;
//int bufferSize = 5;
int var1 = 1; //1st column (var) on which to build a VTK histogram
//int var2 = 5; //2nd column (var) on which to build a VTK histogram
int var2 = 2;
string fileName = "multifield.0001.txt";
//printMinMaxes(fileName, XSIZE * YSIZE * ZSIZE, 10);
thrust::host_vector<float> h_buffer(bufferSize * NUMVARS);
thrust::host_vector<long long> h_data;
thrust::host_vector<long long> h_data2;
FILE *inFile;
if ( (inFile = fopen(fileName.c_str(), "r")) == NULL)
{
fprintf(stderr,"Could not open %s for reading\n", fileName.c_str());
return -2;
}
int xPos = 0, yPos = 0, zPos = 0;
CudaTimer cudaTimer;
WindowsCpuTimer cpuTimer;
//for (int i = 0; i < 9; i++)
//{
// loadTextFile(inFile, XSIZE, YSIZE, ZSIZE, NUMVARS, 10, h_buffer, bufferSize, xPos, yPos, zPos);
//}
while (loadTextFile(inFile, XSIZE * YSIZE * ZSIZE, 1, 1, NUMVARS, 10, h_buffer, bufferSize, xPos, yPos, zPos))
{
#ifdef IS_LOGGING
cout << "Input data:" << endl;
printData(h_buffer.size() / NUMVARS, NUMVARS, 10, h_buffer);
#endif
//thrust::host_vector<int> resultVector1 = doHistogramGPU(XSIZE, YSIZE, ZSIZE, NUMVARS, h_buffer);
doHistogramGPU(XSIZE, YSIZE, ZSIZE, NUMVARS, h_buffer, h_data, h_data2, NUM_BINS, cudaTimer, cpuTimer);
}
#ifdef IS_LOGGING
cout << "Did initial reductions (in buffered segments)..." << endl;
cout << "GPU Keys:" << endl;
for (int i = 0; i < h_data.size(); i++)
{
cout << h_data[i] << " ";
}
cout << endl;
//cout << "GPU Counts:" << endl;
//for (int i = 0; i < h_data2.size(); i++)
//{
// cout << h_data2[i] << " ";
//}
//cout << endl;
#endif
//Do the query
//At this point, we have a single dimensional representation with 1 bin per element, allowing the query with the current specifications
//TEST CODE!!!!!!!!!!!!!!
//for (int i = 0; i < 64; i++)
//{
// h_data[i] = i + 1;
//}
doQuery(XSIZE, YSIZE, ZSIZE, 0, 3, 0, 3, 0, 3, h_data, h_data2);
thrust::pair<DVL, DVL> endPosition;
histogramMapReduceGPU(h_data, h_data2, endPosition, NUMVARS, NUM_BINS, cudaTimer, cpuTimer);
int numRows = h_data.size() / NUMVARS;
#ifdef PRINT_RESULT
cout << "Final multidimensional representation from GPU" << endl;
printHistoData(numRows, NUMVARS, 10, h_data, h_data2);
#endif
fclose(inFile);
//////CPU Baseline/////////////////////////////////////////
//NOTE: Only works for lower number of vars and smaller file record counts - will run out of memory otherwise!
///////////////////////////////////////////////////////////
#ifdef DO_CPU_COMPUTATION
FILE *cpuFile;
string cpuFileName = "multifield.0001.txt";
xPos = yPos = zPos = 0;
if ( (cpuFile = fopen(cpuFileName.c_str(), "r")) == NULL)
{
fprintf(stderr,"Could not open %s for reading\n", cpuFileName.c_str());
return -2;
}
h_buffer.resize(XSIZE * YSIZE * ZSIZE * NUMVARS);
loadTextFile(cpuFile, XSIZE, YSIZE, ZSIZE, NUMVARS, 10, h_buffer, XSIZE * YSIZE * ZSIZE, xPos, yPos, zPos);
doHistogramCPU(XSIZE, YSIZE, ZSIZE, NUMVARS, NUM_BINS, h_buffer);
fclose(cpuFile);
#endif
//////Render a histogram in VTK///////////////////////////////
int histoSize = NUM_BINS;
//Reference: http://www.vtk.org/pipermail/vtkusers/2002-June/011682.html
vtkImageData * imageData = vtkImageData::New();
imageData -> SetExtent(0, histoSize - 1, 0, histoSize - 1, 0, 0);
//imageData -> GetPointData() -> SetScalars(floatArray);
vtkInformation * vtkInfo = vtkInformation::New();
imageData ->AllocateScalars(vtkInfo);
double * renderedHistogram = (double*) imageData -> GetScalarPointer(0, 0, 0);
//std::vector<int> renderedHistogram(histoSize * histoSize);
for (int i = 0; i < histoSize * histoSize; i++)
{
renderedHistogram[i] = 0;
}
double * dataPtr = (double *) imageData ->GetScalarPointer(0,0,0);
for (int i = 0; i < numRows; i++)
{
//h_data - CPU calculated histogram - access element # i * NUMVARS + j
//h_data2 - CPU calculated count for a row - access element # i
//renderedHistogram - what we're building to give to VTK - access which element: row # value in col 0; col# value in col 1 - for this row
renderedHistogram[h_data[i * NUMVARS + var1] * histoSize + h_data[i * NUMVARS + var2]] += h_data2[i];
}
#ifdef PRINT_RESULT
cout << "'Full' histogram for VTK to render (flipped to correspond to VTK):" << endl;
for (int i = histoSize - 1; i >= 0; i--)
{
for (int j = 0; j < histoSize; j++)
{
cout << setw(10) << renderedHistogram[i * histoSize + j] << " ";
}
cout << endl;
}
#endif
vtkChartHistogram2D * chart = vtkChartHistogram2D::New();
chart ->SetInputData(imageData);
//chart ->SetRenderEmpty(true);
//chart ->SetAutoAxes(false);
//chart ->GetAxis(vtkAxis::BOTTOM) -> SetRange(0, 4);
//chart ->GetAxis(vtkAxis::BOTTOM) -> SetTitle("Bottom");
//chart ->GetAxis(vtkAxis::LEFT) -> SetRange(1, 10);
//chart ->GetAxis(vtkAxis::LEFT) -> SetTitle("Left");
//chart ->GetAxis(vtkAxis::FIXED) -> SetRange(1, 10);
//Based on: https://github.com/qsnake/vtk/blob/master/Charts/Testing/Cxx/TestHistogram2D.cxx
vtkColorTransferFunction * colorFunction = vtkColorTransferFunction::New();
colorFunction -> AddRGBSegment(0.0f, 0.0, 1.0, 0.0, 824.0, 0.0f, 0.0f, 1.0f);
colorFunction -> Build();
chart ->SetTransferFunction(colorFunction);
vtkContextView * view = vtkContextView::New();
view ->GetScene() ->AddItem(chart);
view ->GetInteractor() -> Initialize();
view ->GetInteractor() -> Start();
return 0;
}