OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
HeapMemory.cpp
1/*
2 * HeapMemory.cpp
3 *
4 * Created on: Aug 17, 2014
5 * Author: Pietro Incardona
6 */
7
8#include "HeapMemory.hpp"
9#include <cstddef>
10#include <cstring>
11#include <iostream>
12#include <cstdint>
13
14static const int extra_pad = 512;
15
16
22void HeapMemory::fill(unsigned char c)
23{
24 memset(dm,c,size());
25}
26
33bool HeapMemory::allocate(size_t sz)
34{
36 if (dm == NULL)
37 {
38 dmOrig = new byte[sz+alignement+extra_pad];
39 #ifdef GARBAGE_INJECTOR
40 memset(dmOrig,0xFF,sz+alignement+extra_pad);
41 #endif
42 }
43 else
44 {
45 std::cerr << __FILE__ << ":" << __LINE__ << " error memory already allocated\n";
46 return false;
47 }
48
49 dm = dmOrig;
50
51 // align it, we do not know the size of the element we put 1
52 // and we ignore the align check
53 size_t sz_a = sz+alignement;
54 align(alignement,1,(void *&)dm,sz_a);
55
56 this->sz = sz;
57
58 return true;
59}
60
64void HeapMemory::setAlignment(size_t align)
65{
66 this->alignement = align;
67}
68
74{
75 if (dmOrig != NULL)
76 delete [] dmOrig;
77
78 sz = 0;
79 dm = NULL;
80 dmOrig = NULL;
81}
82
83
89bool HeapMemory::copyFromPointer(const void * ptr,size_t sz)
90{
91 memcpy(dm,ptr,sz);
92
93 return true;
94}
95
104{
106
107 if (m.sz > sz)
108 {
109 std::cerr << "Error " << __LINE__ << __FILE__ << ": source buffer is too big to copy";
110 return false;
111 }
112
113 // Copy the memory from m
114 memcpy(dm,m.dm,m.sz);
115 return true;
116}
117
124{
126 const HeapMemory * ofpm = dynamic_cast<const HeapMemory *>(&m);
127
129
130 if (ofpm == NULL)
131 {
132 // copy the memory from device to host and from host to device
133
134 return copyFromPointer(m.getPointer(),m.size());
135 }
136 else
137 {
138 // they are the same memory type, use cuda/thrust buffer copy
139
140 return copyDeviceToDevice(*ofpm);
141 }
142 return false;
143}
144
153size_t HeapMemory::size() const
154{
155 return sz;
156}
157
167bool HeapMemory::resize(size_t sz)
168{
169 // if the allocated memory is enough, do not resize
170 if (sz <= HeapMemory::size())
171 return true;
172
174
175 if (HeapMemory::size() == 0)
176 return HeapMemory::allocate(sz);
177
179 byte * tdm;
180 byte * tdmOrig;
181 tdmOrig = new byte[sz+alignement+extra_pad];
182 #ifdef GARBAGE_INJECTOR
183 memset(tdmOrig,0xFF,sz+alignement+extra_pad);
184 #endif
185 tdm = tdmOrig;
186
188 size_t sz_a = sz+alignement;
189
191 align(alignement,1,(void *&)tdm,sz_a);
192
194
195 memcpy(tdm,dm,HeapMemory::size());
196
197 this->sz = sz;
198
200
202
204
205 dm = tdm;
206 dmOrig = tdmOrig;
207 this->sz = sz;
208
209 return true;
210}
211
219{
220 return dm;
221}
222
229{
230 return dm;
231}
232
239const void * HeapMemory::getPointer() const
240{
241 return dm;
242}
This class allocate, and destroy CPU memory.
virtual bool allocate(size_t sz)
allocate memory
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.
virtual void fill(unsigned char c)
fill host and device memory with the selected byte
virtual bool resize(size_t sz)
resize the memory allocated
virtual void * getPointer()
get a readable pointer with the data
virtual size_t size() const
the the size of the allocated memory
virtual bool copy(const memory &m)
copy memory
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
virtual void * getPointer()=0
return a data pointer
virtual size_t size() const =0
get the size of the buffer