-
Notifications
You must be signed in to change notification settings - Fork 15
Prototyping architecture modeling #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #ifndef NEURA_ARCHITECTURE_H | ||
| #define NEURA_ARCHITECTURE_H | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| #include <set> | ||
| #include <unordered_map> | ||
| #include <optional> | ||
| #include <memory> | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // BasicResource: abstract base class for Tile, Link, etc. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class BasicResource { | ||
| public: | ||
| virtual ~BasicResource() = default; | ||
| virtual int getId() const = 0; | ||
| virtual std::string getType() const = 0; | ||
| }; | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Forward declaration for use in Tile | ||
| class Link; | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Tile | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class Tile : public BasicResource { | ||
| public: | ||
| Tile(int id, int x, int y); | ||
|
|
||
| int getId() const override; | ||
| std::string getType() const override { return "tile"; } | ||
|
|
||
| int getX() const; | ||
| int getY() const; | ||
|
|
||
| void linkDstTile(Link* link, Tile* tile); | ||
| const std::set<Tile*>& getDstTiles() const; | ||
| const std::set<Tile*>& getSrcTiles() const; | ||
| const std::set<Link*>& getOutLinks() const; | ||
| const std::set<Link*>& getInLinks() const; | ||
|
|
||
| private: | ||
| int id; | ||
| int x, y; | ||
| std::set<Tile*> src_tiles; | ||
| std::set<Tile*> dst_tiles; | ||
| std::set<Link*> in_links; | ||
| std::set<Link*> out_links; | ||
| }; | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // Link | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class Link : public BasicResource { | ||
| public: | ||
| Link(int id); | ||
|
|
||
| int getId() const override; | ||
| std::string getType() const override { return "link"; } | ||
|
|
||
| Tile* getSrcTile() const; | ||
| Tile* getDstTile() const; | ||
|
|
||
| void connect(Tile* src, Tile* dst); | ||
|
|
||
| private: | ||
| int id; | ||
| Tile* src_tile; | ||
| Tile* dst_tile; | ||
| }; | ||
|
|
||
| /// Describes the entire CGRA architecture. | ||
| class Architecture { | ||
| public: | ||
| Architecture(int width, int height); | ||
|
|
||
| Tile* getTile(int id); | ||
| Tile* getTile(int x, int y); | ||
|
|
||
| Link* getLink(int id); | ||
|
|
||
| int getNumTiles() const { return static_cast<int>(tiles.size()); } | ||
| std::vector<Tile*> getAllTiles() { return tiles; } | ||
|
|
||
| private: | ||
| std::vector<std::unique_ptr<Tile>> tileStorage; | ||
| std::vector<Tile*> tiles; | ||
| }; | ||
|
|
||
| } // namespace neura | ||
| } // namespace mlir | ||
|
|
||
| #endif // NEURA_ARCHITECTURE_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef NEURA_MAPPING_STATE_H | ||
| #define NEURA_MAPPING_STATE_H | ||
|
|
||
| #include "mlir/IR/Operation.h" | ||
| #include "NeuraDialect/Architecture/Architecture.h" // for BasicResource | ||
| #include <unordered_map> | ||
| #include <unordered_set> | ||
| #include <vector> | ||
| #include <optional> | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
|
|
||
| // Represents a spatial-temporal location: (resource, timeStep) | ||
| using MappingLoc = std::pair<BasicResource*, int>; | ||
|
|
||
| // Tracks placement and routing of ops on the CGRA. | ||
| class MappingState { | ||
| public: | ||
| // Binds a (tile/link, timeStep) location to an operation. | ||
| void bindOp(MappingLoc loc, Operation *op); | ||
|
|
||
| // Checks if a (tile/link, timeStep) is available (unoccupied). | ||
| bool isAvailable(const MappingLoc &loc) const; | ||
|
|
||
| // Gets the operation at a specific (tile/link, timeStep) location. | ||
| std::optional<Operation*> getOpAt(MappingLoc loc) const; | ||
|
|
||
| private: | ||
| std::unordered_map<MappingLoc, Operation*> loc_to_op; | ||
| std::unordered_set<MappingLoc> occupied_locs; | ||
ShangkunLi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace neura | ||
| } // namespace mlir | ||
|
|
||
| #endif // NEURA_MAPPING_STATE_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #include "NeuraDialect/Architecture/Architecture.h" | ||
| #include <cassert> | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::neura; | ||
|
|
||
| Tile::Tile(int id, int x, int y) { | ||
| this->id = id; | ||
| this->x = x; | ||
| this->y = y; | ||
| } | ||
|
|
||
| int Tile::getId() const { | ||
| return id; | ||
| } | ||
|
|
||
| int Tile::getX() const { | ||
| return x; | ||
| } | ||
|
|
||
| int Tile::getY() const { | ||
| return y; | ||
| } | ||
|
|
||
| void Tile::linkDstTile(Link* link, Tile* tile) { | ||
| assert(tile && "Cannot link to a null tile"); | ||
| dst_tiles.insert(tile); | ||
| out_links.insert(link); | ||
| tile->src_tiles.insert(this); | ||
| tile->in_links.insert(link); | ||
| } | ||
|
|
||
| const std::set<Tile*>& Tile::getDstTiles() const { | ||
| return dst_tiles; | ||
| } | ||
|
|
||
| const std::set<Tile*>& Tile::getSrcTiles() const { | ||
| return src_tiles; | ||
| } | ||
|
|
||
| const std::set<Link*>& Tile::getOutLinks() const { | ||
| return out_links; | ||
| } | ||
|
|
||
| const std::set<Link*>& Tile::getInLinks() const { | ||
| return in_links; | ||
| } | ||
|
|
||
| Link::Link(int id) { | ||
| this->id = id; | ||
| } | ||
|
|
||
| int Link::getId() const { | ||
| return id; | ||
| } | ||
|
|
||
| Tile* Link::getSrcTile() const { | ||
| return src_tile; | ||
| } | ||
|
|
||
| Tile* Link::getDstTile() const { | ||
| return dst_tile; | ||
| } | ||
|
|
||
| void Link::connect(Tile* src, Tile* dst) { | ||
| assert(src && dst && "Cannot connect null tiles"); | ||
| src_tile = src; | ||
| dst_tile = dst; | ||
| src->linkDstTile(this, dst); | ||
| } | ||
|
|
||
| Architecture::Architecture(int width, int height) { | ||
| const int num_tiles = width * height; | ||
|
|
||
| tileStorage.reserve(num_tiles); | ||
| tiles.reserve(num_tiles); | ||
|
|
||
| for (int i = 0; i < width; ++i) { | ||
| for (int j = 0; j < height; ++j) { | ||
| auto tile = std::make_unique<Tile>(i * width + j, i, j); | ||
| tiles.push_back(tile.get()); | ||
| tileStorage.push_back(std::move(tile)); | ||
| } | ||
| } | ||
|
|
||
| // TODO: Model topology based on the architecture specs. | ||
| // https://github.com/coredac/dataflow/issues/52. | ||
| int link_id = 0; | ||
| for (int i = 0; i < width; ++i) { | ||
| for (int j = 0; j < height; ++j) { | ||
| Tile* tile = getTile(i, j); | ||
| if (i > 0) { | ||
| auto link_towards_left = std::make_unique<Link>(link_id++); | ||
| link_towards_left->connect(tile, getTile(i - 1, j)); | ||
| } | ||
| if (i < width - 1) { | ||
| auto link_towards_right = std::make_unique<Link>(link_id++); | ||
| link_towards_right->connect(tile, getTile(i + 1, j)); | ||
| } | ||
| if (j > 0) { | ||
| auto link_towards_down = std::make_unique<Link>(link_id++); | ||
| link_towards_down->connect(tile, getTile(i, j - 1)); | ||
| } | ||
| if (j < height - 1) { | ||
| auto link_towards_up = std::make_unique<Link>(link_id++); | ||
| link_towards_up->connect(tile, getTile(i, j + 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Tile* Architecture::getTile(int id) { | ||
| for (const auto &tile : tiles) { | ||
| if (tile->getId() == id) { | ||
| return tile; | ||
| } | ||
| } | ||
| assert(false && "Tile with given ID not found"); | ||
| return nullptr; | ||
| } | ||
|
|
||
| Tile* Architecture::getTile(int x, int y) { | ||
| for (const auto &tile : tiles) { | ||
| if (tile->getX() == x && tile->getY() == y) { | ||
| return tile; | ||
| } | ||
| } | ||
| assert(false && "Tile with given coordinates not found"); | ||
| return nullptr; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #include "NeuraDialect/Mapping/MappingState.h" | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::neura; | ||
|
|
||
| void MappingState::bindOp(MappingLoc loc, Operation *op) { | ||
| loc_to_op[loc] = op; | ||
| occupied_locs.insert(loc); | ||
| } | ||
|
|
||
| bool MappingState::isAvailable(const MappingLoc &loc) const { | ||
| return !occupied_locs.contains(loc); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.