torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LibraryDatabaseStitch.cpp File Reference

This program takes in path to a folder. It goes over all the files with extension .cbit. More...

#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include "SharedFunctions.hpp"
+ Include dependency graph for LibraryDatabaseStitch.cpp:

Go to the source code of this file.

Functions

void PopulateMaps (boost::filesystem::path libPath, TiletypeElementMap &outTileMap, std::vector< ElementConfigMap > &outElemMapVector, std::vector< ConfigBitMap > &outConfigMapVector)
 
int main (int argc, char **argv)
 

Detailed Description

This program takes in path to a folder. It goes over all the files with extension .cbit.

Definition in file LibraryDatabaseStitch.cpp.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 108 of file LibraryDatabaseStitch.cpp.

108  {
109 
110  // This program should receive one parameter - the folder of compressed bits
111  if(argc != 2) {
112  std::cout << "Pass correct parameters. Usage: " << argv[0]
113  << " <folder of compressed bit files" << std::endl;
114  return -1;
115  }
116 
117  // Check if the folder exist
118  boost::filesystem::path libPath(argv[1]);
119  if(!boost::filesystem::exists(libPath)) {
120  std::cout << "The folder " << libPath.string().c_str() << " does not exist" << std::endl;
121  return -1;
122  }
123 
124  // Maps to store the information on tile type, elements and configs
125  std::vector<ConfigBitMap> configMapVector;
126  std::vector<ElementConfigMap> elementMapVector;
127  TiletypeElementMap tileMap;
128 
129  // Write the database file to disk
130  std::string dbFileName = kFamily + ".ldb";
131  boost::filesystem::path dbFilePath = libPath / dbFileName;
132  std::ofstream dbFile(dbFilePath.string().c_str(), std::ofstream::binary);
133  if(!dbFile.good()) {
134  std::cout << "Could not open file " << dbFilePath.string() << " to write" << std::endl;
135  return -1;
136  }
137 
138  // Populate the maps
139  PopulateMaps(libPath, tileMap, elementMapVector, configMapVector);
140 
141  std::cout << "Opened file " << dbFilePath.string() << " to store library database" << std::endl;
142  dbFile.write("<<<<BITLIBDB>>>>", 16);
143 // std::cout << "Tile map size " << sizeof(tileMap.size()) << std::endl;
144  uint32_t dataSize;
145  dataSize = tileMap.size();
146  dbFile.write((char *) (&dataSize), sizeof(dataSize));
147 
148 
149  // Now go over the map data structure and write it to a file
150  // File format is given at the top of this file
151  TiletypeElementMap::const_iterator pTiletypes = tileMap.begin();
152  TiletypeElementMap::const_iterator eTiletypes = tileMap.end();
153  while(pTiletypes != eTiletypes) {
154 
155  // Write the tile name char count, name string and element count
156  dataSize = pTiletypes->first.size();
157  dbFile.write((const char *) &dataSize, sizeof(dataSize));
158  dbFile.write((const char *) pTiletypes->first.c_str(), pTiletypes->first.size());
159  dataSize = pTiletypes->second.size();
160  dbFile.write((const char*) &dataSize, sizeof(dataSize));
161  std::cout << "Tile type " << pTiletypes->first << " has " << pTiletypes->second.size()
162  << " elements." << std::endl;
163 
164  // Iterate over elements
165  ElementConfigMap::const_iterator pElements = pTiletypes->second.begin();
166  ElementConfigMap::const_iterator eElements = pTiletypes->second.end();
167  while(pElements != eElements) {
168 
169  // Write element name char count, name string and config count
170  dataSize = pElements->first.size();
171  dbFile.write((const char *) &dataSize, sizeof(dataSize));
172  dbFile.write((const char *) pElements->first.c_str(), pElements->first.size());
173  dataSize = pElements->second.size();
174  dbFile.write((const char *) &dataSize, sizeof(dataSize));
175 // std::cout << " Element type " << pElements->first << " has "
176 // << pElements->second.size() << " configs." << std::endl;
177 
178  // Itereate over configs
179  ConfigBitMap::const_iterator pConfigs = pElements->second.begin();
180  ConfigBitMap::const_iterator eConfigs = pElements->second.end();
181  while(pConfigs != eConfigs) {
182  // Write config namem char count, name string and count of bit addresses
183  dataSize = pConfigs->first.size();
184  dbFile.write((const char *) &dataSize, sizeof(dataSize));
185  dbFile.write(pConfigs->first.c_str(), pConfigs->first.size());
186  dataSize = pConfigs->second.size();
187  dbFile.write((const char *) &dataSize, sizeof(dataSize));
188 
189  // Write the bit addresses
190  for(std::vector<uint32_t>::const_iterator iter = pConfigs->second.begin(); iter
191  != pConfigs->second.end(); iter++) {
192  dbFile.write((char *) &*iter, sizeof(uint32_t));
193  }
194 // std::cout << "\t" << pConfigs->first << " " << pConfigs->second.size();
195  pConfigs++;
196  }
197 // std::cout << std::endl;
198  pElements++;
199  }
200  pTiletypes++;
201 
202  }
203 
204  return 0;
205 }
void PopulateMaps(boost::filesystem::path libPath, TiletypeElementMap &outTileMap, std::vector< ElementConfigMap > &outElemMapVector, std::vector< ConfigBitMap > &outConfigMapVector)
std::string string
boost::filesystem::path path
std::map< const std::string, ElementConfigMap > TiletypeElementMap
const std::string kFamily

+ Here is the call graph for this function:

void PopulateMaps ( boost::filesystem::path  libPath,
TiletypeElementMap outTileMap,
std::vector< ElementConfigMap > &  outElemMapVector,
std::vector< ConfigBitMap > &  outConfigMapVector 
)

Definition at line 40 of file LibraryDatabaseStitch.cpp.

41  {
42 
43  // Go over all the files in the directory
44  boost::filesystem::directory_iterator eFiles;
45  boost::filesystem::directory_iterator pFiles(libPath);
46  while(pFiles != eFiles) {
47 
48  // If it is a compressed bit file (.cbit extension)
49  if(pFiles->path().extension() == ".cbit") {
50 
51  std::ifstream libBitFile(pFiles->path().string().c_str(), std::ios::binary);
52  if(!libBitFile.good()) {
53  std::cerr << "ERROR: Could not open micro-bitstream file " << pFiles->path().string() << std::endl;
54  return;
55  }
56 
57  // Get number of bits set and put the bit addresses in a vector
58  int32_t numSetBits, bitAddr;
59  libBitFile.read((char *) &numSetBits, 4);
60  std::cout << pFiles->path().filename() << " opened. Bit count " << numSetBits << std::endl;
61  std::vector<uint32_t> bitAddresses;
62  for(int i = 0; i < numSetBits && !libBitFile.eof(); i++) {
63  libBitFile.read((char *) &bitAddr, 4);
64  bitAddresses.push_back(bitAddr);
65  }
66 
67  // Get the file name without extension and split the name to get tile name, element/source wire and config/sink wire.
68  boost::filesystem::path filePath(pFiles->path());
69  std::string fileName(filePath.replace_extension().filename());
70 
71  std::vector<std::string> splitVector;
72  boost::algorithm::split(splitVector, fileName, boost::algorithm::is_any_of(kNameSeparator));
73  for(std::vector<std::string>::const_iterator splitIter = splitVector.begin(); splitIter
74  != splitVector.end(); splitIter++) {
75  std::cout << " " << *splitIter << std::endl;
76 
77  }
78 
79  // Get the tile type, element(source wire) and config (sink wire)from the split string
80  std::string tileType(splitVector[1]);
81  std::string element(splitVector[2]);
82  std::string config(splitVector[3]);
83 
84  // if tile not found in map, a new tile type has come up. Add it to the map.
85  // Create another map for Element-config and push it in vector so that it is not destroyed.
86  if(outTileMap.find(tileType) == outTileMap.end()) {
87  ElementConfigMap elementConfigMap;
88  outElemMapVector.push_back(elementConfigMap);
89  outTileMap[tileType] = outElemMapVector.back();
90  }
91 
92  // if the element in the tile doesn't exist
93  if(outTileMap[tileType].find(element) == outTileMap[tileType].end()) {
94  ConfigBitMap configBitmap;
95  outConfigMapVector.push_back(configBitmap);
96  outTileMap[tileType][element] = outConfigMapVector.back();
97  }
98 
99  // Mapping the config to bit addresses
100  outTileMap[tileType][element][config] = bitAddresses;
101 
102  }
103 
104  pFiles++;
105  }
106 }
std::string string
std::map< const std::string, std::vector< uint32_t > > ConfigBitMap
boost::filesystem::path path
const std::string kNameSeparator
std::map< const std::string, ConfigBitMap > ElementConfigMap

+ Here is the caller graph for this function: