Skip to content

Commit

Permalink
More constness, removal of vector copies, and movement of general-pur…
Browse files Browse the repository at this point in the history
…pose alpaka functions
  • Loading branch information
VourMa committed Jul 26, 2024
1 parent 77e6b9a commit 9268f49
Show file tree
Hide file tree
Showing 30 changed files with 863 additions and 815 deletions.
29 changes: 29 additions & 0 deletions HeterogeneousCore/AlpakaInterface/interface/binarySearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef HeterogeneousCore_AlpakaInterface_interface_binarySearch_h
#define HeterogeneousCore_AlpakaInterface_interface_binarySearch_h

namespace cms::alpakatools {

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE int binary_search(const unsigned int* data, // Array that we are searching over
const unsigned int search_val, // Value we want to find in data array
const unsigned int ndata) // Number of elements in data array
{
unsigned int low = 0;
unsigned int high = ndata - 1;

while (low <= high) {
unsigned int mid = (low + high) / 2;
unsigned int test_val = data[mid];
if (test_val == search_val)
return mid;
else if (test_val > search_val)
high = mid - 1;
else
low = mid + 1;
}
// Couldn't find search value in array.
return -1;
};

} // namespace cms::alpakatools

#endif // HeterogeneousCore_AlpakaInterface_interface_binarySearch_h
57 changes: 57 additions & 0 deletions HeterogeneousCore/AlpakaInterface/interface/geomFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef HeterogeneousCore_AlpakaInterface_interface_geomFunctions_h
#define HeterogeneousCore_AlpakaInterface_interface_geomFunctions_h

namespace cms::alpakatools {

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float eta(TAcc const& acc, const float x, const float y, const float z) {
float r3 = alpaka::math::sqrt(acc, x * x + y * y + z * z);
float rt = alpaka::math::sqrt(acc, x * x + y * y);
float eta = ((z > 0) - (z < 0)) * alpaka::math::acosh(acc, r3 / rt);
return eta;
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float phi_mpi_pi(TAcc const& acc, const float x) {
if (alpaka::math::abs(acc, x) <= float(M_PI))
return x;

constexpr float o2pi = 1.f / (2.f * float(M_PI));
float n = alpaka::math::round(acc, x * o2pi);
return x - n * float(2.f * float(M_PI));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float phi(TAcc const& acc, const float x, const float y) {
return phi_mpi_pi(acc, float(M_PI) + alpaka::math::atan2(acc, -y, -x));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float deltaPhi(TAcc const& acc, const float x1, const float y1, const float x2, const float y2) {
float phi1 = phi(acc, x1, y1);
float phi2 = phi(acc, x2, y2);
return phi_mpi_pi(acc, (phi2 - phi1));
};

template <typename TAcc>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE float deltaPhiChange(TAcc const& acc, const float x1, const float y1, const float x2, const float y2) {
return deltaPhi(acc, x1, y1, x2 - x1, y2 - y1);
};

ALPAKA_FN_ACC ALPAKA_FN_INLINE float calculate_dPhi(const float phi1, const float phi2) {
// Calculate dPhi
float dPhi = phi1 - phi2;

// Normalize dPhi to be between -pi and pi
if (dPhi > float(M_PI)) {
dPhi -= 2 * float(M_PI);
} else if (dPhi < -float(M_PI)) {
dPhi += 2 * float(M_PI);
}

return dPhi;
};

} // namespace cms::alpakatools

#endif // HeterogeneousCore_AlpakaInterface_interface_geomFunctions_h
13 changes: 9 additions & 4 deletions RecoTracker/LST/interface/LSTOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
class LSTOutput {
public:
LSTOutput() = default;
LSTOutput(std::vector<std::vector<unsigned int>> hitIdx,
std::vector<unsigned int> len,
std::vector<int> seedIdx,
std::vector<short> trackCandidateType) {
LSTOutput(std::vector<std::vector<unsigned int>> const& hitIdx,
std::vector<unsigned int> const& len,
std::vector<int> const& seedIdx,
std::vector<short> const& trackCandidateType) {
hitIdx_ = std::move(hitIdx);
len_ = std::move(len);
seedIdx_ = std::move(seedIdx);
Expand All @@ -21,9 +21,14 @@ class LSTOutput {

enum LSTTCType { T5 = 4, pT3 = 5, pT5 = 7, pLS = 8 };

// Hit indices of each of the LST track candidates.
std::vector<std::vector<unsigned int>> const& hitIdx() const { return hitIdx_; }
// Number of hits of each of the LST track candidates.
std::vector<unsigned int> const& len() const { return len_; }
// Index of the pixel track associated to each of the LST track candidates.
// If not associated to a pixel track, which is the case for T5s, it defaults to -1.
std::vector<int> const& seedIdx() const { return seedIdx_; }
// LSTTCType as per the enum above.
std::vector<short> const& trackCandidateType() const { return trackCandidateType_; }

private:
Expand Down
10 changes: 5 additions & 5 deletions RecoTracker/LST/interface/LSTPhase2OTHitsInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
class LSTPhase2OTHitsInput {
public:
LSTPhase2OTHitsInput() = default;
LSTPhase2OTHitsInput(std::vector<unsigned int> detId,
std::vector<float> x,
std::vector<float> y,
std::vector<float> z,
std::vector<TrackingRecHit const*> hits) {
LSTPhase2OTHitsInput(std::vector<unsigned int> const& detId,
std::vector<float> const& x,
std::vector<float> const& y,
std::vector<float> const& z,
std::vector<TrackingRecHit const*> const& hits) {
detId_ = std::move(detId);
x_ = std::move(x);
y_ = std::move(y);
Expand Down
30 changes: 15 additions & 15 deletions RecoTracker/LST/interface/LSTPixelSeedInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
class LSTPixelSeedInput {
public:
LSTPixelSeedInput() = default;
LSTPixelSeedInput(std::vector<float> px,
std::vector<float> py,
std::vector<float> pz,
std::vector<float> dxy,
std::vector<float> dz,
std::vector<float> ptErr,
std::vector<float> etaErr,
std::vector<float> stateTrajGlbX,
std::vector<float> stateTrajGlbY,
std::vector<float> stateTrajGlbZ,
std::vector<float> stateTrajGlbPx,
std::vector<float> stateTrajGlbPy,
std::vector<float> stateTrajGlbPz,
std::vector<int> q,
std::vector<std::vector<int>> hitIdx) {
LSTPixelSeedInput(std::vector<float> const& px,
std::vector<float> const& py,
std::vector<float> const& pz,
std::vector<float> const& dxy,
std::vector<float> const& dz,
std::vector<float> const& ptErr,
std::vector<float> const& etaErr,
std::vector<float> const& stateTrajGlbX,
std::vector<float> const& stateTrajGlbY,
std::vector<float> const& stateTrajGlbZ,
std::vector<float> const& stateTrajGlbPx,
std::vector<float> const& stateTrajGlbPy,
std::vector<float> const& stateTrajGlbPz,
std::vector<int> const& q,
std::vector<std::vector<int>> const& hitIdx) {
px_ = std::move(px);
py_ = std::move(py);
pz_ = std::move(pz);
Expand Down
21 changes: 14 additions & 7 deletions RecoTracker/LST/plugins/LSTPhase2OTHitsInputProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ void LSTPhase2OTHitsInputProducer::produce(edm::StreamID iID, edm::Event& iEvent

// Vector definitions
std::vector<unsigned int> ph2_detId;
ph2_detId.reserve(phase2OTHits.dataSize());
std::vector<float> ph2_x;
ph2_x.reserve(phase2OTHits.dataSize());
std::vector<float> ph2_y;
ph2_y.reserve(phase2OTHits.dataSize());
std::vector<float> ph2_z;
ph2_z.reserve(phase2OTHits.dataSize());
std::vector<TrackingRecHit const*> ph2_hits;
ph2_hits.reserve(phase2OTHits.dataSize());

for (auto it = phase2OTHits.begin(); it != phase2OTHits.end(); it++) {
const DetId hitId = it->detId();
for (auto hit = it->begin(); hit != it->end(); hit++) {
for (auto const& it : phase2OTHits) {
//for (auto it = phase2OTHits.begin(); it != phase2OTHits.end(); it++) {
const DetId hitId = it.detId();
for (auto const& hit : it) {
//for (auto hit = it->begin(); hit != it->end(); hit++) {
ph2_detId.push_back(hitId.rawId());
ph2_x.push_back(hit->globalPosition().x());
ph2_y.push_back(hit->globalPosition().y());
ph2_z.push_back(hit->globalPosition().z());
ph2_hits.push_back(hit);
ph2_x.push_back(hit.globalPosition().x());
ph2_y.push_back(hit.globalPosition().y());
ph2_z.push_back(hit.globalPosition().z());
ph2_hits.push_back(&hit);
}
}

Expand Down
2 changes: 1 addition & 1 deletion RecoTracker/LSTCore/interface/EndcapGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace lst {

void load(std::string const&);
void fillGeoMapArraysExplicit();
float getdxdy_slope(unsigned int detid) const;
float getdxdy_slope(const unsigned int detid) const;
};
} // namespace lst

Expand Down
8 changes: 4 additions & 4 deletions RecoTracker/LSTCore/interface/EndcapGeometryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace lst {
const float* geoMapPhi;

template <typename TBuff>
void setData(const TBuff& buf) {
void setData(TBuff const& buf) {
geoMapDetId = alpaka::getPtrNative(buf.geoMapDetId_buf);
geoMapPhi = alpaka::getPtrNative(buf.geoMapPhi_buf);
}
Expand All @@ -30,20 +30,20 @@ namespace lst {
Buf<TDev, float> geoMapPhi_buf;
EndcapGeometryDev data_;

EndcapGeometryBuffer(TDev const& dev, unsigned int nEndCapMap)
EndcapGeometryBuffer(TDev const& dev, const unsigned int nEndCapMap)
: geoMapDetId_buf(allocBufWrapper<unsigned int>(dev, nEndCapMap)),
geoMapPhi_buf(allocBufWrapper<float>(dev, nEndCapMap)) {
data_.setData(*this);
}

template <typename TQueue, typename TDevSrc>
inline void copyFromSrc(TQueue queue, const EndcapGeometryBuffer<TDevSrc>& src) {
inline void copyFromSrc(TQueue queue, EndcapGeometryBuffer<TDevSrc> const& src) {
alpaka::memcpy(queue, geoMapDetId_buf, src.geoMapDetId_buf);
alpaka::memcpy(queue, geoMapPhi_buf, src.geoMapPhi_buf);
}

template <typename TQueue, typename TDevSrc>
EndcapGeometryBuffer(TQueue queue, const EndcapGeometryBuffer<TDevSrc>& src, unsigned int nEndCapMap)
EndcapGeometryBuffer(TQueue queue, EndcapGeometryBuffer<TDevSrc> const& src, const unsigned int nEndCapMap)
: EndcapGeometryBuffer(alpaka::getDev(queue), nEndCapMap) {
copyFromSrc(queue, src);
}
Expand Down
50 changes: 25 additions & 25 deletions RecoTracker/LSTCore/interface/LST.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ namespace lst {

template <typename TQueue>
void run(TQueue& queue,
bool verbose,
const LSTESData<alpaka::Dev<TAcc>>* deviceESData,
const std::vector<float> see_px,
const std::vector<float> see_py,
const std::vector<float> see_pz,
const std::vector<float> see_dxy,
const std::vector<float> see_dz,
const std::vector<float> see_ptErr,
const std::vector<float> see_etaErr,
const std::vector<float> see_stateTrajGlbX,
const std::vector<float> see_stateTrajGlbY,
const std::vector<float> see_stateTrajGlbZ,
const std::vector<float> see_stateTrajGlbPx,
const std::vector<float> see_stateTrajGlbPy,
const std::vector<float> see_stateTrajGlbPz,
const std::vector<int> see_q,
const std::vector<std::vector<int>> see_hitIdx,
const std::vector<unsigned int> ph2_detId,
const std::vector<float> ph2_x,
const std::vector<float> ph2_y,
const std::vector<float> ph2_z,
bool no_pls_dupclean,
bool tc_pls_triplets);
const bool verbose,
LSTESData<alpaka::Dev<TAcc>> const* deviceESData,
std::vector<float> const& see_px,
std::vector<float> const& see_py,
std::vector<float> const& see_pz,
std::vector<float> const& see_dxy,
std::vector<float> const& see_dz,
std::vector<float> const& see_ptErr,
std::vector<float> const& see_etaErr,
std::vector<float> const& see_stateTrajGlbX,
std::vector<float> const& see_stateTrajGlbY,
std::vector<float> const& see_stateTrajGlbZ,
std::vector<float> const& see_stateTrajGlbPx,
std::vector<float> const& see_stateTrajGlbPy,
std::vector<float> const& see_stateTrajGlbPz,
std::vector<int> const& see_q,
std::vector<std::vector<int>> const& see_hitIdx,
std::vector<unsigned int> const& ph2_detId,
std::vector<float> const& ph2_x,
std::vector<float> const& ph2_y,
std::vector<float> const& ph2_z,
const bool no_pls_dupclean,
const bool tc_pls_triplets);
const std::vector<std::vector<unsigned int>>& hits() const { return out_tc_hitIdxs_; }
const std::vector<unsigned int>& len() const { return out_tc_len_; }
const std::vector<int>& seedIdx() const { return out_tc_seedIdx_; }
Expand Down Expand Up @@ -74,8 +74,8 @@ namespace lst {
void getOutput(lst::Event<TAcc>& event);
std::vector<unsigned int> getHitIdxs(const short trackCandidateType,
const unsigned int TCIdx,
const unsigned int* TCHitIndices,
const unsigned int* hitIndices);
unsigned int const* TCHitIndices,
unsigned int const* hitIndices);

// Input and output vectors
std::vector<float> in_trkX_;
Expand Down
14 changes: 7 additions & 7 deletions RecoTracker/LSTCore/interface/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace lst {
const int* sdlLayers;
const unsigned int* connectedPixels;

static bool parseIsInverted(short subdet, short side, short module, short layer) {
static bool parseIsInverted(const short subdet, const short side, const short module, const short layer) {
if (subdet == Endcap) {
if (side == NegZ) {
return module % 2 == 1;
Expand Down Expand Up @@ -74,16 +74,16 @@ namespace lst {
}
};

static bool parseIsLower(bool isInvertedx, unsigned int detId) {
static bool parseIsLower(const bool isInvertedx, const unsigned int detId) {
return (isInvertedx) ? !(detId & 1) : (detId & 1);
};

static unsigned int parsePartnerModuleId(unsigned int detId, bool isLowerx, bool isInvertedx) {
static unsigned int parsePartnerModuleId(const unsigned int detId, const bool isLowerx, const bool isInvertedx) {
return isLowerx ? (isInvertedx ? detId - 1 : detId + 1) : (isInvertedx ? detId + 1 : detId - 1);
};

template <typename TBuff>
void setData(const TBuff& buf) {
void setData(TBuff const& buf) {
detIds = alpaka::getPtrNative(buf.detIds_buf);
moduleMap = alpaka::getPtrNative(buf.moduleMap_buf);
mapdetId = alpaka::getPtrNative(buf.mapdetId_buf);
Expand Down Expand Up @@ -144,7 +144,7 @@ namespace lst {

Modules data_;

ModulesBuffer(TDev const& dev, unsigned int nMod, unsigned int nPixs)
ModulesBuffer(TDev const& dev, const unsigned int nMod, const unsigned int nPixs)
: detIds_buf(allocBufWrapper<unsigned int>(dev, nMod)),
moduleMap_buf(allocBufWrapper<uint16_t>(dev, nMod * max_connected_modules)),
mapdetId_buf(allocBufWrapper<unsigned int>(dev, nMod)),
Expand Down Expand Up @@ -175,7 +175,7 @@ namespace lst {
}

template <typename TQueue, typename TDevSrc>
inline void copyFromSrc(TQueue queue, const ModulesBuffer<TDevSrc>& src, bool isFull = true) {
inline void copyFromSrc(TQueue queue, ModulesBuffer<TDevSrc> const& src, const bool isFull = true) {
alpaka::memcpy(queue, detIds_buf, src.detIds_buf);
if (isFull) {
alpaka::memcpy(queue, moduleMap_buf, src.moduleMap_buf);
Expand Down Expand Up @@ -216,7 +216,7 @@ namespace lst {
}

template <typename TQueue, typename TDevSrc>
ModulesBuffer(TQueue queue, const ModulesBuffer<TDevSrc>& src, unsigned int nMod, unsigned int nPixs)
ModulesBuffer(TQueue queue, ModulesBuffer<TDevSrc> const& src, const unsigned int nMod, const unsigned int nPixs)
: ModulesBuffer(alpaka::getDev(queue), nMod, nPixs) {
copyFromSrc(queue, src);
}
Expand Down
2 changes: 1 addition & 1 deletion RecoTracker/LSTCore/interface/ModuleConnectionMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace lst {
void add(std::string const&);
void print();

const std::vector<unsigned int>& getConnectedModuleDetIds(unsigned int detid) const;
const std::vector<unsigned int>& getConnectedModuleDetIds(const unsigned int detid) const;
int size() const;
};

Expand Down
2 changes: 1 addition & 1 deletion RecoTracker/LSTCore/interface/PixelMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace lst {

int* pixelType;

PixelMap(unsigned int sizef = size_superbins)
PixelMap(const unsigned int sizef = size_superbins)
: pixelModuleIndex(0),
connectedPixelsIndex(sizef),
connectedPixelsSizes(sizef),
Expand Down
4 changes: 2 additions & 2 deletions RecoTracker/LSTCore/interface/TiltedGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace lst {

void load(std::string const&);

float getDrDz(unsigned int detid) const;
float getDxDy(unsigned int detid) const;
float getDrDz(const unsigned int detid) const;
float getDxDy(const unsigned int detid) const;
};

} // namespace lst
Expand Down
Loading

0 comments on commit 9268f49

Please sign in to comment.