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

Add HeapMemory

parent fbfb8b4a
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
LINKLIBS = $(PTHREAD_LIBS) $(OPT_LIBS) $(BOOST_LDFLAGS) $(BOOST_PROGRAM_OPTIONS_LIB) $(CUDA_LIBS) $(BOOST_THREAD_LIB)
bin_PROGRAMS = ppline
ppline_SOURCES = main.cpp
ppline_SOURCES = main.cpp Memleak_check.cpp
ppline_CXXFLAGS = $(CUDA_CFLAGS)
ppline_CFLAGS = $(CUDA_CFLAGS)
ppline_LDADD = $(LINKLIBS) -L/usr/lib64/nvidia-bumblebee/
......
#include "config.h"
#include <cstddef>
#include <cuda_runtime.h>
#include "CudaMemory.cuh"
#include "cuda_macro.h"
#include <cstring>
#include "Memleak_check.hpp"
/*! \brief Allocate a chunk of memory
*
......@@ -30,7 +31,21 @@ bool CudaMemory::allocate(size_t sz)
*/
void CudaMemory::destroy()
{
CUDA_SAFE_CALL(cudaFree(dm));
if (dm != NULL)
{
//! Release the allocated memory
CUDA_SAFE_CALL(cudaFree(dm));
}
if (hm != NULL)
{
//! we invalidate hm
CUDA_SAFE_CALL(cudaFreeHost(hm));
#ifdef MEMLEAK_CHECK
//! remove hm
check_delete(hm);
#endif
}
}
/*! \brief Allocate the host buffer
......@@ -42,7 +57,13 @@ void CudaMemory::destroy()
void CudaMemory::allocate_host(size_t sz)
{
if (hm == NULL)
{CUDA_SAFE_CALL(cudaHostAlloc(&hm,sz,cudaHostAllocMapped))}
{
CUDA_SAFE_CALL(cudaHostAlloc(&hm,sz,cudaHostAllocMapped))
#ifdef MEMLEAK_CHECK
//! add hm to the list of allocated memory
check_new(hm,sz);
#endif
}
}
/*! \brief copy the data from a pointer
......@@ -147,6 +168,10 @@ size_t CudaMemory::size()
bool CudaMemory::resize(size_t sz)
{
// if the allocated memory is enough, do not resize
if (sz <= size())
return true;
//! Allocate the device memory if not done yet
if (size() == 0)
......
......@@ -8,7 +8,7 @@
/**
* \brief This class create instructions to allocate, and destroy GPU memory
*
* This class create instructions to allocate, destroy, resize GPU buffer,
* This class allocate, destroy, resize GPU buffer,
* eventually if direct, comunication is not supported, it can instruction
* to create an Host Pinned memory.
*
......@@ -16,9 +16,9 @@
*
* CudaMemory m = new CudaMemory();
*
* m.allocate();
* m.allocate(1000*sizeof(int));
* int * ptr = m.getPointer();
* *ptr[i] = 1000;
* ptr[999] = 1000;
* ....
*
*
......@@ -28,6 +28,7 @@
#define CUDA_MEMORY_CUH_
#include "memory.hpp"
#include <iostream>
class CudaMemory : public memory
{
......@@ -43,6 +44,9 @@ class CudaMemory : public memory
//! host memory
void * hm;
//! Reference counter
size_t ref_cnt;
//! Allocate an host buffer
void allocate_host(size_t sz);
......@@ -72,8 +76,31 @@ class CudaMemory : public memory
public:
//! Increment the reference counter
virtual void incRef()
{ref_cnt++;}
//! Decrement the reference counter
virtual void decRef()
{ref_cnt--;}
//! Return the reference counter
virtual long int ref()
{
return ref_cnt;
}
//! Constructor
CudaMemory():is_hm_sync(false),sz(0),hm(0) {};
CudaMemory():is_hm_sync(false),sz(0),dm(0),hm(0),ref_cnt(0) {};
//! Destructor
~CudaMemory()
{
if(ref_cnt == 0)
destroy();
else
std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
};
};
#endif
......
......@@ -7,27 +7,50 @@
#include "HeapMemory.hpp"
#include <cstddef>
#include <cstring>
#include <iostream>
#include <cstdint>
typedef unsigned char byte;
// If debugging mode include memory leak check
#ifdef MEMLEAK_CHECK
#include "Memleak_check.hpp"
#endif
/*! \brief Allocate a chunk of memory
*
* Allocate a chunk of memory
*
* \param sz size of the chunk of memory to allocate in byte
*
*/
bool HeapMemory::allocate(size_t sz)
{
//! Allocate the device memory
if (dm == NULL)
dm = new byte[sz];
dmOrig = new byte[sz+alignement];
dm = dmOrig;
#ifdef MEMLEAK_CHECK
check_new(dmOrig,sz+alignement);
#endif
// align it, we do not know the size of the element we put 1
// and we ignore the align check
size_t sz_a = sz+alignement;
align(alignement,1,(void *&)dm,sz_a);
this->sz = sz;
return true;
}
/*! \brief set the memory block to be aligned by this number
*
*/
void HeapMemory::setAlignment(size_t align)
{
this->alignement = align;
}
/*! \brief destroy a chunk of memory
*
* Destroy a chunk of memory
......@@ -35,21 +58,27 @@ bool HeapMemory::allocate(size_t sz)
*/
void HeapMemory::destroy()
{
delete [] dm;
if (dmOrig != NULL)
delete [] dmOrig;
#ifdef MEMLEAK_CHECK
check_delete(dmOrig);
#endif
}
/*! \brief copy the data from a pointer
*
* copy the data from a pointer
*
* \param ptr
*/
void HeapMemory::copyFromPointer(void * ptr)
bool HeapMemory::copyFromPointer(void * ptr,size_t sz)
{
// memory copy
memcpy(ptr,dm,sz);
memcpy(dm,ptr,sz);
return true;
}
/*! \brief copy from device to device
......@@ -59,30 +88,30 @@ void HeapMemory::copyFromPointer(void * ptr)
* \param CudaMemory from where to copy
*
*/
void HeapMemory::copyDeviceToDevice(HeapMemory & m)
bool HeapMemory::copyDeviceToDevice(HeapMemory & m)
{
//! The source buffer is too big to copy it
if (m.sz > sz)
{
std::cerr << "Error " << __LINE__ << __FILE__ << ": source buffer is too big to copy";
return;
return false;
}
memcpy(m.dm,dm,m.sz);
// Copy the memory from m
memcpy(dm,m.dm,m.sz);
return true;
}
/*! \brief copy from memory
*
* copy from memory
/*! \brief copy the memory
*
* \param m a memory interface
*
*/
bool HeapMemory::copy(memory & m)
{
//! Here we try to cast memory into OpenFPMwdeviceCudaMemory
HeapMemory * ofpm = dynamic_cast<CudaMemory>(m);
//! Here we try to cast memory into HeapMemory
HeapMemory * ofpm = dynamic_cast<HeapMemory *>(&m);
//! if we fail we get the pointer and simply copy from the pointer
......@@ -90,7 +119,7 @@ bool HeapMemory::copy(memory & m)
{
// copy the memory from device to host and from host to device
return copyFromPointer(m.getPointer());
return copyFromPointer(m.getPointer(),m.size());
}
else
{
......@@ -126,18 +155,30 @@ size_t HeapMemory::size()
bool HeapMemory::resize(size_t sz)
{
// if the allocated memory is enough, do not resize
if (sz <= size())
return true;
//! Allocate the device memory if not done yet
if (size() == 0)
return allocate(sz);
//! Create a new buffer if sz is bigger than the actual size
void * tdm;
tdm = new byte[sz];
byte * tdm;
byte * tdmOrig;
tdmOrig = new byte[sz+alignement];
tdm = tdmOrig;
//! size plus alignment
size_t sz_a = sz+alignement;
//! align it
align(alignement,1,(void *&)tdm,sz_a);
//! copy from the old buffer to the new one
memcpy(dm,tdm,size());
memcpy(tdm,dm,size());
//! free the old buffer
......@@ -146,7 +187,8 @@ bool HeapMemory::resize(size_t sz)
//! change to the new buffer
dm = tdm;
this->sz = sz;
dmOrig = tdmOrig;
this->sz = sz_a;
return true;
}
......
/*
* HeapMempory.hpp
*
* Created on: Aug 17, 2014
* Author: Pietro Incardona
*/
/**
* \brief This class allocate, and destroy CPU memory
*
* Usage:
*
* HeapMemory m = new HeapMemory();
*
* m.allocate(1000*sizeof(int));
* int * ptr = m.getPointer();
* ptr[999] = 1000;
* ....
*
*
*/
#ifndef HEAP_MEMORY_HPP
#define HEAP_MEMORY_HPP
#include "config.h"
#include "memory.hpp"
#include <cstddef>
#include <cstdint>
#include <iostream>
typedef unsigned char byte;
#define MEM_ALIGNMENT 32
class HeapMemory : public memory
{
//! memory alignment
size_t alignement;
//! Size of the memory
size_t sz;
//! device memory
byte * dm;
//! original pointer (before alignment)
byte * dmOrig;
//! Reference counter
long int ref_cnt;
//! copy from same Heap to Heap
bool copyDeviceToDevice(HeapMemory & m);
//! copy from Pointer to Heap
bool copyFromPointer(void * ptr, size_t sz);
//! Set alignment the memory will be aligned with this number
void setAlignment(size_t align);
public:
//! allocate memory
virtual bool allocate(size_t sz);
//! destroy memory
virtual void destroy();
//! copy memory
virtual bool copy(memory & m);
//! the the size of the allocated memory
virtual size_t size();
//! resize the memory allocated
virtual bool resize(size_t sz);
//! get a readable pointer with the data
virtual void * getPointer();
//! Increment the reference counter
virtual void incRef()
{ref_cnt++;}
//! Decrement the reference counter
virtual void decRef()
{ref_cnt--;}
//! Return the reference counter
virtual long int ref()
{
return ref_cnt;
}
//! Constructor, we choose a default alignment of 32 for avx
HeapMemory():alignement(MEM_ALIGNMENT),sz(0),dm(NULL),dmOrig(NULL),ref_cnt(0) {};
~HeapMemory()
{
if(ref_cnt == 0)
destroy();
else
std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
};
};
/*! \brief function to align a pointer equivalent to std::align
*
* function to align a pointer equivalent to std::align
*
*/
inline void *align( std::size_t alignment, std::size_t size,
void *&ptr, std::size_t &space ) {
std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr );
std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment;
std::size_t padding = aligned - pn;
if ( space < size + padding ) return nullptr;
space -= padding;
return ptr = reinterpret_cast< void * >( aligned );
}
#endif
/*
* HeapMempory.hpp
*
* Created on: Aug 17, 2014
* Author: Pietro Incardona
*/
/**
* \brief This class create instructions to allocate, and destroy CPU memory
*
* This class create instructions to allocate, destroy, resize CPU buffer,
*
* Usage:
*
* CudaMemory m = new CudaMemory();
*
* m.allocate();
* int * ptr = m.getPointer();
* *ptr[i] = 1000;
* ....
*
*
*/
#include "memory.hpp"
#include <cstddef>
class HeapMemory : public memory
{
//! Size of the memory
size_t sz;
//! device memory
void * dm;
//! allocate memory
virtual bool allocate(size_t sz);
//! destroy memory
virtual void destroy();
//! copy memory
virtual bool copy(memory & m);
//! the the size of the allocated memory
virtual size_t size();
//! resize the memory allocated
virtual bool resize(size_t sz);
//! get a readable pointer with the data
void * getPointer()
//! copy from same Heap to Heap
void copyDeviceToDevice(HeapMemory & m);
//! copy from Pointer to Heap
void copyFromPointer(void * ptr);
//! copy from a General device
bool copy(memory & m);
//! Constructor
HeapMemory():dm(NULL),sz(0) {};
~HeapMemory() {};
};
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