OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
main_ser.cpp
1 
22 #include "Vector/vector_dist.hpp"
23 
25 
26 struct my_struct
27 {
29  size_t size;
30 
32  char * ptr;
33 
35  std::string str;
36 
39 
41 
42 public:
43 
45  static bool pack() {return true;}
46 
47 
49 
50  my_struct()
51  {
52  size = 0;
53  ptr = NULL;
54  }
55 
57  my_struct(const my_struct & my)
58  {
59  this->operator=(my);
60  }
61 
64  {
65  this->operator=(my);
66  }
67 
68  ~my_struct()
69  {
70  if (ptr != NULL)
71  delete [] ptr;
72  }
73 
75  // we copy pointer and we do double delete
77  {
78  size = my.size;
79  str = my.str;
80  v = my.v;
81 
82  ptr = new char[size];
83  memcpy(ptr,my.ptr,32);
84 
85  return *this;
86  }
87 
89  // we copy pointer and we do double delete
91  {
92  size = my.size;
93  my.size = 0;
94  str.swap(my.str);
95  v.swap(my.v);
96  ptr = my.ptr;
97  my.ptr = 0;
98 
99  return *this;
100  }
101 
103 
105 
107  template<int ... prp> inline void packRequest(size_t & req) const
108  {
109  req += 2*sizeof(size_t) + size + str.size()+1;
110  v.packRequest(req);
111  }
112 
114 
116 
117  static bool noPointers()
118  {
119  return false;
120  }
121 
123 
125 
127  template<typename Memory, int ... prp> inline void pack(ExtPreAlloc<Memory> & mem, Pack_stat & sts) const
128  {
130 
131  //Serialize the number that determine the C-string size
133 
135 
137 
138  // Allocate and copy the string
139  mem.allocate(size);
140  void * t_ptr = mem.getPointer();
141  memcpy(t_ptr,ptr,size);
142 
144 
146 
147  //Serialize the number that determine the C++-string str.size()+1 given by the null terminator
148  Packer<size_t, Memory>::pack(mem,str.size()+1,sts);
149 
150  // Allocate and copy the string
151  mem.allocate(str.size()+1);
152  char * t_ptr2 = (char *)mem.getPointer();
153  memcpy(t_ptr2,str.c_str(),str.size());
154 
155  // Add null terminator
156  t_ptr2[str.size()] = 0;
157 
159 
161  }
162 
164 
166 
168  template<typename Memory, int ... prp> inline void unpack(ExtPreAlloc<Memory> & mem, Unpack_stat & ps)
169  {
171 
172  // unpack the size of the C string
174 
176 
178 
179  // Allocate the C string
180  ptr = new char[size];
181 
182  // source pointer
183  char * ptr_src = (char *)mem.getPointerOffset(ps.getOffset());
184 
185  // copy from the buffer to the destination
186  memcpy(ptr,ptr_src,size);
187 
188  // update the pointer
189  ps.addOffset(size);
190 
192 
194 
195  // get the the C++ string size
196  size_t cpp_size;
197  Unpacker<size_t, Memory>::unpack(mem,cpp_size,ps);
198 
199  // Create the string from the serialized data (de-serialize)
200  char * ptr_src2 = (char *)mem.getPointerOffset(ps.getOffset());
201  str = std::string(ptr_src2);
202  ps.addOffset(cpp_size);
203 
204  // Unpack the vector
206 
208  }
209 
211 
213 
214 };
215 
217 
324 int main(int argc, char* argv[])
325 {
342  // initialize the library
343  openfpm_init(&argc,&argv);
344 
345  // Here we define our domain a 2D box with internals from 0 to 1.0 for x and y
346  Box<2,float> domain({0.0,0.0},{1.0,1.0});
347 
348  // Here we define the boundary conditions of our problem
349  size_t bc[2]={PERIODIC,PERIODIC};
350 
351  // extended boundary around the domain, and the processor domain
352  Ghost<2,float> g(0.01);
353 
354  // my_struct at position 0 in the aggregate
355  constexpr int my_s1 = 0;
356 
357  // my_struct at position 1 in the aggregate
358  constexpr int my_s2 = 1;
359 
361 
363  vd(4096,domain,bc,g);
364 
365  std::cout << "HAS PACK: " << has_pack_agg<aggregate<my_struct,my_struct>>::result::value << std::endl;
366 
368 
389 
391  auto it = vd.getDomainIterator();
392 
393  while (it.isNext())
394  {
395  auto p = it.get();
396 
397  // we define x, assign a random position between 0.0 and 1.0
398  vd.getPos(p)[0] = (float)rand() / RAND_MAX;
399 
400  // we define y, assign a random position between 0.0 and 1.0
401  vd.getPos(p)[1] = (float)rand() / RAND_MAX;
402 
403  // Get the particle position as point
404  Point<2,float> pt = vd.getPos(p);
405 
406  // create a C string from the particle coordinates
407  // and copy into my struct
408  vd.getProp<my_s1>(p).size = 32;
409  vd.getProp<my_s1>(p).ptr = new char[32];
410  strcpy(vd.getProp<my_s1>(p).ptr,pt.toString().c_str());
411 
412  // create a C++ string from the particle coordinates
413  vd.getProp<my_s1>(p).str = std::string(pt.toString());
414 
415  vd.getProp<my_s1>(p).v.add(1);
416  vd.getProp<my_s1>(p).v.add(2);
417  vd.getProp<my_s1>(p).v.add(3);
418 
419  pt = pt * 2.0;
420 
421  // create a C string from the particle coordinates multiplied by 2.0
422  // and copy into my struct
423  vd.getProp<my_s2>(p).size = 32;
424  vd.getProp<my_s2>(p).ptr = new char[32];
425  strcpy(vd.getProp<my_s2>(p).ptr,pt.toString().c_str());
426 
427  // create a C++ string from the particle coordinates
428  vd.getProp<my_s2>(p).str = std::string(pt.toString());
429 
430  vd.getProp<my_s2>(p).v.add(1);
431  vd.getProp<my_s2>(p).v.add(2);
432  vd.getProp<my_s2>(p).v.add(3);
433  vd.getProp<my_s2>(p).v.add(4);
434 
435  // next particle
436  ++it;
437  }
438 
440 
458 
460  // Particles are redistribued across the processors
461  vd.map();
462 
463  // Synchronize the ghost
464  vd.ghost_get<my_s1,my_s2>();
465 
467 
484 
486  vd.write("particles");
487 
489 
503 
505  size_t fg = vd.size_local();
506 
507  Vcluster & v_cl = create_vcluster();
508 
509  // Only the master processor print
510  if (v_cl.getProcessUnitID() == 0)
511  {
512  // Print 4 particles
513  for ( ; fg < vd.size_local()+4 ; fg++)
514  {
515  // Print my struct1 information
516  std::cout << "my_struct1:" << std::endl;
517  std::cout << "C-string: " << vd.getProp<my_s1>(fg).ptr << std::endl;
518  std::cout << "Cpp-string: " << vd.getProp<my_s1>(fg).str << std::endl;
519 
520  for (size_t i = 0 ; i < vd.getProp<my_s1>(fg).v.size() ; i++)
521  std::cout << "Element: " << i << " " << vd.getProp<my_s1>(fg).v.get(i) << std::endl;
522 
523  // Print my struct 2 information
524  std::cout << "my_struct2" << std::endl;
525  std::cout << "C-string: " << vd.getProp<my_s2>(fg).ptr << std::endl;
526  std::cout << "Cpp-string: " << vd.getProp<my_s2>(fg).str << std::endl;
527 
528  for (size_t i = 0 ; i < vd.getProp<my_s2>(fg).v.size() ; i++)
529  std::cout << "Element: " << i << " " << vd.getProp<my_s2>(fg).v.get(i) << std::endl;
530  }
531  }
532 
534 
546 
548  openfpm_finalize();
549 
551 
560 }
size_t size
C string size.
Definition: main_ser.cpp:29
size_t getOffset()
Return the actual counter.
Definition: Pack_stat.hpp:41
std::string str
C++ string.
Definition: main_ser.cpp:35
void packRequest(size_t &req) const
Serialization request.
Definition: main_ser.cpp:107
Unpacker class.
Definition: Packer_util.hpp:20
size_t getProcessUnitID()
Get the process unit id.
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:371
virtual void * getPointer()
Return the pointer of the last allocation.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
void unpack(ExtPreAlloc< Memory > &mem, Unpack_stat &ps)
De-serialize the data structure.
Definition: main_ser.cpp:168
Definition: Ghost.hpp:39
my_struct(my_struct &&my)
Copy constructor from temporal object.
Definition: main_ser.cpp:63
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
Definition: ExtPreAlloc.hpp:92
Implementation of VCluster class.
Definition: VCluster.hpp:36
my_struct & operator=(const my_struct &my)
This is fundamental to avoid crash, otherwise.
Definition: main_ser.cpp:76
char * ptr
C string pointer.
Definition: main_ser.cpp:32
Packing class.
Definition: Packer.hpp:44
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
void addOffset(size_t off)
Increment the offset pointer by off.
Definition: Pack_stat.hpp:31
Unpacking status object.
Definition: Pack_stat.hpp:15
openfpm::vector< int > v
vector
Definition: main_ser.cpp:38
This class represent an N-dimensional box.
Definition: Box.hpp:56
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:36
return if true the aggregate type T has a property that has a complex packing(serialization) method ...
void pack(ExtPreAlloc< Memory > &mem, Pack_stat &sts) const
Serialize the data structure.
Definition: main_ser.cpp:127
Distributed vector.
my_struct(const my_struct &my)
Copy constructor.
Definition: main_ser.cpp:57
Packing status object.
Definition: Pack_stat.hpp:51
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:51
static bool pack()
Functions to check if the packing object is complex.
Definition: main_ser.cpp:45
my_struct & operator=(my_struct &&my)
This is fundamental to avoid crash, otherwise.
Definition: main_ser.cpp:90