-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathBrokenLineFitOnGPU.h
185 lines (155 loc) · 5.95 KB
/
BrokenLineFitOnGPU.h
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
//
// Author: Felice Pantaleo, CERN
//
// #define BROKENLINE_DEBUG
#include <cstdint>
#include <cuda_runtime.h>
#include "CUDADataFormats/TrackingRecHit2DCUDA.h"
#include "CUDACore/cudaCheck.h"
#include "CUDACore/cuda_assert.h"
#include "CondFormats/pixelCPEforGPU.h"
#include "BrokenLine.h"
#include "HelixFitOnGPU.h"
using HitsOnGPU = TrackingRecHit2DSOAView;
using Tuples = pixelTrack::HitContainer;
using OutputSoA = pixelTrack::TrackSoA;
// #define BL_DUMP_HITS
template <int N>
__global__ void kernelBLFastFit(Tuples const *__restrict__ foundNtuplets,
CAConstants::TupleMultiplicity const *__restrict__ tupleMultiplicity,
HitsOnGPU const *__restrict__ hhp,
double *__restrict__ phits,
float *__restrict__ phits_ge,
double *__restrict__ pfast_fit,
uint32_t nHits,
uint32_t offset) {
constexpr uint32_t hitsInFit = N;
assert(hitsInFit <= nHits);
assert(hhp);
assert(pfast_fit);
assert(foundNtuplets);
assert(tupleMultiplicity);
// look in bin for this hit multiplicity
auto local_start = blockIdx.x * blockDim.x + threadIdx.x;
#ifdef BROKENLINE_DEBUG
if (0 == local_start) {
printf("%d total Ntuple\n", foundNtuplets->nbins());
printf("%d Ntuple of size %d for %d hits to fit\n", tupleMultiplicity->size(nHits), nHits, hitsInFit);
}
#endif
for (int local_idx = local_start, nt = Rfit::maxNumberOfConcurrentFits(); local_idx < nt;
local_idx += gridDim.x * blockDim.x) {
auto tuple_idx = local_idx + offset;
if (tuple_idx >= tupleMultiplicity->size(nHits))
break;
// get it from the ntuple container (one to one to helix)
auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
assert(tkid < foundNtuplets->nbins());
assert(foundNtuplets->size(tkid) == nHits);
Rfit::Map3xNd<N> hits(phits + local_idx);
Rfit::Map4d fast_fit(pfast_fit + local_idx);
Rfit::Map6xNf<N> hits_ge(phits_ge + local_idx);
#ifdef BL_DUMP_HITS
__shared__ int done;
done = 0;
__syncthreads();
bool dump = (foundNtuplets->size(tkid) == 5 && 0 == atomicAdd(&done, 1));
#endif
// Prepare data structure
auto const *hitId = foundNtuplets->begin(tkid);
for (unsigned int i = 0; i < hitsInFit; ++i) {
auto hit = hitId[i];
float ge[6];
hhp->cpeParams()
.detParams(hhp->detectorIndex(hit))
.frame.toGlobal(hhp->xerrLocal(hit), 0, hhp->yerrLocal(hit), ge);
#ifdef BL_DUMP_HITS
if (dump) {
printf("Hit global: %d: %d hits.col(%d) << %f,%f,%f\n",
tkid,
hhp->detectorIndex(hit),
i,
hhp->xGlobal(hit),
hhp->yGlobal(hit),
hhp->zGlobal(hit));
printf("Error: %d: %d hits_ge.col(%d) << %e,%e,%e,%e,%e,%e\n",
tkid,
hhp->detetectorIndex(hit),
i,
ge[0],
ge[1],
ge[2],
ge[3],
ge[4],
ge[5]);
}
#endif
hits.col(i) << hhp->xGlobal(hit), hhp->yGlobal(hit), hhp->zGlobal(hit);
hits_ge.col(i) << ge[0], ge[1], ge[2], ge[3], ge[4], ge[5];
}
BrokenLine::BL_Fast_fit(hits, fast_fit);
// no NaN here....
assert(fast_fit(0) == fast_fit(0));
assert(fast_fit(1) == fast_fit(1));
assert(fast_fit(2) == fast_fit(2));
assert(fast_fit(3) == fast_fit(3));
}
}
template <int N>
__global__ void kernelBLFit(CAConstants::TupleMultiplicity const *__restrict__ tupleMultiplicity,
double B,
OutputSoA *results,
double *__restrict__ phits,
float *__restrict__ phits_ge,
double *__restrict__ pfast_fit,
uint32_t nHits,
uint32_t offset) {
assert(N <= nHits);
assert(results);
assert(pfast_fit);
// same as above...
// look in bin for this hit multiplicity
auto local_start = blockIdx.x * blockDim.x + threadIdx.x;
for (int local_idx = local_start, nt = Rfit::maxNumberOfConcurrentFits(); local_idx < nt;
local_idx += gridDim.x * blockDim.x) {
auto tuple_idx = local_idx + offset;
if (tuple_idx >= tupleMultiplicity->size(nHits))
break;
// get it for the ntuple container (one to one to helix)
auto tkid = *(tupleMultiplicity->begin(nHits) + tuple_idx);
Rfit::Map3xNd<N> hits(phits + local_idx);
Rfit::Map4d fast_fit(pfast_fit + local_idx);
Rfit::Map6xNf<N> hits_ge(phits_ge + local_idx);
BrokenLine::PreparedBrokenLineData<N> data;
Rfit::Matrix3d Jacob;
BrokenLine::karimaki_circle_fit circle;
Rfit::line_fit line;
BrokenLine::prepareBrokenLineData(hits, fast_fit, B, data);
BrokenLine::BL_Line_fit(hits_ge, fast_fit, B, data, line);
BrokenLine::BL_Circle_fit(hits, hits_ge, fast_fit, B, data, circle);
results->stateAtBS.copyFromCircle(circle.par, circle.cov, line.par, line.cov, 1.f / float(B), tkid);
results->pt(tkid) = float(B) / float(std::abs(circle.par(2)));
results->eta(tkid) = asinhf(line.par(0));
results->chi2(tkid) = (circle.chi2 + line.chi2) / (2 * N - 5);
#ifdef BROKENLINE_DEBUG
if (!(circle.chi2 >= 0) || !(line.chi2 >= 0))
printf("kernelBLFit failed! %f/%f\n", circle.chi2, line.chi2);
printf("kernelBLFit size %d for %d hits circle.par(0,1,2): %d %f,%f,%f\n",
N,
nHits,
tkid,
circle.par(0),
circle.par(1),
circle.par(2));
printf("kernelBLHits line.par(0,1): %d %f,%f\n", tkid, line.par(0), line.par(1));
printf("kernelBLHits chi2 cov %f/%f %e,%e,%e,%e,%e\n",
circle.chi2,
line.chi2,
circle.cov(0, 0),
circle.cov(1, 1),
circle.cov(2, 2),
line.cov(0, 0),
line.cov(1, 1));
#endif
}
}