aboutsummaryrefslogtreecommitdiff
path: root/sdk/lowlevel/source/NvBlastFamilyGraph.h
blob: 9d2b7cf7c242c1ded65ce7199a13e5a44c7d06c2 (plain) (blame)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.


#ifndef NVBLASTFAMILYGRAPH_H
#define NVBLASTFAMILYGRAPH_H


#include "NvBlastSupportGraph.h"
#include "NvBlastFixedArray.h"
#include "NvBlastFixedBitmap.h"
#include "NvBlastFixedBoolArray.h"
#include "NvBlastMath.h"
#include "NvBlastFixedPriorityQueue.h"
#include "NvBlastMemory.h"


namespace Nv
{
namespace Blast
{


typedef uint32_t NodeIndex;
typedef NodeIndex IslandId;
typedef uint32_t ActorIndex;

/**
Internal implementation of family graph stored on the family.

It processes full NvBlastSupportGraph graph, stores additional information used for faster islands finding, 
keeps and provides access to current islandId for every node.
*/
class FamilyGraph
{
public:

	//////// ctor ////////

	/**
	Constructor. family graph is meant to be placed (with placement new) on family memory.

	\param[in] graph	The graph to instance (see SupportGraph)
	*/
	FamilyGraph(const SupportGraph* graph);


	/**
	Returns memory needed for this class (see fillMemory).

	\param[in] nodeCount	The number of nodes in the graph.
	\param[in] bondCount	The number of bonds in the graph.

	\return the number of bytes required.
	*/
	static size_t	requiredMemorySize(uint32_t nodeCount, uint32_t bondCount)
	{
		return fillMemory(nullptr, nodeCount, bondCount);
	}


	//////// API ////////

	/**
	Function to initialize graph (all nodes added to dirty list for this actor)

	\param[in] actorIndex	The index of the actor to initialize graph with. Must be in the range [0, m_nodeCount).
	\param[in] graph		The static graph data for this family.
	*/
	void			initialize(ActorIndex actorIndex, const SupportGraph* graph);

	/**
	Function to notify graph about removed edges. These nodes will be added to dirty list for this actor. Returns true if bond as removed.

	\param[in] actorIndex	The index of the actor from which the edge is removed. Must be in the range [0, m_nodeCount).
	\param[in] node0		The index of the first node of removed edge. Must be in the range [0, m_nodeCount).
	\param[in] node1		The index of the second node of removed edge. Must be in the range [0, m_nodeCount).
	\param[in] graph		The static graph data for this family.
	*/
	bool			notifyEdgeRemoved(ActorIndex actorIndex, NodeIndex node0, NodeIndex node1, const SupportGraph* graph);
	bool			notifyEdgeRemoved(ActorIndex actorIndex, NodeIndex node0, NodeIndex node1, uint32_t bondIndex, const SupportGraph* graph);

	bool			notifyNodeRemoved(ActorIndex actorIndex, NodeIndex nodeIndex, const SupportGraph* graph);

	/**
	Function to find new islands by examining dirty nodes associated with this actor (they can be associated with actor if 
	notifyEdgeRemoved() were previously called for it.

	\param[in] actorIndex	The index of the actor on which graph part (edges + nodes) findIslands will be performed. Must be in the range [0, m_nodeCount).
	\param[in] scratch		User-supplied scratch memory of size findIslandsRequiredScratch(graphNodeCount) bytes.
	\param[in] graph		The static graph data for this family.

	\return the number of new islands found.
	*/
	uint32_t		findIslands(ActorIndex actorIndex, void* scratch, const SupportGraph* graph);

	/**
	The scratch space required to call the findIslands function, in bytes.

	\param[in] graphNodeCount The number of nodes in the graph.

	\return the number of bytes required.
	*/
	static size_t	findIslandsRequiredScratch(uint32_t graphNodeCount);


	//////// data getters ////////

	/**
	Utility function to get the start of the island ids array. This is an array of size nodeCount.
	Every islandId == NodeIndex of root node in this island, it is set for every Node.

	\return the array of island ids.
	*/
	NvBlastBlockData(IslandId, m_islandIdsOffset, getIslandIds);

	/**
	Utility function to get the start of the dirty node links array. This is an array of size nodeCount.
	*/
	NvBlastBlockData(NodeIndex, m_dirtyNodeLinksOffset, getDirtyNodeLinks);

	/**
	Utility function to get the start of the first dirty node indices array. This is an array of size nodeCount.
	*/
	NvBlastBlockData(uint32_t, m_firstDirtyNodeIndicesOffset, getFirstDirtyNodeIndices);

	/**
	Utility function to get the start of the fast route array. This is an array of size nodeCount.
	*/
	NvBlastBlockData(NodeIndex, m_fastRouteOffset, getFastRoute);

	/**
	Utility function to get the start of the hop counts array. This is an array of size nodeCount.
	*/
	NvBlastBlockData(uint32_t, m_hopCountsOffset, getHopCounts);

	/**
	Utility function to get the pointer of the is edge removed bitmap. This is an bitmap of size bondCount.
	*/
	NvBlastBlockData(FixedBoolArray, m_isEdgeRemovedOffset, getIsEdgeRemoved);

	/**
	Utility function to get the pointer of the is node in dirty list bitmap. This is an bitmap of size nodeCount.
	*/
	NvBlastBlockData(FixedBoolArray, m_isNodeInDirtyListOffset, getIsNodeInDirtyList);


	//////// Debug/Test ////////

	uint32_t	getEdgesCount(const SupportGraph* graph) const;
	bool		hasEdge(NodeIndex node0, NodeIndex node1, const SupportGraph* graph) const;
	bool		canFindRoot(NodeIndex startNode, NodeIndex targetNode, FixedArray<NodeIndex>* visitedNodes, const SupportGraph* graph);


private:

	FamilyGraph& operator = (const FamilyGraph&);

	//////// internal types ////////

	/**
	Used to represent current graph traverse state.
	*/
	struct TraversalState
	{
		NodeIndex mNodeIndex;
		uint32_t mCurrentIndex;
		uint32_t mPrevIndex;
		uint32_t mDepth;

		TraversalState()
		{
		}

		TraversalState(NodeIndex nodeIndex, uint32_t currentIndex, uint32_t prevIndex, uint32_t depth) :
			mNodeIndex(nodeIndex), mCurrentIndex(currentIndex), mPrevIndex(prevIndex), mDepth(depth)
		{
		}
	};

	/**
	Queue element for graph traversal with priority queue.
	*/
	struct QueueElement
	{
		TraversalState* mState;
		uint32_t mHopCount;

		QueueElement()
		{
		}

		QueueElement(TraversalState* state, uint32_t hopCount) : mState(state), mHopCount(hopCount)
		{
		}
	};

	/**
	Queue comparator for graph traversal with priority queue.
	*/
	struct NodeComparator
	{
		NodeComparator()
		{
		}

		bool operator() (const QueueElement& node0, const QueueElement& node1) const
		{
			return node0.mHopCount < node1.mHopCount;
		}
	private:
		NodeComparator& operator = (const NodeComparator&);
	};

	/**
	PriorityQueue for graph traversal. Queue element with smallest hopCounts will be always on top.
	*/
	typedef FixedPriorityQueue<QueueElement, NodeComparator> NodePriorityQueue;


	//////// internal operations ////////

	/**
	Function calculate needed memory and feel it if familyGraph is passed. FamilyGraph is designed to use 
	memory right after itself. So it should be initialized with placement new operation	on memory of memoryNeeded() size.

	\param[in] familyGraph		The pointer to actual FamilyGraph instance which will be filled. Can be nullptr, function will only return required bytes and do nothing.
	\param[in] nodeCount		The number of nodes in the graph.
	\param[in] bondCount		The number of bonds in the graph.

	\return the number of bytes required or filled
	*/
	static size_t	fillMemory(FamilyGraph* familyGraph, uint32_t nodeCount, uint32_t bondCount);

	/**
	Function to find route from on node to another. It uses fastPath first as optimization and then if it fails it performs brute-force traverse (with hop count heuristic)
	*/
	bool			findRoute(NodeIndex startNode, NodeIndex targetNode, IslandId islandId, FixedArray<TraversalState>* visitedNodes, FixedBitmap* isNodeWitness, NodePriorityQueue* priorityQueue, const SupportGraph* graph);

	/**
	Function to try finding targetNode (from startNode) with getFastRoute().
	*/
	bool			tryFastPath(NodeIndex startNode, NodeIndex targetNode, IslandId islandId, FixedArray<TraversalState>* visitedNodes, FixedBitmap* isNodeWitness, const SupportGraph* graph);

	/**
	Function to unwind route upon successful finding of root node or witness.
	We have found either a witness *or* the root node with this traversal. In the event of finding the root node, hopCount will be 0. In the event of finding
	a witness, hopCount will be the hopCount that witness reported as being the distance to the root.
	*/
	void			unwindRoute(uint32_t traversalIndex, NodeIndex lastNode, uint32_t hopCount, IslandId id, FixedArray<TraversalState>* visitedNodes);

	/**
	Function to add node to dirty node list associated with actor.
	*/
	void			addToDirtyNodeList(ActorIndex actorIndex, NodeIndex node);

	/**
	Function used to get adjacentNode using index from adjacencyPartition with check for bondHealths (if it's not removed already)
	*/
	NodeIndex		getAdjacentNode(uint32_t adjacencyIndex, const SupportGraph* graph) const
	{
		const uint32_t bondIndex = graph->getAdjacentBondIndices()[adjacencyIndex];
		return getIsEdgeRemoved()->test(bondIndex) ? invalidIndex<uint32_t>() : graph->getAdjacentNodeIndices()[adjacencyIndex];
	}

};


} // namespace Blast
} // namespace Nv


#endif // ifndef NVBLASTFAMILYGRAPH_H