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.

Removes versioning info from class description for dictionnaries.
Ports comments from CUDA version.
  • Loading branch information
ericcano committed Mar 12, 2024
1 parent c90f14e commit f086201
Show file tree
Hide file tree
Showing 22 changed files with 242 additions and 136 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 trk_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 = trk_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 NTRK, 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, NTRK}}, 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
15 changes: 10 additions & 5 deletions DataFormats/VertexSoA/interface/ZVertexHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
#include "DataFormats/VertexSoA/interface/ZVertexDefinitions.h"
#include "DataFormats/Portable/interface/PortableHostCollection.h"

template <int32_t S>
class ZVertexHostSoA : public PortableHostCollection<reco::ZVertexSoA> {
// This alias is needed to feed the SET_PORTABLEHOSTMULTICOLLECTION_READ_RULES macro without commas.
using ZVertexHostSoABase = PortableHostCollection2<reco::ZVertexSoA, reco::ZVertexTracksSoA>;

template <int32_t NVTX, int32_t NTRK>
class ZVertexHostSoA : public ZVertexHostSoABase {
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, NTRK}}, 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, NTRK}}, host) {}
};

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

#endif // DataFormats_VertexSoA_ZVertexHost_H
25 changes: 17 additions & 8 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),
SOA_COLUMN(float, zv),
SOA_COLUMN(float, wv),
SOA_COLUMN(float, chi2),
SOA_COLUMN(float, ptv2),
SOA_COLUMN(int32_t, ndof),
SOA_COLUMN(uint16_t, sortInd),
SOA_SCALAR(uint32_t, nvFinal))
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(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.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "DataFormats/Portable/interface/PortableHostCollectionReadRules.h"
#include "DataFormats/VertexSoA/interface/ZVertexSoA.h"
#include "DataFormats/VertexSoA/interface/ZVertexHost.h"

SET_PORTABLEHOSTCOLLECTION_READ_RULES(PortableHostCollection<reco::ZVertexSoA>);
SET_PORTABLEHOSTMULTICOLLECTION_READ_RULES(ZVertexHostSoABase);
25 changes: 19 additions & 6 deletions DataFormats/VertexSoA/src/classes_def.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
<lcgdict>
<class name="reco::ZVertexSoA"/>
<class name="PortableHostCollection<reco::ZVertexSoA>"/>
<class name="ZVertexHost" ClassVersion="3">
<version ClassVersion="3" checksum="1989784241"/>
</class>
<class name="reco::ZVertexLayout<128, false>"/>
<class name="reco::ZVertexTracksLayout<128, false>"/>

<!-- Recursive templates (with no data) ensuring we have one CollectionLeaf<index, type> for each layout in the collection -->
<class name="portablecollection::CollectionImpl<0, reco::ZVertexLayout<128, false>, reco::ZVertexTracksLayout<128, false>>"/>
<class name="portablecollection::CollectionImpl<1, reco::ZVertexTracksLayout<128, false>>"/>

<!-- Recursive templates implementing the association of indices and layouts, and containing the data -->
<class name="portablecollection::CollectionLeaf<0, reco::ZVertexLayout<128, false>>"/>
<class name="portablecollection::CollectionLeaf<1, reco::ZVertexTracksLayout<128, false>>"/>

<!-- Collection declaration for dictionary -->
<!-- This alias to PortableHostCollection2<reco::ZVertexSoA, reco::ZVertexTracksSoA> is needed to use with the macro
SET_PORTABLEHOSTMULTICOLLECTION_READ_RULES, which needs the direct collection class -->
<class name="ZVertexHostSoABase"/>
<!-- This class inherits from the collection and needs to be serialized as well -->
<class name="ZVertexHost"/>
<!-- and the product wrapper, classically -->
<class name="edm::Wrapper<ZVertexHost>" splitLevel="0"/>
</lcgdict>
</lcgdict>
12 changes: 7 additions & 5 deletions DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 @@ -68,11 +68,13 @@ int main() {
<< "sortInd\t"
<< "nvFinal\n";

auto vtx_v = zvertex_h.view<reco::ZVertexSoA>();
auto trk_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() << '\n';
auto vi = vtx_v[i];
auto ti = trk_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
72 changes: 40 additions & 32 deletions DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.dev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,57 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::testZVertexSoAT {
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 : cms::alpakatools::uniform_elements(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 : cms::alpakatools::uniform_elements(acc, zvertex_view.metadata().size())) {
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].sortInd() = (uint16_t)j;
}
for (int32_t j : cms::alpakatools::uniform_elements(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 {
if (cms::alpakatools::once_per_grid(acc)) {
ALPAKA_ASSERT_ACC(zvertex_view.nvFinal() == 420);
}
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,
reco::ZVertexTracksSoAView ztracks_view) const {
if (cms::alpakatools::once_per_grid(acc)) {
ALPAKA_ASSERT_ACC(zvertex_view.nvFinal() == 420);
}

for (int32_t j : cms::alpakatools::uniform_elements(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 : cms::alpakatools::uniform_elements(acc, zvertex_view.nvFinal())) {
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].sortInd() == uint32_t(j));
}
for (int32_t j : cms::alpakatools::uniform_elements(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 = cms::alpakatools::divide_up_by(zvertex_view.metadata().size(), items);
auto workDiv = cms::alpakatools::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 ALPAKA_ACCELERATOR_NAMESPACE::testZVertexSoAT
2 changes: 1 addition & 1 deletion DataFormats/VertexSoA/test/alpaka/ZVertexSoA_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ALPAKA_ACCELERATOR_NAMESPACE::testZVertexSoAT {

void runKernels(reco::ZVertexSoAView zvertex_view, Queue& queue);
void runKernels(reco::ZVertexSoAView zvertex_view, reco::ZVertexTracksSoAView ztracks_view, Queue& queue);

} // namespace ALPAKA_ACCELERATOR_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
Loading

0 comments on commit f086201

Please sign in to comment.