Skip to content
Open
17 changes: 12 additions & 5 deletions src/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,17 @@ AnalyserInternalVariablePtr Analyser::AnalyserImpl::internalVariable(const Varia
return rawPtrIt->second;
}

for (const auto &internalVariable : mInternalVariables) {
if (mAnalyserModel->areEquivalentVariables(variable, internalVariable->mVariable)) {
mInternalVariableMap[rawPtr] = internalVariable;
// Two variables are equivalent if and only if they are in the same group of
// the analyser model's equivalent variables cache, which has every variable
// of the model, so an existing internal variable is found by its group.

return internalVariable;
}
auto group = mAnalyserModel->mPimpl->mEquivalentVariableCache.at(rawPtr);
auto internalVariableIt = mInternalVariableGroupMap.find(group);

if (internalVariableIt != mInternalVariableGroupMap.end()) {
mInternalVariableMap[rawPtr] = internalVariableIt->second;

return internalVariableIt->second;
}

// No internal variable exists for the given variable, so create one, track
Expand All @@ -426,6 +431,7 @@ AnalyserInternalVariablePtr Analyser::AnalyserImpl::internalVariable(const Varia
mInternalVariables.push_back(res);

mInternalVariableMap[rawPtr] = res;
mInternalVariableGroupMap[group] = res;

return res;
}
Expand Down Expand Up @@ -2323,6 +2329,7 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)

mInternalVariables.clear();
mInternalVariableMap.clear();
mInternalVariableGroupMap.clear();
mInternalEquations.clear();

mCiCnUnits.clear();
Expand Down
1 change: 1 addition & 0 deletions src/analyser_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Analyser::AnalyserImpl: public Logger::LoggerImpl

AnalyserInternalVariablePtrs mInternalVariables;
std::unordered_map<std::uintptr_t, AnalyserInternalVariablePtr> mInternalVariableMap;
std::unordered_map<size_t, AnalyserInternalVariablePtr> mInternalVariableGroupMap;
AnalyserInternalEquationPtrs mInternalEquations;

GeneratorProfilePtr mGeneratorProfile = GeneratorProfile::create();
Expand Down
88 changes: 43 additions & 45 deletions src/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

#include "libcellml/printer.h"

#include <map>
#include <regex>
#include <set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -71,42 +73,31 @@ std::string printMapVariables(const VariablePairPtr &variablePair, IdList &idLis

std::string printConnections(const ComponentMap &componentMap, const VariableMap &variableMap, IdList &idList, bool autoIds)
{
std::string connections;
ComponentMap serialisedComponentMap;
size_t componentMapIndex1 = 0;
for (auto iterPair = componentMap.begin(); iterPair < componentMap.end(); ++iterPair) {
ComponentPtr currentComponent1 = iterPair->first;
ComponentPtr currentComponent2 = iterPair->second;
ComponentPair currentComponentPair = std::make_pair(currentComponent1, currentComponent2);
// Check whether this set of connections has already been serialised.
bool pairFound = false;
for (const auto &serialisedIterPair : serialisedComponentMap) {
if (serialisedIterPair == currentComponentPair) {
pairFound = true;
break;
}
}
// Continue to the next component pair if the current pair has already been serialised.
if (pairFound) {
++componentMapIndex1;
continue;
// Group the variable equivalence pairs by their pair of parent components, in order of first appearance.
std::vector<std::vector<size_t>> groups;
std::map<std::pair<const Component *, const Component *>, size_t> groupIndex;
for (size_t index = 0; index < componentMap.size(); ++index) {
auto key = std::make_pair(componentMap[index].first.get(), componentMap[index].second.get());
auto found = groupIndex.find(key);
if (found == groupIndex.end()) {
groupIndex.emplace(key, groups.size());
groups.push_back({index});
} else {
groups[found->second].push_back(index);
}
}

std::string connections;
for (const auto &group : groups) {
ComponentPtr currentComponent1 = componentMap[group.front()].first;
ComponentPtr currentComponent2 = componentMap[group.front()].second;
std::string mappingVariables;
const VariablePairPtr &variablePair = variableMap.at(componentMapIndex1);
std::string connectionId = Variable::equivalenceConnectionId(variablePair->variable1(), variablePair->variable2());
mappingVariables += printMapVariables(variablePair, idList, autoIds);
// Check for subsequent variable equivalence pairs with the same parent components.
size_t componentMapIndex2 = componentMapIndex1 + 1;
for (auto iterPair2 = iterPair + 1; iterPair2 < componentMap.end(); ++iterPair2) {
ComponentPtr nextComponent1 = iterPair2->first;
ComponentPtr nextComponent2 = iterPair2->second;
const VariablePairPtr &variablePair2 = variableMap.at(componentMapIndex2);
if ((currentComponent1 == nextComponent1) && (currentComponent2 == nextComponent2)) {
mappingVariables += printMapVariables(variablePair2, idList, autoIds);
connectionId = Variable::equivalenceConnectionId(variablePair2->variable1(), variablePair2->variable2());
}
++componentMapIndex2;
for (size_t index : group) {
mappingVariables += printMapVariables(variableMap.at(index), idList, autoIds);
}
// The connection takes its identifier from the last variable pair of the group.
const VariablePairPtr &lastVariablePair = variableMap.at(group.back());
std::string connectionId = Variable::equivalenceConnectionId(lastVariablePair->variable1(), lastVariablePair->variable2());
// Serialise out the new connection.
connections += "<connection component_1=\"" + currentComponent1->name() + "\"";
if (currentComponent2 != nullptr) {
Expand All @@ -118,8 +109,6 @@ std::string printConnections(const ComponentMap &componentMap, const VariableMap
connections += " id=\"" + makeUniqueId(idList) + "\"";
}
connections += ">" + mappingVariables + "</connection>";
serialisedComponentMap.push_back(currentComponentPair);
++componentMapIndex1;
}

return connections;
Expand Down Expand Up @@ -160,18 +149,20 @@ std::string Printer::PrinterImpl::printMath(const std::string &math)
return "";
}

void buildMapsForComponentsVariables(const ComponentPtr &component, ComponentMap &componentMap, VariableMap &variableMap)
using VariablePairSet = std::set<std::pair<const Variable *, const Variable *>>;

void buildMapsForComponentsVariables(const ComponentPtr &component, ComponentMap &componentMap, VariableMap &variableMap,
VariablePairSet &addedPairs)
{
for (size_t i = 0; i < component->variableCount(); ++i) {
VariablePtr variable = component->variable(i);
for (size_t j = 0; j < variable->equivalentVariableCount(); ++j) {
for (size_t j = 0, n = variable->equivalentVariableCount(); j < n; ++j) {
VariablePtr equivalentVariable = variable->equivalentVariable(j);
Comment on lines +159 to 160

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measured with one variable mapped to N components: printing took 1.7 / 12 / 94 s at N = 1000 / 2000 / 4000. Rather than expose the private helper (we're keeping variable.h unchanged), 5a167b9 makes equivalentVariable(i) and equivalentVariableCount() constant time: a deleted variable marks each of its equivalent variables, and unmarked variables index their list directly. Printing now takes 0.08 / 0.30 / 1.2 s, and output is unchanged.

VariablePairPtr variablePair = VariablePair::create(variable, equivalentVariable);
auto pairFound = std::find_if(variableMap.begin(), variableMap.end(),
[variable, equivalentVariable](const VariablePairPtr &in) {
return (in->variable1() == equivalentVariable) && (in->variable2() == variable);
});
if (pairFound == variableMap.end()) {
// Skip the pair if its reverse is already in the VariableMap.
bool pairFound = addedPairs.count(std::make_pair(equivalentVariable.get(), variable.get())) != 0;
if (!pairFound) {
addedPairs.emplace(variable.get(), equivalentVariable.get());
// Add new unique variable equivalence pair to the VariableMap.
variableMap.push_back(variablePair);
// Get parent components.
Expand All @@ -185,15 +176,22 @@ void buildMapsForComponentsVariables(const ComponentPtr &component, ComponentMap
}
}

void buildMaps(const ComponentEntityPtr &componentEntity, ComponentMap &componentMap, VariableMap &variableMap)
void buildMaps(const ComponentEntityPtr &componentEntity, ComponentMap &componentMap, VariableMap &variableMap,
VariablePairSet &addedPairs)
{
for (size_t i = 0; i < componentEntity->componentCount(); ++i) {
ComponentPtr component = componentEntity->component(i);
buildMapsForComponentsVariables(component, componentMap, variableMap);
buildMaps(component, componentMap, variableMap);
buildMapsForComponentsVariables(component, componentMap, variableMap, addedPairs);
buildMaps(component, componentMap, variableMap, addedPairs);
}
}

void buildMaps(const ComponentEntityPtr &componentEntity, ComponentMap &componentMap, VariableMap &variableMap)
{
VariablePairSet addedPairs;
buildMaps(componentEntity, componentMap, variableMap, addedPairs);
}

std::string Printer::PrinterImpl::printUnits(const UnitsPtr &units, IdList &idList, bool autoIds)
{
std::string repr;
Expand Down
66 changes: 40 additions & 26 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include <numeric>
#include <set>
#include <sstream>
#include <unordered_set>
#include <vector>

#include "libcellml/analyserequation.h"
Expand Down Expand Up @@ -1015,6 +1016,41 @@ std::string makeUniqueId(IdList &idList)
return id;
}

/**
* @brief Find the first variable of a component in a variable's equivalence set.
*
* Visits the equivalence set depth first, in the order of each variable's equivalent variables, and stops at the first
* variable owned by @p component. A visited set gives constant-time membership tests, so the search is O(n) in the size
* of the equivalence set (e.g., a time variable that is equivalent across every component of a large model).
*/
VariablePtr firstEquivalentVariableInComponent(const VariablePtr &variable, const ComponentPtr &component,
std::unordered_set<const Variable *> &visited)
{
for (size_t i = 0, n = variable->equivalentVariableCount(); i < n; ++i) {
auto equivalentVariable = variable->equivalentVariable(i);
Comment on lines +1029 to +1030

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measured with one variable mapped to N components: printing took 1.7 / 12 / 94 s at N = 1000 / 2000 / 4000. Rather than expose the private helper (we're keeping variable.h unchanged), 5a167b9 makes equivalentVariable(i) and equivalentVariableCount() constant time: a deleted variable marks each of its equivalent variables, and unmarked variables index their list directly. Printing now takes 0.08 / 0.30 / 1.2 s, and output is unchanged.

if (visited.insert(equivalentVariable.get()).second) {
if (owningComponent(equivalentVariable) == component) {
return equivalentVariable;
}
auto found = firstEquivalentVariableInComponent(equivalentVariable, component, visited);
if (found != nullptr) {
return found;
}
}
}

return nullptr;
}

VariablePtr firstEquivalentVariableInComponent(const VariablePtr &variable, const ComponentPtr &component)
{
if (owningComponent(variable) == component) {
return variable;
}
std::unordered_set<const Variable *> visited = {variable.get()};
return firstEquivalentVariableInComponent(variable, component, visited);
}

ConnectionMap createConnectionMap(const VariablePtr &variable1, const VariablePtr &variable2)
{
ConnectionMap map;
Expand All @@ -1024,39 +1060,17 @@ ConnectionMap createConnectionMap(const VariablePtr &variable1, const VariablePt
if ((component1 != nullptr) && (component2 != nullptr)) {
for (size_t i = 0; i < component1->variableCount(); ++i) {
auto v = component1->variable(i);
for (const auto &vEquiv : equivalentVariables(v)) {
if (owningComponent(vEquiv) == component2) {
map.insert(std::make_pair(v, vEquiv));
}
// The map keeps the first equivalent variable in component2 (v itself, then depth first).
VariablePtr vEquiv = firstEquivalentVariableInComponent(v, component2);
if (vEquiv != nullptr) {
map.insert(std::make_pair(v, vEquiv));
}
}
}

return map;
}

void recursiveEquivalentVariables(const VariablePtr &variable, std::vector<VariablePtr> &equivalentVariables)
{
for (size_t i = 0; i < variable->equivalentVariableCount(); ++i) {
VariablePtr equivalentVariable = variable->equivalentVariable(i);

if (std::find(equivalentVariables.begin(), equivalentVariables.end(), equivalentVariable) == equivalentVariables.end()) {
equivalentVariables.push_back(equivalentVariable);

recursiveEquivalentVariables(equivalentVariable, equivalentVariables);
}
}
}

std::vector<VariablePtr> equivalentVariables(const VariablePtr &variable)
{
std::vector<VariablePtr> res = {variable};

recursiveEquivalentVariables(variable, res);

return res;
}

bool linkComponentVariableUnits(const ComponentPtr &component, DescriptionList &descriptionList)
{
bool status = true;
Expand Down
13 changes: 7 additions & 6 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,16 +689,17 @@ bool areComponentVariableUnitsUnlinked(const ComponentPtr &component);
ConnectionMap createConnectionMap(const VariablePtr &variable1, const VariablePtr &variable2);

/**
* @brief Make a list of all variables equivalent to the given variable.
* @brief The first variable of @p component in the equivalence set of @p variable.
*
* Collect all the equivalent variables of the given @p variable and return
* them as a list of @ref VariablePtr.
* The equivalence set is visited from @p variable itself, then depth first in the order of each variable's equivalent
* variables, stopping at the first variable owned by @p component.
*
* @param variable The variable to find equivalent variables of.
* @param variable The variable.
* @param component The component.
*
* @return A @c std::vector of @ref VariablePtr.
* @return The first equivalent variable owned by @p component, or @c nullptr if there is none.
*/
std::vector<VariablePtr> equivalentVariables(const VariablePtr &variable);
VariablePtr firstEquivalentVariableInComponent(const VariablePtr &variable, const ComponentPtr &component);

/**
* @brief Test the given @p entities are equal to entities in @p owner.
Expand Down
Loading
Loading