From 62f314ea9dad883f5de47bbf286e833f0029111c Mon Sep 17 00:00:00 2001 From: Pietro Incardona <incardon@mpi-cbg.de> Date: Wed, 22 Apr 2015 18:47:56 +0200 Subject: [PATCH] Added PtrMemory.cpp --- src/memory/PtrMemory.cpp | 155 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 src/memory/PtrMemory.cpp diff --git a/src/memory/PtrMemory.cpp b/src/memory/PtrMemory.cpp new file mode 100644 index 0000000..3c19961 --- /dev/null +++ b/src/memory/PtrMemory.cpp @@ -0,0 +1,155 @@ +/* + * PtrMemory.cpp + * + * Created on: Apr 15, 2015 + * Author: i-bird + */ + +#ifndef PTRMEMORY_CPP_ +#define PTRMEMORY_CPP_ + +#include "PtrMemory.hpp" +#include <cstddef> +#include <cstring> +#include <iostream> +#include <cstdint> + +// If debugging mode include memory leak check +#ifdef MEMLEAK_CHECK +#include "Memleak_check.hpp" +#endif + +/*! \brief Allocate a chunk of memory + * + * \param sz size of the chunk of memory to allocate in byte + * + */ + +bool PtrMemory::allocate(size_t sz) +{ + if (sz <= spm) + return true; + + std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed"; + return false; +} + +/*! \brief destroy a chunk of memory + * + */ +void PtrMemory::destroy() +{ +} + + +/*! \brief copy the data from a pointer + * + * \param ptr + */ +bool PtrMemory::copyFromPointer(void * ptr,size_t sz) +{ + // memory copy + + memcpy(dm,ptr,sz); + + return true; +} + +/*! \brief copy from device to device + * + * copy a piece of memory from device to device + * + * \param PtrMemory from where to copy + * + */ +bool PtrMemory::copyDeviceToDevice(PtrMemory & m) +{ + //! The source buffer is too big to copy it + + if (m.spm > spm) + { + std::cerr << "Error " << __LINE__ << " " << __FILE__ << ": source buffer is too big to copy"; + return false; + } + + // Copy the memory from m + memcpy(dm,m.dm,m.spm); + return true; +} + +/*! \brief copy the memory + * + * \param m a memory interface + * + */ +bool PtrMemory::copy(memory & m) +{ + //! Here we try to cast memory into PtrMemory + PtrMemory * ofpm = dynamic_cast<PtrMemory *>(&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(),m.size()); + } + 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 PtrMemory::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 PtrMemory::resize(size_t sz) +{ + // if the allocated memory is enough, do not resize + if (sz <= spm) + { + this->sz = sz; + return true; + } + + std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed"; + return false; +} + +/*! \brief Return a pointer to the allocated memory + * + * + */ + +void * PtrMemory::getPointer() +{ + return dm; +} + + + +#endif /* PTRMEMORY_CPP_ */ -- GitLab