Skip to content

Commit

Permalink
migrate triplets to SoATemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
slava77devel committed Oct 22, 2024
1 parent 6f1ea3f commit 1bbe663
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 378 deletions.
3 changes: 3 additions & 0 deletions RecoTracker/LSTCore/interface/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ namespace lst {
};
struct Params_T3 {
static constexpr int kLayers = 3, kHits = 6;
using ArrayU8xLayers = edm::StdArray<uint8_t, kLayers>;
using ArrayU16xLayers = edm::StdArray<uint16_t, kLayers>;
using ArrayUxHits = edm::StdArray<unsigned int, kHits>;
};
struct Params_pT3 {
static constexpr int kLayers = 5, kHits = 10;
Expand Down
10 changes: 10 additions & 0 deletions RecoTracker/LSTCore/interface/TripletsHostCollection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef RecoTracker_LSTCore_interface_TripletsHostCollection_h
#define RecoTracker_LSTCore_interface_TripletsHostCollection_h

#include "RecoTracker/LSTCore/interface/TripletsSoA.h"
#include "DataFormats/Portable/interface/PortableHostCollection.h"

namespace lst {
using TripletsHostCollection = PortableHostMultiCollection<TripletsSoA, TripletsOccupancySoA>;
} // namespace lst
#endif
42 changes: 42 additions & 0 deletions RecoTracker/LSTCore/interface/TripletsSoA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef RecoTracker_LSTCore_interface_TripletsSoA_h
#define RecoTracker_LSTCore_interface_TripletsSoA_h

#include <alpaka/alpaka.hpp>
#include "DataFormats/Common/interface/StdArray.h"
#include "DataFormats/SoATemplate/interface/SoALayout.h"

#include "RecoTracker/LSTCore/interface/Constants.h"

namespace lst {
GENERATE_SOA_LAYOUT(TripletsSoALayout,
SOA_COLUMN(ArrayUx2, segmentIndices), // inner and outer segment indices
SOA_COLUMN(Params_T3::ArrayU16xLayers, lowerModuleIndices), // lower module index in each layer
SOA_COLUMN(Params_T3::ArrayU8xLayers, logicalLayers), // layer ID
SOA_COLUMN(Params_T3::ArrayUxHits, hitIndices), // hit indices
SOA_COLUMN(FPX, betaIn), // beta/chord angle of the inner segment
SOA_COLUMN(float, centerX), // lower/anchor-hit based circle center x
SOA_COLUMN(float, centerY), // lower/anchor-hit based circle center y
SOA_COLUMN(float, radius), // lower/anchor-hit based circle radius
#ifdef CUT_VALUE_DEBUG
SOA_COLUMN(float, zOut),
SOA_COLUMN(float, rtOut),
SOA_COLUMN(float, betaInCut),
#endif
SOA_COLUMN(bool, partOfPT5), // is it used in a pT5
SOA_COLUMN(bool, partOfT5), // is it used in a T5
SOA_COLUMN(bool, partOfPT3)); // is it used in a pT3

using TripletsSoA = TripletsSoALayout<>;
using Triplets = TripletsSoA::View;
using TripletsConst = TripletsSoA::ConstView;

GENERATE_SOA_LAYOUT(TripletsOccupancySoALayout,
SOA_COLUMN(unsigned int, nTriplets),
SOA_COLUMN(unsigned int, totOccupancyTriplets));

using TripletsOccupancySoA = TripletsOccupancySoALayout<>;
using TripletsOccupancy = TripletsOccupancySoA::View;
using TripletsOccupancyConst = TripletsOccupancySoA::ConstView;

} // namespace lst
#endif
10 changes: 10 additions & 0 deletions RecoTracker/LSTCore/interface/alpaka/TripletsDeviceCollection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef RecoTracker_LSTCore_interface_TripletsDeviceCollection_h
#define RecoTracker_LSTCore_interface_TripletsDeviceCollection_h

#include "RecoTracker/LSTCore/interface/TripletsSoA.h"
#include "DataFormats/Portable/interface/alpaka/PortableCollection.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
using TripletsDeviceCollection = PortableCollection2<TripletsSoA, TripletsOccupancySoA>;
} // namespace ALPAKA_ACCELERATOR_NAMESPACE::lst
#endif
109 changes: 56 additions & 53 deletions RecoTracker/LSTCore/src/alpaka/Event.dev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include "Event.h"

#include "MiniDoublet.h"
#include "Segment.h"
#include "TrackCandidate.h"
#include "Triplet.h"

using Device = ALPAKA_ACCELERATOR_NAMESPACE::Device;
using Queue = ALPAKA_ACCELERATOR_NAMESPACE::Queue;
using Acc1D = ALPAKA_ACCELERATOR_NAMESPACE::Acc1D;
Expand Down Expand Up @@ -57,8 +62,7 @@ void Event::resetEventSync() {
rangesInGPU_.reset();
rangesBuffers_.reset();
segmentsDC_.reset();
tripletsInGPU_.reset();
tripletsBuffers_.reset();
tripletsDC_.reset();
quintupletsInGPU_.reset();
quintupletsBuffers_.reset();
trackCandidatesDC_.reset();
Expand All @@ -71,7 +75,7 @@ void Event::resetEventSync() {
rangesInCPU_.reset();
miniDoubletsHC_.reset();
segmentsHC_.reset();
tripletsInCPU_.reset();
tripletsHC_.reset();
quintupletsInCPU_.reset();
pixelTripletsInCPU_.reset();
pixelQuintupletsInCPU_.reset();
Expand Down Expand Up @@ -419,7 +423,7 @@ void Event::createSegmentsWithModuleMap() {
}

void Event::createTriplets() {
if (!tripletsInGPU_) {
if (!tripletsDC_) {
WorkDiv1D const createTripletArrayRanges_workDiv = createWorkDiv<Vec1D>({1}, {1024}, {1});

alpaka::exec<Acc1D>(queue_,
Expand All @@ -435,11 +439,24 @@ void Event::createTriplets() {
alpaka::memcpy(queue_, maxTriplets_buf_h, rangesBuffers_->device_nTotalTrips_buf);
alpaka::wait(queue_); // wait to get the value before using it

tripletsInGPU_.emplace();
tripletsBuffers_.emplace(*maxTriplets_buf_h.data(), nLowerModules_, devAcc_, queue_);
tripletsInGPU_->setData(*tripletsBuffers_);

alpaka::memcpy(queue_, tripletsBuffers_->nMemoryLocations_buf, maxTriplets_buf_h);
std::array<int, 2> const triplets_sizes{
{static_cast<int>(*maxTriplets_buf_h.data()), static_cast<int>(nLowerModules_)}};
tripletsDC_.emplace(triplets_sizes, queue_);

auto tripletsOccupancy = tripletsDC_->view<TripletsOccupancySoA>();
auto nTriplets_view =
alpaka::createView(devAcc_, tripletsOccupancy.nTriplets(), tripletsOccupancy.metadata().size());
alpaka::memset(queue_, nTriplets_view, 0u);
auto totOccupancyTriplets_view =
alpaka::createView(devAcc_, tripletsOccupancy.totOccupancyTriplets(), tripletsOccupancy.metadata().size());
alpaka::memset(queue_, totOccupancyTriplets_view, 0u);
auto triplets = tripletsDC_->view<TripletsSoA>();
auto partOfPT5_view = alpaka::createView(devAcc_, triplets.partOfPT5(), triplets.metadata().size());
alpaka::memset(queue_, partOfPT5_view, 0u);
auto partOfT5_view = alpaka::createView(devAcc_, triplets.partOfT5(), triplets.metadata().size());
alpaka::memset(queue_, partOfT5_view, 0u);
auto partOfPT3_view = alpaka::createView(devAcc_, triplets.partOfPT3(), triplets.metadata().size());
alpaka::memset(queue_, partOfPT3_view, 0u);
}

uint16_t nonZeroModules = 0;
Expand Down Expand Up @@ -481,17 +498,18 @@ void Event::createTriplets() {

Vec3D const threadsPerBlockCreateTrip{1, 16, 16};
Vec3D const blocksPerGridCreateTrip{max_blocks, 1, 1};
WorkDiv3D const createTripletsInGPUv2_workDiv =
WorkDiv3D const createTriplets_workDiv =
createWorkDiv(blocksPerGridCreateTrip, threadsPerBlockCreateTrip, elementsPerThread);

alpaka::exec<Acc3D>(queue_,
createTripletsInGPUv2_workDiv,
CreateTripletsInGPUv2{},
createTriplets_workDiv,
CreateTriplets{},
*modulesBuffers_.data(),
miniDoubletsDC_->const_view<MiniDoubletsSoA>(),
segmentsDC_->const_view<SegmentsSoA>(),
segmentsDC_->const_view<SegmentsOccupancySoA>(),
*tripletsInGPU_,
tripletsDC_->view<TripletsSoA>(),
tripletsDC_->view<TripletsOccupancySoA>(),
*rangesInGPU_,
index_gpu_buf.data(),
nonZeroModules);
Expand All @@ -502,7 +520,7 @@ void Event::createTriplets() {
addTripletRangesToEventExplicit_workDiv,
AddTripletRangesToEventExplicit{},
*modulesBuffers_.data(),
*tripletsInGPU_,
tripletsDC_->const_view<TripletsOccupancySoA>(),
*rangesInGPU_);

if (addObjects_) {
Expand Down Expand Up @@ -761,7 +779,8 @@ void Event::createPixelTriplets() {
miniDoubletsDC_->const_view<MiniDoubletsSoA>(),
segmentsDC_->const_view<SegmentsSoA>(),
segmentsDC_->const_view<SegmentsPixelSoA>(),
*tripletsInGPU_,
tripletsDC_->view<TripletsSoA>(),
tripletsDC_->const_view<TripletsOccupancySoA>(),
*pixelTripletsInGPU_,
connectedPixelSize_dev_buf.data(),
connectedPixelIndex_dev_buf.data(),
Expand Down Expand Up @@ -794,7 +813,7 @@ void Event::createQuintuplets() {
createEligibleModulesListForQuintupletsGPU_workDiv,
CreateEligibleModulesListForQuintupletsGPU{},
*modulesBuffers_.data(),
*tripletsInGPU_,
tripletsDC_->const_view<TripletsOccupancySoA>(),
*rangesInGPU_);

auto nEligibleT5Modules_buf = allocBufWrapper<uint16_t>(cms::alpakatools::host(), 1, queue_);
Expand Down Expand Up @@ -826,7 +845,8 @@ void Event::createQuintuplets() {
*modulesBuffers_.data(),
miniDoubletsDC_->const_view<MiniDoubletsSoA>(),
segmentsDC_->const_view<SegmentsSoA>(),
*tripletsInGPU_,
tripletsDC_->view<TripletsSoA>(),
tripletsDC_->const_view<TripletsOccupancySoA>(),
*quintupletsInGPU_,
*rangesInGPU_,
nEligibleT5Modules);
Expand Down Expand Up @@ -974,7 +994,7 @@ void Event::createPixelQuintuplets() {
miniDoubletsDC_->const_view<MiniDoubletsSoA>(),
segmentsDC_->const_view<SegmentsSoA>(),
segmentsDC_->view<SegmentsPixelSoA>(),
*tripletsInGPU_,
tripletsDC_->view<TripletsSoA>(),
*quintupletsInGPU_,
*pixelQuintupletsInGPU_,
connectedPixelSize_dev_buf.data(),
Expand Down Expand Up @@ -1110,8 +1130,10 @@ void Event::addQuintupletsToEventExplicit() {
}

void Event::addTripletsToEventExplicit() {
auto tripletsOccupancy = tripletsDC_->const_view<TripletsOccupancySoA>();
auto nTriplets_view = alpaka::createView(devAcc_, tripletsOccupancy.nTriplets(), nLowerModules_);
auto nTripletsCPU_buf = allocBufWrapper<unsigned int>(cms::alpakatools::host(), nLowerModules_, queue_);
alpaka::memcpy(queue_, nTripletsCPU_buf, tripletsBuffers_->nTriplets_buf);
alpaka::memcpy(queue_, nTripletsCPU_buf, nTriplets_view);

// FIXME: replace by ES host data
auto module_subdets_buf = allocBufWrapper<short>(cms::alpakatools::host(), nLowerModules_, queue_);
Expand Down Expand Up @@ -1441,43 +1463,24 @@ template SegmentsConst Event::getSegments<SegmentsSoA>(bool);
template SegmentsOccupancyConst Event::getSegments<SegmentsOccupancySoA>(bool);
template SegmentsPixelConst Event::getSegments<SegmentsPixelSoA>(bool);

TripletsBuffer<alpaka_common::DevHost>& Event::getTriplets(bool sync) {
if (!tripletsInCPU_) {
// Get nMemoryLocations parameter to initialize host based tripletsInCPU_
auto nMemHost_buf_h = cms::alpakatools::make_host_buffer<unsigned int[]>(queue_, 1u);
alpaka::memcpy(queue_, nMemHost_buf_h, tripletsBuffers_->nMemoryLocations_buf);
alpaka::wait(queue_); // wait for the value before using
template <typename TSoA, typename TDev>
typename TSoA::ConstView Event::getTriplets(bool sync) {
if constexpr (std::is_same_v<TDev, DevHost>) {
return tripletsDC_->const_view<TSoA>();
} else {
if (!tripletsHC_) {
tripletsHC_.emplace(
cms::alpakatools::CopyToHost<PortableMultiCollection<TDev, TripletsSoA, TripletsOccupancySoA>>::copyAsync(
queue_, *tripletsDC_));

auto const nMemHost = *nMemHost_buf_h.data();
tripletsInCPU_.emplace(nMemHost, nLowerModules_, cms::alpakatools::host(), queue_);
tripletsInCPU_->setData(*tripletsInCPU_);

alpaka::memcpy(queue_, tripletsInCPU_->nMemoryLocations_buf, tripletsBuffers_->nMemoryLocations_buf);
#ifdef CUT_VALUE_DEBUG
alpaka::memcpy(queue_, tripletsInCPU_->zOut_buf, tripletsBuffers_->zOut_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->zLo_buf, tripletsBuffers_->zLo_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->zHi_buf, tripletsBuffers_->zHi_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->zLoPointed_buf, tripletsBuffers_->zLoPointed_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->zHiPointed_buf, tripletsBuffers_->zHiPointed_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->dPhiCut_buf, tripletsBuffers_->dPhiCut_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->betaInCut_buf, tripletsBuffers_->betaInCut_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->rtLo_buf, tripletsBuffers_->rtLo_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->rtHi_buf, tripletsBuffers_->rtHi_buf, nMemHost);
#endif
alpaka::memcpy(
queue_, tripletsInCPU_->hitIndices_buf, tripletsBuffers_->hitIndices_buf, Params_T3::kHits * nMemHost);
alpaka::memcpy(
queue_, tripletsInCPU_->logicalLayers_buf, tripletsBuffers_->logicalLayers_buf, Params_T3::kLayers * nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->segmentIndices_buf, tripletsBuffers_->segmentIndices_buf, 2 * nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->betaIn_buf, tripletsBuffers_->betaIn_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->circleRadius_buf, tripletsBuffers_->circleRadius_buf, nMemHost);
alpaka::memcpy(queue_, tripletsInCPU_->nTriplets_buf, tripletsBuffers_->nTriplets_buf);
alpaka::memcpy(queue_, tripletsInCPU_->totOccupancyTriplets_buf, tripletsBuffers_->totOccupancyTriplets_buf);
if (sync)
alpaka::wait(queue_); // host consumers expect filled data
if (sync)
alpaka::wait(queue_); // host consumers expect filled data
}
}
return tripletsInCPU_.value();
return tripletsHC_->const_view<TSoA>();
}
template TripletsConst Event::getTriplets<TripletsSoA>(bool);
template TripletsOccupancyConst Event::getTriplets<TripletsOccupancySoA>(bool);

QuintupletsBuffer<alpaka_common::DevHost>& Event::getQuintuplets(bool sync) {
if (!quintupletsInCPU_) {
Expand Down
19 changes: 11 additions & 8 deletions RecoTracker/LSTCore/src/alpaka/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

#include <optional>

#include "RecoTracker/LSTCore/interface/MiniDoubletsSoA.h"
#include "RecoTracker/LSTCore/interface/SegmentsSoA.h"
#include "RecoTracker/LSTCore/interface/TrackCandidatesHostCollection.h"
#include "RecoTracker/LSTCore/interface/TripletsHostCollection.h"
#include "RecoTracker/LSTCore/interface/alpaka/Constants.h"
#include "RecoTracker/LSTCore/interface/alpaka/LST.h"
#include "RecoTracker/LSTCore/interface/alpaka/MiniDoubletsDeviceCollection.h"
#include "RecoTracker/LSTCore/interface/alpaka/SegmentsDeviceCollection.h"
#include "RecoTracker/LSTCore/interface/alpaka/TrackCandidatesDeviceCollection.h"
#include "RecoTracker/LSTCore/interface/alpaka/TripletsDeviceCollection.h"
#include "RecoTracker/LSTCore/interface/Module.h"

#include "Hit.h"
#include "Segment.h"
#include "Triplet.h"
#include "Kernels.h"
#include "Quintuplet.h"
#include "MiniDoublet.h"
#include "PixelQuintuplet.h"
#include "PixelTriplet.h"
#include "TrackCandidate.h"

#include "HeterogeneousCore/AlpakaInterface/interface/host.h"

Expand Down Expand Up @@ -49,8 +52,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
std::optional<HitsBuffer<Device>> hitsBuffers_;
std::optional<MiniDoubletsDeviceCollection> miniDoubletsDC_;
std::optional<SegmentsDeviceCollection> segmentsDC_;
std::optional<Triplets> tripletsInGPU_;
std::optional<TripletsBuffer<Device>> tripletsBuffers_;
std::optional<TripletsDeviceCollection> tripletsDC_;
std::optional<Quintuplets> quintupletsInGPU_;
std::optional<QuintupletsBuffer<Device>> quintupletsBuffers_;
std::optional<TrackCandidatesDeviceCollection> trackCandidatesDC_;
Expand All @@ -64,7 +66,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
std::optional<HitsBuffer<DevHost>> hitsInCPU_;
std::optional<MiniDoubletsHostCollection> miniDoubletsHC_;
std::optional<SegmentsHostCollection> segmentsHC_;
std::optional<TripletsBuffer<DevHost>> tripletsInCPU_;
std::optional<TripletsHostCollection> tripletsHC_;
std::optional<TrackCandidatesHostCollection> trackCandidatesHC_;
std::optional<ModulesBuffer<DevHost>> modulesInCPU_;
std::optional<QuintupletsBuffer<DevHost>> quintupletsInCPU_;
Expand Down Expand Up @@ -185,7 +187,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
typename TSoA::ConstView getMiniDoublets(bool sync = true);
template <typename TSoA, typename TDev = Device>
typename TSoA::ConstView getSegments(bool sync = true);
TripletsBuffer<DevHost>& getTriplets(bool sync = true);
template <typename TSoA, typename TDev = Device>
typename TSoA::ConstView getTriplets(bool sync = true);
QuintupletsBuffer<DevHost>& getQuintuplets(bool sync = true);
PixelTripletsBuffer<DevHost>& getPixelTriplets(bool sync = true);
PixelQuintupletsBuffer<DevHost>& getPixelQuintuplets(bool sync = true);
Expand Down
10 changes: 5 additions & 5 deletions RecoTracker/LSTCore/src/alpaka/NeuralNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include "RecoTracker/LSTCore/interface/alpaka/Constants.h"
#include "RecoTracker/LSTCore/interface/Module.h"
#include "RecoTracker/LSTCore/interface/MiniDoubletsSoA.h"
#include "RecoTracker/LSTCore/interface/SegmentsSoA.h"
#include "RecoTracker/LSTCore/interface/TripletsSoA.h"

#include "NeuralNetworkWeights.h"
#include "Segment.h"
#include "MiniDoublet.h"
#include "Hit.h"
#include "Triplet.h"

namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {

Expand All @@ -19,7 +19,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
Modules const& modulesInGPU,
MiniDoubletsConst mds,
SegmentsConst segments,
Triplets const& tripletsInGPU,
TripletsConst triplets,
const float* xVec,
const float* yVec,
const unsigned int* mdIndices,
Expand Down Expand Up @@ -59,7 +59,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE::lst {
layer2_adjustment = 1; // get upper segment to be in second layer
}
unsigned int md_idx_for_t5_eta_phi =
segments.mdIndices()[tripletsInGPU.segmentIndices[2 * innerTripletIndex]][layer2_adjustment];
segments.mdIndices()[triplets.segmentIndices()[innerTripletIndex][0]][layer2_adjustment];
bool is_endcap1 = (modulesInGPU.subdets[lowerModuleIndex1] == 4); // true if anchor hit 1 is in the endcap
bool is_endcap2 = (modulesInGPU.subdets[lowerModuleIndex2] == 4); // true if anchor hit 2 is in the endcap
bool is_endcap3 = (modulesInGPU.subdets[lowerModuleIndex3] == 4); // true if anchor hit 3 is in the endcap
Expand Down
Loading

0 comments on commit 1bbe663

Please sign in to comment.