OverSim
StrategyRegions.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <iostream>
25 #include <fstream>
26 #include "StrategyRegions.h"
27 #include <GlobalViewBuilder.h>
28 
30 {
31  EV << "|DD|> StrategyRegions::StrategyRegions (Using Send Strategy 'Regions') <||" << endl;
32 }
33 
34 
36 {
37  // TODO Auto-generated destructor stub
38 }
39 
40 
42 {
43  RegionsStrategyCall* msg = new RegionsStrategyCall("RegionsStrategyCall");
44  RegionDataContainer tmpRegionContainer;
45  tmpRegionContainer.regionData = getCombinedRegionsMap();
46  msg->setRegionData(tmpRegionContainer);
47  msg->setBitLength(REGIONSSTRATEGYCALL_L(msg));
48 
49  return msg;
50 }
51 
52 
54 {
55  std::stringstream tempStr;
56  tempStr << globalViewBuilder->parProxy("gvbSendStrategy").stdstringValue();
57  tempStr << "/";
58  tempStr << getMaxSpread();
59  tempStr << "/";
60  tempStr << getSizePerDim();
61  return tempStr.str();
62 }
63 
64 
66 {
67  regionCountMap combinedRegionMap;
68  regionCountMap branchRegionMap;
69 
70  branchRegionDataMap::const_iterator regionDataMapIterator = regionData.begin();
71 
72  while(regionDataMapIterator != regionData.end()) {
73  branchRegionMap = regionDataMapIterator->second;
74 
75  regionCountMap::iterator bRMapIterator = branchRegionMap.begin();
76 
77  while(bRMapIterator != branchRegionMap.end()) {
78  if(combinedRegionMap.find(bRMapIterator->first) == combinedRegionMap.end()) {
79  combinedRegionMap.insert(regionCountPair(bRMapIterator->first, bRMapIterator->second));
80  } else {
81  combinedRegionMap.find(bRMapIterator->first)->second += bRMapIterator->second;
82  }
83 
84  bRMapIterator++;
85  }
86  regionDataMapIterator++;
87  }
88 
89  return combinedRegionMap;
90 }
91 
92 
94 {
95  RegionsStrategyCall* regionsStrategyCall =
96  dynamic_cast<RegionsStrategyCall*>(globalViewBuilderCall);
97  setBranchCoordinates(regionsStrategyCall->getSrcNode(),
98  regionsStrategyCall->getRegionData().regionData);
99 }
100 
101 
103 {
104  std::stringstream tempStr;
105 
107  regionCountMap::iterator rcmIterator = tmpRCMap.begin();
108 
109  while (rcmIterator != tmpRCMap.end()) {
110  tempStr << rcmIterator->first << ':' << rcmIterator->second << ", ";
111  rcmIterator++;
112  }
113 
114  tempStr << " CRM:" << tmpRCMap.size() << " BC:" << regionData.size();
115 
116  return tempStr.str();
117 }
118 
119 
120 std::vector<std::vector<double> > StrategyRegions::getGlobalViewData()
121 {
123 
124  // measurement of CCD size
125  /*
126  std::fstream f;
127  std::string name("CCD_");
128  name += simTime().str();
129  name += ".bin";
130  f.open(name.c_str(), std::ios::binary|std::ios::out);
131  for (regionCountMap::const_iterator it = tmpRCMap.begin(); it != tmpRCMap.end(); ++it) {
132  f.write((char*)&(it->first), sizeof(it->first));
133  f.write((char*)&(it->second), sizeof(it->second));
134  }
135  f.close();
136  */
137 
138  regionCountMap::iterator rcmIterator = tmpRCMap.begin();
139 
140  std::vector<std::vector<double> > globalViewData;
141 
142  while(rcmIterator != tmpRCMap.end()) {
143  for(int i = 0; i < rcmIterator->second; i++) {
144  globalViewData.push_back(convertRegionToCoordVector(rcmIterator->first, 2));
145  // TODO get the source dimension (2) dynamic from configuration
146  }
147  rcmIterator++;
148  }
149  return globalViewData;
150 }
151 
153 {
154  std::vector<double> tmpDimVector = ncsInfo.getCoords();
155  regionCountMap tmpCountMap;
156  tmpCountMap.insert(regionCountPair(convertCoordVectorToRegion(tmpDimVector),1));
157 
158  setBranchCoordinates(thisNode, tmpCountMap); // i am my own branch here ...
159 }
160 
161 
163 {
164  if(regionData.find(node) != regionData.end()) {
165  regionData.find(node)->second = countMap;
166  } else {
167  regionData.insert(nodeRegionData(node, countMap));
168  }
169 }
170 
171 
172 double StrategyRegions::checkMaxSpread(double dimValue)
173 {
174  double maxSpread = static_cast<double>(getMaxSpread());
175 
176  if(dimValue < 0.0) {
177  if(dimValue > (maxSpread * -1.0)) {
178  return dimValue;
179  } else {
180  return (maxSpread * -1.0);
181  }
182 
183  } else {
184  if(dimValue < maxSpread) {
185  return dimValue;
186  } else {
187  return maxSpread;
188  }
189  }
190 }
191 
192 
193 long StrategyRegions::convertCoordVectorToRegion(std::vector<double> coordVector)
194 {
195  int sizePerDim = getSizePerDim();
196  int rangeShift = static_cast<int> (sizePerDim / 2);
197  int maxSpread = getMaxSpread();
198 
199  long region = 0;
200  int currentDimension = 0;
201 
202  for(Coords::iterator coordIt = coordVector.begin();
203  coordIt < coordVector.end(); coordIt++) {
204  // normalize to coordSize
205  double coordNormalized = ((checkMaxSpread(*coordIt) /
206  static_cast<double>(maxSpread)) *
207  static_cast<double>(rangeShift));
208 
209  // round normalized coords
210  if (coordNormalized > 0) coordNormalized += .5;
211  else coordNormalized -= .5;
212 
213  // shift coords
214  int coordNormAndShifted = static_cast<int>(coordNormalized) +
215  rangeShift;
216 
217  //std::cout << coordNormalized << " " << coordNormAndShifted << std::endl;
218 
219  // TODO special case (wrong rounding?)
220  if (coordNormAndShifted == pow(sizePerDim , currentDimension))
221  coordNormAndShifted -= 1;
222 
223  // calc region number
224  region += coordNormAndShifted * pow(sizePerDim , currentDimension);
225 
226  currentDimension++;
227  }
228 
229  //test
230  /*
231  Coords tmpConvertBack = convertRegionToCoordVector(region, coordVector.size());
232  std::cout << coordVector << " " << tmpConvertBack << std::endl;
233  */
234 
235  return region;
236 }
237 
238 
239 std::vector<double> StrategyRegions::convertRegionToCoordVector(int region, int dimCount)
240 {
241  int sizePerDim = getSizePerDim();
242  int maxSpread = getMaxSpread();
243  int rangeShift = static_cast<int> (sizePerDim / 2);
244 
245  std::vector<double> rCoordVector; // reverse Vector
246  std::vector<double> CoordVector;
247 
248  int normDim = 0;
249  int dimShifted = 0;
250  int subRegionSize = 0;
251  double tmpDimension;
252 
253  for (int curDim = dimCount; curDim >= 1; curDim--) {
254  subRegionSize = pow(sizePerDim, (curDim - 1));
255  dimShifted = static_cast<int>(region / subRegionSize);
256  normDim = dimShifted - rangeShift;
257  region -= subRegionSize * dimShifted;
258  tmpDimension = (static_cast<double>(normDim) /
259  static_cast<double>(rangeShift)) *
260  static_cast<double>(maxSpread);
261  rCoordVector.push_back(tmpDimension);
262  }
263 
264  // reverse vector
265  std::vector<double>::iterator vectorIterator;
266  for(vectorIterator = rCoordVector.end();
267  vectorIterator > rCoordVector.begin(); vectorIterator--) {
268  CoordVector.push_back((*(vectorIterator-1)));
269  }
270 
271  return CoordVector;
272 }
273 
274 
276 {
277  int sizePerDim =
278  globalViewBuilder->parProxy("gvbStrategyRegionsSizePerDimension");
279  return sizePerDim;
280 }
281 
282 
284 {
285  int maxSpread = globalViewBuilder->parProxy("gvbStrategyRegionsMaxSpread");
286  return maxSpread;
287 }
288 
289 
290 void StrategyRegions::cleanUpCoordData(const treeNodeMap& currentTreeChildNodes)
291 {
292  branchRegionDataMap::const_iterator regionDataMapIterator =
293  regionData.begin();
294 
295  if (regionDataMapIterator == regionData.end()) return;
296 
297  do {
298  if (currentTreeChildNodes.find(regionDataMapIterator->first) ==
299  currentTreeChildNodes.end() &&
300  regionDataMapIterator->first != thisNode) {
301  regionDataMapIterator = regionData.erase(regionDataMapIterator);
302  //assert(regionDataMapIterator != regionData.end());
303  } else ++regionDataMapIterator;
304  } while (regionDataMapIterator != regionData.end());
305 }