-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCDistributeNodesProc.cpp
207 lines (149 loc) · 4.64 KB
/
CDistributeNodesProc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// CDistributeNodesProc.cpp
//
// CDistributeNodesProce class
#include "PreComp.h"
#define CRITERIA_TAG CONSTLIT("Criteria")
#define NODE_COUNT_ATTRIB CONSTLIT("nodeCount")
CDistributeNodesProc::~CDistributeNodesProc (void)
// CDistributeNodesProc destructor
{
}
ALERROR CDistributeNodesProc::OnInitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, const CString &sUNID)
// OnInitFromXML
//
// Initialize from XML element
{
ALERROR error;
int i;
// Loop over all elements
for (i = 0; i < pDesc->GetContentElementCount(); i++)
{
CXMLElement *pItem = pDesc->GetContentElement(i);
// See if this is an element handled by our base class
if ((error = InitBaseItemXML(Ctx, pItem)) != ERR_NOTFOUND)
{
if (error != NOERROR)
return error;
}
// Otherwise, treat it as a system definition and insert it in the list
else
{
SSystemEntry *pEntry = m_Systems.Insert();
pEntry->pDesc = pItem;
}
}
// See how many nodes to distribute
if (error = m_DistCount.LoadFromXML(pDesc->GetAttribute(NODE_COUNT_ATTRIB)))
return error;
if (m_DistCount.IsEmpty())
m_DistCount.SetConstant(m_Systems.GetCount());
return NOERROR;
}
ALERROR CDistributeNodesProc::OnProcess (SProcessCtx &Ctx, CTopologyNodeList &NodeList, CString *retsError)
// OnProcess
//
// Process on topology
{
ALERROR error;
int i, j;
CTopologyNodeList NodesToDelete;
// Compute the number of nodes
int iDistCount = m_DistCount.Roll();
int iSystemCount = m_Systems.GetCount();
if (iSystemCount == 0 || iDistCount == 0)
return NOERROR;
// If we have a criteria, the filter the nodes
CTopologyNodeList FilteredNodeList;
CTopologyNodeList *pNodeList = FilterNodes(Ctx.Topology, m_Criteria, NodeList, FilteredNodeList);
if (pNodeList == NULL)
{
*retsError = CONSTLIT("Error filtering nodes");
return ERR_FAIL;
}
// We better have enough nodes
if (pNodeList->GetCount() < iDistCount)
{
*retsError = CONSTLIT("<DistributeNodes>: Not enough available nodes.");
return ERR_FAIL;
}
// If we don't have any constraints between nodes then we just
// pick random nodes from the list
if (m_Criteria.iMinInterNodeDist == 0 && m_Criteria.iMaxInterNodeDist == -1)
{
pNodeList->Shuffle();
for (i = 0; i < iDistCount; i++)
{
if (error = pNodeList->GetAt(i)->InitFromAdditionalXML(Ctx.Topology, m_Systems[i % iSystemCount].pDesc, retsError))
return error;
// Remove this node from NodeList so that it is not re-used by our callers
NodesToDelete.Insert(pNodeList->GetAt(i));
}
}
// Otherwise, we randomly pick nodes until they match the constraints
else
{
pNodeList->Shuffle();
int iNodeCount = pNodeList->GetCount();
int iOffset = 0;
TArray<int> Chosen;
Chosen.InsertEmpty(iDistCount);
int iBestMatch = -1;
TArray<int> Best;
int iTries = 100;
while (iTries-- > 0)
{
// Assign nodes in order
for (i = 0; i < iDistCount; i++)
Chosen[i] = (i + iOffset) % iNodeCount;
// Compute the minimum and maximum internode distance
int iMin = INFINITE_NODE_DIST;
int iMax = 0;
for (i = 0; i < iDistCount - 1; i++)
for (j = i + 1; j < iDistCount; j++)
{
int iDist = Ctx.Topology.GetDistance(pNodeList->GetAt(Chosen[i])->GetID(), pNodeList->GetAt(Chosen[j])->GetID());
if (iDist > iMax)
iMax = iDist;
if (iDist < iMin)
iMin = iDist;
}
// Compute how close we are to meeting the criteria (lower numbers
// are better).
int iMinDiff = (iMin < m_Criteria.iMinInterNodeDist ? m_Criteria.iMinInterNodeDist - iMin : 0);
int iMaxDiff = (m_Criteria.iMaxInterNodeDist != -1 && iMax > m_Criteria.iMaxInterNodeDist ? iMax - m_Criteria.iMaxInterNodeDist : 0);
int iMatch = (iMinDiff * iMinDiff) + (iMaxDiff * iMaxDiff);
// Even if we don't match the criteria, see if this is a better match
// than anything else.
if (iBestMatch == -1 || iMatch < iBestMatch)
{
iBestMatch = iMatch;
Best = Chosen;
}
// If we match the criteria perfectly, then we're done
if (iMatch == 0)
break;
// Otherwise, move down the line of random nodes and loop
iOffset += 1;
if (iOffset >= iNodeCount)
{
pNodeList->Shuffle();
iOffset = 0;
}
}
// Apply the systems
for (i = 0; i < iDistCount; i++)
{
if (error = pNodeList->GetAt(Best[i])->InitFromSystemXML(Ctx.Topology, m_Systems[i % iSystemCount].pDesc, retsError))
return error;
// Remove this node from NodeList so that it is not re-used by our callers
NodesToDelete.Insert(pNodeList->GetAt(Best[i]));
}
}
// Delete
if (Ctx.bReduceNodeList)
{
for (i = 0; i < NodesToDelete.GetCount(); i++)
NodeList.Delete(NodesToDelete[i]);
}
return NOERROR;
}