OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 
17 // If debugging mode include memory leak check
18 #ifdef SE_CLASS2
19 #include "Memleak_check.hpp"
20 #endif
21 
28 bool PtrMemory::allocate(size_t sz)
29 {
30  if (sz <= spm)
31  return true;
32 
33  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed";
34  return false;
35 }
36 
41 {
42 }
43 
44 
49 bool PtrMemory::copyFromPointer(const void * ptr,size_t sz)
50 {
51  // memory copy
52 
53  memcpy(dm,ptr,sz);
54 
55  return true;
56 }
57 
68 {
70 
71  if (m.spm > spm)
72  {
73  std::cerr << "Error " << __LINE__ << " " << __FILE__ << ": source buffer is too big to copy";
74  return false;
75  }
76 
77  // Copy the memory from m
78  memcpy(dm,m.dm,m.spm);
79  return true;
80 }
81 
87 bool PtrMemory::copy(const memory & m)
88 {
90  const PtrMemory * ofpm = dynamic_cast<const PtrMemory *>(&m);
91 
93 
94  if (ofpm == NULL)
95  {
96  // copy the memory from device to host and from host to device
97 
98  return copyFromPointer(m.getPointer(),m.size());
99  }
100  else
101  {
102  // they are the same memory type, use cuda/thrust buffer copy
103 
104  return copyDeviceToDevice(*ofpm);
105  }
106  return false;
107 }
108 
117 size_t PtrMemory::size() const
118 {
119  return spm;
120 }
121 
132 bool PtrMemory::resize(size_t sz)
133 {
134  // if the allocated memory is enough, do not resize
135  if (sz <= spm)
136  {
137  this->spm = sz;
138  return true;
139  }
140 
141  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed";
142  return false;
143 }
144 
152 {
153  return dm;
154 }
155 
163 {
164  return dm;
165 }
166 
167 
174 const void * PtrMemory::getPointer() const
175 {
176  return dm;
177 }
178 
179 
180 #endif /* PTRMEMORY_CPP_ */
virtual void * getDevicePointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:162
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:132
bool copyDeviceToDevice(const PtrMemory &m)
copy from same Heap to Heap
Definition: PtrMemory.cpp:67
void * dm
Pointed memory.
Definition: PtrMemory.hpp:48
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: PtrMemory.cpp:49
virtual void destroy()
destroy memory
Definition: PtrMemory.cpp:40
virtual size_t size() const
the the size of the allocated memory
Definition: PtrMemory.cpp:117
virtual void * getPointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:151
virtual bool allocate(size_t sz)
allocate memory
Definition: PtrMemory.cpp:28
virtual void * getPointer()=0
return a data pointer
size_t spm
Size of the pointed memory.
Definition: PtrMemory.hpp:45
virtual bool copy(const memory &m)
copy memory
Definition: PtrMemory.cpp:87
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:42