Commit 4f3e04c3 authored by incardon's avatar incardon
Browse files

Avoid constructor in memory_c

parent ce09322b
......@@ -282,6 +282,9 @@ class grid_cpu
//! Memory layout specification + memory chunk pointer
Mem data;
//! The memory allocator is not internally created
bool isExternal;
/*! \brief Get 1D vector with the
*
* Get std::vector with element 0 to dim set to 0
......@@ -314,7 +317,7 @@ class grid_cpu
//! Default constructor
grid_cpu()
:g1(getV())
:g1(getV()),isExternal(false)
{
}
......@@ -331,25 +334,26 @@ class grid_cpu
*
*/
template<typename S> grid_cpu(const grid_cpu & g, S & mem)
:isExternal(false)
{
swap(g.duplicate<S>());
}
//! Constructor allocate memory and give them a representation
grid_cpu(std::vector<size_t> & sz)
:g1(sz)
:g1(sz),isExternal(false)
{
}
//! Constructor allocate memory and give them a representation
grid_cpu(std::vector<size_t> && sz)
:g1(sz)
:g1(sz),isExternal(false)
{
}
//! Constructor allocate memory and give them a representation
grid_cpu(size_t (& sz)[dim])
:g1(sz)
:g1(sz),isExternal(false)
{
}
......@@ -364,7 +368,10 @@ class grid_cpu
grid_cpu<dim,T,Mem> grid_new(g1.getSize());
//! Set the allocator and allocate the memory
grid_new.template setMemory<S>();
if (isExternal == true)
grid_new.template setMemory<S>(static_cast<S&>(data.getMemory()));
else
grid_new.template setMemory<S>();
// We know that, if it is 1D we can safely copy the memory
if (dim == 1)
......@@ -424,16 +431,19 @@ class grid_cpu
template<typename S> void setMemory()
{
S * mem = new S();
//! Create and set the memory allocator
data.setMemory(*new S());
data.setMemory(*mem);
//! Allocate the memory and create the reppresentation
data.allocate(g1.size());
//! Allocate the memory and create the representation
if (g1.size() != 0) data.allocate(g1.size());
}
/*! \brief Get the object that provide memory
*
* Create the object that provide memory
* An external allocator is useful with allocator like PreAllocHeapMem
* to have contiguous in memory vectors.
*
* \tparam S memory type
*
......@@ -443,11 +453,16 @@ class grid_cpu
template<typename S> void setMemory(S & m)
{
//! Is external
isExternal = true;
m.incRef();
//! Create and set the memory allocator
data.setMemory(m);
//! Allocate the memory and create the reppresentation
data.allocate(g1.size());
if (g1.size() != 0) data.allocate(g1.size());
}
/*! \brief Return a plain pointer to the internal data
......@@ -460,7 +475,7 @@ class grid_cpu
void * getPointer()
{
return data.mem->getPointer();
return data.mem_r->get_pointer();
}
/*! \brief Get the reference of the selected element
......@@ -588,7 +603,17 @@ class grid_cpu
grid_cpu<dim,T,Mem> grid_new(sz);
//! Set the allocator and allocate the memory
grid_new.template setMemory<S>();
if (isExternal == true)
{
grid_new.template setMemory<S>(static_cast<S&>(data.getMemory()));
// Create an empty memory allocator for the actual structure
setMemory<S>();
}
else
grid_new.template setMemory<S>();
// We know that, if it is 1D we can safely copy the memory
if (dim == 1)
......
......@@ -26,7 +26,7 @@ static void remove_ptr(void * ptr)
// if the element does not exist, print that something wrong happened and return
if ( it == active_ptr.end() )
{
std::cout << "ERROR POINTER NOT FOUND " << ptr << "\n";
std::cout << "Error pointer not found " << ptr << "\n";
return;
}
......@@ -73,6 +73,7 @@ static void check_new(void * data, size_t sz)
*/
static void check_delete(void * data)
{
if (data == NULL) return;
// Delete the pointer
delete_data++;
remove_ptr(data);
......
......@@ -107,6 +107,30 @@ namespace openfpm
}
};
/*! \brief Grow policy define how the vector should grow every time we exceed the size
*
* In this case it return the requested size
*
*/
class grow_policy_identity
{
public:
/*! \brief It say how much the vector must grow
*
* \param original size
* \param requested size
*
* \return how much to grow
*
*/
static size_t grow(size_t original, size_t requested)
{
return requested;
}
};
/*! \brief Grow policy define how the vector should grow every time we exceed the size
*
* In this case it double up the size
......@@ -703,6 +727,16 @@ namespace openfpm
{
base.template setMemory<Memory>(mem);
}
/*! \brief Return the pointer that store the data
*
* \return the pointer that store the data
*
*/
void * getPointer()
{
return base.getPointer();
}
};
/*! \brief Implementation of 1-D std::vector like structure
......
......@@ -3,7 +3,9 @@
#include "map_vector.hpp"
#include "Point_test.hpp"
#include "memory/PreAllocHeapMemory.hpp"
#include <cstring>
#include "Space/Shape/Point.hpp"
#define FIRST_PUSH 1000000
#define SECOND_PUSH 1000000
......@@ -171,6 +173,41 @@ BOOST_AUTO_TEST_CASE( vector_use)
std::cout << "Vector unit test end" << "\n";
}
// Pre alloc test
struct pre_test
{
//! position vector
openfpm::vector<Point<2,float>,openfpm::device_cpu<Point<2,float>>,PreAllocHeapMemory<2>,openfpm::grow_policy_identity> pos;
//! properties vector
openfpm::vector<Point_test<float>,openfpm::device_cpu<Point_test<float>>,PreAllocHeapMemory<2>,openfpm::grow_policy_identity> prp;
};
BOOST_AUTO_TEST_CASE( vector_prealloc )
{
openfpm::vector<pre_test> pb(3);
for (size_t i = 0 ; i < 3 ; i++)
{
// Create the size required to store the particles position and properties to communicate
size_t s1 = openfpm::vector<Point<2,float>>::calculateMem(1024,0);
size_t s2 = openfpm::vector<Point_test<float>>::calculateMem(1024,0);
// Preallocate the memory
size_t sz[2] = {s1,s2};
PreAllocHeapMemory<2> * mem = new PreAllocHeapMemory<2>(sz);
// Set the memory allocator
pb.get(i).pos.setMemory(*mem);
pb.get(i).prp.setMemory(*mem);
// set the size and allocate, using mem warant that pos and prp is contiguous
pb.get(i).pos.resize(1024);
pb.get(i).prp.resize(1024);
}
}
BOOST_AUTO_TEST_SUITE_END()
#endif
......@@ -81,6 +81,26 @@ class memory
*
*/
virtual ~memory() {};
/*! \brief Increment the internal reference counter
*
*
*/
virtual void incRef() = 0;
/*! \brief Decrement the internal reference counter
*
*
*/
virtual void decRef() = 0;
/*! \brief Return the actual reference counter
*
* \return the reference counter
*
*/
virtual long int ref() = 0;
};
#endif
......@@ -40,6 +40,12 @@ class memory_array
ptr = static_cast<T *>(ptr_);
}
//! Return the pointer
void * get_pointer()
{
return ptr;
}
//! operator[]
T & operator[](mem_id i)
{
......@@ -54,7 +60,7 @@ class memory_array
: ptr(static_cast<T *>(ptr))
{
// Initialize the constructors
new (ptr)T[sz];
// new (ptr)T[sz];
};
};
......
......@@ -53,17 +53,27 @@ class memory_c
/*! \brief This function set the object that allocate memory
*
* This object set the object that allocate memory
*
* \param the memory object (do not reuse the passed object this class is going to deallocate this object)
* \param the memory object
*
*/
void setMemory(memory & mem)
{
mem.incRef();
this->mem = &mem;
}
/*! \brief This function get the object that allocate memory
*
* \return memory object to allocate memory
*
*/
memory& getMemory()
{
return *this->mem;
}
/*! \brief This function allocate memory and associate the representation to mem_r
*
* This function allocate memory and associate the representation of that chunk of
......@@ -118,7 +128,16 @@ class memory_c
memory_c():mem(NULL),mem_r(NULL){}
//! destructor
~memory_c(){delete(mem);delete(mem_r);}
~memory_c()
{
if (mem != NULL)
{
mem->decRef();
if (mem->ref() == 0)
delete(mem);
}
delete(mem_r);
}
/*! \brief swap the memory
*
......@@ -257,8 +276,6 @@ class memory_c<multi_array<T>, memory>
public:
/*! \brief This function set the object that allocate memory
*
* This object set the object that allocate memory
*
* \param the memory object
*
......@@ -266,9 +283,21 @@ class memory_c<multi_array<T>, memory>
void setMemory(memory & mem)
{
mem.incRef();
this->mem = &mem;
}
/*! \brief This function get the object that allocate memory
*
* \return memory object to allocate memory
*
*/
memory& getMemory()
{
return *this->mem;
}
/*! \brief This function allocate memory and associate the representation to mem_r
*
* This function allocate memory and associate the representation of that chunk of
......@@ -320,7 +349,16 @@ class memory_c<multi_array<T>, memory>
{}
//! destructor
~memory_c(){delete(mem);delete(mem_r);}
~memory_c()
{
if (mem != NULL)
{
mem->decRef();
if (mem->ref() == 0)
delete(mem);
}
delete(mem_r);
}
//! set the device memory interface, the object that allocate memory
void set_mem(memory & mem)
......
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