6#include "util/cuda_launch.hpp"
7#include <boost/shared_array.hpp>
9#include <initializer_list>
11#include "memory/memory.hpp"
12#include "Space/Shape/Box.hpp"
13#include "grid_key.hpp"
15#include "util/mathutil.hpp"
16#include "iterators/stencil_type.hpp"
25 template<
typename SparseGr
id_type>
26 __device__ __host__
bool check(SparseGrid_type & sggt,
unsigned int dataBlockPos,
unsigned int offset)
32template<
unsigned int dim,
typename T>
42 template<
typename SparseGr
idGpu_type>
43 __device__ __host__
bool check(SparseGridGpu_type & sggt,
unsigned int dataBlockId,
unsigned int offset)
45 auto key = sggt.getCoord(dataBlockId,offset);
69 static bool valid(
size_t v_id,
size_t sz)
94 static bool valid(
size_t v_id,
size_t sz)
100template<
unsigned int dim>
111 size_t nblocks()
const
113 return wthr.x * wthr.y * wthr.z;
118 return thr.x * thr.y * thr.z;
124template<
unsigned int dim>
130 {tot_work *= ite.wthr.x * ite.thr.x;}
133 tot_work *= ite.wthr.x * ite.thr.x;
134 tot_work *= ite.wthr.y * ite.thr.y;
138 tot_work *= ite.wthr.x * ite.thr.x;
139 tot_work *= ite.wthr.y * ite.thr.y;
140 tot_work *= ite.wthr.z * ite.thr.z;
143 return tot_work != 0;
147template<
unsigned int N,
typename T>
class grid_sm;
149template<
unsigned int dim,
typename gr
id_sm_type,
typename T>
156template<
unsigned int dim,
typename stencil=no_stencil,
typename linearizer = gr
id_sm<dim,
void>,
typename warn=pr
int_warning_on_adjustment<dim,linearizer>>
class grid_key_dx_iterator_sub;
165template<
unsigned int N,
typename T>
199 for (
size_t i = 1 ; i < N ; i++)
230 for (
size_t i = 1 ; i < N ; i++)
258 for (
size_t i = 1 ; i < N ; i++)
274 template<
typename a,
typename ...lT>
275 __device__ __host__
inline mem_id
Lin_impl(a v,lT...t)
const
281 template<
typename a> __device__ __host__
inline mem_id
Lin_impl(a v)
const
312 for (
size_t i = 0 ; i < N ; i++)
342 for (
size_t i = 0 ; i < N ; i++)
361 for (
size_t i = 0 ; i < N ; i++)
380 for (
size_t i = 0 ; i < N ; i++)
389 inline size_t totalSize(
const size_t sz)
393 for (
size_t i = 0 ; i < N ; i++)
403 __device__ __host__
inline size_t totalSize(
const size_t (&
sz)[N])
407 for (
size_t i = 0 ; i < N ; i++)
438 __device__ __host__
inline grid_sm(
const size_t (&
sz)[N])
453 template<
typename check=NoCheck,
typename ids_type>
460 if (check::valid(gk.
get(0) + sum_id[0],
sz[0]) ==
false)
463 lid = gk.
get(0) + sum_id[0];
466 for (mem_id i = 1 ; i < N ; i++)
470 if (check::valid(gk.
get(i) + sum_id[i],
sz[i]) ==
false)
489 template<
typename check=NoCheck,
typename ids_type>
496 if (bc[0] == NON_PERIODIC)
498 if (check::valid(gk.
get(0) + sum_id[0],
sz[0]) ==
false)
501 lid = gk.
get(0) + sum_id[0];
505 lid = openfpm::math::positive_modulo(gk.
get(0) + sum_id[0],
sz[0]);
508 for (mem_id i = 1 ; i < N ; i++)
513 if (bc[i] == NON_PERIODIC)
515 if (check::valid(gk.
get(i) + sum_id[i],
sz[i]) ==
false)
522 lid += (openfpm::math::positive_modulo(gk.
get(i) + sum_id[i],
sz[i])) *
sz_s[i-1];
540 for (mem_id i = 1 ; i < N ; i++)
557 __device__ __host__
inline mem_id
LinId(
const size_t (& k)[N])
const
560 for (mem_id i = 1 ; i < N ; i++)
581 for (mem_id i = 1 ; i < N ; i++)
595 template<
typename a,
typename ...lT,
typename enabler =
typename std::enable_if<
sizeof...(lT) == N-1>::type >
596 __device__ __host__
inline mem_id
Lin(a v,lT...t)
const
615 for (mem_id i = 0 ; i < N ; i++)
635 inline mem_id
LinId(mem_id *
id)
const
639 for (mem_id i = 1 ; i < N ; i++)
657 __device__ __host__
inline size_t size()
const
672 for (
size_t i = 0 ; i < N ; i++)
693 for (
size_t i = 0 ; i < N ; i++)
695 if (
sz[i] != g.
sz[i])
704 for (
size_t i = 0 ; i < N ; i++)
736 __device__ __host__
inline size_t size_s(
unsigned int i)
const
750 __device__ __host__
inline size_t size(
unsigned int i)
const
760 __device__ __host__
inline const size_t (&
getSize()
const)[N]
772 for (
size_t i = 0; i < N; ++i)
size[i] =
sz[i];
796 template<
typename T2>
799 return getGPUIterator_impl<N>(*
this,key1,key2,n_thr);
808 struct ite_gpu<N> getGPUIterator(
size_t n_thr = default_kernel_wg_threads_)
const
813 for (
size_t i = 0 ; i < N ; i++)
819 return getGPUIterator_impl<N>(*
this,k1,k2,n_thr);
835 for (
size_t i = 0 ; i < N ; i++)
854 std::stringstream str;
856 for (
size_t i = 0 ; i < N ; i++)
857 str <<
"sz[" << i <<
"]=" <<
size(i) <<
" ";
863 template <
unsigned int,
typename>
friend class grid_sm;
867template<
unsigned int dim,
typename gr
id_sm_type,
typename T>
871 for (
size_t i = 0 ; i < dim ; i++)
872 {tot_work *= key2.
get(i) - key1.
get(i) + 1;}
874 size_t n = (tot_work <= n_thr)?openfpm::math::round_big_2(tot_work):n_thr;
901 {ig.thr.x = ig.thr.x << 1;}
902 else if (dir % 3 == 1)
903 {ig.thr.y = ig.thr.y << 1;}
904 else if (dir % 3 == 2)
905 {ig.thr.z = ig.thr.z << 1;}
914 {ig.wthr.x = (key2.
get(0) - key1.
get(0) + 1) / ig.thr.x + (((key2.
get(0) - key1.
get(0) + 1)%ig.thr.x != 0)?1:0);}
917 {ig.wthr.y = (key2.
get(1) - key1.
get(1) + 1) / ig.thr.y + (((key2.
get(1) - key1.
get(1) + 1)%ig.thr.y != 0)?1:0);}
925 for (
size_t i = 2 ; i < dim ; i++)
926 {ig.wthr.z *= (key2.
get(i) - key1.
get(i) + 1) / ig.thr.z + (((key2.
get(i) - key1.
get(i) + 1)%ig.thr.z != 0)?1:0);}
933 if (dim >= 1 && ig.wthr.x == 1)
934 {ig.thr.x = (key2.
get(0) - key1.
get(0) + 1);}
936 if (dim >= 2 && ig.wthr.y == 1)
937 {ig.thr.y = key2.
get(1) - key1.
get(1) + 1;}
939 if (dim == 3 && ig.wthr.z == 1)
940 {ig.thr.z = key2.
get(2) - key1.
get(2) + 1;}
942 for (
size_t i = 0 ; i < dim ; i++)
944 ig.start.set_d(i,key1.
get(i));
945 ig.stop.set_d(i,key2.
get(i));
987 for(
unsigned int i = 0 ; i < dim ; i++)
1004 k =
new mem_id[dim];
1013 template<
typename a,
typename ...T>
void set(a v, T...t)
1016 invert_assign(t...);
1058 k[
sizeof...(T)] = v;
1059 invert_assign(t...);
1062 template<
typename a,
typename ...T>
void invert_assign(a v)
1067 void invert_assign()
1112 :dim(n),
sz(
sz),gk(n)
1116 for (
size_t i = 0 ; i < dim ; i++)
1118 gk.
set_d(i,dim-i-1);
1139 for ( ; i < dim-1 ; i++)
1141 size_t id = gk.
get(i);
1160 for (
unsigned int s = 0 ; s <= i+1 ; s++)
1162 gk.
set_d(i+1-s,
id+1+s);
1189 if (gk.
get(dim-1) <
static_cast<mem_id
>(
sz-dim+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
__device__ __host__ void setHigh(int i, T val)
set the high interval of the box
__device__ __host__ bool isInsideKey(const KeyType &k) const
Check if the point is inside the region (Border included)
__device__ __host__ void setLow(int i, T val)
set the low interval of the box
Class to check if the edge can be created or not.
static bool valid(size_t v_id, size_t sz)
Check if vertex exist.
size_t getDim()
Get the dimensionality of the iterator.
bool isNext()
Check if there is the next element.
Iterator_g_const(size_t n, size_t sz)
Constructor.
grid_key_dx_r & get()
Return the actual key.
Iterator_g_const & operator++()
Get the next element.
size_t sz
size of the grid (the grid is assumed a square so equal on each dimension)
Class to check if the edge can be created or not.
static bool valid(size_t v_id, size_t sz)
No check is performed.
Declaration grid_key_dx_iterator_sub.
Emulate grid_key_dx with runtime dimensionality.
void set_d(size_t i, mem_id id)
Set the i index.
grid_key_dx_r(size_t dim)
constructor
mem_id * k
structure that store all the index
mem_id get(size_t i)
get the i index
void set(a v, T...t)
set the grid key from a list of numbers
size_t getDim()
Get the dimensionality of the key.
void invert_assign(a v, T...t)
Recursively invert the assignment.
grid_key_dx_r(grid_key_dx_r &key)
constructor from another key
grid_key_dx is the key to access any element in the grid
__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.
__device__ __host__ grid_key_dx< N > InvLinId(mem_id id) const
Construct.
void setDimensions(const size_t(&dims)[N])
Reset the dimension of the grid.
__device__ __host__ grid_sm(const size_t(&sz)[N])
Construct a grid of a specified size.
size_t size_tot
total number of the elements in the grid
mem_id LinId(mem_id *id) const
Linearization of an array of mem_id (long int)
__device__ __host__ void Initialize(const size_t sz)
Initialize the basic structure.
grid_sm(const size_t &sz)
Construct a grid of a specified size.
mem_id LinId(const grid_key_dx< N, ids_type > &gk, const signed char sum_id[N]) const
Linearization of the grid_key_dx with a specified shift.
__device__ __host__ grid_sm< N, T > & operator=(const grid_sm< N, T > &g)
Copy the grid from another grid.
__device__ __host__ void Initialize(const size_t(&sz)[N])
Initialize the basic structure.
__device__ __host__ const size_t(& getSize() const)[N]
Return the size of the grid as an array.
__device__ __host__ void getSize(size_t(&size)[N]) const
Returns the size of the grid in the passed array.
__device__ __host__ mem_id Lin_impl(a v, lT...t) const
linearize an arbitrary set of index
const Box< N, size_t > getBoxKey() const
Return the box enclosing the grid.
__device__ __host__ size_t size_s(unsigned int i) const
mem_id LinIdPtr(size_t *k) const
Linearization of the set of indexes.
grid_sm()
Default constructor.
Box< N, size_t > box
Box enclosing the grid.
__device__ __host__ ~grid_sm()
Destructor.
bool operator!=(const grid_sm< N, T > &g)
Check if the two grid_sm are the same.
void swap(grid_sm< N, T > &g)
swap the grid_sm informations
grid_key_dx_iterator_sub< N > getSubIterator(const grid_key_dx< N > &start, const grid_key_dx< N > &stop) const
Return a sub-grid iterator.
size_t sz_s[N]
size of the grid on each stride (used for linearization)
void Initialize()
Initialize the basic structure.
__device__ __host__ size_t size() const
Return the size of the grid.
friend class grid_sm
It simply mean that all the classes grid are friend of all its specialization.
__device__ __host__ mem_id LinId(const grid_key_dx< N, ids_type > &gk) const
Linearization of the grid_key_dx.
std::string toString() const
Produce a string from the object.
bool operator==(const grid_sm< N, T > &g)
Check if the two grid_sm are the same.
size_t sz[N]
size of the grid
__device__ __host__ grid_sm(const grid_sm< N, T > &g)
copy constructor
__device__ __host__ mem_id Lin_impl(a v) const
Linearize a set of index.
__device__ __host__ size_t size(unsigned int i) const
__device__ __host__ grid_sm(const grid_sm< N, S > &g)
construct a grid from another grid
Box< N, size_t > getBox() const
Return the box enclosing the grid.
mem_id LinId(const grid_key_dx< N, ids_type > &gk, const signed char sum_id[N], const size_t(&bc)[N]) const
Linearization of the grid_key_dx with a specified shift.
__device__ __host__ mem_id LinId(const size_t(&k)[N]) const
Linearization of the grid_key_dx.
__device__ __host__ mem_id Lin(a v, lT...t) const
linearize an arbitrary set of index
Declaration print_warning_on_adjustment.