Skip to content

Commit

Permalink
Splits ZVertexSoA into 2 layouts and wraps those in a multi layout co…
Browse files Browse the repository at this point in the history
…llection.
  • Loading branch information
ericcano committed Feb 15, 2024
1 parent 4193148 commit 4ee4c37
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void SiPixelCompareVertexSoAAlpaka::analyze(const edm::Event& iEvent, const edm:
auto yc = y0 + dydz * zc;
zc += z0;

auto ndofHost = vsoaHost.view()[sic].ndof();
auto ndofHost = vsoaHost.view<reco::ZVertexTracksSoA>()[sic].ndof();
auto chi2Host = vsoaHost.view()[sic].chi2();

const int32_t notFound = -1;
Expand All @@ -130,7 +130,7 @@ void SiPixelCompareVertexSoAAlpaka::analyze(const edm::Event& iEvent, const edm:
auto xg = x0 + dxdz * zg;
auto yg = y0 + dydz * zg;
zg += z0;
auto ndofDevice = vsoaDevice.view()[closestVtxidx].ndof();
auto ndofDevice = vsoaDevice.view<reco::ZVertexTracksSoA>()[closestVtxidx].ndof();
auto chi2Device = vsoaDevice.view()[closestVtxidx].chi2();

hx_->Fill(xc - x0, xg - x0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ void SiPixelMonitorVertexSoAAlpaka::analyze(const edm::Event& iEvent, const edm:
}

auto const& vsoa = *vsoaHandle;
int nVertices = vsoa.view().nvFinal();
auto vtx_view = vsoa.view<reco::ZVertexSoA>();
auto trx_view = vsoa.view<reco::ZVertexTracksSoA>();
int nVertices = vtx_view.nvFinal();
auto bsHandle = iEvent.getHandle(tokenBeamSpot_);
float x0 = 0., y0 = 0., z0 = 0., dxdz = 0., dydz = 0.;
if (!bsHandle.isValid()) {
Expand All @@ -82,19 +84,19 @@ void SiPixelMonitorVertexSoAAlpaka::analyze(const edm::Event& iEvent, const edm:
}

for (int iv = 0; iv < nVertices; iv++) {
auto si = vsoa.view()[iv].sortInd();
auto z = vsoa.view()[si].zv();
auto si = vtx_view[iv].sortInd();
auto z = vtx_view[si].zv();
auto x = x0 + dxdz * z;
auto y = y0 + dydz * z;

z += z0;
hx->Fill(x);
hy->Fill(y);
hz->Fill(z);
auto ndof = vsoa.view()[si].ndof();
hchi2->Fill(vsoa.view()[si].chi2());
hchi2oNdof->Fill(vsoa.view()[si].chi2() / ndof);
hptv2->Fill(vsoa.view()[si].ptv2());
auto ndof = trx_view[si].ndof();
hchi2->Fill(vtx_view[si].chi2());
hchi2oNdof->Fill(vtx_view[si].chi2() / ndof);
hptv2->Fill(vtx_view[si].ptv2());
hntrks->Fill(ndof + 1);
}
hnVertex->Fill(nVertices);
Expand Down
12 changes: 6 additions & 6 deletions DataFormats/VertexSoA/interface/ZVertexDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
#include "DataFormats/VertexSoA/interface/ZVertexHost.h"
#include "DataFormats/Portable/interface/PortableDeviceCollection.h"

template <int32_t S, typename TDev>
class ZVertexDeviceSoA : public PortableDeviceCollection<reco::ZVertexLayout<>, TDev> {
template <int32_t NVTX, int32_t NTRX, typename TDev>
class ZVertexDeviceSoA : public PortableDeviceMultiCollection<TDev, reco::ZVertexSoA, reco::ZVertexTracksSoA> {
public:
ZVertexDeviceSoA() = default; // necessary for ROOT dictionaries

// Constructor which specifies the SoA size
// Constructor which specifies the queue
template <typename TQueue>
explicit ZVertexDeviceSoA(TQueue queue) : PortableDeviceCollection<reco::ZVertexLayout<>, TDev>(S, queue) {}
explicit ZVertexDeviceSoA(TQueue queue)
: PortableDeviceMultiCollection<TDev, reco::ZVertexSoA, reco::ZVertexTracksSoA>({{NVTX, NTRX}}, queue) {}
};

using namespace ::zVertex;
template <typename TDev>
using ZVertexDevice = ZVertexDeviceSoA<MAXTRACKS, TDev>;
using ZVertexDevice = ZVertexDeviceSoA<zVertex::MAXVTX, zVertex::MAXTRACKS, TDev>;

#endif // DataFormats_VertexSoA_interface_ZVertexDevice_h
12 changes: 7 additions & 5 deletions DataFormats/VertexSoA/interface/ZVertexHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
#include "DataFormats/VertexSoA/interface/ZVertexDefinitions.h"
#include "DataFormats/Portable/interface/PortableHostCollection.h"

template <int32_t S>
class ZVertexHostSoA : public PortableHostCollection<reco::ZVertexSoA> {
template <int32_t NVTX, int32_t NTRX>
class ZVertexHostSoA : public PortableHostCollection2<reco::ZVertexSoA, reco::ZVertexTracksSoA> {
public:
ZVertexHostSoA() = default;

// Constructor which specifies the queue
template <typename TQueue>
explicit ZVertexHostSoA(TQueue queue) : PortableHostCollection<reco::ZVertexSoA>(S, queue) {}
explicit ZVertexHostSoA(TQueue queue)
: PortableHostCollection2<reco::ZVertexSoA, reco::ZVertexTracksSoA>({{NVTX, NTRX}}, queue) {}

// Constructor which specifies the DevHost
explicit ZVertexHostSoA(alpaka_common::DevHost const& host) : PortableHostCollection<reco::ZVertexSoA>(S, host) {}
explicit ZVertexHostSoA(alpaka_common::DevHost const& host)
: PortableHostCollection2<reco::ZVertexSoA, reco::ZVertexTracksSoA>({{NVTX, NTRX}}, host) {}
};

//using namespace ::zVertex;
using ZVertexHost = ZVertexHostSoA<zVertex::MAXTRACKS>;
using ZVertexHost = ZVertexHostSoA<zVertex::MAXVTX, zVertex::MAXTRACKS>;

#endif // DataFormats_VertexSoA_ZVertexHost_H
13 changes: 11 additions & 2 deletions DataFormats/VertexSoA/interface/ZVertexSoA.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@
namespace reco {

GENERATE_SOA_LAYOUT(ZVertexLayout,
SOA_COLUMN(int16_t, idv), // vertex index for each associated (original) track (-1 == not associate
SOA_COLUMN(float, zv), // output z-posistion of found vertices
SOA_COLUMN(float, wv), // output weight (1/error^2) on the above
SOA_COLUMN(float, chi2), // vertices chi2
SOA_COLUMN(float, ptv2), // vertices pt^2
SOA_COLUMN(int32_t, ndof), // vertices number of dof (reused as workspace for the number of nearest neighbours FIXME)
SOA_COLUMN(uint16_t, sortInd), // sorted index (by pt2) ascending
SOA_SCALAR(uint32_t, nvFinal)) // the number of vertices

GENERATE_SOA_LAYOUT(
ZVertexTracksLayout,
SOA_COLUMN(int16_t, idv), // vertex index for each associated (original) track (-1 == not associate
SOA_COLUMN(int32_t,
ndof)) // vertices number of dof (reused as workspace for the number of nearest neighbours FIXME)

// Common types for both Host and Device code
using ZVertexSoA = ZVertexLayout<>;
using ZVertexSoAView = ZVertexSoA::View;
using ZVertexSoAConstView = ZVertexSoA::ConstView;

// Common types for both Host and Device code
using ZVertexTracksSoA = ZVertexTracksLayout<>;
using ZVertexTracksSoAView = ZVertexTracksSoA::View;
using ZVertexTracksSoAConstView = ZVertexTracksSoA::ConstView;

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE void init(ZVertexSoAView &vertices) { vertices.nvFinal() = 0; }

} // namespace reco
Expand Down
4 changes: 2 additions & 2 deletions DataFormats/VertexSoA/src/classes_def.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<lcgdict>
<class name="reco::ZVertexSoA"/>
<class name="PortableHostCollection<reco::ZVertexSoA>"/>
<class name="ZVertexHost" ClassVersion="3">
<version ClassVersion="3" checksum="1989784241"/>
<class name="ZVertexHost" ClassVersion="4">
<version ClassVersion="4" checksum="1552837222"/>
</class>
<class name="edm::Wrapper<ZVertexHost>" splitLevel="0"/>
</lcgdict>
14 changes: 8 additions & 6 deletions DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace reco;

namespace ALPAKA_ACCELERATOR_NAMESPACE {
namespace testZVertexSoAT {
void runKernels(ZVertexSoAView zvertex_view, Queue& queue);
void runKernels(ZVertexSoAView zvertex_view, ZVertexTracksSoAView zvertextracks_view, Queue& queue);
}
} // namespace ALPAKA_ACCELERATOR_NAMESPACE

Expand All @@ -44,7 +44,7 @@ int main() {
// Instantiate vertices on device. PortableCollection allocates
// SoA on device automatically.
ZVertexSoACollection zvertex_d(queue);
testZVertexSoAT::runKernels(zvertex_d.view(), queue);
testZVertexSoAT::runKernels(zvertex_d.view(), zvertex_d.view<reco::ZVertexTracksSoA>(), queue);

// Instantate vertices on host. This is where the data will be
// copied to from device.
Expand All @@ -70,11 +70,13 @@ int main() {
<< "\t"
<< "nvFinal" << std::endl;

auto vtx_v = zvertex_h.view<reco::ZVertexSoA>();
auto trx_v = zvertex_h.view<reco::ZVertexTracksSoA>();
for (int i = 0; i < 10; ++i) {
std::cout << (int)zvertex_h.view()[i].idv() << "\t" << zvertex_h.view()[i].zv() << "\t"
<< zvertex_h.view()[i].wv() << "\t" << zvertex_h.view()[i].chi2() << "\t" << zvertex_h.view()[i].ptv2()
<< "\t" << (int)zvertex_h.view()[i].ndof() << "\t" << (int)zvertex_h.view()[i].sortInd() << "\t"
<< (int)zvertex_h.view().nvFinal() << std::endl;
auto vi = vtx_v[i];
auto ti = trx_v[i];
std::cout << (int)ti.idv() << "\t" << vi.zv() << "\t" << vi.wv() << "\t" << vi.chi2() << "\t" << vi.ptv2() << "\t"
<< (int)ti.ndof() << "\t" << vi.sortInd() << "\t" << (int)vtx_v.nvFinal() << std::endl;
}
}

Expand Down
26 changes: 17 additions & 9 deletions DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.dev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,57 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
class TestFillKernel {
public:
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, reco::ZVertexSoAView zvertex_view) const {
ALPAKA_FN_ACC void operator()(TAcc const& acc,
reco::ZVertexSoAView zvertex_view,
reco::ZVertexTracksSoAView ztracks_view) const {
if (cms::alpakatools::once_per_grid(acc)) {
zvertex_view.nvFinal() = 420;
}

for (int32_t j : elements_with_stride(acc, zvertex_view.metadata().size())) {
zvertex_view[j].idv() = (int16_t)j;
zvertex_view[j].zv() = (float)j;
zvertex_view[j].wv() = (float)j;
zvertex_view[j].chi2() = (float)j;
zvertex_view[j].ptv2() = (float)j;
zvertex_view[j].ndof() = (int32_t)j;
zvertex_view[j].sortInd() = (uint16_t)j;
}
for (int32_t j : elements_with_stride(acc, ztracks_view.metadata().size())) {
ztracks_view[j].idv() = (int16_t)j;
ztracks_view[j].ndof() = (int32_t)j;
}
}
};

class TestVerifyKernel {
public:
template <typename TAcc, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
ALPAKA_FN_ACC void operator()(TAcc const& acc, reco::ZVertexSoAView zvertex_view) const {
ALPAKA_FN_ACC void operator()(TAcc const& acc,
reco::ZVertexSoAView zvertex_view,
reco::ZVertexTracksSoAView ztracks_view) const {
if (cms::alpakatools::once_per_grid(acc)) {
ALPAKA_ASSERT_ACC(zvertex_view.nvFinal() == 420);
}

for (int32_t j : elements_with_stride(acc, zvertex_view.nvFinal())) {
assert(zvertex_view[j].idv() == j);
assert(zvertex_view[j].zv() - (float)j < 0.0001);
assert(zvertex_view[j].wv() - (float)j < 0.0001);
assert(zvertex_view[j].chi2() - (float)j < 0.0001);
assert(zvertex_view[j].ptv2() - (float)j < 0.0001);
assert(zvertex_view[j].ndof() == j);
assert(zvertex_view[j].sortInd() == uint32_t(j));
}
for (int32_t j : elements_with_stride(acc, ztracks_view.metadata().size())) {
assert(ztracks_view[j].idv() == j);
assert(ztracks_view[j].ndof() == j);
}
}
};

void runKernels(reco::ZVertexSoAView zvertex_view, Queue& queue) {
void runKernels(reco::ZVertexSoAView zvertex_view, reco::ZVertexTracksSoAView ztracks_view, Queue& queue) {
uint32_t items = 64;
uint32_t groups = divide_up_by(zvertex_view.metadata().size(), items);
auto workDiv = make_workdiv<Acc1D>(groups, items);
alpaka::exec<Acc1D>(queue, workDiv, TestFillKernel{}, zvertex_view);
alpaka::exec<Acc1D>(queue, workDiv, TestVerifyKernel{}, zvertex_view);
alpaka::exec<Acc1D>(queue, workDiv, TestFillKernel{}, zvertex_view, ztracks_view);
alpaka::exec<Acc1D>(queue, workDiv, TestVerifyKernel{}, zvertex_view, ztracks_view);
}

} // namespace testZVertexSoAT
Expand Down
4 changes: 2 additions & 2 deletions RecoTauTag/HLTProducers/src/L2TauTagNNProducerAlpaka.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ void L2TauNNProducerAlpaka::selectGoodTracksAndVertices(const ZVertexHost& patav
if (nHits == 0) {
break;
}
int vtx_ass_to_track = patavtx_soa.view()[trk_idx].idv();
int vtx_ass_to_track = patavtx_soa.view<reco::ZVertexTracksSoA>()[trk_idx].idv();
if (vtx_ass_to_track >= 0 && vtx_ass_to_track < nv) {
auto patatrackPt = patatracks_tsoa.view()[trk_idx].pt();
++nTrkAssociated[vtx_ass_to_track];
Expand Down Expand Up @@ -692,7 +692,7 @@ void L2TauNNProducerAlpaka::fillPatatracks(tensorflow::Tensor& cellGridMatrix,
continue;
const int patatrackNdof = 2 * std::min(6, nHits) - 5;

const int vtx_idx_assTrk = patavtx_soa.view()[it].idv();
const int vtx_idx_assTrk = patavtx_soa.view<reco::ZVertexTracksSoA>()[it].idv();
if (reco::deltaR2(patatrackEta, patatrackPhi, tauEta, tauPhi) < dR2_max) {
std::tie(deta, dphi, eta_idx, phi_idx) =
getEtaPhiIndices(patatrackEta, patatrackPhi, allTaus[tau_idx]->polarP4());
Expand Down
4 changes: 2 additions & 2 deletions RecoTracker/PixelTrackFitting/plugins/PixelTrackDumpAlpaka.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ void PixelTrackDumpAlpakaT<TrackerTraits>::analyze(edm::StreamID streamID,
assert(tracks.view().nTracks());

auto const& vertices = iEvent.get(tokenSoAVertex_);
assert(vertices.view().idv());
assert(vertices.view<reco::ZVertexTracksSoA>().idv());
assert(vertices.view().zv());
assert(vertices.view().wv());
assert(vertices.view().chi2());
assert(vertices.view().ptv2());
assert(vertices.view().ndof());
assert(vertices.view<reco::ZVertexTracksSoA>().ndof());
assert(vertices.view().sortInd());
assert(vertices.view().nvFinal());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void PixelVertexProducerFromSoAAlpaka::produce(edm::StreamID streamID,
err(2, 2) *= 2.; // artifically inflate error
//Copy also the tracks (no intention to be efficient....)
for (auto k = 0U; k < indToEdm.size(); ++k) {
if (soa.view()[k].idv() == int16_t(i))
if (soa.view<reco::ZVertexTracksSoA>()[k].idv() == int16_t(i))
itrk.push_back(k);
}
auto nt = itrk.size();
Expand All @@ -117,7 +117,8 @@ void PixelVertexProducerFromSoAAlpaka::produce(edm::StreamID streamID,
itrk.clear();
continue;
} // remove outliers
(*vertexes).emplace_back(reco::Vertex::Point(x, y, z), err, soa.view()[i].chi2(), soa.view()[i].ndof(), nt);
(*vertexes).emplace_back(
reco::Vertex::Point(x, y, z), err, soa.view()[i].chi2(), soa.view<reco::ZVertexTracksSoA>()[i].ndof(), nt);
auto &v = (*vertexes).back();
v.reserve(itrk.size());
for (auto it : itrk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,48 @@
#include "vertexFinder.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder {

using VtxSoAView = ::reco::ZVertexSoAView;
using WsSoAView = ::vertexFinder::PixelVertexWorkSpaceSoAView;
// this algo does not really scale as it works in a single block...
// enough for <10K tracks we have
//
// based on Rodrighez&Laio algo
//
template <typename TAcc>
ALPAKA_FN_ACC ALPAKA_FN_INLINE void __attribute__((always_inline))
clusterTracksByDensity(const TAcc& acc,
VtxSoAView& pdata,
WsSoAView& pws,
int minT, // min number of neighbours to be "seed"
float eps, // max absolute distance to cluster
float errmax, // max error to be "seed"
float chi2max // max normalized distance to cluster
) {
using namespace vertexFinder;
constexpr bool verbose = false; // in principle the compiler should optmize out if false
const uint32_t threadIdxLocal(alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc)[0u]);

using VtxSoAView = ::reco::ZVertexSoAView;
using TrxSoAView = ::reco::ZVertexTracksSoAView;
using WsSoAView = ::vertexFinder::PixelVertexWorkSpaceSoAView;
// this algo does not really scale as it works in a single block...
// enough for <10K tracks we have
//
// based on Rodrighez&Laio algo
//
template <typename TAcc>
ALPAKA_FN_ACC ALPAKA_FN_INLINE void __attribute__((always_inline))
clusterTracksByDensity(const TAcc& acc,
VtxSoAView& pdata,
TrxSoAView ptrxdata,
WsSoAView& pws,
int minT, // min number of neighbours to be "seed"
float eps, // max absolute distance to cluster
float errmax, // max error to be "seed"
float chi2max // max normalized distance to cluster
) {
using namespace vertexFinder;
constexpr bool verbose = false; // in principle the compiler should optmize out if false
const uint32_t threadIdxLocal(alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc)[0u]);

if constexpr (verbose) {
if (cms::alpakatools::once_per_block(acc))
printf("params %d %f %f %f\n", minT, eps, errmax, chi2max);
}
auto er2mx = errmax * errmax;

auto& __restrict__ data = pdata;
auto& __restrict__ ws = pws;
auto nt = ws.ntrks();
float const* __restrict__ zt = ws.zt();
float const* __restrict__ ezt2 = ws.ezt2();
auto& __restrict__ data = pdata;
auto& __restrict__ trxdata = ptrxdata;
auto& __restrict__ ws = pws;
auto nt = ws.ntrks();
float const* __restrict__ zt = ws.zt();
float const* __restrict__ ezt2 = ws.ezt2();

uint32_t& nvFinal = data.nvFinal();
uint32_t& nvIntermediate = ws.nvIntermediate();

uint8_t* __restrict__ izt = ws.izt();
int32_t* __restrict__ nn = data.ndof();
int32_t* __restrict__ nn = trxdata.ndof();
int32_t* __restrict__ iv = ws.iv();

ALPAKA_ASSERT_ACC(zt);
Expand Down Expand Up @@ -238,13 +241,14 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::vertexFinder {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(const TAcc& acc,
VtxSoAView pdata,
TrxSoAView ptrxdata,
WsSoAView pws,
int minT, // min number of neighbours to be "seed"
float eps, // max absolute distance to cluster
float errmax, // max error to be "seed"
float chi2max // max normalized distance to cluster
) const {
clusterTracksByDensity(acc, pdata, pws, minT, eps, errmax, chi2max);
clusterTracksByDensity(acc, pdata, ptrxdata, pws, minT, eps, errmax, chi2max);
}
};

Expand Down
Loading

0 comments on commit 4ee4c37

Please sign in to comment.