Skip to content
Snippets Groups Projects
Commit 79ecba78 authored by Pietro Incardona's avatar Pietro Incardona
Browse files

Fixing for semantic communications

parent 68d2e016
No related branches found
No related tags found
No related merge requests found
......@@ -61,7 +61,7 @@ public:
//! destroy memory
virtual void destroy();
//! copy from a General device
virtual bool copy(memory & m);
virtual bool copy(const memory & m);
//! the the size of the allocated memory
virtual size_t size();
//! resize the momory allocated
......
......@@ -166,6 +166,19 @@ public:
return (((unsigned char *)mem->getPointer()) + sequence_c[a_seq-1]);
}
/*! \brief Return the pointer of the last allocation
*
* \return the pointer
*
*/
virtual const void * getPointer() const
{
if (a_seq == 0)
return NULL;
return (((unsigned char *)mem->getPointer()) + sequence_c[a_seq-1]);
}
/*! \brief Return the pointer you will get when you do the allocation ip
*
* This particular function exist because the allocation sequence is fixed a priori
......@@ -211,7 +224,7 @@ public:
*
*/
virtual size_t size()
virtual size_t size() const
{
if (a_seq == 0)
return 0;
......@@ -232,7 +245,7 @@ public:
*
*/
virtual bool copy(memory & m)
virtual bool copy(const memory & m)
{
return mem->copy(m);
}
......
......@@ -75,7 +75,7 @@ void HeapMemory::destroy()
*
* \param ptr
*/
bool HeapMemory::copyFromPointer(void * ptr,size_t sz)
bool HeapMemory::copyFromPointer(const void * ptr,size_t sz)
{
// memory copy
......@@ -95,7 +95,7 @@ bool HeapMemory::copyFromPointer(void * ptr,size_t sz)
* \param CudaMemory from where to copy
*
*/
bool HeapMemory::copyDeviceToDevice(HeapMemory & m)
bool HeapMemory::copyDeviceToDevice(const HeapMemory & m)
{
//! The source buffer is too big to copy it
......@@ -119,10 +119,10 @@ bool HeapMemory::copyDeviceToDevice(HeapMemory & m)
* \param m a memory interface
*
*/
bool HeapMemory::copy(memory & m)
bool HeapMemory::copy(const memory & m)
{
//! Here we try to cast memory into HeapMemory
HeapMemory * ofpm = dynamic_cast<HeapMemory *>(&m);
const HeapMemory * ofpm = dynamic_cast<const HeapMemory *>(&m);
//! if we fail we get the pointer and simply copy from the pointer
......@@ -149,7 +149,7 @@ bool HeapMemory::copy(memory & m)
*
*/
size_t HeapMemory::size()
size_t HeapMemory::size() const
{
return sz;
}
......@@ -223,3 +223,14 @@ void * HeapMemory::getPointer()
{
return dm;
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
*
*/
const void * HeapMemory::getPointer() const
{
return dm;
}
......@@ -50,10 +50,10 @@ class HeapMemory : public memory
long int ref_cnt;
//! copy from same Heap to Heap
bool copyDeviceToDevice(HeapMemory & m);
bool copyDeviceToDevice(const HeapMemory & m);
//! copy from Pointer to Heap
bool copyFromPointer(void * ptr, size_t sz);
bool copyFromPointer(const void * ptr, size_t sz);
//! Set alignment the memory will be aligned with this number
void setAlignment(size_t align);
......@@ -65,14 +65,17 @@ public:
//! destroy memory
virtual void destroy();
//! copy memory
virtual bool copy(memory & m);
virtual bool copy(const memory & m);
//! the the size of the allocated memory
virtual size_t size();
virtual size_t size() const;
//! resize the memory allocated
virtual bool resize(size_t sz);
//! get a readable pointer with the data
virtual void * getPointer();
//! get a readable pointer with the data
virtual const void * getPointer() const;
//! Increment the reference counter
virtual void incRef()
{ref_cnt++;}
......@@ -97,6 +100,38 @@ public:
return false;
}
// Copy the Heap memory
HeapMemory & operator=(const HeapMemory & mem)
{
copy(mem);
return *this;
}
// Copy the Heap memory
HeapMemory(const HeapMemory & mem)
:HeapMemory()
{
allocate(mem.size());
copy(mem);
}
HeapMemory(HeapMemory && mem) noexcept
{
//! swap
alignement = mem.alignement;
sz = mem.sz;
dm = mem.dm;
dmOrig = mem.dmOrig;
ref_cnt = mem.ref_cnt;
// reset mem
mem.alignement = MEM_ALIGNMENT;
mem.sz = 0;
mem.dm = NULL;
mem.dmOrig = NULL;
mem.ref_cnt = 0;
}
//! Constructor, we choose a default alignment of 32 for avx
HeapMemory():alignement(MEM_ALIGNMENT),sz(0),dm(NULL),dmOrig(NULL),ref_cnt(0) {};
......
......@@ -78,6 +78,27 @@ template<typename T> void test()
//! [Copy Memory]
}
{
T src;
src.allocate(FIRST_ALLOCATION);
unsigned char * ptr = (unsigned char *)src.getPointer();
for (size_t i = 0 ; i < src.size() ; i++)
ptr[i] = i;
T dst = src;
unsigned char * ptr2 = (unsigned char *)dst.getPointer();
BOOST_REQUIRE(src.getPointer() != dst.getPointer());
for (size_t i = 0 ; i < FIRST_ALLOCATION ; i++)
{
unsigned char c=i;
BOOST_REQUIRE_EQUAL(ptr2[i],c);
}
}
}
BOOST_AUTO_TEST_CASE( use )
......
......@@ -120,6 +120,19 @@ public:
return (((unsigned char *)hp.getPointer()) + sequence_c[a_seq-1]);
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
*
*/
const virtual void * getPointer() const
{
if (a_seq == 0)
return NULL;
return (((unsigned char *)hp.getPointer()) + sequence_c[a_seq-1]);
}
/*! \brief Allocate or resize the allocated memory
*
* Resize the allocated memory, if request is smaller than the allocated, memory
......@@ -143,7 +156,7 @@ public:
*
*/
virtual size_t size()
virtual size_t size() const
{
if (a_seq == 0)
return 0;
......@@ -164,7 +177,7 @@ public:
*
*/
virtual bool copy(memory & m)
virtual bool copy(const memory & m)
{
return hp.copy(m);
}
......
......@@ -46,7 +46,7 @@ void PtrMemory::destroy()
*
* \param ptr
*/
bool PtrMemory::copyFromPointer(void * ptr,size_t sz)
bool PtrMemory::copyFromPointer(const void * ptr,size_t sz)
{
// memory copy
......@@ -62,7 +62,7 @@ bool PtrMemory::copyFromPointer(void * ptr,size_t sz)
* \param PtrMemory from where to copy
*
*/
bool PtrMemory::copyDeviceToDevice(PtrMemory & m)
bool PtrMemory::copyDeviceToDevice(const PtrMemory & m)
{
//! The source buffer is too big to copy it
......@@ -82,10 +82,10 @@ bool PtrMemory::copyDeviceToDevice(PtrMemory & m)
* \param m a memory interface
*
*/
bool PtrMemory::copy(memory & m)
bool PtrMemory::copy(const memory & m)
{
//! Here we try to cast memory into PtrMemory
PtrMemory * ofpm = dynamic_cast<PtrMemory *>(&m);
const PtrMemory * ofpm = dynamic_cast<const PtrMemory *>(&m);
//! if we fail we get the pointer and simply copy from the pointer
......@@ -112,9 +112,9 @@ bool PtrMemory::copy(memory & m)
*
*/
size_t PtrMemory::size()
size_t PtrMemory::size() const
{
return sz;
return spm;
}
/*! \brief Resize the allocated memory
......@@ -132,7 +132,7 @@ bool PtrMemory::resize(size_t sz)
// if the allocated memory is enough, do not resize
if (sz <= spm)
{
this->sz = sz;
this->spm = sz;
return true;
}
......@@ -142,6 +142,7 @@ bool PtrMemory::resize(size_t sz)
/*! \brief Return a pointer to the allocated memory
*
* \return the pointer
*
*/
......@@ -150,6 +151,16 @@ void * PtrMemory::getPointer()
return dm;
}
/*! \brief Return a pointer to the allocated memory
*
* \return the pointer
*
*/
const void * PtrMemory::getPointer() const
{
return dm;
}
#endif /* PTRMEMORY_CPP_ */
......@@ -47,17 +47,14 @@ class PtrMemory : public memory
//! Pointed memory
void * dm;
//! Size of the memory
size_t sz;
//! Reference counter
long int ref_cnt;
//! copy from same Heap to Heap
bool copyDeviceToDevice(PtrMemory & m);
bool copyDeviceToDevice(const PtrMemory & m);
//! copy from Pointer to Heap
bool copyFromPointer(void * ptr, size_t sz);
bool copyFromPointer(const void * ptr, size_t sz);
//! Set alignment the memory will be aligned with this number
void setAlignment(size_t align);
......@@ -69,14 +66,17 @@ public:
//! destroy memory
virtual void destroy();
//! copy memory
virtual bool copy(memory & m);
virtual bool copy(const memory & m);
//! the the size of the allocated memory
virtual size_t size();
virtual size_t size() const;
//! resize the memory allocated
virtual bool resize(size_t sz);
//! get a readable pointer with the data
virtual void * getPointer();
//! get a readable pointer with the data
virtual const void * getPointer() const;
//! Increment the reference counter
virtual void incRef()
{ref_cnt++;}
......@@ -102,12 +102,12 @@ public:
}
// Default constructor
PtrMemory():spm(0),dm(NULL),sz(0),ref_cnt(0)
PtrMemory():spm(0),dm(NULL),ref_cnt(0)
{
};
//! Constructor, we choose a default alignment of 32 for avx
PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),sz(0),ref_cnt(0)
PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),ref_cnt(0)
{
};
......
......@@ -55,13 +55,13 @@ class memory
* \param m from where to copy
*
*/
virtual bool copy(memory & m) = 0;
virtual bool copy(const memory & m) = 0;
/*! \brief get the size of the buffer
*
*/
virtual size_t size() = 0;
virtual size_t size() const = 0;
/*! \brief return a data pointer
*
......@@ -71,6 +71,14 @@ class memory
virtual void * getPointer() = 0;
/*! \brief return a data pointer
*
* return readable pointer with the data stored
*
*/
virtual const void * getPointer() const = 0;
/*! \brief destructor
*
* destructor
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment