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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* HybridSafetyEstimator.h
*
* Created on: 22.11.2015
* Author: sandro.wenzel@cern.ch
*
* (based on prototype implementation by Yang Zhang (Sep 2015)
*/
#ifndef NAVIGATION_HYBRIDSAFETYESTIMATOR_H_
#define NAVIGATION_HYBRIDSAFETYESTIMATOR_H_
#include "VecGeom/navigation/VSafetyEstimator.h"
#include "VecGeom/management/HybridManager2.h"
namespace vecgeom {
inline namespace VECGEOM_IMPL_NAMESPACE {
//! a safety estimator using a (vectorized) search through bounding boxes to exclude certain daughter volumes
//! to talk to
class HybridSafetyEstimator : public VSafetyEstimatorHelper<HybridSafetyEstimator> {
private:
// we keep a reference to the ABBoxManager ( avoids calling Instance() on this guy all the time )
HybridManager2 &fAccelerationStructureManager;
HybridSafetyEstimator()
: VSafetyEstimatorHelper<HybridSafetyEstimator>(), fAccelerationStructureManager(HybridManager2::Instance())
{
}
// convert index to physical daugher
VPlacedVolume const *LookupDaughter(LogicalVolume const *lvol, int id) const
{
assert(id >= 0 && "access with negative index");
assert(size_t(id) < lvol->GetDaughtersp()->size() && "access beyond size of daughterlist ");
return lvol->GetDaughtersp()->operator[](id);
}
public:
// a simple sort class (based on insertionsort)
template <typename T> //, typename Cmp>
static void insertionsort(T *arr, unsigned int N)
{
for (unsigned short i = 1; i < N; ++i) {
T value = arr[i];
short hole = i;
for (; hole > 0 && value.second < arr[hole - 1].second; --hole)
arr[hole] = arr[hole - 1];
arr[hole] = value;
}
}
// helper structure to find the candidate set for safety calculations
size_t GetSafetyCandidates_v(HybridManager2::HybridBoxAccelerationStructure const &accstructure,
Vector3D<Precision> const &point, HybridManager2::BoxIdDistancePair_t *boxsafetypairs,
Precision upper_squared_limit) const
{
size_t count = 0;
Vector3D<float> pointfloat((float)point.x(), (float)point.y(), (float)point.z());
int halfvectorsize, numberOfNodes;
auto boxes_v = fAccelerationStructureManager.GetABBoxes_v(accstructure, halfvectorsize, numberOfNodes);
auto const *nodeToDaughters = accstructure.fNodeToDaughters;
constexpr auto kVS = vecCore::VectorSize<HybridManager2::Float_v>();
for (int index = 0, nodeindex = 0; index < halfvectorsize * 2; index += 2 * (kVS + 1), nodeindex += kVS) {
HybridManager2::Float_v safetytoboxsqr =
ABBoxImplementation::ABBoxSafetySqr(boxes_v[index], boxes_v[index + 1], pointfloat);
auto closer = safetytoboxsqr < HybridManager2::Float_v(upper_squared_limit);
if (!vecCore::MaskEmpty(closer)) {
for (size_t i = 0 /*closer.firstOne()*/; i < kVS; ++i) {
if (vecCore::MaskLaneAt(closer, i)) {
safetytoboxsqr =
ABBoxImplementation::ABBoxSafetySqr(boxes_v[index + 2 * i + 2], boxes_v[index + 2 * i + 3], pointfloat);
auto closer1 = safetytoboxsqr < HybridManager2::Float_v(upper_squared_limit);
if (!vecCore::MaskEmpty(closer1)) {
for (size_t j = 0 /*closer.firstOne()*/; j < kVS; ++j) { // leaf node
if (vecCore::MaskLaneAt(closer1, j)) {
assert(count < VECGEOM_MAXFACETS);
boxsafetypairs[count] = HybridManager2::BoxIdDistancePair_t(nodeToDaughters[nodeindex + i][j],
vecCore::LaneAt(safetytoboxsqr, j));
count++;
}
}
}
}
}
}
}
return count;
}
// Improved safety estimator that can dynamically reduce the upper search limit.
// Internally calls ABBoxSafetyRangeSqr that besides validating a candidate
// if closer than the limit, it also gives the upper limit for the range where
// the candidate can be surely found. This allows to update the search limit after
// each check, giving much less final candidates.
//
// current checked
// box _________
// point | | upper_limit
// x---------|candidate|----------|
// |_________|
// |-------------------|
// updated upper_limit
size_t GetSafetyCandidates2_v(HybridManager2::HybridBoxAccelerationStructure const &accstructure,
Vector3D<Precision> const &point, HybridManager2::BoxIdDistancePair_t *hitlist,
Precision upper_squared_limit) const
{
using Float_v = vecgeom::VectorBackend::Float_v;
size_t count = 0;
int numberOfNodes, size;
auto boxes_v = fAccelerationStructureManager.GetABBoxes_v(accstructure, size, numberOfNodes);
constexpr auto kVS = vecCore::VectorSize<HybridManager2::Float_v>();
auto const *nodeToDaughters = accstructure.fNodeToDaughters;
Vector3D<float> pointfloat((float)point.x(), (float)point.y(), (float)point.z());
// Loop internal nodes (internal node = kVS clusters)
for (size_t index = 0, nodeindex = 0; index < size_t(size) * 2; index += 2 * (kVS + 1), nodeindex += kVS) {
// index = start index for internal node
// nodeindex = cluster index
Float_v distmaxsqr; // Maximum distance that may still touch the box
Float_v safetytonodesqr =
ABBoxImplementation::ABBoxSafetyRangeSqr(boxes_v[index], boxes_v[index + 1], pointfloat, distmaxsqr);
// Find minimum of distmaxsqr
for (size_t i = 0; i < kVS; ++i) {
Precision distmaxsqr_s = vecCore::LaneAt(distmaxsqr, i);
if (distmaxsqr_s < upper_squared_limit) upper_squared_limit = distmaxsqr_s;
}
auto hit = safetytonodesqr < ABBoxManager::Real_t(upper_squared_limit);
if (!vecCore::MaskEmpty(hit)) {
for (size_t i = 0; i < kVS; ++i) {
if (vecCore::MaskLaneAt(hit, i)) {
Float_v safetytoboxsqr = ABBoxImplementation::ABBoxSafetyRangeSqr(
boxes_v[index + 2 * (i + 1)], boxes_v[index + 2 * (i + 1) + 1], pointfloat, distmaxsqr);
auto hit1 = safetytoboxsqr < ABBoxManager::Real_t(upper_squared_limit);
if (!vecCore::MaskEmpty(hit1)) {
// loop bounding boxes in the cluster
for (size_t j = 0; j < kVS; ++j) {
if (vecCore::MaskLaneAt(hit1, j)) {
assert(count < VECGEOM_MAXFACETS);
hitlist[count] = HybridManager2::BoxIdDistancePair_t(nodeToDaughters[nodeindex + i][j],
vecCore::LaneAt(safetytoboxsqr, j));
Precision distmaxsqr_s = vecCore::LaneAt(distmaxsqr, j);
// Reduce the upper limit
if (distmaxsqr_s < upper_squared_limit) upper_squared_limit = distmaxsqr_s;
count++;
}
}
}
}
}
}
}
return count;
}
template <typename AccStructure, typename Func>
VECGEOM_FORCE_INLINE
void BVHSortedSafetyLooper(AccStructure const &accstructure, Vector3D<Precision> const &localpoint, Func &&userhook,
Precision upper_squared_limit) const
{
// The following construct reserves stackspace for objects
// of type IdDistPair_t WITHOUT initializing those objects
using IdDistPair_t = HybridManager2::BoxIdDistancePair_t;
char stackspace[VECGEOM_MAXFACETS * sizeof(IdDistPair_t)];
IdDistPair_t *hitlist = reinterpret_cast<IdDistPair_t *>(&stackspace);
// Get candidates using HybridSafetyEstimator
// HybridSafetyEstimator *hse = static_cast<HybridSafetyEstimator*>(HybridSafetyEstimator::Instance());
// auto ncandidates = hse->GetSafetyCandidates_v(accstructure, localpoint, hitlist, upper_squared_limit);
// Get candidates in vectorized mode
auto ncandidates = GetSafetyCandidates2_v(accstructure, localpoint, hitlist, upper_squared_limit);
// sort candidates according to their bounding volume safety distance
insertionsort(hitlist, ncandidates);
for (size_t index = 0; index < ncandidates; ++index) {
auto hitbox = hitlist[index];
// here we got the hit candidates
// now we execute user specific code to process this "hitbox"
auto done = userhook(hitbox);
if (done) break;
}
}
static constexpr const char *gClassNameString = "HybridSafetyEstimator";
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
virtual Real_v ComputeSafetyForLocalPoint(Vector3D<Real_v> const &localpoint, VPlacedVolume const *pvol,
Bool_v m) const override
{
using vecCore::AssignLane;
using vecCore::LaneAt;
Real_v safety(0.);
if (!vecCore::MaskEmpty(m)) {
// SIMD safety to mother
safety = pvol->SafetyToOut(localpoint);
LogicalVolume const *lvol = pvol->GetLogicalVolume();
// now loop over the voxelized treatment of safety to in
for (unsigned int i = 0; i < vecCore::VectorSize<Real_v>(); ++i) {
if (vecCore::MaskLaneAt(m, i)) {
AssignLane(safety, i,
TreatSafetyToIn(Vector3D<Precision>(LaneAt(localpoint.x(), i), LaneAt(localpoint.y(), i),
LaneAt(localpoint.z(), i)),
lvol, LaneAt(safety, i)));
} else {
AssignLane(safety, i, 0.);
}
}
}
return safety;
}
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
Precision TreatSafetyToIn(Vector3D<Precision> const &localpoint, LogicalVolume const *lvol, Precision outsafety) const
{
// a stack based workspace array
// The following construct reserves stackspace for objects
// of type IdDistPair_t WITHOUT initializing those objects
using IdDistPair_t = HybridManager2::BoxIdDistancePair_t;
char unitstackspace[VECGEOM_MAXDAUGHTERS * sizeof(IdDistPair_t)];
IdDistPair_t *boxsafetylist = reinterpret_cast<IdDistPair_t *>(&unitstackspace);
double safety = outsafety; // we use the outsafety estimate as starting point
double safetysqr = safety * safety;
// safety to bounding boxes
if (safety > 0. && lvol->GetDaughtersp()->size() > 0) {
// calculate squared bounding box safeties in vectorized way
auto ncandidates = GetSafetyCandidates_v(*fAccelerationStructureManager.GetAccStructure(lvol), localpoint,
boxsafetylist, safetysqr);
// not sorting the candidate list ( which one could do )
for (unsigned int candidate = 0; candidate < ncandidates; ++candidate) {
auto boxsafetypair = boxsafetylist[candidate];
if (boxsafetypair.second < safetysqr) {
VPlacedVolume const *cand = LookupDaughter(lvol, boxsafetypair.first);
if (size_t(boxsafetypair.first) > lvol->GetDaughtersp()->size()) break;
auto candidatesafety = cand->SafetyToIn(localpoint);
#ifdef VERBOSE
if (candidatesafety * candidatesafety > boxsafetypair.second && boxsafetypair.second > 0)
std::cerr << "real safety smaller than boxsafety \n";
#endif
if (candidatesafety < safety) {
safety = candidatesafety;
safetysqr = safety * safety;
}
}
}
}
return safety;
}
// this is (almost) the same code as in SimpleABBoxSafetyEstimator --> avoid this
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
virtual Precision ComputeSafetyForLocalPoint(Vector3D<Precision> const &localpoint,
VPlacedVolume const *pvol) const override
{
// safety to mother
double safety = pvol->SafetyToOut(localpoint);
if (safety <= 0.) {
return 0.;
}
return TreatSafetyToIn(localpoint, pvol->GetLogicalVolume(), safety);
}
VECGEOM_FORCE_INLINE
VECCORE_ATT_HOST_DEVICE
virtual Precision ComputeSafetyToDaughtersForLocalPoint(Vector3D<Precision> const &localpoint,
LogicalVolume const *lvol) const override
{
return TreatSafetyToIn(localpoint, lvol, kInfLength);
}
// vector interface
VECGEOM_FORCE_INLINE
virtual void ComputeSafetyForLocalPoints(SOA3D<Precision> const & /*localpoints*/, VPlacedVolume const * /*pvol*/,
Precision * /*safeties*/) const override
{
// // a stack based workspace array
// static __thread ABBoxManager::BoxIdDistancePair_t boxsafetylist[VECGEOM_MAXDAUGHTERS] = {};
//
// // safety to mother -- using vector interface
// pvol->SafetyToOut(localpoints, safeties);
//
// // safety to bounding boxes
// LogicalVolume const *lvol = pvol->GetLogicalVolume();
// if (!(lvol->GetDaughtersp()->size() > 0))
// return;
//
// // get bounding boxes (they are the same for all tracks)
// int numberofboxes;
// auto bboxes = fABBoxManager.GetABBoxes_v(lvol, numberofboxes);
//
// // now loop over particles
// for (int i = 0, ntracks = localpoints.size(); i < ntracks; ++i) {
// double safety = safeties[i];
// if (safeties[i] > 0.) {
// double safetysqr = safeties[i] * safeties[i];
// auto lpoint = localpoints[i];
// // vectorized search through bounding boxes -- quickly excluding many candidates
// auto ncandidates = GetSafetyCandidates_v(lpoint, bboxes, numberofboxes, boxsafetylist, safetysqr);
// // loop over remaining candidates
// for (unsigned int candidate = 0; candidate < ncandidates; ++candidate) {
// auto boxsafetypair = boxsafetylist[candidate];
// if (boxsafetypair.second < safetysqr) {
// VPlacedVolume const *candidate = LookupDaughter(lvol, boxsafetypair.first);
// if (boxsafetypair.first > lvol->GetDaughtersp()->size())
// break;
// auto candidatesafety = candidate->SafetyToIn(lpoint);
//#ifdef VERBOSE
// if (candidatesafety * candidatesafety > boxsafetypair.second && boxsafetypair.second > 0)
// std::cerr << "real safety smaller than boxsafety \n";
//#endif
// if (candidatesafety < safety) {
// safety = candidatesafety;
// safetysqr = safety * safety;
// }
// }
// }
// }
// // write back result
// safeties[i] = safety;
// }
}
static VSafetyEstimator *Instance()
{
static HybridSafetyEstimator instance;
return &instance;
}
}; // end class
} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom
#endif /* NAVIGATION_SIMPLEABBOXSAFETYESTIMATOR_H_ */
|