Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

FMEMultipoleKernel.h
Go to the documentation of this file.
1 
32 #pragma once
33 
36 
37 namespace ogdf {
38 namespace fast_multipole_embedder {
39 
41 {
42  uint32_t begin;
43  uint32_t end;
44 
45  template<typename Func>
46  void for_loop(Func& func)
47  {
48  for(uint32_t i=begin; i<=end; i++) func(i);
49  }
50 };
51 
52 
54 {
55 public:
56  explicit FMEMultipoleKernel(FMEThread* pThread) : FMEKernel(pThread) { }
57 
59  static FMEGlobalContext* allocateContext(ArrayGraph* pGraph, FMEGlobalOptions* pOptions, uint32_t numThreads);
60 
62  static void deallocateContext(FMEGlobalContext* globalContext);
63 
65  void quadtreeConstruction(ArrayPartition& nodePointPartition);
66 
68  void multipoleApproxSingleThreaded(ArrayPartition& nodePointPartition);
69 
71  void multipoleApproxSingleWSPD(ArrayPartition& nodePointPartition);
72 
74  void multipoleApproxNoWSPDStructure(ArrayPartition& nodePointPartition);
75 
77  void multipoleApproxFinal(ArrayPartition& nodePointPartition);
78 
80  void operator()(FMEGlobalContext* globalContext);
81 
83  inline ArrayPartition arrayPartition(uint32_t n)
84  {
85  return arrayPartition(n, threadNr(), numThreads(), 16);
86  }
87 
89  inline ArrayPartition arrayPartition(uint32_t n, uint32_t threadNr, uint32_t numThreads, uint32_t chunkSize)
90  {
91  ArrayPartition result;
92  if (!n)
93  {
94  result.begin = 1;
95  result.end = 0;
96  return result;
97  }
98  if (n>=numThreads*chunkSize)
99  {
100  uint32_t s = n/(numThreads*chunkSize);
101  uint32_t o = s*chunkSize*threadNr;
102  if (threadNr == numThreads-1)
103  {
104  result.begin = o;
105  result.end = n-1;
106  }
107  else
108  {
109  result.begin = o;
110  result.end = o+s*chunkSize-1;
111  }
112  } else
113  {
114  if (threadNr == 0)
115  {
116  result.begin = 0;
117  result.end = n-1;
118  } else
119  {
120  result.begin = 1;
121  result.end = 0;
122  }
123  }
124  return result;
125  }
126 
128  template<typename F>
129  inline void for_loop(const ArrayPartition& partition, F func)
130  {
131  if (partition.begin > partition.end)
132  return;
133  for (uint32_t i=partition.begin; i<= partition.end; i++) func(i);
134  }
135 
137  template<typename F>
138  inline void for_tree_partition(F functor)
139  {
140  for (LinearQuadtree::NodeID id : m_pLocalContext->treePartition.nodes)
141  {
142  functor(id);
143  }
144  }
145 
147  template<typename T, typename C>
148  inline void sort_single(T* ptr, uint32_t n, C comparer)
149  {
150  if (isMainThread())
151  {
152  std::sort(ptr, ptr + n, comparer);
153  }
154  }
155 
157  template<typename T, typename C>
158  inline void sort_parallel(T* ptr, uint32_t n, C comparer)
159  {
160  if (n < numThreads() * 1000 || numThreads() == 1)
161  sort_single(ptr, n, comparer);
162  else
163  sort_parallel(ptr, n, comparer, 0, numThreads());
164  }
165 
167  template<typename T, typename C>
168  inline void sort_parallel(T* ptr, uint32_t n, C comparer, uint32_t threadNrBegin, uint32_t numThreads)
169  {
170  if (n <= 1) return;
171  if (numThreads == 1)
172  {
173  std::sort(ptr, ptr + n, comparer);
174  }
175  else
176  {
177  uint32_t half = n >> 1;
178  uint32_t halfThreads = numThreads >> 1;
179  if (this->threadNr() < threadNrBegin + halfThreads)
180  sort_parallel(ptr, half, comparer, threadNrBegin, halfThreads);
181  else
182  sort_parallel(ptr + half, n - half, comparer, threadNrBegin + halfThreads, halfThreads);
183 
184  // wait until all threads are ready.
185  sync();
186  if (this->threadNr() == threadNrBegin)
187  std::inplace_merge(ptr, ptr + half, ptr + n, comparer);
188  }
189  }
190 
191 private:
192  FMEGlobalContext* m_pGlobalContext = nullptr;
193  FMELocalContext* m_pLocalContext = nullptr;
194 };
195 
196 }
197 }
ArrayPartition arrayPartition(uint32_t n)
creates an array partition with a default chunksize of 16
the main global options for a run
Definition: FMEFunc.h:68
void for_loop(const ArrayPartition &partition, F func)
for loop on a partition
Definitions of various auxiliary classes for FME layout.
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
void sort_parallel(T *ptr, uint32_t n, C comparer, uint32_t threadNrBegin, uint32_t numThreads)
lazy parallel sorting for num_threads = power of two
Declaration of FME kernel.
The fast multipole embedder work thread class.
Definition: FMEThread.h:80
void sort_parallel(T *ptr, uint32_t n, C comparer)
lazy parallel sorting for num_threads = power of two
void for_tree_partition(F functor)
for loop on the tree partition
The namespace for all OGDF objects.
ArrayPartition arrayPartition(uint32_t n, uint32_t threadNr, uint32_t numThreads, uint32_t chunkSize)
returns an array partition for the given threadNr and thread count
void sort_single(T *ptr, uint32_t n, C comparer)
sorting single threaded