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

Adding memory.hpp to devices

parent f6b24b7b
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,8 @@
* External pre-allocated memory, is a class that preallocate memory and than it answer
* to a particular allocation pattern
*
* \warning zero sized allocation are removed from the request pattern
*
* \tparam Base memory allocation class [Example] HeapMemory or CudaMemory
*
*
......@@ -49,8 +51,6 @@ public:
ExtPreAlloc()
:a_seq(0),ref_cnt(0)
{
int debug = 0;
debug++;
}
/*! \brief Preallocated memory sequence
......@@ -64,6 +64,15 @@ public:
{
size_t total_size = 0;
// remove zero size request
for (std::vector<size_t>::iterator it=seq.begin(); it!=seq.end(); )
{
if(*it == 0)
it = seq.erase(it);
else
++it;
}
// Resize the sequence
sequence.resize(seq.size());
sequence_c.resize(seq.size());
......@@ -72,6 +81,7 @@ public:
sequence[i] = seq[i];
sequence_c[i] = total_size;
total_size += seq[i];
}
// Allocate the total size of memory
......@@ -101,11 +111,15 @@ public:
*/
virtual bool allocate(size_t sz)
{
// Zero sized allocation are ignored
if (sz == 0)
return true;
// Check that the size match
if (sequence[a_seq] != sz)
{
std::cerr << "Error: " << __FILE__ << " " << __LINE__ << "expecting: " << sequence[a_seq] << " got: " << sz << " allocation failed \n";
std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " expecting: " << sequence[a_seq] << " got: " << sz << " allocation failed \n";
std::cerr << "NOTE: if you are using this structure with vector remember to use openfpm::vector<...>::calculateMem(...) to get the required allocation sequence\n";
return false;
......
/*
* memory.hpp
*
* Created on: Aug 8, 2014
* Author: Pietro Incardona
*/
/*! \brief This class is an interface
*
* This class is an interface to allocate memory
*
*/
#ifndef MEMORY_HPP_
#define MEMORY_HPP_
typedef long int mem_id;
#include "config.h"
#include <stddef.h>
class memory
{
public:
/*! \brief allocate on device a buffer of
*
* Allocate on the device a buffer of memory
*
* \param sz the size of the buffer
*
*/
virtual bool allocate(size_t sz) = 0;
/*! \brief resize on device a buffer
*
* On the device resize
*
* \param sz the size the resized buffer
*
*/
virtual bool resize(size_t sz) = 0;
/*! \brief Destroy the buffer on the device
*
* destroy the buffer on the device
*
*/
virtual void destroy() = 0;
/*! \brief Copy the memory from one device to another device
*
* \param m from where to copy
*
*/
virtual bool copy(memory & m) = 0;
/*! \brief get the size of the buffer
*
*/
virtual size_t size() = 0;
/*! \brief return a data pointer
*
* return readable pointer with the data stored
*
*/
virtual void * getPointer() = 0;
/*! \brief destructor
*
* destructor
*
*/
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;
/*! \brief Return if the actual memory that is going to be allocated is already initialized
*
* \return true if already initialized
*
*/
virtual bool isInitialized() = 0;
};
#endif
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