CMS 3D CMS Logo

splitVertices.h
Go to the documentation of this file.
1 #ifndef RecoTracker_PixelVertexFinding_plugins_alpaka_splitVertices_h
2 #define RecoTracker_PixelVertexFinding_plugins_alpaka_splitVertices_h
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <cstdint>
7 
8 #include <alpaka/alpaka.hpp>
9 
13 
14 #include "vertexFinder.h"
15 
17 
20  template <typename TAcc>
21  ALPAKA_FN_ACC ALPAKA_FN_INLINE __attribute__((always_inline)) void splitVertices(const TAcc& acc,
23  WsSoAView& ws,
24  float maxChi2) {
25  constexpr bool verbose = false; // in principle the compiler should optmize out if false
26  constexpr uint32_t MAXTK = 512;
27 
28  auto& it = alpaka::declareSharedVar<uint32_t[MAXTK], __COUNTER__>(acc); // track index
29  auto& zz = alpaka::declareSharedVar<float[MAXTK], __COUNTER__>(acc); // z pos
30  auto& newV = alpaka::declareSharedVar<uint8_t[MAXTK], __COUNTER__>(acc); // 0 or 1
31  auto& ww = alpaka::declareSharedVar<float[MAXTK], __COUNTER__>(acc); // z weight
32  auto& nq = alpaka::declareSharedVar<uint32_t, __COUNTER__>(acc); // number of track for this vertex
33 
34  // one vertex per block
35  for (auto kv : cms::alpakatools::independent_groups(acc, data.nvFinal())) {
36  int32_t ndof = data[kv].ndof();
37  if (ndof < 4)
38  continue;
39  if (data[kv].chi2() < maxChi2 * float(ndof))
40  continue;
41 
42  ALPAKA_ASSERT_ACC(ndof < int32_t(MAXTK));
43 
44  if ((uint32_t)ndof >= MAXTK)
45  continue; // too bad FIXME
46 
48  // reset the number of tracks for the current vertex
49  nq = 0u;
50  }
51  alpaka::syncBlockThreads(acc);
52 
53  // cache the data of the tracks associated to the current vertex into shared memory
54  for (auto k : cms::alpakatools::independent_group_elements(acc, ws.ntrks())) {
55  if (ws[k].iv() == int(kv)) {
56  auto index = alpaka::atomicInc(acc, &nq, MAXTK, alpaka::hierarchy::Threads{});
57  it[index] = k;
58  zz[index] = ws[k].zt() - data[kv].zv();
59  newV[index] = zz[index] < 0 ? 0 : 1;
60  ww[index] = 1.f / ws[k].ezt2();
61  }
62  }
63 
64  // the new vertices
65  auto& znew = alpaka::declareSharedVar<float[2], __COUNTER__>(acc);
66  auto& wnew = alpaka::declareSharedVar<float[2], __COUNTER__>(acc);
67  alpaka::syncBlockThreads(acc);
68 
69  ALPAKA_ASSERT_ACC(int(nq) == ndof + 1);
70 
71  int maxiter = 20;
72  // kt-min....
73  bool more = true;
74  while (alpaka::syncBlockThreadsPredicate<alpaka::BlockOr>(acc, more)) {
75  more = false;
77  znew[0] = 0;
78  znew[1] = 0;
79  wnew[0] = 0;
80  wnew[1] = 0;
81  }
82  alpaka::syncBlockThreads(acc);
83 
84  for (auto k : cms::alpakatools::uniform_elements(acc, nq)) {
85  auto i = newV[k];
86  alpaka::atomicAdd(acc, &znew[i], zz[k] * ww[k], alpaka::hierarchy::Threads{});
87  alpaka::atomicAdd(acc, &wnew[i], ww[k], alpaka::hierarchy::Threads{});
88  }
89  alpaka::syncBlockThreads(acc);
90 
92  znew[0] /= wnew[0];
93  znew[1] /= wnew[1];
94  }
95  alpaka::syncBlockThreads(acc);
96 
97  for (auto k : cms::alpakatools::uniform_elements(acc, nq)) {
98  auto d0 = fabs(zz[k] - znew[0]);
99  auto d1 = fabs(zz[k] - znew[1]);
100  auto newer = d0 < d1 ? 0 : 1;
101  more |= newer != newV[k];
102  newV[k] = newer;
103  }
104  --maxiter;
105  if (maxiter <= 0)
106  more = false;
107  }
108 
109  // avoid empty vertices
110  if (0 == wnew[0] || 0 == wnew[1])
111  continue;
112 
113  // quality cut
114  auto dist2 = (znew[0] - znew[1]) * (znew[0] - znew[1]);
115 
116  auto chi2Dist = dist2 / (1.f / wnew[0] + 1.f / wnew[1]);
117 
118  if constexpr (verbose) {
120  printf("inter %d %f %f\n", 20 - maxiter, chi2Dist, dist2 * data[kv].wv());
121  }
122 
123  if (chi2Dist < 4)
124  continue;
125 
126  // get a new global vertex
127  auto& igv = alpaka::declareSharedVar<uint32_t, __COUNTER__>(acc);
129  igv = alpaka::atomicAdd(acc, &ws.nvIntermediate(), 1u, alpaka::hierarchy::Blocks{});
130  alpaka::syncBlockThreads(acc);
131  for (auto k : cms::alpakatools::uniform_elements(acc, nq)) {
132  if (1 == newV[k])
133  ws[it[k]].iv() = igv;
134  }
135 
136  // synchronise the threads before starting the next iteration of the loop over the vertices and resetting the shared memory
137  alpaka::syncBlockThreads(acc);
138  } // loop on vertices
139  }
140 
142  public:
143  template <typename TAcc>
144  ALPAKA_FN_ACC void operator()(const TAcc& acc, VtxSoAView data, WsSoAView ws, float maxChi2) const {
145  splitVertices(acc, data, ws, maxChi2);
146  }
147  };
148 
149 } // namespace ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder
150 
151 #endif // RecoTracker_PixelVertexFinding_plugins_alpaka_splitVertices_h
ALPAKA_FN_ACC constexpr bool once_per_block(TAcc const &acc)
ALPAKA_FN_ACC auto uniform_elements(TAcc const &acc, TArgs... args)
Definition: workdivision.h:311
ALPAKA_FN_ACC auto independent_group_elements(TAcc const &acc, TArgs... args)
ALPAKA_FN_ACC auto independent_groups(TAcc const &acc, TArgs... args)
ZVertexSoAHeterogeneousLayout<>::View ZVertexSoAView
ALPAKA_FN_ACC ALPAKA_FN_INLINE VtxSoAView WsSoAView float maxChi2
Definition: splitVertices.h:24
T1 atomicInc(T1 *a, T2 b)
Definition: cudaCompat.h:48
std::vector< Block > Blocks
Definition: Block.h:99
static const MaxIter maxiter
splitVertices(pdata, pws, maxChi2ForSplit)
PixelVertexWSSoALayout<>::View PixelVertexWorkSpaceSoAView
::vertexFinder::PixelVertexWorkSpaceSoAView WsSoAView
static constexpr float d0
ALPAKA_FN_ACC void operator()(const TAcc &acc, VtxSoAView data, WsSoAView ws, float maxChi2) const
static constexpr float d1
T1 atomicAdd(T1 *a, T2 b)
Definition: cudaCompat.h:61
ALPAKA_FN_ACC ALPAKA_FN_INLINE void __attribute__((always_inline)) clusterTracksByDensity(const TAcc &acc