SUNphi  1.0
main.cpp
1 //#define DEBUG_TENS
2 //#define DEBUG_BINDER
3 //#define DEBUG_INDEXER
4 //#define DEBUG_REL_BINDER
5 
6 #include <SUNphi.hpp>
7 
8 #include <iostream>
9 
10 using namespace std;
11 using namespace SUNphi;
12 
13 /// Grid to be partitioned
14 template <typename I=int64_t>
16 {
17  /// Voluem of the grid
18  I vol;
19 
20  /// Sides of the grid
22 
23  /// Partitioning Level Pair of Pointers
24  using PLPP=
26 
27  /// List of fathers which are parted by this grid, and sister
28  /// (result of the partitioning)
29  ///
30  /// Sister could be accessed through father search, but it's easier
31  /// to provide a shortcut
33 
34  /// List of children grid partitioning this grid
36 
37  /// Visit all nodes executing the passed function on current node
38  /// and appending to the list of visited
39  ///
40  /// Internal implementation
41  template <typename F>
42  void visitAll(Vector<PartitioningLevel*>& visited, ///< List of visited nodes
43  const F& fun) ///< Function to be run on this node
44  {
45  visited.push_back(this);
46 
47  fun(this);
48 
49  for(auto& fatherSister : fatherSisters)
50  if(auto& father=fatherSister.first;not visited.isContained(father))
51  father->visitAll(visited,fun);
52 
53  for(auto& childPair : children)
54  for(auto& child : {childPair.first,childPair.second})
55  if(not visited.isContained(child))
56  child->visitAll(visited,fun);
57  }
58 
59 public:
60 
61  /// Name of the partitioning
62  const std::string name;
63 
64  /// Visit all nodes executing the passed function on current node
65  /// and appending to the list of visited
66  template <typename F>
67  void visitAll(const F& fun) ///< Function to be run on this node
68  {
69  /// List of visited node
70  Vector<PartitioningLevel*> visited;
71 
72  visitAll(visited,fun);
73  }
74 
75  /// List all elements of the partitioning with unfixed nodes
77  {
78  /// List to be returned
80 
81  visitAll([&list](PartitioningLevel* pl)
82  {
83  if(pl->vol==0)
84  list.push_back(pl);
85  });
86 
87  return
88  list;
89  }
90 
91  /// Determines if the volume is set
92  bool volumeIsSet()
93  const
94  {
95  return
96  vol!=0;
97  }
98 
99  /// Determines if the volume has an upper bound coming from direct
100  /// fathers or if the volume is set
102  const
103  {
104  if(volumeIsSet())
105  return
106  true;
107 
108  for(auto& fatherSister : fatherSisters)
109  if(auto& father=fatherSister.first;father->volumeIsSet())
110  return
111  true;
112 
113  return
114  false;
115  }
116 
117  /// Constructor
118  PartitioningLevel(const int nDim,
119  const std::string& name) :
120  vol(0),
121  sides(nDim,0),
122  name(name)
123  {
124  }
125 
126  /// Returns the volume
127  const I& getVol()
128  const
129  {
130  return
131  vol;
132  }
133 
134  void addFatherSister(PartitioningLevel& f,
136  {
137  fatherSisters.push_back({&f,&s});
138 
139  f.addChild(*this,s);
140  }
141 
142  void addChild(PartitioningLevel<I>& a,
143  PartitioningLevel<I>& b)
144  {
145  children.push_back({&a,&b});
146  }
147 
148  bool setVol(const I& v)
149  {
150  if(vol!=0)
151  return
152  false;
153 
154  if(vol==v)
155  return
156  true;
157 
158  for(auto& fatherSister : fatherSisters)
159  {
160  PartitioningLevel* father=
161  fatherSister.first;
162 
163  const I& fathVol=
164  father->getVol();
165 
166  if(fathVol and fathVol%v!=0)
167  return
168  false;
169  }
170 
171  for(auto& childPair : children)
172  for(auto& child : {childPair.first,childPair.second})
173  {
174  const I& childVol=
175  child->getVol();
176 
177  if(childVol and v%childVol!=0)
178  return
179  false;
180  }
181 
182  vol=
183  v;
184 
185  bool wentWell=
186  true;
187 
188  auto fatherSister=
189  fatherSisters.begin();
190 
191  while(wentWell and fatherSister!=fatherSisters.end())
192  {
193  PartitioningLevel& father=
194  *fatherSister->first;
195 
196  PartitioningLevel& sister=
197  *fatherSister->second;
198 
199  if(father.getVol())
200  {
201  RUNLOG<<"Father "<<father.name<<" has already set volume";
202  if(not sister.getVol())
203  {
204  RUNLOG<<"Sister "<<sister.name<<" has no set volume";
205 
206  SCOPE_INDENT(runLog);
207  wentWell&=
208  sister.setVol(father.getVol()/v);
209  }
210  else
211  RUNLOG<<"Also sister "<<sister.name<<" had set volume";
212  }
213  else
214  {
215  RUNLOG<<"Father "<<father.name<<" has no set volume";
216  if(sister.getVol())
217  {
218  RUNLOG<<"Sister "<<sister.name<<" had it, computing father "<<father.name<<" volume";
219 
220  SCOPE_INDENT(runLog);
221  wentWell&=
222  father.setVol(v*sister.getVol());
223  }
224  else
225  RUNLOG<<"Neither sister "<<sister.name<<" had set volume, cannot fix anything";
226  }
227 
228  fatherSister++;
229  }
230 
231  if(fatherSisters.size()==0)
232  RUNLOG<<name<<" has no father";
233 
234  if(auto childPair=children.begin();wentWell)
235  while(wentWell and childPair!=children.end())
236  {
237  auto& firstChild=
238  *(childPair->first);
239 
240  auto& secondChild=
241  *(childPair->second);
242 
243  const I firstVol=
244  firstChild.getVol();
245 
246  const I secondVol=
247  secondChild.getVol();
248 
249  if(firstVol and not secondVol)
250  {
251  RUNLOG<<"Second child "<<secondChild.name<<" has no set volume";
252 
253  SCOPE_INDENT(runLog);
254 
255  wentWell&=
256  secondChild.setVol(v/firstVol);
257  }
258 
259  if(secondVol and not firstVol)
260  {
261  RUNLOG<<"First child "<<firstChild.name<<" has no set volume";
262 
263  SCOPE_INDENT(runLog);
264 
265  wentWell&=
266  childPair->first->setVol(v/secondVol);
267  }
268 
269  childPair++;
270  }
271 
272  if(wentWell)
273  RUNLOG<<"Volume of "<<name<<" set to "<<v;
274  else
275  {
276  RUNLOG<<"Could not set volume of "<<name<<" to "<<v;
277  vol=
278  0;
279  }
280 
281  return
282  wentWell;
283  }
284 };
285 
286 template <typename I=int64_t>
287 void createPartitioningHierarchy(PartitioningLevel<I>& f,
288  PartitioningLevel<I>& c1,
289  PartitioningLevel<I>& c2)
290 {
291  c1.addFatherSister(f,c2);
292  c2.addFatherSister(f,c1);
293 
294  f.addChild(c1,c2);
295 }
296 
297 int main()
298 {
299  Sitmo::Rng rng;
300 
301  rng.seed(trueRandomGenerator);
302 
303  //enc.setKey({235425});
304  runLog()<<rng.serialize();
305 
307 
308  RUNLOG<<mrg.serialize();
309  std::ostringstream os;
310  os<<*static_cast<_MasterRandomGenerator*>(&mrg);
311  runLog()<<os.str()<<" "<<_MasterRandomGenerator::word_size;
312 
313  //test2.deBinarize(test1.binarize());
314 
315  // runLog()<<"Bin";
316 
317  // {
318  // SCOPE_INDENT(runLog);
319  // runLog()<<test2;
320  // }
321 
322  // runLog()<<(void*)&(test1.test().a().first[0])<<" "<<(void*)&(test2.test().a().first[0]);
323 
324  // // (*static_cast<Logger::LoggerLine*>(nullptr))<<(*static_cast<RemRef<SerializeWrapper<double>>*>(nullptr));
325  // ciccio=10.0;
326  // // decltype(*static_cast<Logger*>(nullptr)<<double{}) &a="";
327 
328  // std::ostringstream os;
329  // os<<std::mt19937_64{};
330  // runLog()<<os.str();
331 
332  // runLog()<<ciccio;
333 
334  // runLog()<<canPrint<Logger,Serializable<double>>;
335 
336  // std::get<0>(test.ser)=
337  // 10.0;
338 
339  // runLog()<<test.ser.mappedSize;
340  // runLog()<<test.d;
341 
342 
343  // SerializableScalar<Test> test{"test",{}};
344  // Serializable<Test2,NO_DEFAULT> t2{"t2"};
345 
346  // YAML::Emitter out;
347 
348  // out<<t2.serialize();
349 
350  // runLog()<<out.c_str();
351 
352  // // test().a=11.0;
353  // // runLog()<<test().a();
354 
355  // Serializer ser;
356  // ser<<t2;
357 
358  // DeSerializer deSer(ser.get().c_str());
359 
360  // deSer>>t2;
361 
362  // Serializer ser2;
363  // ser2<<t2;
364 
365  // //runLog()<<t2;
366 
367  // // Binarizer bin=t2.binarize();
368  // // runLog()<<bin.size();
369 
370  // // t2().ciccio="scompigliato";
371  // // t2().test().a=1235;
372  // // ser2<<t2;
373  // // runLog()<<ser2.get();
374 
375  // // t2.deBinarize(bin);
376  // // Serializer ser3;
377  // // ser3<<t2;
378  // // runLog()<<ser3.get();
379 
380 
381  // // double d;
382  // // int i;
383 
384  // // auto l=serList(d,"d",10.0,
385  // // i,"i",10);
386  // // runLog()<<((&std::get<0>(std::get<0>(l)))!=&d);
387 
388  // // SerializableScalar<double> _aref("a",NO_DEFAULT);
389 
390  // // runLog()<<isSerializableScalar<SerializableScalar<double>>;
391 
392  return 0;
393 }
bool setVol(const I &v)
Definition: main.cpp:148
void visitAll(Vector< PartitioningLevel * > &visited, const F &fun)
Definition: main.cpp:42
Vector< PartitioningLevel * > getAllWithUnfixedVol()
List all elements of the partitioning with unfixed nodes.
Definition: main.cpp:76
Rng(const uint32_t &s=DEFAULT_SEED)
Default constructor.
Definition: Sitmo2.hpp:273
Vector< PLPP > children
List of children grid partitioning this grid.
Definition: main.cpp:35
bool volumeIsBound() const
Definition: main.cpp:101
const std::string name
Name of the partitioning.
Definition: main.cpp:62
#define RUNLOG
Create the line.
Definition: Logger.hpp:432
const std::vector< uint64_t > & seeds
Constant access to the seeds.
void divWithMod(Vector< TOut > &quotient, Vector< TOut > &remainder, const Vector &divisor) const
Returns the result and remainder of the division.
Definition: Vector.hpp:310
PartitioningLevel(const int nDim, const std::string &name)
Constructor.
Definition: main.cpp:118
I vol
Voluem of the grid.
Definition: main.cpp:18
Grid to be partitioned.
Definition: main.cpp:15
bool volumeIsSet() const
Determines if the volume is set.
Definition: main.cpp:92
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
Definition: Add.hpp:87
Vector< I > sides
Sides of the grid.
Definition: main.cpp:21
#define SCOPE_INDENT(VAR)
Mark the stream to be more indented.
Definition: File.hpp:16
const I & getVol() const
Returns the volume.
Definition: main.cpp:127
void visitAll(const F &fun)
Definition: main.cpp:67
Vector< PLPP > fatherSisters
Definition: main.cpp:32