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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
// This file is part of VecGeom and is distributed under the
// conditions in the file LICENSE.txt in the top directory.
// For the full list of authors see CONTRIBUTORS.txt and `git log`.
/// \brief A collection of template helpers to automize implementation
/// of UnplacedVolume interfaces.
/// \file volumes/UnplacedVolumeImplHelper.h
/// \author First version created by Sandro Wenzel
#ifndef VOLUMES_UNPLACEDVOLUMEIMPLHELPER_H_
#define VOLUMES_UNPLACEDVOLUMEIMPLHELPER_H_
#include "VecGeom/base/Global.h"
#include "VecGeom/base/SOA3D.h"
#include "VecGeom/volumes/UnplacedVolume.h"
#include "VecGeom/management/VolumeFactory.h"
#ifndef __clang__
#pragma GCC diagnostic push
// We ignore warnings of this type in this file.
// The warning occurred due to potential overflow of memory address locations output[i]
// where i is an unsigned long long in a loop. It can be safely ignored since such
// memory locations do in fact not exist (~multiple petabyte in memory).
#pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
#endif
namespace vecgeom {
VECGEOM_DEVICE_DECLARE_CONV_TEMPLATE_2t(class, CommonUnplacedVolumeImplHelper, typename, typename);
inline namespace VECGEOM_IMPL_NAMESPACE {
// kernels
template <class Implementation, typename Real_v>
VECGEOM_FORCE_INLINE
static void DistanceToOutLoop(typename Implementation::UnplacedStruct_t const *shapestruct, const size_t offset,
const size_t size, SOA3D<Precision> const &points, SOA3D<Precision> const &directions,
Precision const *step_max, Precision *output)
{
using vecCore::FromPtr;
for (decltype(points.size()) i(offset); i < size; i += vecCore::VectorSize<Real_v>()) {
Vector3D<Real_v> point(FromPtr<Real_v>(points.x() + i), FromPtr<Real_v>(points.y() + i),
FromPtr<Real_v>(points.z() + i));
Vector3D<Real_v> dir(FromPtr<Real_v>(directions.x() + i), FromPtr<Real_v>(directions.y() + i),
FromPtr<Real_v>(directions.z() + i));
Real_v stepMax_v = FromPtr<Real_v>(&step_max[i]);
Real_v result;
Implementation::template DistanceToOut<Real_v>(*shapestruct, point, dir, stepMax_v, result);
vecCore::Store(result, &output[i]);
}
}
template <class Implementation, typename Real_v>
VECGEOM_FORCE_INLINE
static void SafetyToOutLoop(typename Implementation::UnplacedStruct_t const *shapestruct, const size_t offset,
const size_t size, SOA3D<Precision> const &points, Precision *output)
{
using vecCore::FromPtr;
const decltype(points.size()) len(size);
for (decltype(points.size()) i(offset); i < len; i += vecCore::VectorSize<Real_v>()) {
Vector3D<Real_v> point(FromPtr<Real_v>(points.x() + i), FromPtr<Real_v>(points.y() + i),
FromPtr<Real_v>(points.z() + i));
Real_v result(kInfLength);
Implementation::template SafetyToOut<Real_v>(*shapestruct, point, result);
vecCore::Store(result, &output[i]);
}
}
/*!
* A template class with the aim to automatically implement
* interfaces of UnplacedVolume by connecting them to the separately
* available (reusable) kernels.
*
* This is an application of the CRT pattern in order to reduce
* repeating code.
*/
template <class Implementation, class BaseUnplVol = VUnplacedVolume>
class CommonUnplacedVolumeImplHelper : public BaseUnplVol {
public:
using UnplacedStruct_t = typename Implementation::UnplacedStruct_t;
using UnplacedVolume_t = typename Implementation::UnplacedVolume_t;
// bring in constructors
using BaseUnplVol::BaseUnplVol;
// bring in some members from base (to avoid name hiding)
using BaseUnplVol::DistanceToIn;
using BaseUnplVol::DistanceToOut;
using BaseUnplVol::SafetyToIn;
using BaseUnplVol::SafetyToOut;
VECCORE_ATT_HOST_DEVICE
VECGEOM_FORCE_INLINE
virtual Precision DistanceToOut(Vector3D<Precision> const &p, Vector3D<Precision> const &d,
Precision step_max = kInfLength) const override
{
#ifndef VECCORE_CUDA
assert(d.IsNormalized() && " direction not normalized in call to DistanceToOut ");
#endif
Precision output = kInfLength;
Implementation::template DistanceToOut(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, d, step_max,
output);
//#ifdef VECGEOM_DISTANCE_DEBUG
// DistanceComparator::CompareDistanceToOut( this, output, point, direction, stepMax );
//#endif
// detect -inf responses which are often an indication for a real bug
#ifndef VECCORE_CUDA
assert(!((output < 0.) && std::isinf((Precision)output)));
#endif
return output;
}
// the extended DistanceToOut interface
VECCORE_ATT_HOST_DEVICE
VECGEOM_FORCE_INLINE
virtual Precision DistanceToOut(Vector3D<Precision> const &p, Vector3D<Precision> const &d,
Vector3D<Precision> &normal, bool &convex,
Precision step_max = kInfLength) const override
{
(void)p;
(void)d;
(void)normal;
(void)convex;
(void)step_max;
assert(false);
return 0.;
}
VECCORE_ATT_HOST_DEVICE
VECGEOM_FORCE_INLINE
virtual bool Contains(Vector3D<Precision> const &p) const override
{
bool output(false);
Implementation::Contains(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return output;
}
VECCORE_ATT_HOST_DEVICE
VECGEOM_FORCE_INLINE
virtual EnumInside Inside(Vector3D<Precision> const &p) const override
{
Inside_t output(0);
Implementation::Inside(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return (EnumInside)output;
}
VECCORE_ATT_HOST_DEVICE
virtual Precision DistanceToIn(Vector3D<Precision> const &p, Vector3D<Precision> const &d,
const Precision step_max = kInfLength) const override
{
#ifndef VECCORE_CUDA
assert(d.IsNormalized() && " direction not normalized in call to DistanceToOut ");
#endif
Precision output(kInfLength);
Implementation::DistanceToIn(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, d, step_max, output);
return output;
}
VECCORE_ATT_HOST_DEVICE
virtual Precision SafetyToOut(Vector3D<Precision> const &p) const override
{
Precision output(kInfLength);
Implementation::SafetyToOut(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return output;
}
VECCORE_ATT_HOST_DEVICE
virtual Precision SafetyToIn(Vector3D<Precision> const &p) const override
{
Precision output(kInfLength);
Implementation::SafetyToIn(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return output;
}
virtual int MemorySize() const override { return sizeof(*this); }
};
/*! \brief Template implementation helper for the case of unplaced volumes/shapes
* where the vector interface is implemented in terms of SIMD vector instructions.
*/
template <class Implementation, class BaseUnplVol = VUnplacedVolume>
class SIMDUnplacedVolumeImplHelper : public CommonUnplacedVolumeImplHelper<Implementation, BaseUnplVol> {
public:
using Real_v = vecgeom::VectorBackend::Real_v;
using UnplacedVolume_t = typename Implementation::UnplacedVolume_t;
using Common_t = CommonUnplacedVolumeImplHelper<Implementation, BaseUnplVol>;
static constexpr bool SIMDHELPER = true; // property expressing that this helper provides true external SIMD support
// bring in constructor
using Common_t::Common_t;
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v DistanceToOutVec(Vector3D<Real_v> const &p, Vector3D<Real_v> const &d,
Real_v const &step_max) const override
{
Real_v output;
Implementation::template DistanceToOut<Real_v>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, d,
step_max, output);
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v DistanceToInVec(Vector3D<Real_v> const &p, Vector3D<Real_v> const &d,
Real_v const &step_max) const override
{
Real_v output(kInfLength);
Implementation::template DistanceToIn<Real_v>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, d,
step_max, output);
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v SafetyToOutVec(Vector3D<Real_v> const &p) const override
{
Real_v output(kInfLength);
Implementation::template SafetyToOut<Real_v>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v SafetyToInVec(Vector3D<Real_v> const &p) const override
{
Real_v output(kInfLength);
Implementation::template SafetyToIn<Real_v>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), p, output);
return output;
}
using UnplacedStruct_t = typename Implementation::UnplacedStruct_t;
using Common_t::DistanceToOut;
using Common_t::SafetyToOut;
virtual void DistanceToOut(SOA3D<Precision> const &points, SOA3D<Precision> const &directions,
Precision const *const step_max, Precision *const output) const override
{
auto offset = points.size() - points.size() % vecCore::VectorSize<VectorBackend::Real_v>();
auto &shape = ((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct();
// vector loop treatment
DistanceToOutLoop<Implementation, VectorBackend::Real_v>(&shape, 0, offset, points, directions, step_max, output);
// tail treatment
DistanceToOutLoop<Implementation, ScalarBackend::Real_v>(&shape, offset, points.size(), points, directions,
step_max, output);
}
virtual void SafetyToOut(SOA3D<Precision> const &points, Precision *const output) const override
{
auto offset = points.size() - points.size() % vecCore::VectorSize<VectorBackend::Real_v>();
auto &shape = ((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct();
// vector loop treatment
SafetyToOutLoop<Implementation, VectorBackend::Real_v>(&shape, 0, offset, points, output);
// tail treatment
SafetyToOutLoop<Implementation, ScalarBackend::Real_v>(&shape, offset, points.size(), points, output);
}
};
/*!
* \brief Template implementation helper for the case of unplaced volumes/shapes
* where the vector interface is implemented in terms of loops over scalar version.
*/
template <class Implementation, class BaseUnplVol = VUnplacedVolume>
class LoopUnplacedVolumeImplHelper : public CommonUnplacedVolumeImplHelper<Implementation, BaseUnplVol> {
public:
using Real_v = vecgeom::VectorBackend::Real_v;
using Real_s = vecgeom::ScalarBackend::Real_v;
using UnplacedVolume_t = typename Implementation::UnplacedVolume_t;
using Common_t = CommonUnplacedVolumeImplHelper<Implementation, BaseUnplVol>;
static constexpr bool SIMDHELPER = false;
// constructors
using Common_t::Common_t;
using Common_t::DistanceToIn;
using Common_t::DistanceToOut;
using Common_t::SafetyToIn;
using Common_t::SafetyToOut;
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v DistanceToOutVec(Vector3D<Real_v> const &p, Vector3D<Real_v> const &d,
Real_v const &step_max) const override
{
// implementation of a vector interface in terms of a scalar interface
Real_v output(kInfLength);
using vecCore::LaneAt;
for (size_t i = 0; i < vecCore::VectorSize<Real_v>(); ++i) {
Vector3D<Real_s> ps(LaneAt(p.x(), i), LaneAt(p.y(), i), LaneAt(p.z(), i)); // scalar vector
Vector3D<Real_s> ds(LaneAt(d.x(), i), LaneAt(d.y(), i), LaneAt(d.z(), i)); // scalar direction;
Real_s result;
Implementation::template DistanceToOut<Real_s>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), ps, ds,
LaneAt(step_max, i), result);
vecCore::AssignLane(output, i, result);
}
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v DistanceToInVec(Vector3D<Real_v> const &p, Vector3D<Real_v> const &d,
Real_v const &step_max) const override
{
// implementation of a vector interface in terms of a scalar interface
Real_v output(kInfLength);
using vecCore::LaneAt;
for (size_t i = 0; i < vecCore::VectorSize<Real_v>(); ++i) {
Vector3D<Real_s> ps(LaneAt(p.x(), i), LaneAt(p.y(), i), LaneAt(p.z(), i)); // scalar vector
Vector3D<Real_s> ds(LaneAt(d.x(), i), LaneAt(d.y(), i), LaneAt(d.z(), i)); // scalar direction;
Real_s tmp(-1.);
Implementation::template DistanceToIn<Real_s>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), ps, ds,
LaneAt(step_max, i), tmp);
vecCore::AssignLane(output, i, tmp);
}
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v SafetyToOutVec(Vector3D<Real_v> const &p) const override
{
// implementation of a vector interface in terms of a scalar interface
Real_v output(-1.);
using vecCore::LaneAt;
for (size_t i = 0; i < vecCore::VectorSize<Real_v>(); ++i) {
Vector3D<Real_s> ps(LaneAt(p.x(), i), LaneAt(p.y(), i), LaneAt(p.z(), i)); // scalar vector
Real_s tmp(kInfLength);
Implementation::template SafetyToOut<Real_s>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), ps, tmp);
vecCore::AssignLane(output, i, tmp);
}
return output;
}
// the explicit SIMD interface
VECCORE_ATT_HOST_DEVICE
virtual Real_v SafetyToInVec(Vector3D<Real_v> const &p) const override
{
// implementation of a vector interface in terms of a scalar interface
Real_v output(kInfLength);
using vecCore::LaneAt;
for (size_t i = 0; i < vecCore::VectorSize<Real_v>(); ++i) {
Vector3D<Real_s> ps(LaneAt(p.x(), i), LaneAt(p.y(), i), LaneAt(p.z(), i)); // scalar vector
Real_s tmp;
Implementation::template SafetyToIn<Real_s>(((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct(), ps, tmp);
vecCore::AssignLane(output, i, tmp);
}
return output;
}
using UnplacedStruct_t = typename Implementation::UnplacedStruct_t;
virtual void DistanceToOut(SOA3D<Precision> const &points, SOA3D<Precision> const &directions,
Precision const *const step_max, Precision *const output) const override
{
auto &shape = ((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct();
DistanceToOutLoop<Implementation, Precision>(&shape, 0, points.size(), points, directions, step_max, output);
}
virtual void SafetyToOut(SOA3D<Precision> const &points, Precision *const output) const override
{
auto &shape = ((UnplacedVolume_t *)this)->UnplacedVolume_t::GetStruct();
SafetyToOutLoop<Implementation, Precision>(&shape, 0, points.size(), points, output);
}
};
} // namespace VECGEOM_IMPL_NAMESPACE
} // namespace vecgeom
#ifndef __clang__
#pragma GCC diagnostic pop
#endif
#endif /* VOLUMES_UnplacedVolumeImplHelper_H_ */
|