Commit f9b1bd5e authored by incardon's avatar incardon

Merge branch 'Copy_Decomposition'

parents 8aca8f78 12a3ac70
...@@ -334,15 +334,33 @@ private: ...@@ -334,15 +334,33 @@ private:
// Receive counter // Receive counter
size_t recv_cnt; size_t recv_cnt;
// reference counter of the object in case is shared between object
long int ref_cnt;
public: public:
//! Increment the reference counter
void incRef()
{ref_cnt++;}
//! Decrement the reference counter
void decRef()
{ref_cnt--;}
//! Return the reference counter
long int ref()
{
return ref_cnt;
}
/*! \brief Cartesian decomposition constructor /*! \brief Cartesian decomposition constructor
* *
* \param v_cl Virtual cluster, used internally to handle or pipeline communication * \param v_cl Virtual cluster, used internally to handle or pipeline communication
* *
*/ */
CartDecomposition(Vcluster & v_cl) CartDecomposition(Vcluster & v_cl)
:nn_prcs<dim,T>(v_cl),v_cl(v_cl) :nn_prcs<dim,T>(v_cl),v_cl(v_cl),ref_cnt(0)
{ {
// Reset the box to zero // Reset the box to zero
bbox.zero(); bbox.zero();
......
...@@ -131,7 +131,7 @@ public: ...@@ -131,7 +131,7 @@ public:
* *
* \param id sub-domain id * \param id sub-domain id
* *
* \return the number of internal ghost box * \return the number of external ghost box
* *
*/ */
inline size_t getLocalNEGhost(size_t id) inline size_t getLocalNEGhost(size_t id)
......
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
* \snippet grid_dist_id_unit_test.hpp Create and access a distributed grid complex * \snippet grid_dist_id_unit_test.hpp Create and access a distributed grid complex
* ### Synchronize a distributed grid for complex structures * ### Synchronize a distributed grid for complex structures
* \snippet grid_dist_id_unit_test.hpp Synchronized distributed grid complex * \snippet grid_dist_id_unit_test.hpp Synchronized distributed grid complex
* ### Construct two grid with the same decomposition
* \snippet grid_dist_id_unit_test.hpp Construct two grid with the same decomposition
* *
*/ */
template<unsigned int dim, typename St, typename T, typename Decomposition,typename Memory=HeapMemory , typename device_grid=grid_cpu<dim,T> > template<unsigned int dim, typename St, typename T, typename Decomposition,typename Memory=HeapMemory , typename device_grid=grid_cpu<dim,T> >
...@@ -54,7 +56,7 @@ class grid_dist_id ...@@ -54,7 +56,7 @@ class grid_dist_id
Vcluster_object_array<device_grid> loc_grid; Vcluster_object_array<device_grid> loc_grid;
//! Space Decomposition //! Space Decomposition
Decomposition dec; Decomposition & dec;
//! Extension of each grid: Domain and ghost + domain //! Extension of each grid: Domain and ghost + domain
openfpm::vector<GBoxes<device_grid::dims>> gdb_ext; openfpm::vector<GBoxes<device_grid::dims>> gdb_ext;
...@@ -350,9 +352,12 @@ class grid_dist_id ...@@ -350,9 +352,12 @@ class grid_dist_id
public: public:
//! constructor //! constructor
grid_dist_id(Vcluster v_cl, Decomposition & dec, const size_t (& g_sz)[dim], const Box<dim,St> & domain, const Ghost<dim,T> & ghost) grid_dist_id(Decomposition & dec, const size_t (& g_sz)[dim], const Box<dim,St> & domain, const Ghost<dim,St> & ghost)
:domain(domain),ghost(ghost),loc_grid(NULL),v_cl(v_cl),dec(dec) :domain(domain),ghost(ghost),dec(dec),v_cl(*global_v_cluster)
{ {
// Increment the reference counter of the decomposition
dec.incRef();
check_size(g_sz); check_size(g_sz);
// For a 5x5 grid you have 4x4 Cell // For a 5x5 grid you have 4x4 Cell
...@@ -363,27 +368,13 @@ public: ...@@ -363,27 +368,13 @@ public:
cd_sm.setDimensions(domain,c_g,0); cd_sm.setDimensions(domain,c_g,0);
// fill the global size of the grid // fill the global size of the grid
for (int i = 0 ; i < dim ; i++) {this->g_sz[i] = g_sz[i];} for (size_t i = 0 ; i < dim ; i++) {this->g_sz[i] = g_sz[i];}
// Get the number of processor and calculate the number of sub-domain
// for decomposition
size_t n_proc = v_cl.getProcessingUnits();
size_t n_sub = n_proc * SUB_UNIT_FACTOR;
// Calculate the maximum number (before merging) of sub-domain on
// each dimension
size_t div[dim];
for (int i = 0 ; i < dim ; i++)
{div[i] = openfpm::math::round_big_2(pow(n_sub,1.0/dim));}
// Create the sub-domains
dec.setParameters(div);
// Create local grid // Create local grid
Create(); Create();
// Calculate ghost boxes // Calculate ghost boxes
dec.calculateGhostBoxes(ghost); dec.calculateGhostBoxes();
} }
/*! \brief Constrcuctor /*! \brief Constrcuctor
...@@ -394,8 +385,11 @@ public: ...@@ -394,8 +385,11 @@ public:
* *
*/ */
grid_dist_id(const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,St> & g) grid_dist_id(const size_t (& g_sz)[dim],const Box<dim,St> & domain, const Ghost<dim,St> & g)
:domain(domain),ghost(g),dec(Decomposition(*global_v_cluster)),v_cl(*global_v_cluster) :domain(domain),ghost(g),dec(*new Decomposition(*global_v_cluster)),v_cl(*global_v_cluster)
{ {
// Increment the reference counter of the decomposition
dec.incRef();
// check that the grid has valid size // check that the grid has valid size
check_size(g_sz); check_size(g_sz);
...@@ -541,6 +535,11 @@ public: ...@@ -541,6 +535,11 @@ public:
//! Destructor //! Destructor
~grid_dist_id() ~grid_dist_id()
{ {
dec.decRef();
// if we reach the 0, destroy the object
if (dec.ref() == 0)
delete &dec;
} }
/*! \brief Get the Virtual Cluster machine /*! \brief Get the Virtual Cluster machine
......
...@@ -612,6 +612,106 @@ void Test3D_complex(const Box<3,float> & domain, long int k) ...@@ -612,6 +612,106 @@ void Test3D_complex(const Box<3,float> & domain, long int k)
} }
} }
// Test duplicated topology
void Test3D_dup(const Box<3,float> & domain, long int k)
{
long int big_step = k / 30;
big_step = (big_step == 0)?1:big_step;
long int small_step = 1;
Vcluster & v_cl = *global_v_cluster;
if ( v_cl.getProcessingUnits() > 32 )
return;
print_test( "Testing 3D duplicate topology complex k<=",k);
// 3D test
for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
{
BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );
// grid size
size_t sz[3];
sz[0] = k;
sz[1] = k;
sz[2] = k;
// factor
float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);
// Ghost
Ghost<3,float> g(0.01 / factor);
//! [Construct two grid with the same decomposition]
// Distributed grid with id decomposition
grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist1(sz,domain,g);
// another grid with the same decomposition
grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist2(g_dist1.getDecomposition(),sz,domain,g);
//! [Construct two grid with the same decomposition]
BOOST_REQUIRE_EQUAL(g_dist2.getDecomposition().ref(),2);
auto dom_g1 = g_dist1.getDomainIterator();
auto dom_g2 = g_dist2.getDomainIterator();
bool check = true;
while (dom_g1.isNext())
{
auto key1 = dom_g1.get();
auto key2 = dom_g2.get();
check &= (key1 == key2)?true:false;
++dom_g1;
++dom_g2;
}
BOOST_REQUIRE_EQUAL(check,true);
}
// 3D test
for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
{
BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );
// grid size
size_t sz[3];
sz[0] = k;
sz[1] = k;
sz[2] = k;
// factor
float factor = pow(global_v_cluster->getProcessingUnits()/2.0f,1.0f/3.0f);
// Ghost
Ghost<3,float> g(0.01 / factor);
//! [Construct two grid with the same decomposition]
// Distributed grid with id decomposition
grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist1 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(sz,domain,g);
// another grid with the same decomposition
grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist2 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(g_dist1->getDecomposition(),sz,domain,g);
//! [Construct two grid with the same decomposition]
BOOST_REQUIRE_EQUAL(g_dist2->getDecomposition().ref(),2);
delete g_dist1;
BOOST_REQUIRE_EQUAL(g_dist2->getDecomposition().ref(),1);
BOOST_REQUIRE_EQUAL(g_dist2->getDecomposition().getLocalNEGhost(0) != 0, true);
BOOST_REQUIRE_EQUAL(g_dist2->getDecomposition().check_consistency(),false);
}
}
BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use) BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
{ {
// Domain // Domain
...@@ -632,6 +732,7 @@ BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use) ...@@ -632,6 +732,7 @@ BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
k = std::pow(k, 1/3.); k = std::pow(k, 1/3.);
Test3D(domain3,k); Test3D(domain3,k);
Test3D_complex(domain3,k); Test3D_complex(domain3,k);
Test3D_dup(domain3,k);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
......
...@@ -40,6 +40,24 @@ public: ...@@ -40,6 +40,24 @@ public:
return key; return key;
} }
/* \brief Check if two key are the same
*
* \param key_t key to check
*
* \return true if the two key are equal
*
*/
inline bool operator==(const grid_dist_key_dx<dim> & key_t)
{
if (g_c != key_t.g_c)
return false;
// Check the two key index by index
return getKey() == key_t.getKey();
}
/*! \brief Create a new key moving the old one /*! \brief Create a new key moving the old one
* *
* \param i dimension id * \param i dimension id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment