|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Alex Singer |
| 4 | + * @date March 2025 |
| 5 | + * @brief Implementation of a model grouper class which groups models together |
| 6 | + * which must be legalized together in a flat placement. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "model_grouper.h" |
| 10 | +#include <queue> |
| 11 | +#include <unordered_map> |
| 12 | +#include <unordered_set> |
| 13 | +#include <vector> |
| 14 | +#include "cad_types.h" |
| 15 | +#include "logic_types.h" |
| 16 | +#include "prepack.h" |
| 17 | +#include "vtr_assert.h" |
| 18 | +#include "vtr_log.h" |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Recursive helper function which gets the models in the given pattern |
| 22 | + * block. |
| 23 | + * |
| 24 | + * @param pattern_block |
| 25 | + * The pattern block to get the models of. |
| 26 | + * @param models |
| 27 | + * A set of the models found so far. |
| 28 | + * @param block_visited |
| 29 | + * A vector of flags for each pattern block to signify which blocks have |
| 30 | + * been visited. |
| 31 | + */ |
| 32 | +static void get_pattern_models(t_pack_pattern_block* pattern_block, |
| 33 | + std::unordered_set<int>& models, |
| 34 | + std::vector<bool>& block_visited) { |
| 35 | + // If the pattern block is invalid or this block has been visited, return. |
| 36 | + if (pattern_block == nullptr || block_visited[pattern_block->block_id]) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Mark this block as visited and insert its model into the models vector. |
| 41 | + block_visited[pattern_block->block_id] = true; |
| 42 | + models.insert(pattern_block->pb_type->model->index); |
| 43 | + |
| 44 | + // Go through this block's connections and get their pattern models. |
| 45 | + t_pack_pattern_connections* connection = pattern_block->connections; |
| 46 | + while (connection != nullptr) { |
| 47 | + get_pattern_models(connection->from_block, models, block_visited); |
| 48 | + get_pattern_models(connection->to_block, models, block_visited); |
| 49 | + connection = connection->next; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +ModelGrouper::ModelGrouper(const Prepacker& prepacker, |
| 54 | + t_model* user_models, |
| 55 | + t_model* library_models, |
| 56 | + int log_verbosity) { |
| 57 | + /** |
| 58 | + * Group the models together based on their pack patterns. If model A and |
| 59 | + * model B form a pattern, and model B and model C form a pattern, then |
| 60 | + * models A, B, and C are in a group together. |
| 61 | + * |
| 62 | + * An efficient way to find this is to represent this problem as a graph, |
| 63 | + * where each node is a model and each edge is a relationship where a model |
| 64 | + * is in a pack pattern with another model. We can then perform BFS to find |
| 65 | + * the connected sub-graphs which will be the groups. |
| 66 | + */ |
| 67 | + |
| 68 | + // Get the number of models |
| 69 | + // TODO: Clean up the models vectors in VTR. |
| 70 | + std::unordered_map<int, char*> model_name; |
| 71 | + unsigned num_models = 0; |
| 72 | + t_model* model = library_models; |
| 73 | + while (model != nullptr) { |
| 74 | + model_name[model->index] = model->name; |
| 75 | + num_models++; |
| 76 | + model = model->next; |
| 77 | + } |
| 78 | + model = user_models; |
| 79 | + while (model != nullptr) { |
| 80 | + model_name[model->index] = model->name; |
| 81 | + num_models++; |
| 82 | + model = model->next; |
| 83 | + } |
| 84 | + |
| 85 | + // Create an adjacency list for the edges. An edge is formed where two |
| 86 | + // models share a pack pattern together. |
| 87 | + std::vector<std::unordered_set<int>> adj_list(num_models); |
| 88 | + for (const t_pack_patterns& pack_pattern : prepacker.get_all_pack_patterns()) { |
| 89 | + // Get the models within this pattern. |
| 90 | + std::unordered_set<int> models_in_pattern; |
| 91 | + std::vector<bool> block_visited(pack_pattern.num_blocks, false); |
| 92 | + get_pattern_models(pack_pattern.root_block, models_in_pattern, block_visited); |
| 93 | + VTR_ASSERT_SAFE(!models_in_pattern.empty()); |
| 94 | + |
| 95 | + // Debug print the models within the pattern. |
| 96 | + if (log_verbosity >= 20) { |
| 97 | + VTR_LOG("Pattern: %s\n\t", pack_pattern.name); |
| 98 | + for (int model_idx : models_in_pattern) { |
| 99 | + VTR_LOG("%s ", model_name[model_idx]); |
| 100 | + } |
| 101 | + VTR_LOG("\n"); |
| 102 | + } |
| 103 | + |
| 104 | + // Connect each of the models to the first model in the pattern. Since |
| 105 | + // we only care if there exist a path from each model to another, we do |
| 106 | + // not need to connect the models in a clique. |
| 107 | + int first_model_idx = *models_in_pattern.begin(); |
| 108 | + for (int model_idx : models_in_pattern) { |
| 109 | + adj_list[model_idx].insert(first_model_idx); |
| 110 | + adj_list[first_model_idx].insert(model_idx); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Perform BFS to group the models. |
| 115 | + VTR_LOGV(log_verbosity >= 20, |
| 116 | + "Finding model groups...\n"); |
| 117 | + std::queue<int> node_queue; |
| 118 | + model_group_id_.resize(num_models, ModelGroupId::INVALID()); |
| 119 | + for (int model_idx = 0; model_idx < (int)num_models; model_idx++) { |
| 120 | + // If this model is already in a group, skip it. |
| 121 | + if (model_group_id_[model_idx].is_valid()) { |
| 122 | + VTR_LOGV(log_verbosity >= 20, |
| 123 | + "\t(%d -> %d)\n", model_idx, model_group_id_[model_idx]); |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + ModelGroupId group_id = ModelGroupId(group_ids_.size()); |
| 128 | + // Put the model in this group and push to the queue. |
| 129 | + model_group_id_[model_idx] = group_id; |
| 130 | + node_queue.push(model_idx); |
| 131 | + |
| 132 | + while (!node_queue.empty()) { |
| 133 | + // Pop a node from the queue, and explore its neighbors. |
| 134 | + int node_model_idx = node_queue.front(); |
| 135 | + node_queue.pop(); |
| 136 | + for (int neighbor_model_idx : adj_list[node_model_idx]) { |
| 137 | + // If this neighbor is already in this group, skip it. |
| 138 | + if (model_group_id_[neighbor_model_idx].is_valid()) { |
| 139 | + VTR_ASSERT_SAFE(model_group_id_[neighbor_model_idx] == group_id); |
| 140 | + continue; |
| 141 | + } |
| 142 | + // Put the neighbor in this group and push it to the queue. |
| 143 | + model_group_id_[neighbor_model_idx] = group_id; |
| 144 | + node_queue.push(neighbor_model_idx); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + VTR_LOGV(log_verbosity >= 20, |
| 149 | + "\t(%d -> %d)\n", model_idx, model_group_id_[model_idx]); |
| 150 | + group_ids_.push_back(group_id); |
| 151 | + } |
| 152 | + |
| 153 | + // Create a lookup between each group and the models it contains. |
| 154 | + groups_.resize(groups().size()); |
| 155 | + for (int model_idx = 0; model_idx < (int)num_models; model_idx++) { |
| 156 | + groups_[model_group_id_[model_idx]].push_back(model_idx); |
| 157 | + } |
| 158 | + |
| 159 | + // Debug printing for each group. |
| 160 | + if (log_verbosity >= 20) { |
| 161 | + for (ModelGroupId group_id : groups()) { |
| 162 | + const std::vector<int>& group = groups_[group_id]; |
| 163 | + VTR_LOG("Group %zu:\n", group_id); |
| 164 | + VTR_LOG("\tSize = %zu\n", group.size()); |
| 165 | + VTR_LOG("\tContained models:\n"); |
| 166 | + for (int model_idx : group) { |
| 167 | + VTR_LOG("\t\t%s\n", model_name[model_idx]); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments