CGRA-ME
Mapper.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #ifndef ___MAPPER_H__
12 #define ___MAPPER_H__
13 
14 #include <CGRA/CGRA.h>
15 #include <CGRA/ConfigStore.h>
16 #include <CGRA/Mapping.h>
17 #include <CGRA/OpGraph.h>
18 #include <CGRA/TupleHash.h>
19 
20 #include <iosfwd>
21 #include <map>
22 #include <regex>
23 #include <vector>
24 
28 class Mapper
29 {
30  public:
31  virtual Mapping mapOpGraph(std::shared_ptr<OpGraph> opgraph, int II, const MRRG& mrrg, std::unordered_map<std::string, std::string> fix_port) = 0;
32 
33  virtual ~Mapper();
34  protected:
35  Mapper(std::shared_ptr<CGRA> cgra, int timelimit);
36 
37  std::shared_ptr<CGRA> cgra; //< Architecture Object
38  int timelimit; //< The mapper timeout in seconds
39 };
40 
44 using MapperMaker = std::function<std::unique_ptr<Mapper>(std::shared_ptr<CGRA>, int, const ConfigStore&)>;
45 
67 public:
72 
73  MapperRegistry(const MapperRegistry&) = default;
74  MapperRegistry(MapperRegistry&&) = default;
75  MapperRegistry& operator=(const MapperRegistry&) = default;
77 
84  std::unique_ptr<Mapper> createMapper(const std::string& mapper_id, std::shared_ptr<CGRA> cgra, int timelimit, const ConfigStore& args) const;
85 
99  void registerMapper(
100  std::string mapper_id, MapperMaker maker, bool is_composite, std::string mapper_description,
101  std::vector<ImplicitTuple<std::string, ImplicitlyToString, std::string>> required_arg_names_defaults_and_descriptions,
102  std::vector<std::pair<std::string, std::string>> optional_arg_regexes_and_descriptions
103  );
104 
111 
117  ConfigStore getMapperArgs(const std::string& mapper_id, const ConfigStore& args) const;
118 
122  void printMapperList(std::ostream& os) const;
123 
127  void printDefaultsAsINI(std::ostream& os) const;
128 
135  void printHelpForMapper(const std::string& mapper_id, const ConfigStore& args, std::ostream& os) const;
136 private:
137  const std::string makeKeyPrefix(const std::string& mapper_id) const {
138  return m_mapper_makers.at(mapper_id).is_composite ? mapper_id + '.' : ""; // composite mappers expect all keys to be prefixed
139  }
140 
144 
145  std::string description;
146  std::map<std::string, std::pair<std::regex, std::string>> allowed_arg_regexes_and_documentation;
148 
149  auto findMatchingArg(const std::string& arg) const {
150  const auto matches_key = [&](const auto& name_and_regex_and_doc) { return regex_match(arg, name_and_regex_and_doc.second.first); };
151  return std::find_if(allowed_arg_regexes_and_documentation.begin(), allowed_arg_regexes_and_documentation.end(), matches_key);
152  }
153 
154  bool hasMatchingArg(const std::string& arg) const {
155  return
158  }
159  };
160 
161  std::map<std::string, MakerAndDescription> m_mapper_makers;
162 };
163 
201 public:
206  std::string mapper_id, MapperMaker maker, bool is_composite, std::string mapper_description,
207  std::vector<ImplicitTuple<std::string, ImplicitlyToString, std::string>> required_arg_names_defaults_and_descriptions,
208  std::vector<std::pair<std::string, std::string>> optional_arg_regexes_and_descriptions
209  );
210 
215 private:
217 };
218 
219 #endif
220 
MapperRegistry::printMapperList
void printMapperList(std::ostream &os) const
Print a message with every mapper's id and description.
Definition: Mapper.cpp:130
ImplicitTuple
Definition: TupleHash.h:105
MapperRegistry::getAllRegisteredArgDefaults
ConfigStore getAllRegisteredArgDefaults() const
Retrieve all registered default values for required arguments as a ConfigStore.
Definition: Mapper.cpp:94
OpGraph.h
MapperRegistry::m_mapper_makers
std::map< std::string, MakerAndDescription > m_mapper_makers
Definition: Mapper.h:161
MapperRegistry::makeKeyPrefix
const std::string makeKeyPrefix(const std::string &mapper_id) const
Definition: Mapper.h:137
MRRG
Definition: MRRG.h:216
MapperRegistry::MakerAndDescription::registered_defaults
ConfigStore registered_defaults
Definition: Mapper.h:147
MapperRegistry::MakerAndDescription::description
std::string description
Definition: Mapper.h:145
MapperRegistry::MakerAndDescription::maker
MapperMaker maker
Definition: Mapper.h:142
Mapper::mapOpGraph
virtual Mapping mapOpGraph(std::shared_ptr< OpGraph > opgraph, int II, const MRRG &mrrg, std::unordered_map< std::string, std::string > fix_port)=0
ConfigStore.h
MapperRegistry
Holds instances of mapper makers and creates mappers by ID, while resolving arguments.
Definition: Mapper.h:66
AutoRegisterMapper
Special helper for registering mappers to the default mapper registry.
Definition: Mapper.h:200
ConfigStore
Definition: ConfigStore.h:76
Mapper
Common interface for mappers.
Definition: Mapper.h:28
MapperRegistry::MakerAndDescription::findMatchingArg
auto findMatchingArg(const std::string &arg) const
Definition: Mapper.h:149
MapperRegistry::MakerAndDescription::is_composite
bool is_composite
Definition: Mapper.h:143
MapperRegistry::printHelpForMapper
void printHelpForMapper(const std::string &mapper_id, const ConfigStore &args, std::ostream &os) const
Print a detailed message about a mapper and the suggested args, including all required and optional a...
Definition: Mapper.cpp:189
Mapping
Definition: Mapping.h:31
Mapper::~Mapper
virtual ~Mapper()
Definition: Mapper.cpp:26
ConfigStore::hasKey
bool hasKey(const std::string &key) const
Definition: ConfigStore.h:209
MapperRegistry::operator=
MapperRegistry & operator=(const MapperRegistry &)=default
MapperRegistry::printDefaultsAsINI
void printDefaultsAsINI(std::ostream &os) const
Print an INI file with all registered defaults and some comments based on short descriptions.
Definition: Mapper.cpp:144
Mapper::Mapper
Mapper(std::shared_ptr< CGRA > cgra, int timelimit)
Definition: Mapper.cpp:21
MapperRegistry::MakerAndDescription::allowed_arg_regexes_and_documentation
std::map< std::string, std::pair< std::regex, std::string > > allowed_arg_regexes_and_documentation
Definition: Mapper.h:146
CGRA.h
AutoRegisterMapper::getDefaultRegistry
static const MapperRegistry & getDefaultRegistry()
Public read-only access to the mapper registry that this class automatically adds mappers to.
Definition: Mapper.h:214
Mapper::cgra
std::shared_ptr< CGRA > cgra
Definition: Mapper.h:45
TupleHash.h
AutoRegisterMapper::getDefaultMutableRegisty
static MapperRegistry & getDefaultMutableRegisty()
Definition: Mapper.cpp:224
Mapping.h
MapperMaker
std::function< std::unique_ptr< Mapper >(std::shared_ptr< CGRA >, int, const ConfigStore &)> MapperMaker
Function type for registering mappers.
Definition: Mapper.h:44
MapperRegistry::getMapperArgs
ConfigStore getMapperArgs(const std::string &mapper_id, const ConfigStore &args) const
Compute the arguments that would be passed when creating mapper mapper_id.
Definition: Mapper.cpp:102
AutoRegisterMapper::AutoRegisterMapper
AutoRegisterMapper(std::string mapper_id, MapperMaker maker, bool is_composite, std::string mapper_description, std::vector< ImplicitTuple< std::string, ImplicitlyToString, std::string >> required_arg_names_defaults_and_descriptions, std::vector< std::pair< std::string, std::string >> optional_arg_regexes_and_descriptions)
Register a mapper with the default registry. See MapperRegistry::registerMapper for documentation.
Definition: Mapper.cpp:236
MapperRegistry::registerMapper
void registerMapper(std::string mapper_id, MapperMaker maker, bool is_composite, std::string mapper_description, std::vector< ImplicitTuple< std::string, ImplicitlyToString, std::string >> required_arg_names_defaults_and_descriptions, std::vector< std::pair< std::string, std::string >> optional_arg_regexes_and_descriptions)
Register a new mapper under the name mapper_id.
Definition: Mapper.cpp:66
MapperRegistry::MakerAndDescription::hasMatchingArg
bool hasMatchingArg(const std::string &arg) const
Definition: Mapper.h:154
MapperRegistry::createMapper
std::unique_ptr< Mapper > createMapper(const std::string &mapper_id, std::shared_ptr< CGRA > cgra, int timelimit, const ConfigStore &args) const
Given a mapper_id, create an instance of that mapper with the given arguments.
Definition: Mapper.cpp:36
MapperRegistry::MakerAndDescription
Definition: Mapper.h:141
MapperRegistry::MapperRegistry
MapperRegistry(std::vector< ImplicitTuple< std::string, ImplicitlyToString, std::string >> common_defaults)
Initialize with some common defaults.
Definition: Mapper.cpp:30
Mapper::timelimit
int timelimit
Definition: Mapper.h:46