Untitled

mail@pastecode.io avatarunknown
plain_text
25 days ago
10 kB
1
Indexable
Never
void legalize() //yu--modified(0824)
{

    Cell_in_top.clear();
    Cell_in_bottom.clear();
    for (int i = 0; i < Instance_cnt; i++)
    {
        if (partition_map[i] == 0)
            Cell_in_top.push_back(i);
        else if (partition_map[i] == 1)
            Cell_in_bottom.push_back(i);
    }

       
    //create size_array
    struct s{
        bool operator() (const pair<int,int> &a, const pair<int,int> &b)
        {
            return (a.second > b.second);   //descending order
        }
    }sortbysize;
    
    vector<pair<int, int>> top_size_array;// [inst_num][size]
    vector<pair<int , int>> bot_size_array;
    vector<pair<int, int>> top_size_array_macro;// [inst_num][size]
    vector<pair<int, int>> bot_size_array_macro;
    top_size_array.resize(Cell_in_top.size(), make_pair(-1, -1));
    bot_size_array.resize(Cell_in_bottom.size(), make_pair(-1, -1));
    for(int i=0; i<top_size_array.size(); i++){
        top_size_array[i].first = Cell_in_top[i];
        top_size_array[i].second = LibCellSize[top][Lib_for_Inst[Cell_in_top[i]]];
        if(LibCell_isMacro[0][Lib_for_Inst[Cell_in_top[i]]]){ // is macro
            top_size_array_macro.push_back(make_pair(top_size_array[i].first, top_size_array[i].second));
        }
        // cout << "be---C" << top_size_array[i].first+1 << ", size: " << top_size_array[i].second << endl;
    }
    for(int i=0; i<bot_size_array.size(); i++){
        bot_size_array[i].first = Cell_in_bottom[i];
        bot_size_array[i].second = LibCellSize[bot][Lib_for_Inst[Cell_in_bottom[i]]];
        if(LibCell_isMacro[0][Lib_for_Inst[Cell_in_bottom[i]]]){ // is macro
            bot_size_array_macro.push_back(make_pair(bot_size_array[i].first, bot_size_array[i].second));
        }
        // cout << "be---C" << bot_size_array[i].first+1 << ", size: " << bot_size_array[i].second << endl;
    }
    
    
    sort(top_size_array.begin(), top_size_array.end(), sortbysize);
    sort(bot_size_array.begin(), bot_size_array.end(), sortbysize);
    sort(top_size_array_macro.begin(), top_size_array_macro.end(), sortbysize);
    sort(bot_size_array_macro.begin(), bot_size_array_macro.end(), sortbysize);

    int stop = 0;
    int flag = -1;
    /*yu--modified(0826)*/
    using namespace std::chrono;

    const int timeoutMinutes = 1; //count down 1 min
    const auto timeout = minutes(timeoutMinutes);

    auto startTime = high_resolution_clock::now();
    int counter = 0;
    /*yu--modified end*/
    while (1)
    {
        /*yu--modified(0826)*/
        auto currentTime = high_resolution_clock::now();
        auto elapsedTime = duration_cast<minutes>(currentTime - startTime);

        
        if (elapsedTime >= timeout) {
            //can change to another legalize
            std::cout << "Timeout reached. Terminating." << std::endl;
            std::cout << "Press Enter to terminate...";
            std::cin.get(); // Wait for user to press Enter
            break;
        }
        
        /*yu--modified end*/
        cout << "------top_size: " << top_size_array.size() << ", bot_size: " << bot_size_array.size() << endl; 
        cout << "TopDieRealArea: " << TopDieRealArea << ", BottomDieRealArea: " << BottomDieRealArea << endl;
        cout << "TopDieMaxArea: " << TopDieMaxArea << ", BottomDieMaxArea: " << BottomDieMaxArea << endl;

        if ((TopDieRealArea < TopDieMaxArea) && (BottomDieRealArea < BottomDieMaxArea))
        {
            stop++;
        }
        if (stop == 1)
        {
            if (TopDieMaxArea < 50000 || BottomDieMaxArea < 50000)
                break;
        }
        if (stop == 5)
        {
            if (TopDieMaxArea < 100000000 || BottomDieMaxArea < 1000000)
                break;
        }
        if (stop == 10)
        {
            break;
        }
        if (TopDieRealArea >= TopDieMaxArea)
            flag = 0;
        else if (BottomDieRealArea >= BottomDieMaxArea)
            flag = 1;
        
        int swap_inst = -1;
        
        if (NumofTech == 2)
        {
            if (flag == 0)
            {
                for (int i = 0; i < top_size_array.size(); i++)
                {
                    int idx = top_size_array[i].first;
                    if ((BottomDieRealArea + LibCellSize[bot][Lib_for_Inst[idx]]) < BottomDieMaxArea)
                    {
                        if (!LibCell_isMacro[0][Lib_for_Inst[idx]]) //only swap std cell
                        {
                            swap_inst = idx;
                            // Remove the element from top_size_array
                            top_size_array.erase(top_size_array.begin() + i);
                            // Since we removed an element, adjust the index and size
                            i--;
                            
                            break;
                        }
                    }
                    
                }
                if (swap_inst != -1)
                {
                    // swap instance
                    partition_map[swap_inst] = 1;
                    BottomDieRealArea += LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea -= LibCellSize[top][Lib_for_Inst[swap_inst]];
                }
                else{
                    cout << "Can not find inst swap to bot!!!\n" << endl;
                    for (int i = 0; i < top_size_array_macro.size(); i++){
                        int idx = top_size_array_macro[i].first;
                        swap_inst = idx;
                        top_size_array_macro.erase(top_size_array_macro.begin() + i);
                        i--;
                        break;
                    }
                    // swap instance
                    partition_map[swap_inst] = 1;
                    BottomDieRealArea += LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea -= LibCellSize[top][Lib_for_Inst[swap_inst]];
                    //swap another macro
                    for (int i = 0; i < top_size_array_macro.size(); i++){
                        int idx = top_size_array_macro[i].first;
                        swap_inst = idx;
                        top_size_array_macro.erase(top_size_array_macro.begin() + i);
                        i--;
                        break;
                    }
                    // swap instance
                    partition_map[swap_inst] = 1;
                    BottomDieRealArea += LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea -= LibCellSize[top][Lib_for_Inst[swap_inst]];
                }
            }
            else if (flag == 1)
            {
                for (int i = 0; i < bot_size_array.size(); i++)
                {
                    int idx = bot_size_array[i].first;
                    if ((TopDieRealArea + LibCellSize[top][Lib_for_Inst[idx]]) < TopDieMaxArea)
                    {
                        if (!LibCell_isMacro[0][Lib_for_Inst[idx]]) //only swap std cell
                        {
                            swap_inst = idx;
                            // Remove the element from bot_size_array
                            bot_size_array.erase(bot_size_array.begin() + i);
                            // Since we removed an element, adjust the index and size
                            i--;
                             
                            break;
                        }
                    }
                    
                }
                if (swap_inst != -1)
                {
                    // swap instance
                    partition_map[swap_inst] = 0;
                    BottomDieRealArea -= LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea += LibCellSize[top][Lib_for_Inst[swap_inst]];
                    
                }
                else{
                    cout << "Can not find inst swap to top!!!\n" << endl;
                    for (int i = 0; i < bot_size_array_macro.size(); i++){
                        int idx = bot_size_array_macro[i].first;
                        swap_inst = idx;
                        bot_size_array_macro.erase(bot_size_array_macro.begin() + i);
                        i--;
                        break;
                    }
                    // swap instance
                    partition_map[swap_inst] = 0;
                    BottomDieRealArea -= LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea += LibCellSize[top][Lib_for_Inst[swap_inst]];
                    //swap another macro
                    for (int i = 0; i < bot_size_array_macro.size(); i++){
                        int idx = bot_size_array_macro[i].first;
                        swap_inst = idx;
                        bot_size_array_macro.erase(bot_size_array_macro.begin() + i);
                        i--;
                        break;
                    }
                    // swap instance
                    partition_map[swap_inst] = 0;
                    BottomDieRealArea -= LibCellSize[bot][Lib_for_Inst[swap_inst]];
                    TopDieRealArea += LibCellSize[top][Lib_for_Inst[swap_inst]];
                    
                }
            }
        }
        else if (NumofTech == 1)
        {
            if (flag == 0)
            {
                for (int i = 0; i < top_size_array.size(); i++)
                {
                    int idx = top_size_array[i].first;
                    if ((BottomDieRealArea + LibCellSize[bot][Lib_for_Inst[idx]]) < BottomDieMaxArea)
                    {
                        if (!LibCell_isMacro[0][Lib_for_Inst[idx]]) //only swap std cell
                        {
                            swap_inst = idx;
                            // Remove the element from top_size_array
                            top_size_array.erase(top_size_array.begin() + i);
                            // Since we removed an element, adjust the index and size
                            i--;