Newer
Older
/*
* HeapMemory.cpp
*
* Created on: Aug 17, 2014
* Author: Pietro Incardona
*/
#include "HeapMemory.hpp"

Pietro Incardona
committed
#include <cstddef>

Pietro Incardona
committed
typedef unsigned char byte;

Pietro Incardona
committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*! \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;
}