OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
grid_base_impl_layout.hpp
1 /*
2  * grid_base_impl_layout.hpp
3  *
4  * Created on: May 2, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_GRID_GRID_BASE_IMPL_LAYOUT_HPP_
9 #define OPENFPM_DATA_SRC_GRID_GRID_BASE_IMPL_LAYOUT_HPP_
10 
20 template<typename S>
21 struct allocate
22 {
24  size_t sz;
25 
27  allocate(size_t sz)
28  :sz(sz){};
29 
31  template<typename T>
32  void operator()(T& t) const
33  {
35  t.setMemory(*new S());
36 
38  if (sz != 0) t.allocate(sz);
39  }
40 };
41 
42 
52 template<typename s_m>
53 struct frswap
54 {
55  s_m & swap_src;
56  s_m & swap_dst;
57 
59  frswap(s_m & swap_dst, s_m & swap_src)
60  :swap_src(swap_src),swap_dst(swap_dst)
61  {};
62 
64  template<typename T>
65  void operator()(T& t) const
66  {
67  boost::fusion::at_c<T::value>(swap_dst).swap(boost::fusion::at_c<T::value>(swap_src));
68  }
69 };
70 
71 
73 template<unsigned int p, typename layout, typename data_type, typename g1_type, typename key_type, unsigned int sel = 2*is_layout_mlin<layout>::value + is_layout_inte<layout>::value >
74 struct mem_get
75 {
76  static inline auto get(const data_type & data_, const g1_type & g1, const key_type & v1) -> decltype(boost::fusion::at_c<p>(data_.mem_r->operator[](g1.LinId(v1)))) &
77  {
78  return boost::fusion::at_c<p>(data_.mem_r->operator[](g1.LinId(v1)));
79  }
80 
81  static inline auto get_lin(const data_type & data_, const g1_type & g1, const size_t lin_id) -> decltype(boost::fusion::at_c<p>(data_.mem_r->operator[](lin_id))) &
82  {
83  return boost::fusion::at_c<p>(data_.mem_r->operator[](lin_id));
84  }
85 };
86 
88 template<unsigned int p, typename layout, typename data_type, typename g1_type, typename key_type>
89 struct mem_get<p,layout,data_type,g1_type,key_type,1>
90 {
91  static inline auto get(const data_type & data_, const g1_type & g1, const key_type & v1) -> decltype(boost::fusion::at_c<p>(data_).mem_r->operator[](g1.LinId(v1)))
92  {
93  return boost::fusion::at_c<p>(data_).mem_r->operator[](g1.LinId(v1));
94  }
95 
96  static inline auto get_lin(const data_type & data_, const g1_type & g1, size_t lin_id) -> decltype(boost::fusion::at_c<p>(data_).mem_r->operator[](lin_id))
97  {
98  return boost::fusion::at_c<p>(data_).mem_r->operator[](lin_id);
99  }
100 };
101 
102 
104 template<typename S, typename layout, typename data_type, typename g1_type, unsigned int sel = 2*is_layout_mlin<layout>::value + is_layout_inte<layout>::value >
105 struct mem_setm
106 {
107  static inline void setMemory(data_type & data_, const g1_type & g1, bool & is_mem_init)
108  {
109  S * mem = new S();
110 
112  data_.setMemory(*mem);
113 
115  if (g1.size() != 0) data_.allocate(g1.size());
116 
117  is_mem_init = true;
118  }
119 };
120 
122 template<typename S, typename layout, typename data_type, typename g1_type>
123 struct mem_setm<S,layout,data_type,g1_type,1>
124 {
125  static inline void setMemory(data_type & data_, const g1_type & g1, bool & is_mem_init)
126  {
128  allocate<S> all(g1.size());
129 
131  boost::fusion::for_each(data_,all);
132 
133  is_mem_init = true;
134  }
135 };
136 
137 
139 template<unsigned int dim , typename T, typename layout, typename data_type, typename g1_type, typename key_type, unsigned int sel = 2*is_layout_mlin<layout>::value + is_layout_inte<layout>::value >
140 struct mem_geto
141 {
142  static inline encapc<dim,T,typename layout::type> get(data_type & data_, const g1_type & g1, const key_type & v1)
143  {
144  return encapc<dim,T,typename layout::type>(data_.mem_r->operator[](g1.LinId(v1)));
145  }
146 
147  static inline encapc<dim,T,typename layout::type> get_lin(data_type & data_, const size_t & v1)
148  {
149  return encapc<dim,T,typename layout::type>(data_.mem_r->operator[](v1));
150  }
151 };
152 
154 template<unsigned int dim, typename T,typename layout, typename data_type, typename g1_type, typename key_type>
155 struct mem_geto<dim,T,layout,data_type,g1_type,key_type,1>
156 {
157  static inline encapc<dim,T,typename layout::type> get(data_type & data_, const g1_type & g1, const key_type & v1)
158  {
159  return encapc<dim,T,typename layout::type>(data_,g1.LinId(v1));
160  }
161 
162  static inline encapc<dim,T,typename layout::type> get_lin(data_type & data_, const size_t & v1)
163  {
164  return encapc<dim,T,typename layout::type>(data_,v1);
165  }
166 };
167 
168 
170 template<typename grid_type, typename S , typename layout, typename data_type, unsigned int sel = 2*is_layout_mlin<layout>::value + is_layout_inte<layout>::value >
172 {
173  static inline void set(grid_type & grid_new, grid_type & old, data_type & data_)
174  {
175  grid_new.setMemory(static_cast<S&>(data_.getMemory()));
176 
177  // Create an empty memory allocator for the actual structure
178 
179  old.setMemory();
180  }
181 };
182 
184 template<typename grid_type, typename S , typename layout, typename data_type>
185 struct mem_setext<grid_type,S,layout,data_type,1>
186 {
187  static inline void set(grid_type & grid_new, grid_type & old, data_type & data_)
188  {
189  std::cerr << __FILE__ << ":" << "__LINE__" << " Error, " << demangle(typeid(grid_type).name()) << " this structure does not support setting from external memory" << std::endl;
190  }
191 };
192 
193 
195 template<typename T , typename layout, typename data_type, typename grid_type, unsigned int sel = 2*is_layout_mlin<layout>::value + is_layout_inte<layout>::value >
196 struct mem_swap
197 {
198  static inline void swap(data_type & data_dst, data_type & data_src)
199  {
200  // move the data
201  data_dst.swap(data_src);
202  }
203 };
204 
206 template<typename T , typename layout, typename data_type, typename grid_type>
207 struct mem_swap<T,layout,data_type,grid_type,1>
208 {
209  static inline void swap(data_type & data_dst, data_type & data_src)
210  {
211  // swap the data for each property
212  frswap<decltype(data_dst)> sw(data_dst,data_src);
213 
214  boost::mpl::for_each_ref< boost::mpl::range_c<int,0,T::max_prop> >(sw);
215  }
216 };
217 
218 #endif /* OPENFPM_DATA_SRC_GRID_GRID_BASE_IMPL_LAYOUT_HPP_ */
void operator()(T &t) const
It call the allocate function for each member.
allocate(size_t sz)
constructor it fix the size
Case memory_traits_lin.
size_t sz
size to allocate
Case memory_traits_lin.
Case memory_traits_lin.
Case memory_traits_lin.
Case memory_traits_lin.
void operator()(T &t) const
It call the allocate function for each member.
This is a distributed grid.
frswap(s_m &swap_dst, s_m &swap_src)
constructor
static void setMemory(data_type &data_, const g1_type &g1, bool &is_mem_init)
this class is a functor for "for_each" algorithm
this class is a functor for "for_each" algorithm
static void setMemory(data_type &data_, const g1_type &g1, bool &is_mem_init)