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

Changes to make it compatible with the new OpenFPM_data project aka OpenFPM_mem_manager

parent 4a4c8ba5
No related branches found
No related tags found
No related merge requests found
SUBDIRS = src
bin_PROGRAMS =
\ No newline at end of file
autogen.sh 0 → 100755
This diff is collapsed.
......@@ -2,6 +2,8 @@
#include <cuda_runtime.h>
#include "CudaMemory.cuh"
#include "cuda_macro.h"
#include <cstring>
/*! \brief Allocate a chunk of memory
*
......@@ -14,7 +16,11 @@ bool CudaMemory::allocate(size_t sz)
{
//! Allocate the device memory
if (dm == NULL)
{CUDA_SAFE_CALL(cudaAlloc(&dv,sz,cudaAlloc));}
{CUDA_SAFE_CALL(cudaMalloc(&dm,sz));}
this->sz = sz;
return true;
}
/*! \brief destroy a chunk of memory
......@@ -24,39 +30,81 @@ bool CudaMemory::allocate(size_t sz)
*/
void CudaMemory::destroy()
{
CUDA_SAFE_CALL(cudaDestroy(dv));
CUDA_SAFE_CALL(cudaFree(dm));
}
/*! \brief copy the device memory to a pointer
/*! \brief Allocate the host buffer
*
* copy the device memory to a pointer
* Allocate the host buffer
*
*/
void CudaMemory::copyToPointer()
void CudaMemory::allocate_host(size_t sz)
{
if (hm == NULL)
{CUDA_SAFE_CALL(cudaHostAlloc(&hm,sz,cudaHostAllocMapped))}
}
/*! \brief copy the data from a pointer
*
* copy the data from a pointer
*
* \param ptr
* \return true if success
*/
bool CudaMemory::copyFromPointer(void * ptr)
{
// check if we have a host buffer, if not allocate it
if (hm == NULL)
{CUDA_SAFE_CALL(cudaHostAlloc(&hm,sz*sizeof(T),cudaHostAllocMapped))}
allocate_host(sz);
// get the device pointer
// put on queue a copy from device to host
void * dvp;
CUDA_SAFE_CALL(cudaHostGetDevicePointer(&dvp,hm,0));
t.call();
// memory copy
// put on queue a memory copy from pointers
memcpy(ptr,dvp,sz);
return true;
}
void CudaMemory::copyDeviceToDevice()
/*! \brief copy from device to device
*
* copy a piece of memory from device to device
*
* \param CudaMemory from where to copy
*
* \return true is success
*/
bool CudaMemory::copyDeviceToDevice(CudaMemory & m)
{
// put on queue a copy from device to device
//! 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 false;
}
t.call();
//! Copy the memory
CUDA_SAFE_CALL(cudaMemcpy(m.dm,dm,m.sz,cudaMemcpyDeviceToDevice));
return true;
}
bool CudaMemory::copy(memory m)
/*! \brief copy from memory
*
* copy from memory
*
* \param m a memory interface
*
*/
bool CudaMemory::copy(memory & m)
{
//! Here we try to cast memory into OpenFPMwdeviceCudaMemory
CudaMemory * ofpm = dynamic_cast<CudaMemory>(m);
CudaMemory * ofpm = dynamic_cast<CudaMemory *>(&m);
//! if we fail we get the pointer and simply copy from the pointer
......@@ -64,33 +112,91 @@ bool CudaMemory::copy(memory m)
{
// copy the memory from device to host and from host to device
copyFromPointer(t);
return copyFromPointer(m.getPointer());
}
else
{
// they are the same memory type, use cuda/thrust buffer copy
copyDeviceToDevice();
return copyDeviceToDevice(*ofpm);
}
}
bool CudaMemory::copy(OpenFPMwdeviceCudaMemory m)
{
// they are the same type of memory so copy from device to device
copyDeviceToDevice();
}
/*! \brief Get the size of the allocated memory
*
* Get the size of the allocated memory
*
* \return the size of the allocated memory
*
*/
size_t CudaMemory::size()
{
dv->size();
return sz;
}
/*! \brief Resize the allocated memory
*
* Resize the allocated memory, if request is smaller than the allocated memory
* is not resized
*
* \param sz size
* \return true if the resize operation complete correctly
*
*/
bool CudaMemory::resize(size_t sz)
{
//! Allocate the device memory
if (dv == NULL)
{dv = new boost::shared_ptr<void>(new thrust::device_vector<void>());}
else
{dv.get()->resize(sz);}
//! 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;
if (this->sz < sz)
CUDA_SAFE_CALL(cudaMalloc(&tdm,sz));
//! copy from the old buffer to the new one
CUDA_SAFE_CALL(cudaMemcpy(dm,tdm,size(),cudaMemcpyDeviceToDevice));
//! free the old buffer
destroy();
//! change to the new buffer
dm = tdm;
this->sz = sz;
return true;
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
*
*/
void * CudaMemory::getPointer()
{
//! if the host buffer is synchronized with the device buffer return the host buffer
if (is_hm_sync)
return hm;
//! we have to synchronize
//| allocate an host mempory
if (hm == NULL)
allocate_host(sz);
//! copy from device to host memory
CUDA_SAFE_CALL(cudaMemcpy(dm,hm,sz,cudaMemcpyDeviceToHost));
return hm;
}
......@@ -14,33 +14,67 @@
*
* Usage:
*
* TO DO
* CudaMemory m = new CudaMemory();
*
* m.allocate();
* int * ptr = m.getPointer();
* *ptr[i] = 1000;
* ....
*
* This class in general is used by OpenFPM_data project to basically
* record all the operation made and generate a set of instructions
*
*/
#ifndef CUDA_MEMORY_CUH_
#define CUDA_MEMORY_CUH_
#include "memory.hpp"
class CudaMemory : public memory
{
//!
//! Is the host memory synchronized with the GPU memory
bool is_hm_sync;
//! Size of the memory
size_t sz;
//! device memory
void * dm;
//! host memory
void * hm;
//! Allocate an host buffer
void allocate_host(size_t sz);
//! allocate memory
virtual bool allocate(size_t sz);
//! destroy memory
virtual void destroy();
//! copy memory
virtual bool copy(memory m);
//! copy from a General device
virtual bool copy(memory & m);
//! the the size of the allocated memory
virtual size_t size();
//! resize the momory allocated
virtual bool resize(size_t sz);
//! get a readable pointer with the data
void * getPointer();
//! copy from GPU to GPU buffer directly
bool copyDeviceToDevice(CudaMemory & m);
//! copy from Pointer to GPU
bool copyFromPointer(void * ptr);
//! This function notify that the device memory is not sync with
//! the host memory, is called when a task is performed that write
//! on the buffer
void isNotSync() {is_hm_sync = false;}
public:
//! Constructor
CudaMemory():is_hm_sync(false),sz(0),hm(0) {};
};
#endif
......@@ -6,5 +6,158 @@
*/
#include "HeapMemory.hpp"
#include <cstddef>
typedef unsigned char byte;
/*! \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];
this->sz = sz;
return true;
}
/*! \brief destroy a chunk of memory
*
* Destroy a chunk of memory
*
*/
void HeapMemory::destroy()
{
delete [] dm;
}
/*! \brief copy the data from a pointer
*
* copy the data from a pointer
*
* \param ptr
*/
void HeapMemory::copyFromPointer(void * ptr)
{
// memory copy
memcpy(ptr,dm,sz);
}
/*! \brief copy from device to device
*
* copy a piece of memory from device to device
*
* \param CudaMemory from where to copy
*
*/
void 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;
}
memcpy(m.dm,dm,m.sz);
}
/*! \brief copy from memory
*
* copy from 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);
//! if we fail we get the pointer and simply copy from the pointer
if (ofpm == NULL)
{
// copy the memory from device to host and from host to device
return copyFromPointer(m.getPointer());
}
else
{
// they are the same memory type, use cuda/thrust buffer copy
return copyDeviceToDevice(*ofpm);
}
return false;
}
/*! \brief Get the size of the allocated memory
*
* Get the size of the allocated memory
*
* \return the size of the allocated memory
*
*/
size_t HeapMemory::size()
{
return sz;
}
/*! \brief Resize the allocated memory
*
* Resize the allocated memory, if request is smaller than the allocated memory
* is not resized
*
* \param sz size
* \return true if the resize operation complete correctly
*
*/
bool HeapMemory::resize(size_t sz)
{
//! 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];
//! copy from the old buffer to the new one
memcpy(dm,tdm,size());
//! free the old buffer
destroy();
//! change to the new buffer
dm = tdm;
this->sz = sz;
return true;
}
/*! \brief Return a readable pointer with your data
*
* Return a readable pointer with your data
*
*/
void * HeapMemory::getPointer()
{
return dm;
}
......@@ -5,8 +5,59 @@
* Author: Pietro Incardona
*/
class HeapMemory
/**
* \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