Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bksearch fix #351

Merged
merged 2 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 105 additions & 10 deletions mkFit/HitStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,69 @@ struct HitMatchPair
}
};

template <class T> class CcPool
{
std::vector<T> m_mem;
std::size_t m_pos = 0;
std::size_t m_size = 0;

public:
void reset(std::size_t size)
{
if (size > m_mem.size())
m_mem.resize(size);
m_pos = 0;
m_size = size;

// printf("CcP reset to %zu\n", size);
}

CcPool(std::size_t size=0)
{
if (size) reset(size);
}

T* allocate(std::size_t n)
{
if (m_pos + n > m_size) throw std::bad_alloc();
T* ret = & m_mem[m_pos];
m_pos += n;
// printf("CcP alloc %zu\n", n);
return ret;
}

void deallocate(T *p, std::size_t n) noexcept
{
// we do not care, implied on reset().
// printf("CcP dealloc %zu\n", n);
}
};

template <class T> class CcAlloc
{
CcPool<T> *m_pool;

public:
typedef T value_type;

CcAlloc(CcPool<T> *p) : m_pool(p) {}

const void* pool_id() const { return m_pool; }

T* allocate(std::size_t n)
{
return m_pool->allocate(n);
}

void deallocate(T *p, std::size_t n) noexcept
{
m_pool->deallocate(p, n);
}
};

template <class T, class U>
bool operator==(const CcAlloc<T> &a, const CcAlloc<U> &b) { return a.pool_id() == b.pool_id(); }


//------------------------------------------------------------------------------

Expand Down Expand Up @@ -488,9 +551,11 @@ inline float getScoreCand(const TrackCand& cand1, bool penalizeTailMissHits=fals

// This inheritance sucks but not doing it will require more changes.

class CombCandidate : public std::vector<TrackCand>
class CombCandidate : public std::vector<TrackCand, CcAlloc<TrackCand>>
{
public:
using allocator_type = CcAlloc<TrackCand>;

enum SeedState_e { Dormant = 0, Finding, Finished };

TrackCand m_best_short_cand;
Expand All @@ -510,13 +575,31 @@ class CombCandidate : public std::vector<TrackCand>
std::vector<HitMatchPair> m_overlap_hits; // XXXX HitMatchPair could be a member in TrackCand


CombCandidate() :
CombCandidate(const allocator_type& alloc) :
std::vector<TrackCand, CcAlloc<TrackCand>>(alloc),
m_state(Dormant), m_pickup_layer(-1)
{}

// Need this so resize of EventOfCombinedCandidates::m_candidates can reuse vectors used here.
// Required by std::uninitialized_fill_n when declaring vector<CombCandidate> in EventOfCombCandidates
CombCandidate(const CombCandidate& o) :
std::vector<TrackCand, CcAlloc<TrackCand>>(o),
m_state(o.m_state),
m_pickup_layer(o.m_pickup_layer),
m_lastHitIdx_before_bkwsearch(o.m_lastHitIdx_before_bkwsearch),
m_nInsideMinusOneHits_before_bkwsearch(o.m_nInsideMinusOneHits_before_bkwsearch),
m_nTailMinusOneHits_before_bkwsearch(o.m_nTailMinusOneHits_before_bkwsearch),
#ifdef DUMPHITWINDOW
m_seed_algo(o.m_seed_algo),
m_seed_label(o.m_seed_label),
#endif
m_hots_size(o.m_hots_size),
m_hots(o.m_hots),
m_overlap_hits(o.m_overlap_hits)
{}

// Required for std::swap().
CombCandidate(CombCandidate&& o) :
std::vector<TrackCand>(std::move(o)),
std::vector<TrackCand, CcAlloc<TrackCand>>(std::move(o)),
m_best_short_cand(std::move(o.m_best_short_cand)),
m_state(o.m_state),
m_pickup_layer(o.m_pickup_layer),
Expand All @@ -538,13 +621,13 @@ class CombCandidate : public std::vector<TrackCand>
// for (auto &tc : *this) tc.setCombCandidate(this);
}

// Need this for std::swap when filtering EventOfCombinedCandidates::m_candidates.
// Required for std::swap when filtering EventOfCombinedCandidates::m_candidates.
// We do not call clear() on vectors as this will be done via EoCCs reset.
// Probably would be better (clearer) if there was a special function that does
// the swap in here or in EoCCs.
CombCandidate& operator=(CombCandidate&& o)
{
std::vector<TrackCand>::operator=( std::move(o) );
std::vector<TrackCand, CcAlloc<TrackCand>>::operator=( std::move(o) );
m_best_short_cand = std::move(o.m_best_short_cand);
m_state = o.m_state;
m_pickup_layer = o.m_pickup_layer;
Expand All @@ -566,8 +649,9 @@ class CombCandidate : public std::vector<TrackCand>

void Reset(int max_cands_per_seed, int expected_num_hots)
{
reserve(max_cands_per_seed); // we should never exceed this
clear();
std::vector<TrackCand, CcAlloc<TrackCand>> tmp(get_allocator());
swap(tmp);
reserve(max_cands_per_seed); // we *must* never exceed this

m_best_short_cand.setScore( getScoreWorstPossible() );

Expand Down Expand Up @@ -660,6 +744,8 @@ inline void TrackCand::addHitIdx(int hitIdx, int hitLyr, float chi2)

class EventOfCombCandidates
{
CcPool<TrackCand> m_cc_pool;

public:
std::vector<CombCandidate> m_candidates;

Expand All @@ -668,22 +754,31 @@ class EventOfCombCandidates

public:
EventOfCombCandidates(int size=0) :
m_cc_pool (),
m_candidates(),
m_capacity (0),
m_size (0)
{}

void Reset(int new_capacity, int max_cands_per_seed, int expected_num_hots = 128)
{
m_cc_pool.reset(new_capacity * max_cands_per_seed);
if (new_capacity > m_capacity)
{
m_candidates.resize(new_capacity);
CcAlloc<TrackCand> alloc(&m_cc_pool);
std::vector<CombCandidate> tmp(new_capacity, alloc);
m_candidates.swap(tmp);
m_capacity = new_capacity;
}
for (int s = 0; s < m_capacity; ++s)
for (int s = 0; s < new_capacity; ++s)
{
m_candidates[s].Reset(max_cands_per_seed, expected_num_hots);
}
for (int s = new_capacity; s < m_capacity; ++s)
{
m_candidates[s].Reset(0, 0);
}

m_size = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion mkFit/MkBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ void MkBuilder::find_tracks_load_seeds(const TrackVec &in_seeds)
// m_tracks can be used for BkFit.
m_tracks.clear();

m_event_of_comb_cands.Reset((int) in_seeds.size(), m_job->params().maxCandsPerSeed);
m_event_of_comb_cands.Reset((int) in_seeds.size(), m_job->max_max_cands());

import_seeds(in_seeds, [&](const Track& seed, int region){ m_event_of_comb_cands.InsertSeed(seed, region); });
}
Expand Down
6 changes: 5 additions & 1 deletion mkFit/MkBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class MkJob

const auto& steering_params(int i) { return m_iter_config.m_steering_params[i]; }

const auto& params() const { return m_iter_config.m_params; }
const auto& params() const { return m_iter_config.m_params; }
const auto& params_bks() const { return m_iter_config.m_backward_params; }

int max_max_cands() const
{ return std::max(params().maxCandsPerSeed, params_bks().maxCandsPerSeed); }

const std::vector<bool>* get_mask_for_layer(int layer)
{
Expand Down
12 changes: 8 additions & 4 deletions mkFit/MkFinder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1488,10 +1488,14 @@ void MkFinder::BkFitFitTracks(const EventOfHits & eventofhits,

if (CurNode[i] >= 0 && HoTNodeArr[ i ][ CurNode[i] ].m_hot.layer == layer)
{
// XXXX - this is now different - overlap info has not yet been exported

// XXXX Note if I remove this hit (i.e., m_hot.index = -1), the overlap might stay.
// Need to account for this in exportTrack().
// Skip the overlap hits -- if they exist.
// 1. Overlap hit gets placed *after* the original hit in TrackCand::exportTrack()
// which is *before* in the reverse iteration that we are doing here.
// 2. Seed-hit merging can result in more than two hits per layer.
// while (CurHit[i] > 0 && HoTArr[ i ][ CurHit[i] - 1 ].layer == layer) --CurHit[i];
while (HoTNodeArr[ i ][ CurNode[i] ].m_prev_idx >= 0 &&
HoTNodeArr[ i ][ HoTNodeArr[ i ][ CurNode[i] ].m_prev_idx ].m_hot.layer == layer)
CurNode[i] = HoTNodeArr[ i ][ CurNode[i] ].m_prev_idx;

const Hit &hit = L.GetHit( HoTNodeArr[ i ][ CurNode[i] ].m_hot.index );

Expand Down