OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
PtrMemory.cpp
1 /*
2  * PtrMemory.cpp
3  *
4  * Created on: Apr 15, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef PTRMEMORY_CPP_
9 #define PTRMEMORY_CPP_
10 
11 #include "PtrMemory.hpp"
12 #include <cstddef>
13 #include <cstring>
14 #include <iostream>
15 #include <cstdint>
16 
22 void PtrMemory::fill(unsigned char c)
23 {
24  memset(dm,c,this->size());
25 }
26 
33 bool PtrMemory::allocate(size_t sz)
34 {
35  if (sz <= spm)
36  return true;
37 
38  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed";
39  return false;
40 }
41 
46 {
47 }
48 
49 
54 bool PtrMemory::copyFromPointer(const void * ptr,size_t sz)
55 {
56  // memory copy
57 
58  memcpy(dm,ptr,sz);
59 
60  return true;
61 }
62 
73 {
75 
76  if (m.spm > spm)
77  {
78  std::cerr << "Error " << __LINE__ << " " << __FILE__ << ": source buffer is too big to copy";
79  return false;
80  }
81 
82  // Copy the memory from m
83  memcpy(dm,m.dm,m.spm);
84  return true;
85 }
86 
92 bool PtrMemory::copy(const memory & m)
93 {
95  const PtrMemory * ofpm = dynamic_cast<const PtrMemory *>(&m);
96 
98 
99  if (ofpm == NULL)
100  {
101  // copy the memory from device to host and from host to device
102 
103  return copyFromPointer(m.getPointer(),m.size());
104  }
105  else
106  {
107  // they are the same memory type, use cuda/thrust buffer copy
108 
109  return copyDeviceToDevice(*ofpm);
110  }
111  return false;
112 }
113 
122 size_t PtrMemory::size() const
123 {
124  return spm;
125 }
126 
137 bool PtrMemory::resize(size_t sz)
138 {
139  // if the allocated memory is enough, do not resize
140  if (sz <= spm)
141  {
142  this->spm = sz;
143  return true;
144  }
145 
146  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed";
147  return false;
148 }
149 
157 {
158  return dm;
159 }
160 
168 {
169  return dm;
170 }
171 
172 
179 const void * PtrMemory::getPointer() const
180 {
181  return dm;
182 }
183 
184 
185 #endif /* PTRMEMORY_CPP_ */
virtual void * getDevicePointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:167
virtual size_t size() const =0
get the size of the buffer
virtual bool resize(size_t sz)
resize the memory allocated
Definition: PtrMemory.cpp:137
bool copyDeviceToDevice(const PtrMemory &m)
copy from same Heap to Heap
Definition: PtrMemory.cpp:72
virtual size_t size() const
the the size of the allocated memory
Definition: PtrMemory.cpp:122
void * dm
Pointed memory.
Definition: PtrMemory.hpp:45
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: PtrMemory.cpp:54
virtual void destroy()
destroy memory
Definition: PtrMemory.cpp:45
virtual void fill(unsigned char c)
fill memory with the selected byte
Definition: PtrMemory.cpp:22
virtual void * getPointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:156
virtual bool allocate(size_t sz)
allocate memory
Definition: PtrMemory.cpp:33
virtual void * getPointer()=0
return a data pointer
size_t spm
Size of the pointed memory.
Definition: PtrMemory.hpp:42
virtual bool copy(const memory &m)
copy memory
Definition: PtrMemory.cpp:92
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:39