Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

FMEKernel.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
38 #include <list>
39 
40 namespace ogdf {
41 namespace fast_multipole_embedder {
42 
43 class FMEKernel
44 {
45 public:
46  explicit FMEKernel(FMEThread* pThread) : m_pThread(pThread) { }
47 
48  inline void sync() { m_pThread->sync(); }
49 
51  inline uint32_t threadNr() const { return m_pThread->threadNr(); }
52 
54  inline uint32_t numThreads() const { return m_pThread->numThreads(); }
55 
57  inline bool isMainThread() const { return m_pThread->isMainThread(); }
58 
60  inline bool isSingleThreaded() const { return m_pThread->numThreads() == 1; };
61 
62 private:
64 };
65 
66 
67 #define OGDF_FME_KERNEL_USE_OLD
68 
69 #define OGDF_FME_KERNEL_COMPUTE_FORCE_PROTECTION_FACTOR 0.25f
70 // makro for force computation via SSE s / max(s*0.5, (dx*dx + dy*dy))
71 #define OGDF_FME_KERNEL_MM_COMPUTE_FORCE(dx,dy,s) \
72  _mm_div_ps((s), \
73  _mm_max_ps(_mm_mul_ps((s), \
74  _mm_set1_ps(OGDF_FME_KERNEL_COMPUTE_FORCE_PROTECTION_FACTOR)), \
75  _mm_add_ps(_mm_mul_ps((dx),(dx)), \
76  _mm_mul_ps((dy),(dy)))))
77 #define OGDF_FME_KERNEL_COMPUTE_FORCE(dx,dy,s) \
78  (s/(max<float>(s*OGDF_FME_KERNEL_COMPUTE_FORCE_PROTECTION_FACTOR, (dx)*(dx) + (dy)*(dy))))
79 
80 inline double move_nodes(float* x, float* y, const uint32_t begin, const uint32_t end, const float* fx, const float* fy, const float t)
81 {
82  double dsq_max = 0.0;
83  for (uint32_t i=begin; i <= end; i++)
84  {
85  double dsq = fx[i]*fx[i] + fy[i]*fy[i];
86  x[i] += fx[i]*t;
87  y[i] += fy[i]*t;
88  Math::updateMax(dsq_max, dsq);
89  }
90  return dsq_max;
91 }
92 
93 
94 inline void eval_edges(const ArrayGraph& graph, const uint32_t begin, const uint32_t end, float* fx, float* fy)
95 {
96  const float* x = graph.nodeXPos();
97  const float* y = graph.nodeYPos();
98  const float* e = graph.desiredEdgeLength();
99  for (uint32_t i=begin; i <= end; i++)
100  {
101  const auto& e_info = graph.edgeInfo(i);
102  const uint32_t a = e_info.a;
103  const uint32_t b = e_info.b;
104  const auto& a_info = graph.nodeInfo(a);
105  const auto& b_info = graph.nodeInfo(b);
106 
107  float dx = x[a] - x[b];
108  float dy = y[a] - y[b];
109  float dsq = dx*dx + dy*dy;
110  float f = dsq == 0 ? 0 : (logf(dsq)*0.5f-logf(e[i])) * 0.25f;
111  float fa = (float)(f/((float)a_info.degree));
112  float fb = (float)(f/((float)b_info.degree));
113  fx[a] -= dx*fa;
114  fy[a] -= dy*fa;
115  fx[b] += dx*fb;
116  fy[b] += dy*fb;
117  }
118 }
119 
120 
122 inline void eval_direct(float* x, float* y, float* s, float* fx, float* fy, size_t n)
123 {
124  for (uint32_t i=0; i < n; i++)
125  {
126  for (uint32_t j=i+1; j < n; j++)
127  {
128  float dx = x[i] - x[j];
129  float dy = y[i] - y[j];
130 #ifdef OGDF_FME_KERNEL_USE_OLD
131  float s_sum = s[i]+s[j];
132 #else
133  float s_sum = s[i]*s[j];
134 #endif
135  float f = OGDF_FME_KERNEL_COMPUTE_FORCE(dx, dy, s_sum);
136  fx[i] += dx*f;
137  fy[i] += dy*f;
138  fx[j] -= dx*f;
139  fy[j] -= dy*f;
140  }
141  }
142 }
143 
144 
146 inline void eval_direct(float* x1, float* y1, float* s1, float* fx1, float* fy1, size_t n1,
147  float* x2, float* y2, float* s2, float* fx2, float* fy2, size_t n2)
148 {
149  for (uint32_t i=0; i < n1; i++)
150  {
151  for (uint32_t j=0; j < n2; j++)
152  {
153  float dx = x1[i] - x2[j];
154  float dy = y1[i] - y2[j];
155 #ifdef OGDF_FME_KERNEL_USE_OLD
156  float s_sum = s1[i]+s2[j];
157 #else
158  float s_sum = s1[i]*s2[j];
159 #endif
160  float f = OGDF_FME_KERNEL_COMPUTE_FORCE(dx, dy, s_sum);
161  fx1[i] += dx*f;
162  fy1[i] += dy*f;
163  fx2[j] -= dx*f;
164  fy2[j] -= dy*f;
165  }
166  }
167 }
168 
169 
170 #ifndef OGDF_FME_KERNEL_USE_SSE_DIRECT
171 inline void eval_direct_fast(float* x, float* y, float* s, float* fx, float* fy, size_t n)
173 {
174  eval_direct(x, y, s, fx, fy, n);
175 }
176 
178 inline void eval_direct_fast(float* x1, float* y1, float* s1, float* fx1, float* fy1, size_t n1,
179  float* x2, float* y2, float* s2, float* fx2, float* fy2, size_t n2)
180 {
181  eval_direct(x1, y1, s1, fx1, fy1, n1,
182  x2, y2, s2, fx2, fy2, n2);
183 }
184 
185 #else
186 void eval_direct_fast(float* x, float* y, float* s, float* fx, float* fy, size_t n);
189 void eval_direct_fast(
190  float* x1, float* y1, float* s1, float* fx1, float* fy1, size_t n1,
191  float* x2, float* y2, float* s2, float* fx2, float* fy2, size_t n2);
192 #endif
193 
195 void fast_multipole_l2p(double* localCoeffiecients, uint32_t numCoeffiecients, double centerX, double centerY,
196  float x, float y, float q, float& fx, float &fy);
197 
198 void fast_multipole_p2m(double* mulitCoeffiecients, uint32_t numCoeffiecients, double centerX, double centerY,
199  float x, float y, float q);
200 
202 {
203 public:
204  inline void edgeForces(const ArrayGraph& graph, float* fx, float* fy)
205  {
206  eval_edges(graph, 0, graph.numEdges()-1, fx, fy);
207  }
208 
209  inline void repForces(ArrayGraph& graph, float* fx, float* fy)
210  {
211  eval_direct_fast(graph.nodeXPos(), graph.nodeYPos(), graph.nodeSize(), fx, fy, graph.numNodes());
212  }
213 
214  inline double moveNodes(ArrayGraph& graph, float* fx, float* fy, float timeStep)
215  {
216  return move_nodes(graph.nodeXPos(), graph.nodeYPos(), 0, graph.numNodes()-1, fx, fy, timeStep);
217  }
218 
219  inline double simpleIteration(ArrayGraph& graph, float* fx, float* fy, float timeStep)
220  {
221  repForces(graph, fx, fy);
222  edgeForces(graph, fx, fy);
223  return moveNodes(graph, fx, fy, timeStep);
224  }
225 
226 
227  inline double simpleEdgeIteration(ArrayGraph& graph, float* fx, float* fy, float timeStep)
228  {
229  edgeForces(graph, fx, fy);
230  return moveNodes(graph, fx, fy, timeStep);
231  }
232 
233 
234  inline void simpleForceDirected(ArrayGraph& graph, float timeStep, uint32_t minIt, uint32_t maxIt, uint32_t preProcIt, double threshold)
235  {
236  bool earlyExit = false;
237  float* fx = (float*)OGDF_MALLOC_16(sizeof(float)*graph.numNodes());
238  float* fy = (float*)OGDF_MALLOC_16(sizeof(float)*graph.numNodes());
239 
240  for (uint32_t i = 0; i<preProcIt; i++)
241  {
242  for (uint32_t j = 0; j<graph.numNodes(); j++)
243  {
244  fx[j] = 0.0f;
245  fy[j] = 0.0f;
246  }
247  simpleEdgeIteration(graph, fx, fy, timeStep);
248  }
249  for(uint32_t i = 0; i < maxIt && !earlyExit; i++)
250  {
251  for (uint32_t j = 0; j<graph.numNodes(); j++)
252  {
253  fx[j] = 0.0f;
254  fy[j] = 0.0f;
255  }
256  double dsq = simpleIteration(graph, fx, fy, timeStep);
257  if (dsq < threshold && i>(minIt))
258  earlyExit = true;
259  }
260 
261  OGDF_FREE_16(fx);
262  OGDF_FREE_16(fy);
263  }
264 
265 private:
266 };
267 
268 
270 {
271 public:
272 #if 0
273  FMESingleKernel(FMEThread* pThread) : FMEKernel(pThread) {};
274 #endif
275 
276  void operator()(ArrayGraph& graph, float timeStep, uint32_t minIt, uint32_t maxIt, double threshold)
277  {
278  simpleForceDirected(graph, timeStep, minIt, maxIt, 20, threshold);
279  }
280 };
281 
282 }
283 }
void simpleForceDirected(ArrayGraph &graph, float timeStep, uint32_t minIt, uint32_t maxIt, uint32_t preProcIt, double threshold)
Definition: FMEKernel.h:234
uint32_t numEdges() const
Returns the number of edges.
Definition: ArrayGraph.h:59
void eval_direct_fast(float *x, float *y, float *s, float *fx, float *fy, size_t n)
kernel function to evaluate forces between n points with coords x, y directly. result is stored in fx...
Definition: FMEKernel.h:172
double move_nodes(float *x, float *y, const uint32_t begin, const uint32_t end, const float *fx, const float *fy, const float t)
Definition: FMEKernel.h:80
uint32_t threadNr() const
returns the index of the thread ( 0.. numThreads()-1 )
Definition: FMEThread.h:87
float * desiredEdgeLength()
Returns the edge length array for all edges.
Definition: ArrayGraph.h:186
void eval_edges(const ArrayGraph &graph, const uint32_t begin, const uint32_t end, float *fx, float *fy)
Definition: FMEKernel.h:94
Definition of utility functions for FME layout.
void fast_multipole_p2m(double *mulitCoeffiecients, uint32_t numCoeffiecients, double centerX, double centerY, float x, float y, float q)
Declaration of class ArrayGraph.
double simpleEdgeIteration(ArrayGraph &graph, float *fx, float *fy, float timeStep)
Definition: FMEKernel.h:227
uint32_t numThreads() const
returns the total number of threads in the pool
Definition: FMEThread.h:90
void edgeForces(const ArrayGraph &graph, float *fx, float *fy)
Definition: FMEKernel.h:204
uint32_t threadNr() const
returns the index of the thread ( 0.. numThreads()-1 )
Definition: FMEKernel.h:51
bool isMainThread() const
returns true if this is the main thread ( the main thread is always the first thread ) ...
Definition: FMEThread.h:93
void repForces(ArrayGraph &graph, float *fx, float *fy)
Definition: FMEKernel.h:209
Declaration of class FMEThread.
bool isSingleThreaded() const
returns true if this run only uses one thread )
Definition: FMEKernel.h:60
NodeAdjInfo & nodeInfo(uint32_t i)
Returns the adjacency information for the node at index i in m_nodeAdj.
Definition: ArrayGraph.h:141
float * nodeXPos()
Returns the x coord array for all nodes.
Definition: ArrayGraph.h:165
uint32_t numNodes() const
Returns the number of nodes.
Definition: ArrayGraph.h:56
void operator()(ArrayGraph &graph, float timeStep, uint32_t minIt, uint32_t maxIt, double threshold)
Definition: FMEKernel.h:276
The fast multipole embedder work thread class.
Definition: FMEThread.h:80
float * nodeSize()
Returns the node size array for all nodes.
Definition: ArrayGraph.h:177
uint32_t a
First node of the pair.
Definition: EdgeChain.h:53
EdgeAdjInfo & edgeInfo(uint32_t i)
Returns the adjacency information for the edge at index i in m_edgeAdj.
Definition: ArrayGraph.h:147
bool isMainThread() const
returns true if this is the main thread ( the main thread is always the first thread ) ...
Definition: FMEKernel.h:57
void fast_multipole_l2p(double *localCoeffiecients, uint32_t numCoeffiecients, double centerX, double centerY, float x, float y, float q, float &fx, float &fy)
kernel function to evalute a local expansion at point x,y result is added to fx, fy ...
Basic declarations, included by all source files.
double simpleIteration(ArrayGraph &graph, float *fx, float *fy, float timeStep)
Definition: FMEKernel.h:219
double moveNodes(ArrayGraph &graph, float *fx, float *fy, float timeStep)
Definition: FMEKernel.h:214
void eval_direct(float *x, float *y, float *s, float *fx, float *fy, size_t n)
kernel function to evaluate forces between n points with coords x, y directly. result is stored in fx...
Definition: FMEKernel.h:122
#define OGDF_MALLOC_16(s)
16-byte aligned memory allocation macro
Definition: FastUtils.h:117
uint32_t numThreads() const
returns the total number of threads in the pool
Definition: FMEKernel.h:54
The namespace for all OGDF objects.
void updateMax(T &max, const T &newValue)
Stores the maximum of max and newValue in max.
Definition: Math.h:93
float * nodeYPos()
Returns the y coord array for all nodes.
Definition: ArrayGraph.h:171
#define OGDF_FREE_16(ptr)
16-byte aligned memory deallocation macro
Definition: FastUtils.h:120
#define OGDF_FME_KERNEL_COMPUTE_FORCE(dx, dy, s)
Definition: FMEKernel.h:77