From d34f5d676ba9fe39368ff8e5a7f6eb103d91d293 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 6 Nov 2022 15:38:00 -0500 Subject: [PATCH] Better encapsulate data in Scalars and Variables. --- Specter/src/Platform/OpenGL/OpenGLContext.cpp | 2 +- Specter/src/Specter/Core/Parameter.h | 29 +++++++++++++++---- Specter/src/Specter/Core/SpectrumManager.cpp | 8 ++--- Specter/src/Specter/Core/SpectrumManager.h | 4 +-- Specter/src/Specter/Physics/PhysicsLayer.cpp | 2 +- Specter/src/Specter/Utils/RandomGenerator.h | 4 +-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Specter/src/Platform/OpenGL/OpenGLContext.cpp b/Specter/src/Platform/OpenGL/OpenGLContext.cpp index 8263229..486b5e3 100644 --- a/Specter/src/Platform/OpenGL/OpenGLContext.cpp +++ b/Specter/src/Platform/OpenGL/OpenGLContext.cpp @@ -26,7 +26,7 @@ namespace Specter { int status = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); //Report graphics status - SPEC_TRACE("Loaded OpenGL with glad Init status {0}", status); + SPEC_INFO("Loaded OpenGL with glad Init status {0}", status); SPEC_INFO("Loaded OpenGL renderer"); SPEC_INFO("Vendor: {0}", glGetString(GL_VENDOR)); diff --git a/Specter/src/Specter/Core/Parameter.h b/Specter/src/Specter/Core/Parameter.h index 888b6fe..144ff64 100644 --- a/Specter/src/Specter/Core/Parameter.h +++ b/Specter/src/Specter/Core/Parameter.h @@ -64,6 +64,12 @@ namespace Specter { //in the manager. To help with this, Variables are atomics. So unlike Parameters they are implicity thread safe on read and write. //However, this does not mean they can be modified in the analysis! To the AnalysisStage they should be treated as constant, while the UI //should view them as modifiable. These are real god damn dangerous, but I think the power they offer outweighs the risk, for now. + struct VariableData + { + std::atomic value = 0.0; + bool isValid = true; + }; + class Variable { public: @@ -71,17 +77,28 @@ namespace Specter { Variable(const std::string& name); ~Variable(); - inline void SetValue(double value) { *(m_pdata) = value; } - inline double GetValue() { return *(m_pdata); } + inline void SetValue(double value) { m_pdata->value = value; } + inline double GetValue() { return m_pdata->value; } inline const std::string& GetName() { return m_name; } void SetName(const std::string& name); friend class SpectrumManager; private: - std::shared_ptr> m_pdata; + std::shared_ptr m_pdata; std::string m_name; }; + + //Similar to parameters are scalers. Scalers are basically just counters tied to specific + //data channels in an experiment. They can be quite useful for checking the status of + //various experimental components. In our implementation, simply bind a Scaler as you would do for a + //Parameter, and then call the Increment() function when the appropriate data channel is present. + struct ScalerData + { + std::atomic value; + bool isValid = true; + }; + class Scaler { public: @@ -89,15 +106,15 @@ namespace Specter { Scaler(const std::string& name); ~Scaler(); - inline void Increment() { ++(*m_pdata); } + inline void Increment() { ++(m_pdata->value); } inline const std::string& GetName() { return m_name; } - inline uint64_t GetCounts() { return *m_pdata; } + inline uint64_t GetCounts() { return m_pdata->value; } void SetName(const std::string& name); friend class SpectrumManager; private: - std::shared_ptr> m_pdata; + std::shared_ptr m_pdata; std::string m_name; }; diff --git a/Specter/src/Specter/Core/SpectrumManager.cpp b/Specter/src/Specter/Core/SpectrumManager.cpp index 4eaf79f..14ae940 100644 --- a/Specter/src/Specter/Core/SpectrumManager.cpp +++ b/Specter/src/Specter/Core/SpectrumManager.cpp @@ -260,7 +260,7 @@ namespace Specter { auto scalerIter = m_scalerMap.find(graph.second->GetScaler()); if (scalerIter != m_scalerMap.end()) { - scalerVal = *(scalerIter->second); + scalerVal = scalerIter->second->value; graph.second->UpdatePoints(step, scalerVal); } } @@ -385,7 +385,7 @@ namespace Specter { auto iter = m_varMap.find(var.GetName()); if (iter == m_varMap.end()) { - m_varMap[var.GetName()].reset(new std::atomic(0.0)); + m_varMap[var.GetName()].reset(new VariableData()); } var.m_pdata = m_varMap[var.GetName()]; } @@ -409,7 +409,7 @@ namespace Specter { auto iter = m_scalerMap.find(scaler.GetName()); if (iter == m_scalerMap.end()) { - m_scalerMap[scaler.GetName()].reset(new std::atomic(0)); + m_scalerMap[scaler.GetName()].reset(new ScalerData()); } scaler.m_pdata = m_scalerMap[scaler.GetName()]; @@ -427,7 +427,7 @@ namespace Specter { std::scoped_lock guard(m_managerMutex); for (auto& scaler : m_scalerMap) { - *(scaler.second) = 0; + scaler.second->value = 0; } } diff --git a/Specter/src/Specter/Core/SpectrumManager.h b/Specter/src/Specter/Core/SpectrumManager.h index 7c8eecd..e812e19 100644 --- a/Specter/src/Specter/Core/SpectrumManager.h +++ b/Specter/src/Specter/Core/SpectrumManager.h @@ -113,8 +113,8 @@ namespace Specter { std::unordered_map> m_histoMap; std::unordered_map> m_cutMap; std::unordered_map> m_paramMap; - std::unordered_map>> m_varMap; - std::unordered_map>> m_scalerMap; + std::unordered_map> m_varMap; + std::unordered_map> m_scalerMap; std::unordered_map> m_graphMap; HistogramArgs m_nullHistoResult; //For handling bad query diff --git a/Specter/src/Specter/Physics/PhysicsLayer.cpp b/Specter/src/Specter/Physics/PhysicsLayer.cpp index 2d40db1..7731e0c 100644 --- a/Specter/src/Specter/Physics/PhysicsLayer.cpp +++ b/Specter/src/Specter/Physics/PhysicsLayer.cpp @@ -141,7 +141,7 @@ namespace Specter { for (auto& stage : m_physStack) stage->AnalyzePhysicsEvent(event); - //Now that the analysis stack has filled all our NavParameters with data, update the histogram counts + //Now that the analysis stack has filled all our Parameters with data, update the histogram counts m_manager->UpdateHistograms(); //Invalidate all parameters to get ready for next event m_manager->InvalidateParameters(); diff --git a/Specter/src/Specter/Utils/RandomGenerator.h b/Specter/src/Specter/Utils/RandomGenerator.h index 7a03630..8f8b557 100644 --- a/Specter/src/Specter/Utils/RandomGenerator.h +++ b/Specter/src/Specter/Utils/RandomGenerator.h @@ -52,9 +52,9 @@ namespace Specter { } private: - static std::mt19937& GetGenerator() + static std::mt19937_64& GetGenerator() { - static thread_local std::mt19937 generator((std::random_device())()); + static thread_local std::mt19937_64 generator((std::random_device())()); return generator; } };