Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
FastUtils.h
Go to the documentation of this file.
1
32#pragma once
33
35
36#ifdef OGDF_SYSTEM_UNIX
37# include <sys/time.h>
38#endif
39
40#ifdef OGDF_FME_KERNEL_USE_SSE
42#endif
43
44namespace ogdf {
45namespace fast_multipole_embedder {
46
47// use SSE for Multipole computations
48//#define OGDF_FME_KERNEL_USE_SSE
49
50// simple parallel quadtree sort
51//#define OGDF_FME_PARALLEL_QUADTREE_SORT
52
53// use SSE for direct interaction (this is slower than the normal direct computation)
54//#define OGDF_FME_KERNEL_USE_SSE_DIRECT
55
56inline void OGDF_FME_Print_Config() {
57#ifdef OGDF_FME_KERNEL_USE_SSE
58 std::cout << "OGDF_FME_KERNEL_USE_SSE" << std::endl;
59#endif
60#ifdef OGDF_FME_PARALLEL_QUADTREE_SORT
61 std::cout << "OGDF_FME_PARALLEL_QUADTREE_SORT" << std::endl;
62#endif
63#ifdef OGDF_FME_KERNEL_USE_SSE_DIRECT
64 std::cout << "OGDF_FME_KERNEL_USE_SSE_DIRECT" << std::endl;
65#endif
66}
67
70
71template<typename T>
72inline bool is_align_16(T* ptr) {
73 return !(((size_t)(void*)ptr) & 0x0F);
74}
75
76template<typename T>
77inline T* align_16_prev_ptr(T* t) {
78 return (T*)(((size_t)((void*)t)) & ~0x0F);
79}
80
81template<typename T>
82inline T* align_16_next_ptr(T* t) {
83 return (T*)((((size_t)((void*)t)) + 15) & ~0x0F);
84}
85
86#ifdef OGDF_SYSTEM_UNIX
87inline timeval GetDiffTime(timeval _then, double& dtime) {
90 gettimeofday(&now, nullptr);
91 timeval diff;
92
93 diff.tv_sec = now.tv_sec - then.tv_sec;
94 diff.tv_usec = now.tv_usec - then.tv_usec;
95 while (diff.tv_usec < 0) {
96 diff.tv_sec--;
97 diff.tv_usec = 1000000 + now.tv_usec - then.tv_usec;
98 }
99
100 dtime = (double)diff.tv_sec;
101 dtime += (double)diff.tv_usec / 1e6;
102
103 return (timeval)now;
104}
105#endif
106
107inline void printProfiledTime(double t, const char* text) {
108 std::cout << t << "s\t" << text << std::endl;
109}
110
111inline void printProfiledTime(double t, double sum, const char* text) {
112 std::cout << t << "s\t" << text << "\t" << (t / sum) * 100.0 << "%" << std::endl;
113}
114
116#define OGDF_MALLOC_16(s) System::alignedMemoryAlloc16((s))
117
119#define OGDF_FREE_16(ptr) System::alignedMemoryFree((ptr))
120
122template<typename MNR_T, typename C_T>
124 MNR_T x = (MNR_T)ix;
125 MNR_T y = (MNR_T)iy;
126 // bit length of the result
127 const unsigned int BIT_LENGTH = static_cast<unsigned int>(sizeof(MNR_T)) << 3;
128 // set all bits
129 MNR_T mask = 0x0;
130 mask = ~mask;
131
132 for (unsigned int i = (BIT_LENGTH >> 1); i > 0; i = i >> 1) {
133 // increase frequency
134 mask = mask ^ (mask << i);
135 x = (x | (x << i)) & mask;
136 y = (y | (y << i)) & mask;
137 }
138 return x | (y << 1);
139}
140
142template<typename MNR_T, typename C_T>
143inline void mortonNumberInv(MNR_T mnr, C_T& x, C_T& y) {
144 // bit length of the coordinates
145 unsigned int BIT_LENGTH = static_cast<unsigned int>(sizeof(C_T)) << 3;
146 // set least significant bit
147 MNR_T mask = 0x1;
148 // set coords to zero
149 x = y = 0;
150 for (unsigned int i = 0; i < BIT_LENGTH; i++) {
151 x = (C_T)(x | (mnr & mask));
152 mnr = mnr >> 1;
153 y = (C_T)(y | (mnr & mask));
154 mask = mask << 1;
155 }
156}
157
159template<typename T>
161 uint32_t BIT_LENGTH = static_cast<uint32_t>(sizeof(T)) << 3;
162 T mask = 0x1;
163 mask = mask << (BIT_LENGTH - 1);
164 for (uint32_t i = 0; i < BIT_LENGTH; i++) {
165 if (mask & n) {
166 return i;
167 }
168 mask = mask >> 1;
169 }
170 return BIT_LENGTH;
171}
172
176 return 0x1 << (msb - 1);
177}
178
181public:
183 explicit RandomNodeSet(const Graph& G) : m_graph(G) { allocate(); }
184
187
189 node chooseNode() const {
190 int i = m_numNodesChoosen
192 nodesLeft() - 1); //(int)((double)nodesLeft()*rand()/(RAND_MAX+1.0));
193 return m_array[i];
194 }
195
197 void removeNode(node v) {
198 int i = m_nodeIndex[v];
199 int j = m_numNodesChoosen;
200 node w = m_array[j];
201 std::swap(m_array[i], m_array[j]);
202 m_nodeIndex[w] = i;
203 m_nodeIndex[v] = j;
205 }
206
207 bool isAvailable(node v) const { return m_nodeIndex[v] >= m_numNodesChoosen; }
208
210 int nodesLeft() const { return m_numNodes - m_numNodesChoosen; }
211
212private:
213 void allocate() {
218 int i = 0;
219 for (node v : m_graph.nodes) {
220 m_array[i] = v;
221 m_nodeIndex[v] = i;
222 i++;
223 }
224 }
225
226 void deallocate() { delete[] m_array; }
227
230
233
236
239
242};
243
244inline void gridGraph(Graph& G, int n, int m) {
245 G.clear();
246 node v;
247 node* topRow = new node[m];
248 topRow[0] = G.newNode();
249 for (int j = 1; j < m; j++) {
250 topRow[j] = G.newNode();
251 G.newEdge(topRow[j - 1], topRow[j]);
252 }
253 for (int i = 1; i < n; i++) {
254 v = G.newNode();
255 G.newEdge(topRow[0], v);
256 topRow[0] = v;
257 for (int j = 1; j < m; j++) {
258 v = G.newNode();
259 G.newEdge(topRow[j - 1], v);
260 G.newEdge(topRow[j], v);
261 topRow[j] = v;
262 }
263 }
264 delete[] topRow;
265}
266
267inline void randomGridGraph(Graph& G, int n, int m, double missinNodesPercentage = 0.03) {
268 gridGraph(G, n, m);
269 int numberOfNodesToDelete = (int)((double)G.numberOfNodes() * missinNodesPercentage);
270
272 for (int i = 0; i < numberOfNodesToDelete; i++) {
273 node v = rndSet.chooseNode();
274 rndSet.removeNode(v);
275 G.delNode(v);
276 }
277}
278
280template<class TYP>
281class BinCoeff {
282public:
283 explicit BinCoeff(unsigned int n) : m_max_n(n) { init_array(); }
284
286
288 void init_array() {
289 using ptr = TYP*;
290 unsigned int i, j;
291 m_binCoeffs = new ptr[m_max_n + 1];
292 for (i = 0; i <= m_max_n; i++) {
293 m_binCoeffs[i] = new TYP[i + 1];
294 }
295
296 //Pascalsches Dreieck
297 for (i = 0; i <= m_max_n; i++) {
298 m_binCoeffs[i][0] = m_binCoeffs[i][i] = 1;
299 }
300
301 for (i = 2; i <= m_max_n; i++) {
302 for (j = 1; j < i; j++) {
303 m_binCoeffs[i][j] = m_binCoeffs[i - 1][j - 1] + m_binCoeffs[i - 1][j];
304 }
305 }
306 }
307
309 void free_array() {
310 unsigned int i;
311 for (i = 0; i <= m_max_n; i++) {
312 delete[] m_binCoeffs[i];
313 }
314 delete[] m_binCoeffs;
315 }
316
317 //Returns n over k.
318 inline TYP value(unsigned int n, unsigned int k) const { return m_binCoeffs[n][k]; }
319
320private:
321 unsigned int m_max_n;
322
325};
326
327// nothing
328struct EmptyArgType { };
329
330//
331// Function Invoker for 8 args
332//
333template<typename FunctionType, typename ArgType1 = EmptyArgType, typename ArgType2 = EmptyArgType,
334 typename ArgType3 = EmptyArgType, typename ArgType4 = EmptyArgType,
335 typename ArgType5 = EmptyArgType, typename ArgType6 = EmptyArgType,
336 typename ArgType7 = EmptyArgType, typename ArgType8 = EmptyArgType>
362
363//
364// Function Invoker for 7 args
365//
366template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
367 typename ArgType4, typename ArgType5, typename ArgType6, typename ArgType7>
392
393//
394// Function Invoker for 6 args
395//
396template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
397 typename ArgType4, typename ArgType5, typename ArgType6>
414
415//
416// Function Invoker for 5 args
417//
418template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
419 typename ArgType4, typename ArgType5>
435
436//
437// Function Invoker for 4 args
438//
439template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3, typename ArgType4>
453
454//
455// Function Invoker for 3 args
456//
457template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3>
470
471//
472// Function Invoker for 2 args
473//
474template<typename FunctionType, typename ArgType1, typename ArgType2>
486
487//
488// Function Invoker for 1 args
489//
490template<typename FunctionType, typename ArgType1>
500
501//
502// Function Invoker for 0 args
503//
504template<typename FunctionType>
513
514template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
515 typename ArgType4, typename ArgType5, typename ArgType6, typename ArgType7, typename ArgType8>
522
523template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
524 typename ArgType4, typename ArgType5, typename ArgType6, typename ArgType7, typename ArgType8>
531
532template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
533 typename ArgType4, typename ArgType5, typename ArgType6>
540
541template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3,
542 typename ArgType4, typename ArgType5>
549
550template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3, typename ArgType4>
556
557template<typename FunctionType, typename ArgType1, typename ArgType2, typename ArgType3>
562
563template<typename FunctionType, typename ArgType1, typename ArgType2>
568
569template<typename FunctionType, typename ArgType1>
573
574template<typename FunctionType>
578
579}
580}
Declaration of class GraphAttributes which extends a Graph by additional attributes.
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:521
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition Graph_d.h:622
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition Graph_d.h:589
Dynamic arrays indexed with nodes.
Definition NodeArray.h:125
void init()
Reinitializes the array. Associates the array with no graph.
Definition NodeArray.h:266
Class for the representation of nodes.
Definition Graph_d.h:177
binomial coeffs from Hachuls FMMM
Definition FastUtils.h:281
void free_array()
Free space for BK.
Definition FastUtils.h:309
TYP ** m_binCoeffs
holds the binominal coefficients
Definition FastUtils.h:324
void init_array()
Init BK -matrix for values n, k in 0 to t.
Definition FastUtils.h:288
TYP value(unsigned int n, unsigned int k) const
Definition FastUtils.h:318
utility class to select multiple nodes randomly
Definition FastUtils.h:180
int nodesLeft() const
number of nodes available
Definition FastUtils.h:210
node chooseNode() const
chooses a node from the available nodes in O(1)
Definition FastUtils.h:189
NodeArray< int > m_nodeIndex
the index in the array of the nodes
Definition FastUtils.h:235
void removeNode(node v)
removes a node from available nodes (assumes v is available) in O(1)
Definition FastUtils.h:197
RandomNodeSet(const Graph &G)
init the random node set with the given graph. takes O(n)
Definition FastUtils.h:183
node * m_array
the set of all nodes (at the end the available nodes)
Definition FastUtils.h:232
int randomNumber(int low, int high)
Returns random integer between low and high (including).
Include of header files for SSE-intrinsics.
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
void randomGridGraph(Graph &G, int n, int m, double missinNodesPercentage=0.03)
Definition FastUtils.h:267
uint32_t prevPowerOfTwo(uint32_t n)
returns the prev power of two
Definition FastUtils.h:174
void gridGraph(Graph &G, int n, int m)
Definition FastUtils.h:244
FuncInvoker< FunctionType, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8 > createFuncInvoker(FunctionType func, ArgType1 _arg1, ArgType2 _arg2, ArgType3 _arg3, ArgType4 _arg4, ArgType5 _arg5, ArgType6 _arg6, ArgType7 _arg7, ArgType8 _arg8)
Definition FastUtils.h:517
void printProfiledTime(double t, const char *text)
Definition FastUtils.h:107
MNR_T mortonNumber(C_T ix, C_T iy)
common template for bit-interleaving to compute the morton number assumes sizeOf(MNR_T) = 2*sizeOf(C_...
Definition FastUtils.h:123
void mortonNumberInv(MNR_T mnr, C_T &x, C_T &y)
common template for extracting the coordinates from a morton number assumes sizeOf(MNR_T) = 2*sizeOf(...
Definition FastUtils.h:143
uint32_t mostSignificantBit(T n)
returns the index of the most signficant bit set. 0 = most signif, bitlength-1 = least signif
Definition FastUtils.h:160
The namespace for all OGDF objects.
FuncInvoker(FunctionType f, ArgType1 _arg1, ArgType2 _arg2, ArgType3 _arg3, ArgType4 _arg4, ArgType5 _arg5, ArgType6 _arg6, ArgType7 _arg7)
Definition FastUtils.h:370
FuncInvoker(FunctionType f, ArgType1 _arg1, ArgType2 _arg2, ArgType3 _arg3, ArgType4 _arg4, ArgType5 _arg5, ArgType6 _arg6)
Definition FastUtils.h:400
FuncInvoker(FunctionType f, ArgType1 _arg1, ArgType2 _arg2, ArgType3 _arg3, ArgType4 _arg4, ArgType5 _arg5, ArgType6 _arg6, ArgType7 _arg7, ArgType8 _arg8)
Definition FastUtils.h:338