-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
236 lines (217 loc) · 8.09 KB
/
main.cpp
File metadata and controls
236 lines (217 loc) · 8.09 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
#include <iostream>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <string>
#include <regex>
#include <unistd.h>
#include <sycl/sycl.hpp>
#include <sycl/ext/intel/fpga_extensions.hpp>
#include "CLUEAlgoFPGA.h"
#define NLAYERS 100
using namespace std;
void exclude_stats_outliers(std::vector<float> &v) {
if (v.size() == 1)
return;
float mean = std::accumulate(v.begin(), v.end(), 0.0) / v.size();
float sum_sq_diff =
std::accumulate(v.begin(), v.end(), 0.0, [mean](float acc, float x) {
return acc + (x - mean) * (x - mean);
});
float stddev = sycl::sqrt(sum_sq_diff / (v.size() - 1));
std::cout << "Sigma cut outliers: " << stddev << std::endl;
float z_score_threshold = 3.0;
v.erase(std::remove_if(v.begin(), v.end(),
[mean, stddev, z_score_threshold](float x) {
float z_score = std::abs(x - mean) / stddev;
return z_score > z_score_threshold;
}),
v.end());
}
pair<float, float> stats(const std::vector<float> &v) {
float m = std::accumulate(v.begin(), v.end(), 0.0) / v.size();
float sum = std::accumulate(v.begin(), v.end(), 0.0, [m](float acc, float x) {
return acc + (x - m) * (x - m);
});
auto den = v.size() > 1 ? (v.size() - 1) : v.size();
return {m, sycl::sqrt(sum / den)};
}
void printTimingReport(std::vector<float> &vals, int repeats,
const std::string label = "SUMMARY ") {
int precision = 2;
float mean = 0.f;
float sigma = 0.f;
exclude_stats_outliers(vals);
tie(mean, sigma) = stats(vals);
std::cout << label << " 1 outliers(" << repeats << "/" << vals.size() << ") "
<< std::fixed << std::setprecision(precision) << mean << " +/- "
<< sigma << " [ms]" << std::endl;
exclude_stats_outliers(vals);
tie(mean, sigma) = stats(vals);
std::cout << label << " 2 outliers(" << repeats << "/" << vals.size() << ") "
<< std::fixed << std::setprecision(precision) << mean << " +/- "
<< sigma << " [ms]" << std::endl;
}
std::string create_outputfileName(const std::string &inputFileName,
const float dc, const float rhoc,
const float outlierDeltaFactor) {
// C++20
// auto suffix = std::format("_{:.2f}_{:.2f}_{:.2f}.csv", dc, rhoc,
// outlierDeltaFactor);
char suffix[100];
snprintf(suffix, 100, "_dc_%.2f_rho_%.2f_outl_%.2f.csv", dc, rhoc,
outlierDeltaFactor);
std::string tmpFileName;
std::regex regexp("input");
std::regex_replace(back_inserter(tmpFileName), inputFileName.begin(),
inputFileName.end(), regexp, "output");
std::string outputFileName;
std::regex regexp2(".csv");
std::regex_replace(back_inserter(outputFileName), tmpFileName.begin(),
tmpFileName.end(), regexp2, suffix);
return outputFileName;
}
void readDataFromFile(const std::string &inputFileName, std::vector<float> &x,
std::vector<float> &y, std::vector<int> &layer,
std::vector<float> &weight) {
// make dummy layers
for (int l = 0; l < NLAYERS; l++) {
// open csv file
std::ifstream iFile(inputFileName);
std::string value = "";
// Iterate through each line and split the content using delimeter
while (getline(iFile, value, ',')) {
x.push_back(std::stof(value));
getline(iFile, value, ',');
y.push_back(std::stof(value));
getline(iFile, value, ',');
layer.push_back(std::stoi(value) + l);
getline(iFile, value);
weight.push_back(std::stof(value));
}
iFile.close();
}
}
void mainRun(const std::string &inputFileName,
const std::string &outputFileName, const float dc,
const float rhoc, const float outlierDeltaFactor,
const bool use_accelerator, const int repeats,
const bool verbose) {
//////////////////////////////
// read toy data from csv file
//////////////////////////////
std::cout << "Start to load input points" << std::endl;
std::vector<float> x;
std::vector<float> y;
std::vector<int> layer;
std::vector<float> weight;
readDataFromFile(inputFileName, x, y, layer, weight);
std::cout << "Finished loading input points" << std::endl;
// Vector to perform some bread and butter analysis on the timing
vector<float> vals;
std::cout << x.size() << std::endl ;
//////////////////////////////
// run CLUE algorithm
//////////////////////////////
std::cout << "Start to run CLUE algorithm" << std::endl;
#if FPGA_SIMULATOR
auto selector = sycl::ext::intel::fpga_simulator_selector_v;
#elif FPGA_HARDWARE
auto selector = sycl::ext::intel::fpga_selector_v;
#else // #if FPGA_EMULATOR
auto selector = sycl::ext::intel::fpga_emulator_selector_v;
#endif
sycl::queue q{selector};
auto device = q.get_device();
std::cout << "Running on device: "
<< device.get_info<sycl::info::device::name>().c_str()
<< std::endl;
CLUEAlgoFPGA<TilesConstants, NLAYERS> clueAlgo(dc, rhoc, outlierDeltaFactor,
verbose,q);
vals.clear();
for (unsigned r = 0 ; r < repeats ; ++r){
if (!clueAlgo.setPoints(x.size(), &x[0], &y[0], &layer[0], &weight[0]))
exit(EXIT_FAILURE);
// measure excution time of makeClusters
auto start = std::chrono::high_resolution_clock::now();
try{
clueAlgo.makeClusters();
}catch(sycl::exception & err){
std::cout << err.what() << " (" << err.code() << ")\n";
}
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "Iteration " << r;
std::cout << " | Elapsed time: " << elapsed.count() * 1000 << " ms\n";
// Skip first event
if (r != 0 or repeats == 1) {
vals.push_back(elapsed.count() * 1000);
}
}
printTimingReport(vals, repeats, "SUMMARY FPGA:");
// output result to outputFileName. -1 means all points.
if(verbose)
clueAlgo.verboseResults(outputFileName, -1);
}
int main(int argc, char *argv[]) {
//////////////////////////////
// MARK -- set algorithm parameters
//////////////////////////////
extern char *optarg;
bool use_accelerator = false;
bool verbose = false;
float dc = 20.f, rhoc = 80.f, outlierDeltaFactor = 2.f;
int repeats = 10;
int TBBNumberOfThread = 1;
int opt;
std::string inputFileName;
while ((opt = getopt(argc, argv, "i:d:r:o:e:t:uv")) != -1) {
switch (opt) {
case 'i': /* input filename */
inputFileName = string(optarg);
break;
case 'd': /* delta_c */
dc = stof(string(optarg));
break;
case 'r': /* critical density */
rhoc = stof(string(optarg));
break;
case 'o': /* outlier factor */
outlierDeltaFactor = stof(string(optarg));
break;
case 'e': /* number of repeated session(s) a the selected input file */
repeats = stoi(string(optarg));
break;
case 't': /* number of TBB threads */
TBBNumberOfThread = stoi(string(optarg));
std::cout << "Using " << TBBNumberOfThread;
std::cout << " TBB Threads" << std::endl;
break;
case 'u': /* Use accelerator */
use_accelerator = true;
break;
case 'v': /* Verbose output */
verbose = true;
break;
default:
std::cout << "bin/main -i [fileName] -d [dc] -r [rhoc] -o "
"[outlierDeltaFactor] -e [repeats] -t "
"[NumTBBThreads] -u -v"
<< std::endl;
exit(EXIT_FAILURE);
}
}
//////////////////////////////
// MARK -- set input and output files
//////////////////////////////
std::cout << "Input file: " << inputFileName << std::endl;
std::string outputFileName =
create_outputfileName(inputFileName, dc, rhoc, outlierDeltaFactor);
std::cout << "Output file: " << outputFileName << std::endl;
//////////////////////////////
// MARK -- test run
//////////////////////////////
mainRun(inputFileName, outputFileName, dc, rhoc, outlierDeltaFactor,
use_accelerator, repeats, verbose);
return 0;
}