Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7ad8f98
macOS: support macOS 14 and later.
agarny Apr 14, 2026
4a7d551
Improvements to the overall speed and memory use of libCellML.
agarny Apr 14, 2026
c3c5050
Improve caching of equivalent variables in analyser model.
hsorby Apr 27, 2026
6e53a5b
Merge branch 'main' into issue1368
agarny Apr 27, 2026
f67af43
Remove extra blank line in analysermodel_p.h.
hsorby Apr 27, 2026
9a9bcbc
Fix formatting of find and unite functions in analysermodel_p.h.
hsorby Apr 27, 2026
b197390
Address review comments.
hsorby May 20, 2026
678e123
Merge branch 'main' into cache-equiv-vars
hsorby May 20, 2026
84a4665
Fix code formatting in analysermodel.cpp.
hsorby May 21, 2026
e072671
Merge branch 'cache-equiv-vars' of github.com:hsorby/libcellml into c…
hsorby May 21, 2026
cdf3922
Build equivalent variable cache when AnalyserModel is created.
hsorby May 21, 2026
d01bffc
Remove duplicate build equivalent cache vars call.
hsorby May 21, 2026
1221fcd
Address review comments.
hsorby May 21, 2026
d9997b1
Merge branch 'main' into cache-equiv-vars
hsorby May 21, 2026
617f531
Merge branch 'main' into issue1368.
agarny May 25, 2026
84fa4c1
Removed our original C++20 standard code.
agarny May 25, 2026
eb5d08a
Analyser: removed the use of some sets.
agarny May 25, 2026
6164f8f
Removed the SHARED keyword from `add_library()` in `tests/CMakeLists.…
agarny May 25, 2026
45f2e1c
Merge branch 'main' into cache-equiv-vars
agarny Jun 9, 2026
f0606c5
Merge branch 'cache-equiv-vars' into issue1368
agarny Jun 9, 2026
ead0820
Don't reserve.
agarny Jun 9, 2026
7f008c1
Some minor cleaning up.
agarny Jun 9, 2026
131a762
Put back something that got mistakenly deleted during the last merge.
agarny Jun 9, 2026
85e409b
Merge branch 'main' into issue1368
agarny Jun 9, 2026
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
2 changes: 1 addition & 1 deletion .github/workflows/deploy-on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ jobs:
if [[ "macOS" == "${{ matrix.name }}" ]]; then
echo "Setting macos_archs as: macos_archs='x86_64 arm64'"
echo 'macos_archs=x86_64 arm64' >> $GITHUB_OUTPUT
echo "macos_deployment_target=15.0" >> $GITHUB_OUTPUT
echo "macos_deployment_target=14" >> $GITHUB_OUTPUT
else
echo "Setting macos_archs as: macos_archs='x86_64'"
echo "macos_archs=x86_64" >> $GITHUB_OUTPUT
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
cmake_minimum_required(VERSION 3.18.0)

if(APPLE)
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "Minimum OS X deployment version.")
set(CMAKE_OSX_DEPLOYMENT_TARGET 14 CACHE STRING "Minimum OS X deployment version.")
endif()

set(PROJECT_NAME libCellML)
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ endfunction()

if(LIBCELLML_COVERAGE)
append_target_property(cellml COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
append_target_property(cellml LINK_FLAGS "-fprofile-arcs -ftest-coverage")
target_link_options(cellml PUBLIC $<BUILD_INTERFACE:-fprofile-arcs> $<BUILD_INTERFACE:-ftest-coverage>)

# Share some paths with interested parties (tests)
set(GCOV_ANALYSIS_PATH "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cellml.dir" PARENT_SCOPE)
Expand All @@ -300,7 +300,7 @@ endif()

if(LIBCELLML_LLVM_COVERAGE)
append_target_property(cellml COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping")
append_target_property(cellml LINK_FLAGS "-fprofile-instr-generate")
target_link_options(cellml PUBLIC $<BUILD_INTERFACE:-fprofile-instr-generate>)
endif()

install(TARGETS cellml EXPORT libcellml-targets
Expand Down
258 changes: 134 additions & 124 deletions src/analyser.cpp

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions src/analyser_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "libcellml/analyser.h"
#include "libcellml/generatorprofile.h"
#include "libcellml/issue.h"

Expand Down Expand Up @@ -160,12 +161,13 @@ class Analyser::AnalyserImpl: public Logger::LoggerImpl
AnalyserExternalVariablePtrs mExternalVariables;

AnalyserInternalVariablePtrs mInternalVariables;
std::unordered_map<Variable *, AnalyserInternalVariablePtr> mInternalVariableCache;
AnalyserInternalEquationPtrs mInternalEquations;

GeneratorProfilePtr mGeneratorProfile = GeneratorProfile::create();

std::map<std::string, UnitsPtr> mStandardUnits;
std::map<AnalyserEquationAstPtr, UnitsPtr> mCiCnUnits;
std::unordered_map<std::string, UnitsPtr> mStandardUnits;
std::unordered_map<AnalyserEquationAstPtr, UnitsPtr> mCiCnUnits;

AnalyserImpl();

Expand All @@ -181,10 +183,6 @@ class Analyser::AnalyserImpl: public Logger::LoggerImpl
void analyseComponent(const ComponentPtr &component);
void analyseComponentVariables(const ComponentPtr &component);

void equivalentVariables(const VariablePtr &variable,
VariablePtrs &equivalentVariables) const;
VariablePtrs equivalentVariables(const VariablePtr &variable) const;

void analyseEquationAst(const AnalyserEquationAstPtr &ast);

void updateUnitsMapWithStandardUnit(const std::string &unitsName,
Expand Down Expand Up @@ -251,7 +249,7 @@ class Analyser::AnalyserImpl: public Logger::LoggerImpl
static bool isExternalVariable(const AnalyserInternalVariablePtr &variable);

bool isStateRateBased(const AnalyserEquationPtr &analyserEquation,
AnalyserEquationPtrs &checkedEquations);
std::unordered_set<AnalyserEquation *> &checkedEquations);

void addInvalidVariableIssue(const AnalyserInternalVariablePtr &variable,
Issue::ReferenceRule referenceRule);
Expand Down
8 changes: 4 additions & 4 deletions src/analyserequation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ size_t AnalyserEquation::stateCount() const
return mPimpl->mStates.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::states() const
const std::vector<AnalyserVariablePtr> &AnalyserEquation::states() const
{
return mPimpl->mStates;
}
Expand All @@ -168,7 +168,7 @@ size_t AnalyserEquation::computedConstantCount() const
return mPimpl->mComputedConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::computedConstants() const
const std::vector<AnalyserVariablePtr> &AnalyserEquation::computedConstants() const
{
return mPimpl->mComputedConstants;
}
Expand All @@ -187,7 +187,7 @@ size_t AnalyserEquation::algebraicVariableCount() const
return mPimpl->mAlgebraicVariables.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::algebraicVariables() const
const std::vector<AnalyserVariablePtr> &AnalyserEquation::algebraicVariables() const
{
return mPimpl->mAlgebraicVariables;
}
Expand All @@ -206,7 +206,7 @@ size_t AnalyserEquation::externalVariableCount() const
return mPimpl->mExternalVariables.size();
}

std::vector<AnalyserVariablePtr> AnalyserEquation::externalVariables() const
const std::vector<AnalyserVariablePtr> &AnalyserEquation::externalVariables() const
{
return mPimpl->mExternalVariables;
}
Expand Down
2 changes: 1 addition & 1 deletion src/analyserequationast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void AnalyserEquationAst::setType(Type type)
mPimpl->mType = type;
}

std::string AnalyserEquationAst::value() const
const std::string &AnalyserEquationAst::value() const
{
return mPimpl->mValue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/analyserexternalvariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::vector<VariablePtr>::iterator AnalyserExternalVariable::AnalyserExternalVar
const std::string &componentName,
const std::string &variableName)
{
return std::find_if(mDependencies.begin(), mDependencies.end(), [=](const auto &v) {
return std::find_if(mDependencies.begin(), mDependencies.end(), [&](const auto &v) {
return (owningModel(v) == model)
&& (owningComponent(v)->name() == componentName)
&& (v->name() == variableName);
Expand All @@ -40,7 +40,7 @@ std::vector<VariablePtr>::iterator AnalyserExternalVariable::AnalyserExternalVar

std::vector<VariablePtr>::iterator AnalyserExternalVariable::AnalyserExternalVariableImpl::findDependency(const VariablePtr &variable)
{
return std::find_if(mDependencies.begin(), mDependencies.end(), [=](const auto &v) {
return std::find_if(mDependencies.begin(), mDependencies.end(), [&](const auto &v) {
return v == variable;
});
}
Expand Down Expand Up @@ -155,7 +155,7 @@ VariablePtr AnalyserExternalVariable::dependency(const ModelPtr &model,
nullptr;
}

std::vector<VariablePtr> AnalyserExternalVariable::dependencies() const
const std::vector<VariablePtr> &AnalyserExternalVariable::dependencies() const
{
return mPimpl->mDependencies;
}
Expand Down
103 changes: 74 additions & 29 deletions src/analysermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ limitations under the License.

#include "libcellml/analysermodel.h"

#include "libcellml/analyservariable.h"

#include "analysermodel_p.h"
#include "analyservariable_p.h"
#include "utilities.h"

namespace libcellml {

static const std::vector<AnalyserEquationPtr> NO_ANALYSER_EQUATION;
static const std::vector<AnalyserVariablePtr> NO_ANALYSER_VARIABLE;

AnalyserModelPtr AnalyserModel::AnalyserModelImpl::create(const ModelPtr &model)
{
return std::shared_ptr<AnalyserModel> {new AnalyserModel(model)};
auto res = std::shared_ptr<AnalyserModel> {new AnalyserModel(model)};

if (model) {
res->mPimpl->buildEquivalentVariablesCache();
}

return res;
}

AnalyserModel::AnalyserModelImpl::AnalyserModelImpl(const ModelPtr &model)
Expand All @@ -43,6 +51,38 @@ AnalyserModel::~AnalyserModel()
delete mPimpl;
}

void AnalyserModel::AnalyserModelImpl::buildEquivalentVariablesCache(const ComponentPtr &component)
{
for (size_t i = 0; i < component->variableCount(); ++i) {
auto variable = component->variable(i);

for (size_t j = 0; j < variable->equivalentVariableCount(); ++j) {
auto equivalentVariable = variable->equivalentVariable(j);
auto v1 = reinterpret_cast<uintptr_t>(variable.get());
auto v2 = reinterpret_cast<uintptr_t>(equivalentVariable.get());

if (v2 < v1) {
std::swap(v1, v2);
}

uniteEquivalentVariableAddresses(v1, v2);
}
}

for (size_t i = 0; i < component->componentCount(); ++i) {
buildEquivalentVariablesCache(component->component(i));
}
}

void AnalyserModel::AnalyserModelImpl::buildEquivalentVariablesCache()
{
mEquivalentVariableCache.clear();

for (size_t i = 0; i < mModel->componentCount(); ++i) {
buildEquivalentVariablesCache(mModel->component(i));
}
}

bool AnalyserModel::isValid() const
{
switch (mPimpl->mType) {
Expand Down Expand Up @@ -104,10 +144,10 @@ size_t AnalyserModel::stateCount() const
return mPimpl->mStates.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::states() const
const std::vector<AnalyserVariablePtr> &AnalyserModel::states() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_VARIABLE;
}

return mPimpl->mStates;
Expand All @@ -132,10 +172,10 @@ size_t AnalyserModel::constantCount() const
return mPimpl->mConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::constants() const
const std::vector<AnalyserVariablePtr> &AnalyserModel::constants() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_VARIABLE;
}

return mPimpl->mConstants;
Expand All @@ -159,10 +199,10 @@ size_t AnalyserModel::computedConstantCount() const
return mPimpl->mComputedConstants.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::computedConstants() const
const std::vector<AnalyserVariablePtr> &AnalyserModel::computedConstants() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_VARIABLE;
}

return mPimpl->mComputedConstants;
Expand All @@ -186,10 +226,10 @@ size_t AnalyserModel::algebraicVariableCount() const
return mPimpl->mAlgebraicVariables.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::algebraicVariables() const
const std::vector<AnalyserVariablePtr> &AnalyserModel::algebraicVariables() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_VARIABLE;
}

return mPimpl->mAlgebraicVariables;
Expand All @@ -213,10 +253,10 @@ size_t AnalyserModel::externalVariableCount() const
return mPimpl->mExternalVariables.size();
}

std::vector<AnalyserVariablePtr> AnalyserModel::externalVariables() const
const std::vector<AnalyserVariablePtr> &AnalyserModel::externalVariables() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_VARIABLE;
}

return mPimpl->mExternalVariables;
Expand All @@ -237,8 +277,20 @@ AnalyserVariablePtr AnalyserModel::analyserVariable(const VariablePtr &variable)
return {};
}

// Check the cache first.

auto analyserVariableIt = mPimpl->mAnalyserVariables.find(variable.get());

if (analyserVariableIt != mPimpl->mAnalyserVariables.end()) {
return analyserVariableIt->second;
}

// Not in the cache, so do the equivalence-based search.

for (const auto &analyserVariable : analyserVariables(shared_from_this())) {
if (areEquivalentVariables(variable, analyserVariable->variable())) {
mPimpl->mAnalyserVariables.emplace(variable.get(), analyserVariable);

return analyserVariable;
}
}
Expand All @@ -255,10 +307,10 @@ size_t AnalyserModel::analyserEquationCount() const
return mPimpl->mAnalyserEquations.size();
}

std::vector<AnalyserEquationPtr> AnalyserModel::analyserEquations() const
const std::vector<AnalyserEquationPtr> &AnalyserModel::analyserEquations() const
{
if (!isValid()) {
return {};
return NO_ANALYSER_EQUATION;
}

return mPimpl->mAnalyserEquations;
Expand Down Expand Up @@ -498,25 +550,18 @@ bool AnalyserModel::areEquivalentVariables(const VariablePtr &variable1,
// turn, this means that we can speed up any feature (e.g., code generation)
// that also relies on that utility.

auto v1 = reinterpret_cast<uintptr_t>(variable1.get());
auto v2 = reinterpret_cast<uintptr_t>(variable2.get());

if (v1 > v2) {
std::swap(v1, v2);
if ((variable1 == nullptr) || (variable2 == nullptr)) {
return false;
}

auto key = AnalyserModel::AnalyserModelImpl::VariableKeyPair {v1, v2};
auto it = mPimpl->mCachedEquivalentVariables.find(key);

if (it != mPimpl->mCachedEquivalentVariables.end()) {
return it->second;
if (variable1 == variable2) {
return true;
}

auto res = libcellml::areEquivalentVariables(variable1, variable2);
const auto v1 = reinterpret_cast<uintptr_t>(variable1.get());
const auto v2 = reinterpret_cast<uintptr_t>(variable2.get());

mPimpl->mCachedEquivalentVariables.emplace(key, res);

return res;
return mPimpl->findVariableAddress(v1) == mPimpl->findVariableAddress(v2);
}

} // namespace libcellml
Loading
Loading