torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SegmentsUnitTest.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Unit test for the Segments class.
18 
19 #include <boost/test/unit_test.hpp>
22 #include "torc/common/Devices.hpp"
25 #include <iostream>
26 #include <iomanip>
27 
28 namespace torc {
29 namespace architecture {
30 
31 BOOST_AUTO_TEST_SUITE(architecture)
32 
33  // functions not tested:
34  // const Segments::IrregularArc* getIrregularArc(TileIndex inTileIndex,
35  // WireIndex inSourceWireIndex, WireIndex inSinkWireIndex);
36  // uint32_t getTotalWireCount(void) const;
37  // uint32_t getTotalSegmentCount(void) const;
38  // uint32_t getCompactSegmentCount(void) const;
39  // uint32_t getIrregularArcCount(void) const;
40  // const Segments::SegmentReference& getTilewireSegment(const Tilewire& inTilewire) const;
41  // members not tested:
42  // Array2D<CompactSegmentTilewire> mCompactSegments;
43  // Array2D<SegmentReference> mTilewireSegments;
44  // Array2D<IrregularArc> mIrregularArcs;
45  // CompactSegmentCount mCompactSegmentCount;
46  // uint32_t mIrregularArcCount;
47  // uint32_t mTotalWireCount;
48  // uint32_t mTotalSegmentCount;
49 
58  typedef boost::uint64_t uint64_t;
60  const Tiles& mTiles;
62  std::map<Tilewire, CompactSegmentIndex> mUsage;
63  const CompactSegmentIndex cUsagePruned;
64  const CompactSegmentIndex cUsageUndefined;
65  uint64_t mTilewireCount;
67  uint64_t mTilewiresPruned;
69  static uint64_t sTotalTilewireCount;
70  static uint64_t sTotalTilewiresAnalyzed;
71  static uint64_t sTotalTilewiresPruned;
72  static uint64_t sTotalTilewiresUndefined;
73 public:
74  /// \brief Basic constructor.
75  segments_unit_test_helper(DDB& inDDB) : mDDB(inDDB), mTiles(mDDB.getTiles()),
76  mSegments(mDDB.getSegments()),
77  cUsagePruned(CompactSegmentIndex(CompactSegmentIndex::undefined())),
78  cUsageUndefined(CompactSegmentIndex(static_cast<boost::uint32_t>(-2))), mTilewireCount(0),
79  mTilewiresAnalyzed(0), mTilewiresPruned(0), mTilewiresUndefined(0) {
80  // functions tested during database initialization and deletion:
81  // Segments(void);
82  // size_t readTilewireSegments(DigestStream& inStream);
83  // size_t readSegments(DigestStream& inStream);
84  // size_t readIrregularArcs(DigestStream& inStream);
85  }
86  /// \brief Outputs statistics on segment expansion.
87  static void statistics(void) {
88  std::cerr << "=======================================" << std::endl;
89  std::cerr << "Total tilewire count: " << std::setw(12) << sTotalTilewireCount
90  << std::endl;
91  std::cerr << "Total tilewires analyzed: " << std::setw(12) << sTotalTilewiresAnalyzed
92  << std::endl;
93  std::cerr << "Total tilewires pruned: " << std::setw(12) << sTotalTilewiresPruned
94  << std::endl;
95  std::cerr << "Total tilewires undefined: " << std::setw(12) << sTotalTilewiresUndefined
96  << std::endl;
97  }
98  /// \brief Analyze segment expansion, and detect any tilewire conflicts.
99  void operator ()(void) {
100  // functions tested:
101  // const Segments::SegmentReference& getTilewireSegment(const Tilewire& inTilewire);
102  TileCount tileCount = mTiles.getTileCount();
103  mUsage.clear();
104 // int totalTrivialCount = 0;
105 // int totalDefinedCount = 0;
106 // for(TileIndex tileIndex; tileIndex < tileCount; tileIndex++) {
107 // const TileInfo& tileInfo = mTiles.getTileInfo(tileIndex);
108 // TileTypeIndex tileTypeIndex = tileInfo.getTypeIndex();
109 // WireCount wireCount = mTiles.getWireCount(tileTypeIndex);
110 // for(WireIndex wireIndex; wireIndex < wireCount; wireIndex++) {
111 // Tilewire tilewire(tileIndex, wireIndex);
112 // const SegmentReference& segmentReference
113 // = mSegments.getTilewireSegment(tilewire);
114 // if(segmentReference.isDefined()) { totalDefinedCount++; continue; }
115 // if(segmentReference.isTrivial()) { totalTrivialCount++; continue; }
116 // // this segment is real, so we want to track things
117 // }
118 // }
119  //std::cerr << "totalTrivialCount: " << totalTrivialCount << std::endl;
120  //std::cerr << "totalDefinedCount: " << totalDefinedCount << std::endl;
121  //BOOST_CHECK_EQUAL(totalWireCount, mSegments.getTotalWireCount());
122 
123  // clear the usage information for all tilewires
124  for(TileIndex tileIndex; tileIndex < tileCount; tileIndex++) {
125  const Array<SegmentReference>& tilewireSegments
126  = mSegments.mTilewireSegments[tileIndex];
127  WireCount wireCount = WireCount(tilewireSegments.getSize());
128  for(WireIndex wireIndex; wireIndex < wireCount; wireIndex++) {
129  Tilewire tilewire(tileIndex, wireIndex);
130  mUsage[tilewire] = cUsageUndefined;
131  mTilewireCount++;
132  }
133  }
134 
135  // iterate through all tilewires and look for any conflicting segment ownership
136  for(TileIndex tileIndex; tileIndex < tileCount; tileIndex++) {
137  const Array<SegmentReference>& tilewireSegments
138  = mSegments.mTilewireSegments[tileIndex];
139  WireCount wireCount = WireCount(tilewireSegments.getSize());
140  for(WireIndex wireIndex; wireIndex < wireCount; wireIndex++) {
141  Tilewire tilewire(tileIndex, wireIndex);
142  analyzeSegmentTilewire(tilewire);
143  TilewireVector segmentTilewires;
144  mDDB.expandSegment(tilewire, segmentTilewires, DDB::eExpandDirectionNone);
145  TilewireVector::const_iterator p = segmentTilewires.begin();
146  TilewireVector::const_iterator e = segmentTilewires.end();
147  while(p < e) {
148  analyzeSegmentTilewire(*p++);
149  }
150  }
151  }
152 
153  // look for any tilewires without segment definitions
154  for(TileIndex tileIndex; tileIndex < tileCount; tileIndex++) {
155  const Array<SegmentReference>& tilewireSegments
156  = mSegments.mTilewireSegments[tileIndex];
157  WireCount wireCount = WireCount(tilewireSegments.getSize());
158  for(WireIndex wireIndex; wireIndex < wireCount; wireIndex++) {
159  Tilewire tilewire(tileIndex, wireIndex);
160  CompactSegmentIndex usage = mUsage[tilewire];
161  if(usage == cUsageUndefined) {
162  mTilewiresUndefined++;
163  std::cerr << "Undefined: " << tilewire << std::endl;
164  BOOST_CHECK(mUsage[tilewire] != cUsageUndefined);
165  } else if(usage == cUsagePruned) {
166  mTilewiresPruned++;
167  }
168  }
169  }
170  std::cerr << "---------------------------------------" << std::endl;
171  std::cerr << "Tilewire count: " << std::setw(12) << mTilewireCount
172  << std::endl;
173  std::cerr << "Tilewires analyzed: " << std::setw(12) << mTilewiresAnalyzed
174  << std::endl;
175  std::cerr << "Tilewires pruned: " << std::setw(12) << mTilewiresPruned
176  << std::endl;
177  std::cerr << "Tilewires undefined: " << std::setw(12) << mTilewiresUndefined
178  << std::endl;
179  sTotalTilewireCount += mTilewireCount;
180  sTotalTilewiresAnalyzed += mTilewiresAnalyzed;
181  sTotalTilewiresPruned += mTilewiresPruned;
182  sTotalTilewiresUndefined += mTilewiresUndefined;
183  }
184  /// \brief Verifies and enforces segment ownership of the specified tilewire.
185  void analyzeSegmentTilewire(const Tilewire& inTilewire) {
186  mTilewiresAnalyzed++;
187  // extract the tilewire information
188  TileIndex tileIndex = inTilewire.getTileIndex();
189  WireIndex wireIndex = inTilewire.getWireIndex();
190  const Array<SegmentReference>& tilewireSegments = mSegments.mTilewireSegments[tileIndex];
191  // look up the segment reference information
192  const SegmentReference& segmentReference = tilewireSegments[wireIndex];
193  CompactSegmentIndex compactSegmentIndex = segmentReference.getCompactSegmentIndex();
194  TileIndex anchorTileIndex = segmentReference.getAnchorTileIndex();
195  (void) anchorTileIndex;
196  // look up the current tilewire membership
197  CompactSegmentIndex usageCompactSegmentIndex = mUsage[inTilewire];
198  if(compactSegmentIndex == usageCompactSegmentIndex) {
199  // there has been no change
200  //std::cerr << "Match: " << static_cast<int>(compactSegmentIndex) << " for "
201  // << inTilewire << std::endl;
202  } else if(usageCompactSegmentIndex == cUsageUndefined) {
203  // this wire was not previously defined, so we accept the new value
204  mUsage[inTilewire] = compactSegmentIndex;
205  //std::cerr << "Definition: " << static_cast<int>(compactSegmentIndex) << " for "
206  // << inTilewire << std::endl;
207  } else {
208  std::cerr << "Mismatch (" << static_cast<int>(compactSegmentIndex) << " vs. "
209  << static_cast<int>(usageCompactSegmentIndex) << ") for " << inTilewire
210  << std::endl;
211  BOOST_CHECK_EQUAL(static_cast<int>(compactSegmentIndex),
212  static_cast<int>(usageCompactSegmentIndex));
213  }
214  }
215 };
216 
217 /// \brief Total number of tilewires visited.
219 /// \brief Total number of tilewires analyzed (visited or expanded).
221 /// \brief Total number of tilewires pruned from the device (absent from XDLRC).
223 /// \brief Total number of tilewires never defined (sanity check).
225 
226 // this test is disabled because of its length and because it adds nothing to SegmentsRegressionTest
227 #if 0
228 /// \brief Unit test for the Segments class.
229 /// \todo Combine and separate SegmentsUnitTest, SegmentsRegressionTest, and DDBUnitTest
230 BOOST_AUTO_TEST_CASE(SegmentsUnitTest) {
231 
232  // iterate over the devices
234  torc::common::DeviceVector::const_iterator dp = devices.begin();
235  torc::common::DeviceVector::const_iterator de = devices.end();
236  // defer to regression test for now
237  while(dp < de) {
238  const std::string& device = *dp++;
239  if(device.empty()) break;
240  DDB ddb(device);
241  std::cerr << ddb;
242  segments_unit_test_helper segmentTester(ddb);
243  segmentTester();
245  }
246 
247  BOOST_CHECK_MESSAGE(false, "WARNING: Need to check segment packing.");
248 
249 }
250 #endif
251 
252 BOOST_AUTO_TEST_SUITE_END()
253 
254 } // namespace architecture
255 } // namespace torc
const WireIndex & getWireIndex(void) const
Returns the wire index.
Definition: Tilewire.hpp:66
Encapsulation of a tile index in an unsigned 32-bit integer.
CompactSegmentIndex getCompactSegmentIndex(void) const
Definition: Segments.hpp:82
std::vector< Tilewire > TilewireVector
Vector of Tilewire objects.
Definition: Tilewire.hpp:101
Header for the Segments class.
static uint64_t sTotalTilewiresUndefined
Total number of tilewires never defined (sanity check).
Device database, including complete wiring and logic support.
Definition: DDB.hpp:42
BOOST_AUTO_TEST_CASE(ArcUnitTest)
Unit test for the Arc class.
Definition: ArcUnitTest.cpp:29
std::map< Tilewire, CompactSegmentIndex > mUsage
void analyzeSegmentTilewire(const Tilewire &inTilewire)
Verifies and enforces segment ownership of the specified tilewire.
static uint64_t sTotalTilewiresPruned
Total number of tilewires pruned from the device (absent from XDLRC).
Header for the DDBStreamHelper class.
Array2D< SegmentReference > mTilewireSegments
The segment references for every wire in every tile.
Definition: Segments.hpp:119
Encapsulation of a wire index in an unsigned 16-bit integer.
TileCount getTileCount(void) const
Returns the tile count for this device.
Definition: Tiles.hpp:149
std::string string
static void statistics(void)
Outputs statistics on segment expansion.
segments_unit_test_helper(DDB &inDDB)
Basic constructor.
Header for torc::physical output stream helpers.
Encapsulation of a device tile and wire pair.
Definition: Tilewire.hpp:39
Tile map, tile type, and wire information for the family and device.
Definition: Tiles.hpp:36
Encapsulation of a wire count in an unsigned 16-bit integer.
Encapsulation of a tile count in an unsigned 32-bit integer.
Header for the Devices class.
Encapsulation of a compact segment index in an unsigned 32-bit integer.
static uint64_t sTotalTilewiresAnalyzed
Total number of tilewires analyzed (visited or expanded).
Encapsulation of compact segment index and an anchoring tile index.
Definition: Segments.hpp:78
static uint64_t sTotalTilewireCount
Total number of tilewires visited.
Encapsulation of a tile type index in an unsigned 16-bit integer.
Header for the DDB class.
const TileIndex & getTileIndex(void) const
Returns the tile index.
Definition: Tilewire.hpp:64
std::vector< std::string > DeviceVector
Vector of device names.
Definition: Devices.hpp:119
Segment and irregular arc data for the device.
Definition: Segments.hpp:40
static const DeviceVector & getUnitTestDevices(void)
Returns a subset of devices for unit tests.
Definition: Devices.hpp:209
Encapsulation of a static array.
Definition: Array.hpp:39
void expandSegment(const Tilewire &inTilewire, TilewireVector &outTilewires, EExpandDirection inExpandDirection=eExpandDirectionNone)
Expands the given tilewire's segment.
Definition: DDB.cpp:154
uint32_t getSize(void) const
Returns the array size.
Definition: Array.hpp:104