8#ifndef OPENFPM_DATA_SRC_GRID_COPY_GRID_FAST_HPP_
9#define OPENFPM_DATA_SRC_GRID_COPY_GRID_FAST_HPP_
11#include "Grid/iterators/grid_key_dx_iterator.hpp"
13template<
unsigned int dim>
16 size_t striding_src[dim - 1];
17 size_t striding_dst[dim - 1];
34template<
bool lin_or_
inte,
typename data_type,
typename S>
55 static_cast<S *
>(boost::fusion::at_c<T::value>(data_dst).mem)->copyDeviceToDevice(*
static_cast<S *
>(boost::fusion::at_c<T::value>(
data_src).mem));
69template<
typename data_type,
typename S>
92 static_cast<S *
>(data_dst.mem)->copyDeviceToDevice(*
static_cast<S *
>(
data_src.mem));
103template<
bool is_complex,
unsigned int N,
typename gr
id,
typename ginfo>
106 static void copy(ginfo & gs_src,
120 gd_dst.set(sub_dst.template getStencil<0>(),gd_src,sub_src.template getStencil<0>());
132template<
typename gr
id,
typename ginfo>
135 static void copy(ginfo & gs_src,
146 lin_src += bx_src.
getLow(2) * gs_src.size_s(1);
147 lin_dst += bx_dst.
getLow(2) * gs_dst.size_s(1);
148 for (
size_t i = bx_src.
getLow(2) ; i <= bx_src.
getHigh(2) ; i++)
150 lin_src += bx_src.
getLow(1) * gs_src.size_s(0);
151 lin_dst += bx_dst.
getLow(1) * gs_dst.size_s(0);
152 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
154 lin_src += bx_src.
getLow(0);
155 lin_dst += bx_dst.
getLow(0);
156 for (
size_t k = bx_src.
getLow(0) ; k <= bx_src.
getHigh(0) ; k++)
158 gd_dst.set(lin_dst,gd_src,lin_src);
163 lin_src -= bx_src.
getHigh(0) + 1;
164 lin_dst -= bx_dst.
getHigh(0) + 1;
165 lin_src += gs_src.size_s(0);
166 lin_dst += gs_dst.size_s(0);
168 lin_src -= (bx_src.
getHigh(1) + 1)*gs_src.size_s(0);
169 lin_dst -= (bx_dst.
getHigh(1) + 1)*gs_dst.size_s(0);
170 lin_src += gs_src.size_s(1);
171 lin_dst += gs_dst.size_s(1);
181template<
typename gr
id,
typename ginfo>
184 static void copy(ginfo & gs_src,
196 lin_src += bx_src.
getLow(1) * gs_src.size_s(0);
197 lin_dst += bx_dst.
getLow(1) * gs_dst.size_s(0);
198 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
200 lin_src += bx_src.
getLow(0);
201 lin_dst += bx_dst.
getLow(0);
202 for (
size_t k = bx_src.
getLow(0) ; k <= bx_src.
getHigh(0) ; k++)
204 gd_dst.set(lin_dst,gd_src,lin_src);
209 lin_src -= bx_src.
getHigh(0) + 1;
210 lin_dst -= bx_dst.
getHigh(0) + 1;
211 lin_src += gs_src.size_s(0);
212 lin_dst += gs_dst.size_s(0);
224template<
typename gr
id,
typename ginfo>
227 static void copy(ginfo & gs_src,
238 lin_src += bx_src.
getLow(0);
239 lin_dst += bx_dst.
getLow(0);
240 for (
size_t k = bx_src.
getLow(0) ; k <= bx_src.
getHigh(0) ; k++)
242 gd_dst.set(lin_dst,gd_src,lin_src);
254template<
unsigned int object_size>
256 unsigned char * ptr_dst,
257 unsigned char * ptr_src,
260 for (
size_t i = bx_src.
getLow(2) ; i <= bx_src.
getHigh(2) ; i++)
262 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
264 memcpy(ptr_dst,ptr_src,sr.n_cpy*object_size);
266 ptr_dst += sr.striding_dst[0];
267 ptr_src += sr.striding_src[0];
269 ptr_dst += sr.striding_dst[1] - sr.tot_y*sr.striding_dst[0];
270 ptr_src += sr.striding_src[1] - sr.tot_y*sr.striding_src[0];
274template<
unsigned int object_size,
unsigned int n_cpy>
276 unsigned char * ptr_dst,
277 unsigned char * ptr_src,
280 for (
size_t i = bx_src.
getLow(2) ; i <= bx_src.
getHigh(2) ; i++)
282 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
284 __builtin_memcpy(ptr_dst,ptr_src,n_cpy*object_size);
286 ptr_dst += sr.striding_dst[0];
287 ptr_src += sr.striding_src[0];
289 ptr_dst += sr.striding_dst[1] - sr.tot_y*sr.striding_dst[0];
290 ptr_src += sr.striding_src[1] - sr.tot_y*sr.striding_src[0];
297template<
unsigned int object_size>
299 unsigned char * ptr_dst,
300 unsigned char * ptr_src,
303 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
305 memcpy(ptr_dst,ptr_src,sr.n_cpy*object_size);
307 ptr_dst += sr.striding_dst[0];
308 ptr_src += sr.striding_src[0];
312template<
unsigned int object_size,
unsigned int n_cpy>
314 unsigned char * ptr_dst,
315 unsigned char * ptr_src,
318 for (
size_t j = bx_src.
getLow(1) ; j <= bx_src.
getHigh(1) ; j++)
320 __builtin_memcpy(ptr_dst,ptr_src,n_cpy*object_size);
322 ptr_dst += sr.striding_dst[0];
323 ptr_src += sr.striding_src[0];
327template<
unsigned int dim>
330 template<
unsigned int object_size>
331 static void call(
unsigned char * ptr_src,
332 unsigned char * ptr_dst,
336 std::cout << __FILE__ <<
":" << __LINE__ <<
" error, wrong specialization. this case is not handled" << std::endl;
343 template<
unsigned int object_size>
344 static void call(
unsigned char * ptr_src,
345 unsigned char * ptr_dst,
352 copy_grid_fast_shortx_3<object_size,1>(bx_src,ptr_dst,ptr_src,sr);
355 copy_grid_fast_shortx_3<object_size,2>(bx_src,ptr_dst,ptr_src,sr);
359 copy_grid_fast_shortx_3<object_size,3>(bx_src,ptr_dst,ptr_src,sr);
363 copy_grid_fast_shortx_3<object_size,4>(bx_src,ptr_dst,ptr_src,sr);
366 copy_grid_fast_shortx_3<object_size,5>(bx_src,ptr_dst,ptr_src,sr);
369 copy_grid_fast_shortx_3<object_size,6>(bx_src,ptr_dst,ptr_src,sr);
373 copy_grid_fast_shortx_3<object_size,7>(bx_src,ptr_dst,ptr_src,sr);
377 copy_grid_fast_shortx_3<object_size,8>(bx_src,ptr_dst,ptr_src,sr);
381 copy_grid_fast_longx_3<object_size>(bx_src,ptr_dst,ptr_src,sr);
389 template<
unsigned int object_size>
390 static void call(
unsigned char * ptr_src,
391 unsigned char * ptr_dst,
398 copy_grid_fast_shortx_2<object_size,1>(bx_src,ptr_dst,ptr_src,sr);
401 copy_grid_fast_shortx_2<object_size,2>(bx_src,ptr_dst,ptr_src,sr);
405 copy_grid_fast_shortx_2<object_size,3>(bx_src,ptr_dst,ptr_src,sr);
409 copy_grid_fast_shortx_2<object_size,4>(bx_src,ptr_dst,ptr_src,sr);
412 copy_grid_fast_shortx_2<object_size,5>(bx_src,ptr_dst,ptr_src,sr);
415 copy_grid_fast_shortx_2<object_size,6>(bx_src,ptr_dst,ptr_src,sr);
419 copy_grid_fast_shortx_2<object_size,7>(bx_src,ptr_dst,ptr_src,sr);
423 copy_grid_fast_shortx_2<object_size,8>(bx_src,ptr_dst,ptr_src,sr);
427 copy_grid_fast_longx_2<object_size>(bx_src,ptr_dst,ptr_src,sr);
433template<
unsigned int dim_prp,
unsigned int prp,
typename gr
id_type>
438 return (
unsigned char *)(&gd.template get_unsafe<prp>(key));
442template<
unsigned int prp,
typename gr
id_type>
447 return (
unsigned char *)(&gd.template get_unsafe<prp>(key)[
id[0]]);
451template<
unsigned int prp,
typename gr
id_type>
456 return (
unsigned char *)(&gd.template get_unsafe<prp>(key)[
id[0]][
id[1]]);
460template<
unsigned int dim,
unsigned int dim_prp,
unsigned int prp,
typename gr
id_type>
482 sr.striding_src[0] = ptr_final_src - ptr_start_src;
483 sr.striding_dst[0] = ptr_final_dst - ptr_start_dst;
496 sr.striding_src[1] = ptr_final_src - ptr_start_src;
497 sr.striding_dst[1] = ptr_final_dst - ptr_start_dst;
504template<
unsigned int dim,
typename T>
507 template<
unsigned int prp,
typename gr
id>
508 static void process(
const grid & gd_src,
grid & gd_dst,
514 unsigned char * ptr_src = (
unsigned char *)(&gd_src.template get<prp>(bx_src.
getKP1()));
515 unsigned char * ptr_dst = (
unsigned char *)(&gd_dst.template get<prp>(bx_dst.
getKP1()));
521template<
unsigned int dim,
typename T,
unsigned int N1>
524 template<
unsigned int prp,
typename gr
id>
525 static void process(
const grid & gd_src,
grid & gd_dst,
530 for (
id[0] = 0 ;
id[0] < N1 ;
id[0]++)
534 unsigned char * ptr_src = (
unsigned char *)(&gd_src.template get<prp>(bx_src.
getKP1())[
id[0]]);
535 unsigned char * ptr_dst = (
unsigned char *)(&gd_dst.template get<prp>(bx_dst.
getKP1())[
id[0]]);
543template<
unsigned int dim,
typename T,
unsigned int N1,
unsigned int N2>
546 template<
unsigned int prp,
typename gr
id>
547 static void process(
const grid & gd_src,
grid & gd_dst,
552 for (
id[1] = 0 ;
id[1] < N1 ;
id[1]++)
554 for (
id[0] = 0 ;
id[0] < N2 ;
id[0]++)
558 unsigned char * ptr_src = (
unsigned char *)(&gd_src.template get<prp>(bx_src.
getKP1())[
id[1]][
id[0]]);
559 unsigned char * ptr_dst = (
unsigned char *)(&gd_dst.template get<prp>(bx_dst.
getKP1())[
id[1]][
id[0]]);
567template<
typename gr
id,
typename ginfo>
583 ginfo & gs_src,ginfo & gs_dst,
586 :gd_src(gd_src),gd_dst(gd_dst),gs_src(gs_src),gs_dst(gs_dst),bx_src(bx_src),bx_dst(bx_dst),cnt(cnt)
593 typedef typename boost::mpl::at<typename grid::value_type::type,boost::mpl::int_<T::value>>::type prp_type;
599template<
bool is_
inte,
unsigned int dim,
typename gr
id,
typename ginfo>
602 static void copy(
const ginfo & gs_src,
603 const ginfo & gs_dst,
617template<
unsigned int dim,
typename gr
id,
typename ginfo>
620 static void copy(
const ginfo & gs_src,
621 const ginfo & gs_dst,
630 boost::mpl::for_each_ref<boost::mpl::range_c<int,0,grid::value_type::max_prop>>(mpf);
638template<
typename gr
id,
typename ginfo>
641 static void copy(ginfo & gs_src,
661template<
typename gr
id,
typename ginfo>
664 static void copy(ginfo & gs_src,
687template <
bool is_complex,
705 static void pack(
grid & gr, it & sub_it, dtype & dest)
710 while (sub_it.isNext())
713 object_si_d<encap_src,encap_dst,OBJ_ENCAP,prp...>(gr.get_o(sub_it.get()),dest.get(
id));
732template <
typename grid,
748 static void pack(
grid & gr, it & sub_it, dtype & dest)
754 auto & gs_src = gr.getGrid();
758 lin_src += start.
get(2) * gs_src.size_s(1);
759 for (
long int i = start.
get(2) ; i <= stop.
get(2) ; i++)
761 lin_src += start.
get(1) * gs_src.size_s(0);
762 for (
long int j = start.
get(1) ; j <= stop.
get(1) ; j++)
764 lin_src += start.
get(0);
765 for (
long int k = start.
get(0) ; k <= stop.
get(0) ; k++)
768 object_si_d<encap_src,encap_dst,OBJ_ENCAP,prp...>(gr.get_o(lin_src),dest.get(
id));
773 lin_src -= stop.
get(0) + 1;
774 lin_src += gs_src.size_s(0);
776 lin_src -= (stop.
get(1) + 1)*gs_src.size_s(0);
777 lin_src += gs_src.size_s(1);
790template <
typename grid,
806 static void pack(
grid & gr, it & sub_it, dtype & dest)
812 auto & gs_src = gr.getGrid();
816 lin_src += start.
get(1) * gs_src.size_s(0);
817 for (
long int j = start.
get(1) ; j <= stop.
get(1) ; j++)
819 lin_src += start.
get(0);
820 for (
long int k = start.
get(0) ; k <= stop.
get(0) ; k++)
823 object_si_d<encap_src,encap_dst,OBJ_ENCAP,prp...>(gr.get_o(lin_src),dest.get(
id));
828 lin_src -= stop.
get(0) + 1;
829 lin_src += gs_src.size_s(0);
837template<
unsigned int dim,
int obj_
byte,
typename git,
typename gr
id>
840 static void pack(
grid & gr,
842 unsigned char * ptr_dest,
847 std::cout << __FILE__ <<
":" << __LINE__ <<
" critical error, we shoukd never be here" << std::endl;
851template<
int obj_
byte,
typename git,
typename gr
id>
854 static void pack(
grid & gr,
856 unsigned char * ptr_dest,
861 size_t tot_y = sub_it.getStop().get(1) - sub_it.getStart().get(1) + 1;
863 for (
size_t i = 0 ; i < tot_y ; i++)
865 memcpy(ptr_dest,ptr,n_cpy * obj_byte);
868 ptr_dest += n_cpy * obj_byte;
873template<
int obj_
byte,
typename git,
typename gr
id>
876 static void pack(
grid & gr,
878 unsigned char * ptr_dest,
883 size_t tot_y = sub_it.getStop().get(1) - sub_it.getStart().get(1) + 1;
884 size_t tot_z = sub_it.getStop().get(2) - sub_it.getStart().get(2) + 1;
891 unsigned char * ptr_final = (
unsigned char *)&(gr.template get<0>(one));
892 unsigned char * ptr_start = (
unsigned char *)&(gr.template get<0>(zero));
894 size_t stride_y = ptr_final - ptr_start;
896 for (
size_t i = 0 ; i < tot_z ; i++)
898 for (
size_t i = 0 ; i < tot_y ; i++)
900 memcpy(ptr_dest,ptr,n_cpy * obj_byte);
903 ptr_dest += n_cpy * obj_byte;
905 ptr += stride_y - tot_y*stride_x;
914template<
unsigned int dim,
unsigned int n_cpy ,
int obj_
byte,
typename git,
typename gr
id>
917 static void pack(
grid & gr,
919 unsigned char * ptr_dest,
923 std::cout << __FILE__ <<
":" << __LINE__ <<
" critical error, we shoukd never be here" << std::endl;
927template<
unsigned int n_cpy,
int obj_
byte,
typename git,
typename gr
id>
930 static void pack(
grid & gr,
932 unsigned char * ptr_dest,
936 size_t tot_y = sub_it.getStop().get(1) - sub_it.getStart().get(1) + 1;
938 for (
size_t i = 0 ; i < tot_y ; i++)
940 __builtin_memcpy(ptr_dest,ptr,n_cpy * obj_byte);
943 ptr_dest += n_cpy * obj_byte;
948template<
unsigned int n_cpy,
int obj_
byte,
typename git,
typename gr
id>
951 static void pack(
grid & gr,
953 unsigned char * ptr_dest,
957 size_t tot_y = sub_it.getStop().get(1) - sub_it.getStart().get(1) + 1;
958 size_t tot_z = sub_it.getStop().get(2) - sub_it.getStart().get(2) + 1;
965 unsigned char * ptr_final = (
unsigned char *)(&gr.template get_unsafe<0>(one));
966 unsigned char * ptr_start = (
unsigned char *)(&gr.template get<0>(zero));
968 size_t stride_y = ptr_final - ptr_start;
970 for (
size_t i = 0 ; i < tot_z ; i++)
972 for (
size_t i = 0 ; i < tot_y ; i++)
974 __builtin_memcpy(ptr_dest,ptr,n_cpy * obj_byte);
977 ptr_dest += n_cpy * obj_byte;
979 ptr += stride_y - tot_y*stride_x;
994template <
unsigned int dim,
1014 if (dim == 1 || dim > 3)
1016 pack_with_iterator<true,dim,grid,encap_src,encap_dst,boost_vct,it,dtype,prp...>::pack(gr,sub_it,dest);
1034 unsigned char * ptr_final = (
unsigned char *)(&gr.template get_unsafe<0>(one));
1035 unsigned char * ptr_start = (
unsigned char *)(&gr.template get<0>(zero));
1037 size_t stride = ptr_final - ptr_start;
1040 for (
size_t i = 1 ; i < dim ; i++)
1041 {n_tot += sub_it.getStop().get(i) - sub_it.getStart().get(i) + 1;}
1043 size_t n_cpy = sub_it.getStop().get(0) - sub_it.getStart().get(0) + 1;
1045 unsigned char * ptr_dest = (
unsigned char *)dest.getPointer();
1126template <
typename grid,
1148 auto & gs_src = gr.getGrid();
1153 lin_src += start.
get(0);
1154 for (
long int k = start.
get(0) ; k <= stop.
get(0) ; k++)
1157 object_si_d<encap_src,encap_dst,OBJ_ENCAP,prp...>(gr.get_o(lin_src),dest.get(
id));
1162 lin_src -= stop.
get(0) + 1;
1163 lin_src += gs_src.size_s(0);
1178template <
unsigned int dim,
1200 while (sub_it.isNext())
1204 object_s_di<encap_src,encap_dst,OBJ_ENCAP,prp...>(src.get(
id),gr.get_o(sub_it.get()));
1221template <
typename grid,
1249 auto & gs_dst = gr.getGrid();
1255 lin_dst += start.
get(2) * gs_dst.size_s(1);
1256 for (
long int i = start.
get(2) ; i <= stop.
get(2) ; i++)
1258 lin_dst += start.
get(1) * gs_dst.size_s(0);
1259 for (
long int j = start.
get(1) ; j <= stop.
get(1) ; j++)
1261 lin_dst += start.
get(0);
1262 for (
long int k = start.
get(0) ; k <= stop.
get(0) ; k++)
1265 object_s_di<encap_src,encap_dst,OBJ_ENCAP,prp...>(src.get(
id),gr.get_o(lin_dst));
1270 lin_dst -= stop.
get(0) + 1;
1271 lin_dst += gs_dst.size_s(0);
1273 lin_dst -= (stop.
get(1) + 1)*gs_dst.size_s(0);
1274 lin_dst += gs_dst.size_s(1);
This class represent an N-dimensional box.
__device__ __host__ T getLow(int i) const
get the i-coordinate of the low bound interval of the box
__device__ __host__ T getHigh(int i) const
get the high interval of the box
grid_key_dx< dim > getKP2() const
Get the point p12 as grid_key_dx.
grid_key_dx< dim > getKP1() const
Get the point p1 as grid_key_dx.
This is a distributed grid.
Declaration grid_key_dx_iterator_sub.
bool isNext()
Check if there is the next element.
grid_key_dx is the key to access any element in the grid
void zero()
Set to zero the key.
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
__device__ __host__ index_type get(index_type i) const
Get the i index.
void operator()(T &t)
It call the copy function for each property.
data_type & data_src
set of pointers
void operator()(T &t) const
It call the copy function for each property.
copy_fast_1d_device_memory(data_type &data_src, data_type &data_dst)
constructor
this class is a functor for "for_each" algorithm
copy_fast_1d_device_memory(data_type &data_src, data_type &data_dst)
constructor
void operator()(T &t) const
It call the copy function for each property.
data_type & data_src
set of pointers
This is a way to quickly copy a grid into another grid.
It create a boost::fusion vector with the selected properties.
It copy the properties from one object to another.
It copy the properties from one object to another.
static void pack(grid &gr, it &sub_it, dtype &dest)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void pack(grid &gr, it &sub_it, dtype &dest)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void pack(grid &gr, it &sub_it, dtype &dest)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void pack(grid &gr, it &sub_it, dtype &dest)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void pack(grid &gr, it &sub_it, dtype &dest)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void unpack(grid &gr, it &sub_it, stype &src)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.
static void unpack(grid &gr, it &sub_it, stype &src)
Pack an N-dimensional grid into a vector like structure B given an iterator of the grid.