OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
HeapMemory.hpp
1/*
2 * HeapMempory.hpp
3 *
4 * Created on: Aug 17, 2014
5 * Author: Pietro Incardona
6 */
7
8#ifndef HEAP_MEMORY_HPP
9#define HEAP_MEMORY_HPP
10
11#include "config.h"
12#include "memory.hpp"
13#include <cstddef>
14#include <cstdint>
15#include <iostream>
16
17typedef unsigned char byte;
18
19#define MEM_ALIGNMENT 32
20
21
39class HeapMemory : public memory
40{
42 size_t alignement;
43
45 size_t sz;
46
48 byte * dm;
50 byte * dmOrig;
52 long int ref_cnt;
53
55 bool copyFromPointer(const void * ptr, size_t sz);
56
58 void setAlignment(size_t align);
59
60public:
61
63 bool copyDeviceToDevice(const HeapMemory & m);
64
66 virtual bool flush() {return true;};
68 virtual bool allocate(size_t sz);
70 virtual void destroy();
72 virtual bool copy(const memory & m);
74 virtual size_t size() const;
76 virtual bool resize(size_t sz);
78 virtual void * getPointer();
79
81 virtual const void * getPointer() const;
82
84 virtual void * getDevicePointer();
85
90 virtual void fill(unsigned char c);
91
93 virtual void hostToDevice(){};
94
96 virtual void deviceToHost(){};
97
99 virtual void deviceToHost(size_t start, size_t stop) {};
100
102 virtual void hostToDevice(size_t start, size_t stop) {};
103
105 virtual void incRef()
106 {ref_cnt++;}
107
109 virtual void decRef()
110 {ref_cnt--;}
111
113 virtual long int ref()
114 {
115 return ref_cnt;
116 }
117
124 {
125 return false;
126 }
127
128 // Copy the Heap memory
129 HeapMemory & operator=(const HeapMemory & mem)
130 {
131 copy(mem);
132 return *this;
133 }
134
135 // Copy the Heap memory
136 HeapMemory(const HeapMemory & mem)
137 :HeapMemory()
138 {
139 allocate(mem.size());
140 copy(mem);
141 }
142
143 HeapMemory(HeapMemory && mem) noexcept
144 {
146 alignement = mem.alignement;
147 sz = mem.sz;
148 dm = mem.dm;
149 dmOrig = mem.dmOrig;
150 ref_cnt = mem.ref_cnt;
151
152 mem.alignement = MEM_ALIGNMENT;
153 mem.sz = 0;
154 mem.dm = NULL;
155 mem.dmOrig = NULL;
156 mem.ref_cnt = 0;
157 }
158
160 HeapMemory():alignement(MEM_ALIGNMENT),sz(0),dm(NULL),dmOrig(NULL),ref_cnt(0) {};
161
162 virtual ~HeapMemory() noexcept
163 {
164 if(ref_cnt == 0)
166 else
167 std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
168 };
169
175 void swap(HeapMemory & mem)
176 {
177 size_t alignement_tmp;
178 size_t sz_tmp;
179 byte * dm_tmp;
180 byte * dmOrig_tmp;
181 long int ref_cnt_tmp;
182
183 alignement_tmp = alignement;
184 sz_tmp = sz;
185 dm_tmp = dm;
186 dmOrig_tmp = dmOrig;
187 ref_cnt_tmp = ref_cnt;
188
190 sz = mem.sz;
191 dm = mem.dm;
192 dmOrig = mem.dmOrig;
193 ref_cnt = mem.ref_cnt;
194
195 mem.alignement = alignement_tmp;
196 mem.sz = sz_tmp;
197 mem.dm = dm_tmp;
198 mem.dmOrig = dmOrig_tmp;
199 mem.ref_cnt = ref_cnt_tmp;
200 }
201
207 constexpr static bool isDeviceHostSame()
208 {
209 return true;
210 }
211};
212
223inline size_t align_number(size_t al, size_t number)
224{
225 return number + ((number % al) != 0)*(al - number % al);
226}
227
234inline void *align( std::size_t alignment, std::size_t size,
235 void *&ptr, std::size_t &space ) {
236 std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr );
237 std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment;
238 std::size_t padding = aligned - pn;
239 if ( space < size + padding ) return nullptr;
240 space -= padding;
241 return ptr = reinterpret_cast< void * >( aligned );
242}
243
244#endif
This class allocate, and destroy CPU memory.
virtual void decRef()
Decrement the reference counter.
HeapMemory(HeapMemory &&mem) noexcept
virtual bool flush()
flush the memory
virtual void incRef()
Increment the reference counter.
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
byte * dmOrig
original pointer (before alignment)
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
static constexpr bool isDeviceHostSame()
Return true if the device and the host pointer are the same.
virtual long int ref()
Return the reference counter.
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
virtual void hostToDevice(size_t start, size_t stop)
Do nothing.
virtual bool resize(size_t sz)
resize the memory allocated
virtual void * getPointer()
get a readable pointer with the data
virtual void deviceToHost(size_t start, size_t stop)
Do nothing.
virtual size_t size() const
the the size of the allocated memory
virtual void deviceToHost()
Do nothing.
virtual bool copy(const memory &m)
copy memory
long int ref_cnt
Reference counter.
bool isInitialized()
Allocated Memory is never initialized.
void swap(HeapMemory &mem)
Swap the memory.
virtual void hostToDevice()
Do nothing.
HeapMemory()
Constructor, we choose a default alignment of 32 for avx.
size_t alignement
memory alignment
size_t sz
Size of the memory.
virtual void * getDevicePointer()
get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents
virtual void destroy()
destroy memory
bool copyDeviceToDevice(const HeapMemory &m)
copy from same Heap to Heap
byte * dm
device memory
this class is a functor for "for_each" algorithm