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

Explore not storing/copying the sorted hits and hit prefetching. #208

Merged
merged 1 commit into from
May 24, 2019
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
34 changes: 15 additions & 19 deletions mkFit/HitStructures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ void LayerOfHits::SuckInHits(const HitVec &hitv)
const int size = hitv.size();
const bool is_brl = is_barrel();

#ifdef COPY_SORTED_HITS
if (m_capacity < size)
{
free_hits();
alloc_hits(1.02 * size);
}
#endif

if (!Config::usePhiQArrays)
if (Config::usePhiQArrays)
{
m_hit_phis.resize(size);
m_hit_qs.resize(size);
}
m_hit_phis.resize(size);

struct HitInfo
{
Expand All @@ -93,10 +96,10 @@ void LayerOfHits::SuckInHits(const HitVec &hitv)
};

std::vector<HitInfo> ha(size);
std::vector<udword> hit_qphiFines(size);
std::vector<udword> hit_qphiFines(size);

{
for (int i =0; i < size; ++i)
for (int i = 0; i < size; ++i)
{
auto const& h = hitv[i];

Expand All @@ -121,24 +124,12 @@ void LayerOfHits::SuckInHits(const HitVec &hitv)
{
int j = sort.GetRanks()[i];

// XXXX MT: Endcap has special check - try to get rid of this!
// Also, WTF ... this brings in holes as pos i is not filled.
// If this stays I need i_offset variable.
/* UNCOMMENT FOR DEBUGS??
if ( ! is_brl && (hitv[j].r() > m_qmax || hitv[j].r() < m_qmin))
{
printf("LayerOfHits::SuckInHits WARNING hit out of r boundary of disk\n"
" layer %d hit %d hit_r %f limits (%f, %f)\n",
layer_id(), j, hitv[j].r(), m_qmin, m_qmax);
// Figure out of this needs to stay ... and fix it
// --m_size;
// continue;
}
*/

// Could fix the mis-sorts. Set ha size to size + 1 and fake last entry to avoid ifs.

#ifdef COPY_SORTED_HITS
memcpy(&m_hits[i], &hitv[j], sizeof(Hit));
#endif

if (Config::usePhiQArrays)
{
m_hit_phis[i] = ha[j].phi;
Expand All @@ -159,6 +150,11 @@ void LayerOfHits::SuckInHits(const HitVec &hitv)
m_phi_bin_infos[q_bin][phi_bin].second++;
}

#ifndef COPY_SORTED_HITS
delete [] m_hit_ranks;
m_hit_ranks = sort.RelinquishRanks();
m_ext_hits = & hitv;
#endif

// Check for mis-sorts due to lost precision (not really important).
// float phi_prev = 0;
Expand Down
41 changes: 32 additions & 9 deletions mkFit/HitStructures.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ inline bool sortTrksByPhiMT(const Track& t1, const Track& t2)
// Note: the same code is used for barrel and endcap. In barrel the longitudinal
// bins are in Z and in endcap they are in R -- here this coordinate is called Q

// When not defined, hits are accessed from the original hit vector and
// only sort ranks are kept for proper access.
//
//#define COPY_SORTED_HITS

class LayerOfHits
{
private:
#ifdef COPY_SORTED_HITS
Hit *m_hits = 0;
int m_capacity = 0;
#else
unsigned int *m_hit_ranks = 0; // allocated by IceSort via new []
const HitVec *m_ext_hits;
#endif

public:
const LayerInfo *m_layer_info = 0;
Hit *m_hits = 0;
vecvecPhiBinInfo_t m_phi_bin_infos;
std::vector<float> m_hit_phis;
std::vector<float> m_hit_qs;

float m_qmin, m_qmax, m_fq;
int m_nq = 0;
int m_capacity = 0;

int layer_id() const { return m_layer_info->m_layer_id; }
bool is_barrel() const { return m_layer_info->is_barrel(); }
Expand Down Expand Up @@ -137,24 +149,21 @@ class LayerOfHits

protected:

void setup_bins(float qmin, float qmax, float dq);

#ifdef COPY_SORTED_HITS
void alloc_hits(int size)
{
m_hits = (Hit*) _mm_malloc(sizeof(Hit) * size, 64);
m_capacity = size;
for (int ihit = 0; ihit < m_capacity; ihit++){m_hits[ihit] = Hit();}
if (Config::usePhiQArrays)
{
m_hit_phis.resize(size);
m_hit_qs .resize(size);
}
}

void free_hits()
{
_mm_free(m_hits);
}
#endif

void setup_bins(float qmin, float qmax, float dq);

void set_phi_bin(int q_bin, int phi_bin, uint16_t &hit_count, uint16_t &hits_in_bin)
{
Expand Down Expand Up @@ -184,7 +193,11 @@ class LayerOfHits

~LayerOfHits()
{
#ifdef COPY_SORTED_HITS
free_hits();
#else
delete [] m_hit_ranks;
#endif
}

void SetupLayer(const LayerInfo &li);
Expand All @@ -207,6 +220,16 @@ class LayerOfHits

void SuckInHits(const HitVec &hitv);

#ifdef COPY_SORTED_HITS
int GetHitIndex(int i) const { return i; }
const Hit& GetHit(int i) const { return m_hits[i]; }
const Hit* GetHitArray() const { return m_hits; }
#else
int GetHitIndex(int i) const { return m_hit_ranks[i]; }
const Hit& GetHit(int i) const { return (*m_ext_hits)[m_hit_ranks[i]]; }
const Hit* GetHitArray() const { return & (*m_ext_hits)[0]; }
#endif

void SelectHitIndices(float q, float phi, float dq, float dphi, std::vector<int>& idcs, bool isForSeeding=false, bool dump=false);

void PrintBins();
Expand Down
16 changes: 16 additions & 0 deletions mkFit/Ice/IceRevisitedRadix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ RadixSort::~RadixSort()
DELETEARRAY(mRanks);
}

//----------------------------------------------------------------------
/**
* Detach mRanks. After this the caller is responsible for
* freeing this array via delete [] operator.
*/
//----------------------------------------------------------------------
udword* RadixSort::RelinquishRanks()
{
udword *ranks = mRanks;
mRanks = 0;
DELETEARRAY(mRanks2);
mCurrentSize = 0;
return ranks;
}


//----------------------------------------------------------------------
/**
* Resizes the inner lists.
Expand Down
4 changes: 4 additions & 0 deletions mkFit/Ice/IceRevisitedRadix.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class RadixSort
//i.e. in the order you may further process your data
const udword* GetRanks() const { return mRanks; }

//! Detach mRanks. After this the caller is responsible for
//! freeing this array via delete [] operator.
udword* RelinquishRanks();

//! mIndices2 gets trashed on calling the sort routine, but
//otherwise you can recycle it the way you want.
udword* GetRecyclable() const { return mRanks2; }
Expand Down
25 changes: 13 additions & 12 deletions mkFit/MkBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,10 @@ void MkBuilder::find_seeds()
// XXMT4K ... configurable input layers ... or hardcode something else for endcap.
// Could come from TrackerInfo ...
// But what about transition ... TrackerInfo as well or arbitrary combination of B/E seed layers ????
const Hit * lay0hits = m_event_of_hits.m_layers_of_hits[0].m_hits;
const Hit * lay1hits = m_event_of_hits.m_layers_of_hits[1].m_hits;
const Hit * lay2hits = m_event_of_hits.m_layers_of_hits[2].m_hits;

const LayerOfHits &loh0 = m_event_of_hits.m_layers_of_hits[0];
const LayerOfHits &loh1 = m_event_of_hits.m_layers_of_hits[1];
const LayerOfHits &loh2 = m_event_of_hits.m_layers_of_hits[2];

// make seed tracks
TrackVec & seedtracks = m_event->seedTracks_;
Expand All @@ -624,9 +625,9 @@ void MkBuilder::find_seeds()
seedtrack.setLabel(iseed);

// use to set charge
const Hit & hit0 = lay0hits[seed_idcs[iseed][0]];
const Hit & hit1 = lay1hits[seed_idcs[iseed][1]];
const Hit & hit2 = lay2hits[seed_idcs[iseed][2]];
const Hit & hit0 = loh0.GetHit(seed_idcs[iseed][0]);
const Hit & hit1 = loh1.GetHit(seed_idcs[iseed][1]);
const Hit & hit2 = loh2.GetHit(seed_idcs[iseed][2]);

seedtrack.setCharge(calculateCharge(hit0,hit1,hit2));

Expand Down Expand Up @@ -816,12 +817,12 @@ void MkBuilder::map_track_hits(TrackVec & tracks)
{
if (layer_has_hits[ilayer])
{
const auto & lof_m_hits = m_event_of_hits.m_layers_of_hits[ilayer].m_hits;
const auto &loh = m_event_of_hits.m_layers_of_hits[ilayer];
const auto size = m_event->layerHits_[ilayer].size();

for (size_t index = 0; index < size; ++index)
{
const auto mcHitID = lof_m_hits[index].mcHitID();
const auto mcHitID = loh.GetHit(index).mcHitID();
min = std::min(min, mcHitID);
max = std::max(max, mcHitID);
}
Expand All @@ -834,12 +835,12 @@ void MkBuilder::map_track_hits(TrackVec & tracks)
{
if (layer_has_hits[ilayer])
{
const auto & lof_m_hits = m_event_of_hits.m_layers_of_hits[ilayer].m_hits;
const auto &loh = m_event_of_hits.m_layers_of_hits[ilayer];
const auto size = m_event->layerHits_[ilayer].size();

for (size_t index = 0; index < size; ++index)
{
trackHitMap[lof_m_hits[index].mcHitID()-min] = index;
trackHitMap[loh.GetHit(index).mcHitID()-min] = index;
}
}
}
Expand Down Expand Up @@ -902,8 +903,8 @@ void MkBuilder::remap_track_hits(TrackVec & tracks)
int hitlyr = track.getHitLyr(i);
if (hitidx >= 0)
{
const auto & lof_m_hits = m_event_of_hits.m_layers_of_hits[hitlyr].m_hits;
track.setHitIdx(i, trackHitMap[lof_m_hits[hitidx].mcHitID()-min]);
const auto & loh = m_event_of_hits.m_layers_of_hits[hitlyr];
track.setHitIdx(i, trackHitMap[loh.GetHit(hitidx).mcHitID() - min]);
}
}
}
Expand Down
Loading