Skip to content

Commit

Permalink
fix: check if ConcurrentParam::Save has been called yet
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Sep 9, 2024
1 parent 6943f6a commit 903b18d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/iguana/algorithms/clas12/ZVertexFilter/Algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ namespace iguana::clas12 {
}

concurrent_key_t ZVertexFilter::PrepareEvent(int const runnum, concurrent_key_t key) const {
m_log->Trace("calling PrepareEvent({}, {})", runnum, key);
if(o_runnum->NeedsHashing()) {
std::hash<int> hash_ftn;
auto hash_key = hash_ftn(runnum);
if(!o_runnum->HasKey(hash_key))
Reload(runnum, hash_key);
return hash_key;
}
if(o_runnum->Load(key) != runnum)
if(o_runnum->IsEmpty() || o_runnum->Load(key) != runnum)
Reload(runnum, key);
return key;
}

void ZVertexFilter::Reload(int const runnum, concurrent_key_t key) const {
std::lock_guard<std::mutex> const lock(m_mutex); // NOTE: be sure to lock successive `ConcurrentParam::Save` calls !!!
m_log->Trace("-> calling Reload({}, {})", runnum, key);
o_runnum->Save(runnum, key);
o_zcuts->Save(GetOptionVector<double>("cuts", {GetConfig()->InRange("runs", runnum), "cuts"}), key);
// FIXME: handle PIDs
Expand Down
23 changes: 23 additions & 0 deletions src/iguana/services/ConcurrentParam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ namespace iguana {
template <typename T>
void SingleThreadParam<T>::Save(T const& value, concurrent_key_t const key)
{
this->m_empty = false;
m_value = value;
}

template <typename T>
void MemoizedParam<T>::Save(T const& value, concurrent_key_t const key)
{
std::lock_guard<std::mutex> const lock(this->m_mutex);
this->m_empty = false;
m_container.insert({key, value});
}

template <typename T>
void ThreadPoolParam<T>::Save(T const& value, concurrent_key_t const key)
{
this->m_empty = false;
throw std::runtime_error("TODO: 'threadpool' model not yet implemented");
}

Expand All @@ -102,4 +105,24 @@ namespace iguana {
throw std::runtime_error("TODO: 'threadpool' model not yet implemented");
}

// ==================================================================================
// GetSize() methods
// ==================================================================================
template <typename T>
std::size_t SingleThreadParam<T>::GetSize() const
{
return 1;
}

template <typename T>
std::size_t MemoizedParam<T>::GetSize() const
{
return m_container.size();
}

template <typename T>
std::size_t ThreadPoolParam<T>::GetSize() const
{
throw std::runtime_error("TODO: 'threadpool' model not yet implemented");
}
}
12 changes: 12 additions & 0 deletions src/iguana/services/ConcurrentParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ namespace iguana {
/// @returns true if hashing is needed
bool NeedsHashing() const { return m_needs_hashing; }

/// @returns the size of the internal data storage container
virtual std::size_t GetSize() const = 0;

/// @returns true if no value has been saved
bool IsEmpty() const { return m_empty; }

protected:

/// whether this `ConcurrentParam` needs hashing for calling `::Load` or `::Save`
Expand All @@ -48,6 +54,9 @@ namespace iguana {
/// mutex
std::mutex m_mutex;

/// whether this `ConcurrentParam` has something saved
bool m_empty{true};

};

// ==================================================================================
Expand All @@ -64,6 +73,7 @@ namespace iguana {
T const Load(concurrent_key_t const key = 0) const override;
void Save(T const& value, concurrent_key_t const key = 0) override;
bool HasKey(concurrent_key_t const key) const override;
std::size_t GetSize() const override;

private:
T m_value;
Expand All @@ -86,6 +96,7 @@ namespace iguana {
T const Load(concurrent_key_t const key = 0) const override;
void Save(T const& value, concurrent_key_t const key = 0) override;
bool HasKey(concurrent_key_t const key) const override;
std::size_t GetSize() const override;

private:
container_t m_container;
Expand All @@ -109,6 +120,7 @@ namespace iguana {
T const Load(concurrent_key_t const key = 0) const override;
void Save(T const& value, concurrent_key_t const key = 0) override;
bool HasKey(concurrent_key_t const key) const override;
std::size_t GetSize() const override;

private:
container_t m_container;
Expand Down

0 comments on commit 903b18d

Please sign in to comment.