EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SeedfinderSyclTest.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SeedfinderSyclTest.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
15 #include "Acts/Seeding/Seed.hpp"
20 
21 #include <chrono>
22 #include <cmath>
23 #include <ctime>
24 #include <fstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <limits>
28 #include <memory>
29 #include <sstream>
30 #include <string>
31 
32 #include <boost/type_erasure/any_cast.hpp>
33 
34 #include "ATLASCuts.hpp"
35 #include "CommandLineArguments.h"
36 #include "SpacePoint.hpp"
37 
38 auto readFile(const std::string& filename) -> std::vector<const SpacePoint*> {
39  std::string line;
40  std::vector<const SpacePoint*> readSP;
41 
42  std::ifstream spFile(filename);
43  if (spFile.is_open()) {
44  int id = 0;
45  while (std::getline(spFile, line)) {
46  std::stringstream ss(line);
47  std::string linetype;
48  ss >> linetype;
49  if (linetype == "lxyz") {
50  float x;
51  float y;
52  float z;
53  float r;
54  float varianceR;
55  float varianceZ;
56  int layer;
57  ss >> layer >> x >> y >> z >> varianceR >> varianceZ;
58  r = std::sqrt(x * x + y * y);
59  float f22 = varianceR;
60  float wid = varianceZ;
61  float cov = wid * wid * .08333;
62  if (cov < f22)
63  cov = f22;
64  if (std::abs(z) > 450.) {
65  varianceZ = 9. * cov;
66  varianceR = .06;
67  } else {
68  varianceR = 9. * cov;
69  varianceZ = .06;
70  }
71  readSP.emplace_back(
72  new SpacePoint(x, y, z, r, layer, varianceR, varianceZ, id));
73  ++id;
74  }
75  }
76  }
77  return readSP;
78 }
79 
80 template <typename external_spacepoint_t>
84  // silicon detector max
85  config.rMax = 160.;
86  config.deltaRMin = 5.;
87  config.deltaRMax = 160.;
88  config.collisionRegionMin = -250.;
89  config.collisionRegionMax = 250.;
90  config.zMin = -2800.;
91  config.zMax = 2800.;
92  config.maxSeedsPerSpM = 5;
93  // 2.7 eta
94  config.cotThetaMax = 7.40627;
95  config.sigmaScattering = 1.00000;
96  config.minPt = 500.;
97  config.bFieldInZ = 0.00199724;
98  config.beamPos = {-.5, -.5};
99  config.impactMax = 10.;
100 
101  // for sycl
102  config.nTrplPerSpBLimit = 100;
103  config.nAvgTrplPerSpBLimit = 6;
104  return config;
105 }
106 
107 template <typename external_spacepoint_t>
111  Acts::SpacePointGridConfig gridConf{};
112  gridConf.bFieldInZ = config.bFieldInZ;
113  gridConf.minPt = config.minPt;
114  gridConf.rMax = config.rMax;
115  gridConf.zMax = config.zMax;
116  gridConf.zMin = config.zMin;
117  gridConf.deltaRMax = config.deltaRMax;
118  gridConf.cotThetaMax = config.cotThetaMax;
119  return gridConf;
120 }
121 
122 auto main(int argc, char** argv) -> int {
123  auto start_prep = std::chrono::system_clock::now();
124 
125  CommandLineArguments cmdlTool;
126  cmdlTool.parse(argc, argv);
127 
128  if (!cmdlTool.inpFileName.empty() && !cmdlTool.inpFileExists) {
129  std::cerr << "Input file not found\n";
130  return -1;
131  }
132 
133  auto spVec = readFile(cmdlTool.inpFileName);
134 
135  auto bottomBinFinder = std::make_shared<Acts::BinFinder<SpacePoint>>(
137  auto topBinFinder = std::make_shared<Acts::BinFinder<SpacePoint>>(
139  auto config = setupSeedfinderConfiguration<SpacePoint>();
140 
142  Acts::Sycl::DeviceExperimentCuts deviceAtlasCuts;
143  config.seedFilter = std::make_unique<Acts::SeedFilter<SpacePoint>>(
145 
146  Acts::Sycl::Seedfinder<SpacePoint> syclSeedfinder(
147  config, deviceAtlasCuts, Acts::Sycl::QueueWrapper(cmdlTool.deviceName));
148  Acts::Seedfinder<SpacePoint> normalSeedfinder(config);
149  auto covarianceTool = [=](const SpacePoint& sp, float /*unused*/,
150  float /*unused*/,
151  float_t /*unused*/) -> Acts::Vector2D {
152  return {sp.varianceR, sp.varianceZ};
153  };
154  std::unique_ptr<Acts::SpacePointGrid<SpacePoint>> grid =
155  Acts::SpacePointGridCreator::createGrid<SpacePoint>(
157 
158  auto spGroup = Acts::BinnedSPGroup<SpacePoint>(
159  spVec.begin(), spVec.end(), covarianceTool, bottomBinFinder, topBinFinder,
160  std::move(grid), config);
161 
162  auto end_prep = std::chrono::system_clock::now();
163 
164  std::chrono::duration<double> elapsec_prep = end_prep - start_prep;
165  double prepTime = elapsec_prep.count();
166 
167  if (!cmdlTool.csvFormat) {
168  std::cout << "read " << spVec.size() << " SP from file "
169  << cmdlTool.inpFileName << std::endl;
170  std::cout << "Preparation time: " << std::to_string(prepTime) << std::endl;
171  }
172 
173  // -------------------------------------- //
174  // ----------- EXECUTE ON CPU ----------- //
175  // -------------------------------------- //
176 
177  auto start_cpu = std::chrono::system_clock::now();
178  uint group_count = 0;
179  std::vector<std::vector<Acts::Seed<SpacePoint>>> seedVector_cpu;
180 
181  if (!cmdlTool.onlyGpu) {
182  for (auto groupIt = spGroup.begin(); !(groupIt == spGroup.end());
183  ++groupIt) {
184  seedVector_cpu.push_back(normalSeedfinder.createSeedsForGroup(
185  groupIt.bottom(), groupIt.middle(), groupIt.top()));
186  group_count++;
187  if (!cmdlTool.allgroup && group_count >= cmdlTool.groups) {
188  break;
189  }
190  }
191  }
192 
193  auto end_cpu = std::chrono::system_clock::now();
194 
195  if (!cmdlTool.csvFormat) {
196  std::cout << "Analyzed " << group_count << " groups for CPU" << std::endl;
197  }
198 
199  // -------------------------------------- //
200  // -------- EXECUTE ON GPU - SYCL ------- //
201  // -------------------------------------- //
202 
203  auto start_sycl = std::chrono::system_clock::now();
204 
205  group_count = 0;
206  std::vector<std::vector<Acts::Seed<SpacePoint>>> seedVector_sycl;
207 
208  for (auto groupIt = spGroup.begin(); !(groupIt == spGroup.end()); ++groupIt) {
209  seedVector_sycl.push_back(syclSeedfinder.createSeedsForGroup(
210  groupIt.bottom(), groupIt.middle(), groupIt.top()));
211  group_count++;
212  if (!cmdlTool.allgroup && group_count >= cmdlTool.groups) {
213  break;
214  }
215  }
216  auto end_sycl = std::chrono::system_clock::now();
217 
218  if (!cmdlTool.csvFormat) {
219  std::cout << "Analyzed " << group_count << " groups for SYCL" << std::endl;
220  }
221 
222  std::chrono::duration<double> elapsec_cpu = end_cpu - start_cpu;
223  double cpuTime = elapsec_cpu.count();
224 
225  std::chrono::duration<double> elapsec_sycl = end_sycl - start_sycl;
226  double syclTime = elapsec_sycl.count();
227 
228  auto textWidth = 20;
229  auto numWidth = 11;
230 
231  int nSeed_cpu = 0;
232  int nSeed_sycl = 0;
233  int nMatch = 0;
234 
235  if (cmdlTool.matches && !cmdlTool.onlyGpu) {
236  for (auto& outVec : seedVector_cpu) {
237  nSeed_cpu += outVec.size();
238  }
239 
240  for (auto& outVec : seedVector_sycl) {
241  nSeed_sycl += outVec.size();
242  }
243 
244  for (size_t i = 0; i < seedVector_cpu.size(); i++) {
245  auto regionVec_cpu = seedVector_cpu[i];
246  auto regionVec_sycl = seedVector_sycl[i];
247 
248  std::vector<std::vector<SpacePoint>> seeds_cpu;
249  std::vector<std::vector<SpacePoint>> seeds_sycl;
250 
251  for (const auto& sd : regionVec_cpu) {
252  std::vector<SpacePoint> seed_cpu;
253  seed_cpu.push_back(*(sd.sp()[0]));
254  seed_cpu.push_back(*(sd.sp()[1]));
255  seed_cpu.push_back(*(sd.sp()[2]));
256  seeds_cpu.push_back(seed_cpu);
257  }
258  for (const auto& sd : regionVec_sycl) {
259  std::vector<SpacePoint> seed_sycl;
260  seed_sycl.push_back(*(sd.sp()[0]));
261  seed_sycl.push_back(*(sd.sp()[1]));
262  seed_sycl.push_back(*(sd.sp()[2]));
263  seeds_sycl.push_back(seed_sycl);
264  }
265 
266  for (auto seed : seeds_cpu) {
267  for (auto other : seeds_sycl) {
268  if (seed[0] == other[0] && seed[1] == other[1] &&
269  seed[2] == other[2]) {
270  nMatch++;
271  break;
272  }
273  }
274  }
275  }
276  }
277 
278  if (!cmdlTool.csvFormat) {
279  std::cout << std::endl;
280  std::cout
281  << "------------------------- Time Metric -------------------------"
282  << std::endl;
283  std::cout << std::setw(textWidth) << " Device:";
284  std::cout << std::setw(numWidth) << "CPU";
285  std::cout << std::setw(numWidth) << "SYCL";
286  std::cout << std::setw(textWidth) << "Speedup/ Agreement" << std::endl;
287  std::cout << std::setw(textWidth) << " Time (s):";
288  std::cout << std::setw(numWidth) << std::to_string(cpuTime);
289  std::cout << std::setw(numWidth) << std::to_string(syclTime);
290  std::cout << std::setw(textWidth) << std::to_string(cpuTime / syclTime);
291  std::cout << std::endl;
292 
293  if (cmdlTool.matches && !cmdlTool.onlyGpu) {
294  std::cout << std::setw(textWidth) << " Seeds found:";
295  std::cout << std::setw(numWidth) << std::to_string(nSeed_cpu);
296  std::cout << std::setw(numWidth) << std::to_string(nSeed_sycl);
297  std::cout << std::setw(textWidth)
298  << std::to_string(float(nMatch) / float(nSeed_cpu) * 100);
299  std::cout << std::endl;
300  }
301 
302  std::cout
303  << "---------------------------------------------------------------"
304  << std::endl;
305  std::cout << std::endl;
306  } else {
307  std::cout << cpuTime << ',' << syclTime << ',' << cpuTime / syclTime << ','
308  << nSeed_cpu << ',' << nSeed_sycl << ',' << nMatch << '\n';
309  }
310 
311  for (const auto* S : spVec) {
312  delete[] S;
313  }
314 
315  return 0;
316 }