CMS 3D CMS Logo

splitVertices.h
Go to the documentation of this file.
1 #ifndef RecoPixelVertexing_PixelVertexFinding_splitVertices_h
2 #define RecoPixelVertexing_PixelVertexFinding_splitVertices_h
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <cstdint>
7 #include <alpaka/alpaka.hpp>
10 
11 #include "vertexFinder.h"
12 
14  namespace vertexFinder {
17  template <typename TAcc>
18  ALPAKA_FN_ACC ALPAKA_FN_INLINE __attribute__((always_inline)) void splitVertices(const TAcc& acc,
20  WsSoAView& pws,
21  float maxChi2) {
22  constexpr bool verbose = false; // in principle the compiler should optmize out if false
23  const uint32_t threadIdxLocal(alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc)[0u]);
24 
25  auto& __restrict__ data = pdata;
26  auto& __restrict__ ws = pws;
27  auto nt = ws.ntrks();
28  float const* __restrict__ zt = ws.zt();
29  float const* __restrict__ ezt2 = ws.ezt2();
30  float* __restrict__ zv = data.zv();
31  float* __restrict__ wv = data.wv();
32  float const* __restrict__ chi2 = data.chi2();
33  uint32_t& nvFinal = data.nvFinal();
34 
35  int32_t const* __restrict__ nn = data.ndof();
36  int32_t* __restrict__ iv = ws.iv();
37 
42 
43  constexpr uint32_t MAXTK = 512;
44 
45  auto& it = alpaka::declareSharedVar<uint32_t[MAXTK], __COUNTER__>(acc); // track index
46  auto& zz = alpaka::declareSharedVar<float[MAXTK], __COUNTER__>(acc); // z pos
47  auto& newV = alpaka::declareSharedVar<uint8_t[MAXTK], __COUNTER__>(acc); // 0 or 1
48  auto& ww = alpaka::declareSharedVar<float[MAXTK], __COUNTER__>(acc); // z weight
49  auto& nq = alpaka::declareSharedVar<uint32_t, __COUNTER__>(acc); // number of track for this vertex
50 
51  const uint32_t blockIdx(alpaka::getIdx<alpaka::Grid, alpaka::Blocks>(acc)[0u]);
52  const uint32_t gridDimension(alpaka::getWorkDiv<alpaka::Grid, alpaka::Blocks>(acc)[0u]);
53 
54  // one vertex per block
55  for (auto kv = blockIdx; kv < nvFinal; kv += gridDimension) {
56  if (nn[kv] < 4)
57  continue;
58  if (chi2[kv] < maxChi2 * float(nn[kv]))
59  continue;
60 
61  ALPAKA_ASSERT_OFFLOAD(nn[kv] < int32_t(MAXTK));
62 
63  if ((uint32_t)nn[kv] >= MAXTK)
64  continue; // too bad FIXME
65 
66  nq = 0u;
67  alpaka::syncBlockThreads(acc);
68 
69  // copy to local
71  if (iv[k] == int(kv)) {
72  auto old = alpaka::atomicInc(acc, &nq, MAXTK, alpaka::hierarchy::Threads{});
73  zz[old] = zt[k] - zv[kv];
74  newV[old] = zz[old] < 0 ? 0 : 1;
75  ww[old] = 1.f / ezt2[k];
76  it[old] = k;
77  }
78  }
79 
80  // the new vertices
81  auto& znew = alpaka::declareSharedVar<float[2], __COUNTER__>(acc);
82  auto& wnew = alpaka::declareSharedVar<float[2], __COUNTER__>(acc);
83  alpaka::syncBlockThreads(acc);
84 
85  ALPAKA_ASSERT_OFFLOAD(int(nq) == nn[kv] + 1);
86 
87  int maxiter = 20;
88  // kt-min....
89  bool more = true;
90  while (alpaka::syncBlockThreadsPredicate<alpaka::BlockOr>(acc, more)) {
91  more = false;
92  if (0 == threadIdxLocal) {
93  znew[0] = 0;
94  znew[1] = 0;
95  wnew[0] = 0;
96  wnew[1] = 0;
97  }
98  alpaka::syncBlockThreads(acc);
99 
100  for (auto k : cms::alpakatools::elements_with_stride(acc, nq)) {
101  auto i = newV[k];
102  alpaka::atomicAdd(acc, &znew[i], zz[k] * ww[k], alpaka::hierarchy::Threads{});
103  alpaka::atomicAdd(acc, &wnew[i], ww[k], alpaka::hierarchy::Threads{});
104  }
105  alpaka::syncBlockThreads(acc);
106 
107  if (0 == threadIdxLocal) {
108  znew[0] /= wnew[0];
109  znew[1] /= wnew[1];
110  }
111  alpaka::syncBlockThreads(acc);
112 
113  for (auto k : cms::alpakatools::elements_with_stride(acc, nq)) {
114  auto d0 = fabs(zz[k] - znew[0]);
115  auto d1 = fabs(zz[k] - znew[1]);
116  auto newer = d0 < d1 ? 0 : 1;
117  more |= newer != newV[k];
118  newV[k] = newer;
119  }
120  --maxiter;
121  if (maxiter <= 0)
122  more = false;
123  }
124 
125  // avoid empty vertices
126  if (0 == wnew[0] || 0 == wnew[1])
127  continue;
128 
129  // quality cut
130  auto dist2 = (znew[0] - znew[1]) * (znew[0] - znew[1]);
131 
132  auto chi2Dist = dist2 / (1.f / wnew[0] + 1.f / wnew[1]);
133 
134  if (verbose && 0 == threadIdxLocal)
135  printf("inter %d %f %f\n", 20 - maxiter, chi2Dist, dist2 * wv[kv]);
136 
137  if (chi2Dist < 4)
138  continue;
139 
140  // get a new global vertex
141  auto& igv = alpaka::declareSharedVar<uint32_t, __COUNTER__>(acc);
142  if (0 == threadIdxLocal)
143  igv = alpaka::atomicAdd(acc, &ws.nvIntermediate(), 1u, alpaka::hierarchy::Blocks{});
144  alpaka::syncBlockThreads(acc);
145  for (auto k : cms::alpakatools::elements_with_stride(acc, nq)) {
146  if (1 == newV[k])
147  iv[it[k]] = igv;
148  }
149 
150  } // loop on vertices
151  }
152 
154  public:
155  template <typename TAcc>
156  ALPAKA_FN_ACC void operator()(const TAcc& acc, VtxSoAView pdata, WsSoAView pws, float maxChi2) const {
157  splitVertices(acc, pdata, pws, maxChi2);
158  }
159  };
160  } // namespace vertexFinder
161 } // namespace ALPAKA_ACCELERATOR_NAMESPACE
162 #endif // RecoPixelVertexing_PixelVertexFinding_plugins_splitVertices.h
const uint32_t gridDimension(alpaka::getWorkDiv< alpaka::Grid, alpaka::Blocks >(acc)[0u])
const uint32_t threadIdxLocal(alpaka::getIdx< alpaka::Block, alpaka::Threads >(acc)[0u])
ALPAKA_FN_ACC ALPAKA_FN_INLINE void VtxSoAView & pdata
ALPAKA_FN_ACC void operator()(const TAcc &acc, VtxSoAView pdata, WsSoAView pws, float maxChi2) const
ZVertexSoAHeterogeneousLayout<>::View ZVertexSoAView
ALPAKA_FN_ACC ALPAKA_FN_INLINE VtxSoAView WsSoAView float maxChi2
Definition: splitVertices.h:21
T1 atomicInc(T1 *a, T2 b)
Definition: cudaCompat.h:48
ALPAKA_FN_ACC ALPAKA_FN_INLINE void VtxSoAView WsSoAView & pws
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
const uint32_t blockIdx(alpaka::getIdx< alpaka::Grid, alpaka::Blocks >(acc)[0u])
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