ls1-MarDyn
ls1-MarDyn molecular dynamics code
CommunicationBuffer.h
1/*
2 * CommunicationBuffer.h
3 *
4 * Created on: 16 Oct 2017
5 * Author: tchipevn
6 */
7#ifdef ENABLE_MPI
8#ifndef SRC_PARALLEL_COMMUNICATIONBUFFER_H_
9#define SRC_PARALLEL_COMMUNICATIONBUFFER_H_
10
11#include "molecules/MoleculeForwardDeclaration.h"
12#include "utils/mardyn_assert.h"
13
14#include <vector>
15#include <stddef.h>
16#include <mpi.h>
17
18// do not uncomment the if, it will break halo copies of the kddecomposition!
19//#if (not defined(NDEBUG))
20#define LS1_SEND_UNIQUE_ID_FOR_HALO_COPIES
21//#pragma message "Compilation info: Unique IDs of Halo-Molecules are always present."
22//#endif
23
38
39 friend class CommunicationBufferTest;
40
41public:
43 clear();
44 }
45 size_t getDynamicSize();
46
47 void clear();
48
49 void resizeForAppendingLeavingMolecules(unsigned long numMols);
50 void resizeForAppendingHaloMolecules(unsigned long numMols);
51 void resizeForAppendingForceMolecules(unsigned long numMols);
52
53 unsigned char * getDataForSending();
54 size_t getNumElementsForSending();
55 void resizeForRawBytes(unsigned long numBytes);
56
57 // write
58 void addLeavingMolecule(size_t indexOfMolecule, const Molecule& m);
59 void addHaloMolecule(size_t indexOfMolecule, const Molecule& m);
60 void addForceMolecule(size_t indexOfMolecule, const Molecule& m);
61
62 // read
63 void readLeavingMolecule(size_t indexOfMolecule, Molecule& m) const;
64 void readHaloMolecule(size_t indexOfMolecule, Molecule& m) const;
65 void readForceMolecule(size_t indexOfMolecule, Molecule& m) const;
66
67 void resizeForReceivingMolecules(unsigned long& numLeaving, unsigned long& numHalo);
68 void resizeForReceivingMolecules(unsigned long& numForces);
69
70 size_t getNumHalo() const {
71 return _numHalo;
72 }
73
74 size_t getNumLeaving() const {
75 return _numLeaving;
76 }
77
78 size_t getNumForces() const {
79 return _numForces;
80 }
81
82 static MPI_Datatype getMPIDataType() {
83 return MPI_CHAR;
84 }
85
86private:
87 static size_t _numBytesHalo;
88 static size_t _numBytesLeaving;
89 static size_t _numBytesForces; // where is this set?
90
91 enum class ParticleType_t {HALO=0, LEAVING=1, FORCE=3};
92 size_t getStartPosition(ParticleType_t type, size_t indexOfMolecule) const;
93
97 template<typename T>
98 size_t emplaceValue(size_t indexInBytes, T passByValue);
99
100 template<typename T>
101 size_t readValue(size_t indexInBytes, T& passByReference) const;
102
103 typedef unsigned char byte_t;
104 std::vector<byte_t> _buffer;
105 size_t _numLeaving, _numHalo, _numForces;
106};
107
108template<typename T>
109inline size_t CommunicationBuffer::emplaceValue(size_t indexInBytes, T passByValue) {
110 const size_t numBytesOfT = sizeof(T);
111 size_t ret = indexInBytes + numBytesOfT;
112
113 mardyn_assert(_buffer.size() >= ret);
114
115 const byte_t * pointer = reinterpret_cast<byte_t *> (&passByValue);
116 for (size_t i = 0; i < numBytesOfT; ++i) {
117 _buffer[indexInBytes + i] = pointer[i];
118 }
119
120 return ret;
121}
122
123template<typename T>
124inline size_t CommunicationBuffer::readValue(size_t indexInBytes, T& passByReference) const {
125 const size_t numBytesOfT = sizeof(T);
126 size_t ret = indexInBytes + numBytesOfT;
127
128 mardyn_assert(_buffer.size() >= ret);
129
130 byte_t * pointer = reinterpret_cast<byte_t *> (&passByReference);
131 for (size_t i = 0; i < numBytesOfT; ++i) {
132 pointer[i] = _buffer[indexInBytes + i];
133 }
134
135 return ret;
136}
137
138
139#endif /* SRC_PARALLEL_COMMUNICATIONBUFFER_H_ */
140#endif /* ENABLE_MPI */
Definition: CommunicationBuffer.h:37
FullMolecule modeled as LJ sphere with point polarities.
Definition: FullMolecule.h:18
Enumeration class corresponding to the type schema type.
Definition: vtk-unstructured.h:1746