-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprpl-ownershipMap.cpp
253 lines (226 loc) · 6.72 KB
/
prpl-ownershipMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "prpl-ownershipMap.h"
pRPL::OwnershipMap::
OwnershipMap() {
_itrSubspc2Map = _vSubspcIDs.begin();
}
void pRPL::OwnershipMap::
init(const pRPL::IntVect &vSubspcIDs,
const pRPL::IntVect &vPrcIDs) {
_vSubspcIDs = vSubspcIDs;
_itrSubspc2Map = _vSubspcIDs.begin();
_mPrcSubspcs.clear();
for(int iPrc = 0; iPrc < vPrcIDs.size(); iPrc++) {
_mPrcSubspcs.insert(make_pair(vPrcIDs[iPrc], IntVect()));
}
}
void pRPL::OwnershipMap::
clearMapping() {
map<int, pRPL::IntVect>::iterator itrMap = _mPrcSubspcs.begin();
while(itrMap != _mPrcSubspcs.end()) {
itrMap->second.clear();
itrMap++;
}
_itrSubspc2Map = _vSubspcIDs.begin();
}
void pRPL::OwnershipMap::
mapping(pRPL::MappingMethod mapMethod,
int nSubspcs2Map) {
clearMapping();
int nActualSubspcs2Map = (nSubspcs2Map > 0)?nSubspcs2Map:_vSubspcIDs.size();
map<int, pRPL::IntVect>::iterator itrMap = _mPrcSubspcs.begin();
if(mapMethod == pRPL::CYLC_MAP) {
for(int iSubspc = 0; iSubspc < nActualSubspcs2Map; iSubspc++) {
if(_itrSubspc2Map != _vSubspcIDs.end()) {
itrMap->second.push_back(*_itrSubspc2Map);
_itrSubspc2Map++;
itrMap++;
if(itrMap == _mPrcSubspcs.end()) {
itrMap = _mPrcSubspcs.begin();
}
}
else {
break;
}
} // end of for(iSubspc) loop
}
else if(mapMethod == pRPL::BKFR_MAP) {
bool goForward = true;
for(int iSubspc = 0; iSubspc < nActualSubspcs2Map; iSubspc++) {
if(_itrSubspc2Map != _vSubspcIDs.end()) {
itrMap->second.push_back(*_itrSubspc2Map);
_itrSubspc2Map++;
if(goForward) {
itrMap++;
if(itrMap == _mPrcSubspcs.end()) {
itrMap--;
goForward = false;
}
}
else {
itrMap--;
if(itrMap == _mPrcSubspcs.begin()) {
goForward = true;
}
}
}
else {
break;
}
} // end of for(iSubspc) loop
}
}
int pRPL::OwnershipMap::
mappingNextTo(int prcID) {
int mappedSubspcID = pRPL::ERROR_ID;
map<int, pRPL::IntVect>::iterator itrMap = _mPrcSubspcs.find(prcID);
if(itrMap == _mPrcSubspcs.end()) {
cerr << __FILE__ << " " << __FUNCTION__ \
<< " Error: invalid process ID (" \
<< prcID << ")" << endl;
}
else if(_itrSubspc2Map != _vSubspcIDs.end()) {
mappedSubspcID = *_itrSubspc2Map;
itrMap->second.push_back(mappedSubspcID);
_itrSubspc2Map++;
}
else {
cerr << __FILE__ << " " << __FUNCTION__ \
<< " Error: all SubCellspaces have been mapped" \
<< endl;
}
return mappedSubspcID;
}
bool pRPL::OwnershipMap::
mappingTo(int subspcID,
int prcID) {
if(std::find(_vSubspcIDs.begin(), _vSubspcIDs.end(), subspcID) == _vSubspcIDs.end()) {
cerr << __FILE__ << " " << __FUNCTION__ \
<< " Error: invalid SubCellspace ID (" << subspcID \
<< ")" << endl;
return false;
}
map<int, pRPL::IntVect>::iterator itrMap = _mPrcSubspcs.find(prcID);
if(itrMap == _mPrcSubspcs.end()) {
cerr << __FILE__ << " " << __FUNCTION__ \
<< " Error: invalid process ID (" \
<< prcID << ")" << endl;
return false;
}
itrMap->second.push_back(subspcID);
return true;
}
bool pRPL::OwnershipMap::
allMapped() const {
return (_itrSubspc2Map == _vSubspcIDs.end());
}
const pRPL::IntVect* pRPL::OwnershipMap::
subspcIDsOnPrc(int prcID) const {
const pRPL::IntVect *pvSubspcIDs = NULL;
map<int, pRPL::IntVect>::const_iterator itrMap = _mPrcSubspcs.find(prcID);
if(itrMap != _mPrcSubspcs.end()) {
pvSubspcIDs = &(itrMap->second);
}
return pvSubspcIDs;
}
pRPL::IntVect pRPL::OwnershipMap::
mappedSubspcIDs() const {
pRPL::IntVect vMappedIDs;
pRPL::IntVect::const_iterator itrMap = _vSubspcIDs.begin();
while(itrMap != _itrSubspc2Map) {
vMappedIDs.push_back(*itrMap);
itrMap++;
}
return vMappedIDs;
}
int pRPL::OwnershipMap::
findPrcBySubspcID(int subspcID) const {
int prcID = pRPL::ERROR_ID;
map<int, pRPL::IntVect>::const_iterator itrPrc = _mPrcSubspcs.begin();
while(itrPrc != _mPrcSubspcs.end()) {
const pRPL::IntVect &vSubspcs = itrPrc->second;
if(std::find(vSubspcs.begin(), vSubspcs.end(), subspcID) != vSubspcs.end()) {
prcID = itrPrc->first;
break;
}
itrPrc++;
}
return prcID;
}
bool pRPL::OwnershipMap::
findSubspcIDOnPrc(int subspcID,
int prcID) const {
bool found = false;
const pRPL::IntVect *pvSubspcIDs = subspcIDsOnPrc(prcID);
if(pvSubspcIDs != NULL) {
if(std::find(pvSubspcIDs->begin(), pvSubspcIDs->end(), subspcID) != pvSubspcIDs->end()) {
found = true;
}
}
return found;
}
bool pRPL::OwnershipMap::
map2buf(vector<char> &buf) const {
buf.clear();
int bufPtr = 0;
map<int, pRPL::IntVect>::const_iterator itrMap = _mPrcSubspcs.begin();
while(itrMap != _mPrcSubspcs.end()) {
int iPrc = itrMap->first;
const vector<int> &vSubspcIDs = itrMap->second;
int nSubspcs = vSubspcIDs.size();
bufPtr = buf.size();
buf.resize(buf.size() + (nSubspcs + 2) * sizeof(int));
memcpy((void *)&(buf[bufPtr]), &iPrc, sizeof(int));
bufPtr += sizeof(int);
memcpy((void *)&(buf[bufPtr]), &nSubspcs, sizeof(int));
bufPtr += sizeof(int);
/*
for(int iID = 0; iID < nSubspcs; iID++) {
memcpy((void *)&(buf[bufPtr]), &(vSubspcIDs[iID]), sizeof(int));
bufPtr += sizeof(int);
}
*/
memcpy((void *)&(buf[bufPtr]), &(vSubspcIDs[0]), nSubspcs*sizeof(int));
bufPtr += nSubspcs * sizeof(int);
itrMap++;
}
return true;
}
bool pRPL::OwnershipMap::
buf2map(const vector<char> &buf) {
_mPrcSubspcs.clear();
int bufPtr = 0;
int iPrc = pRPL::ERROR_ID;
int nSubspcs = 0;
while(bufPtr < buf.size()) {
memcpy(&iPrc, (void *)&(buf[bufPtr]), sizeof(int));
bufPtr += sizeof(int);
memcpy(&nSubspcs, (void *)&(buf[bufPtr]), sizeof(int));
bufPtr += sizeof(int);
_mPrcSubspcs[iPrc].resize(nSubspcs);
memcpy(&(_mPrcSubspcs[iPrc][0]), (void *)&(buf[bufPtr]), nSubspcs*sizeof(int));
bufPtr += nSubspcs * sizeof(int);
/*
for(int iID = 0; iID < nSubspcs; iID++) {
memcpy(&(_mPrcSubspcs[iPrc][iID]), (void *)&(buf[bufPtr]), sizeof(int));
bufPtr += sizeof(int);
}
*/
}
return true;
}
ostream& pRPL::
operator<<(ostream &os, const pRPL::OwnershipMap &mOwnerships) {
map<int, pRPL::IntVect>::const_iterator itrMap = mOwnerships._mPrcSubspcs.begin();
while(itrMap != mOwnerships._mPrcSubspcs.end()) {
cout << "Process [" << itrMap->first << "] gets " \
<< itrMap->second.size() << " SubCellspaces: ";
pRPL::IntVect::const_iterator itrSubID = itrMap->second.begin();
while(itrSubID != itrMap->second.end()) {
cout << *itrSubID << " ";
itrSubID++;
}
cout << endl;
itrMap++;
}
return os;
}