Peano 4
Loading...
Searching...
No Matches
VertexWiseContinuousMemoryPool.cpph
Go to the documentation of this file.
3
4
5template <class T>
6tarch::logging::Log toolbox::particles::memorypool::VertexWiseContinuousMemoryPool<T>::_log( "toolbox::particles::memorypool::VertexWiseContinuousMemoryPool<T>" );
7
8
9template <class T>
13
14
15template <class T>
19
20
21template <class T>
23 assertion( not container.empty() or _gatheredDataPointer==nullptr );
24 if (_gatheredDataPointer!=nullptr) {
25 logDebug( "scatter()", "scatter " << container.size() << " element(s)" );
26 const int oldSize = container.size();
27 container.clear();
28 for (int i=0; i<oldSize; i++) {
29 container.push_back( new T( _gatheredDataPointer[i]) );
30 }
31 tarch::freeMemory( _gatheredDataPointer, tarch::MemoryLocation::Heap );
32 _gatheredDataPointer = nullptr;
33 }
34}
35
36
37template <class T>
39 container.clear();
40 _gatheredDataPointer = nullptr;
41}
42
43
44template <class T>
47 assertion( not container.empty() or _gatheredDataPointer==nullptr );
48 if (_gatheredDataPointer==nullptr) {
49 return p;
50 }
51 else {
52 logDebug( "scatterAndUpdateIterator()", "scatter " << container.size() << " element(s)" );
53
54 Container newContainer;
55 typename Container::iterator result;
56
57 for (auto oldContainerIterator: container) {
58 newContainer.push_back( new T(*oldContainerIterator) );
59 if (oldContainerIterator==*p) {
60 result = newContainer.end();
61 result--;
62 }
63 }
64
65 container.clear();
66 container.splice( container.begin(), newContainer );
67 tarch::freeMemory( _gatheredDataPointer, tarch::MemoryLocation::Heap );
68 _gatheredDataPointer = nullptr;
69
70 return result;
71 }
72}
73
74
75template <class T>
77 assertion( not container.empty() or _gatheredDataPointer==nullptr );
78 if (_gatheredDataPointer==nullptr and not container.empty()) {
79 logDebug( "gather()", "gather " << container.size() << " element(s)" );
80 _gatheredDataPointer = tarch::allocateMemory<T>( container.size(), tarch::MemoryLocation::Heap );
81 int entry = 0;
82 for (auto& p: container) {
83 _gatheredDataPointer[entry] = *p;
84 delete p;
85 entry++;
86 }
87
88 typename Container::iterator p = container.begin();
89 for (int entry=0; entry<container.size(); entry++) {
90 *p = _gatheredDataPointer + entry;
91 p++;
92 }
93 }
94}
95
96
97template <class T>
99 return _gatheredDataPointer!=nullptr;
100}
101
102
103template <class T>
104void toolbox::particles::memorypool::VertexWiseContinuousMemoryPool<T>::replace( typename Container::iterator p, T* newCopy ) {
105 assertion( not container.empty() or _gatheredDataPointer==nullptr );
106 if ( isGathered() ) {
107 (**p) = *newCopy;
108 delete newCopy;
109 }
110 else {
111 container.erase(p);
112 container.push_back(newCopy);
113 }
114}
#define assertion(expr)
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
Log Device.
Definition Log.h:516
void freeMemory(void *data, MemoryLocation location)
@ Heap
Create data on the heap of the local device.
Memory pool offering continuous global memory for a particle species.
Container::iterator scatterAndUpdateIterator(const typename Container::iterator &p)
Scatter the data if not scattered yet and return the updated iterator.
void scatter()
If the data are scattered already, nothing is to be done.
void clearAndReset()
Clears the underlying list of pointers and resets _gatheredDataPointer to nullptr.
void replace(typename Container::iterator p, T *newCopy)
Replace particle.