GlobalPlacer.cpp

 avatar
user_9702121
c_cpp
7 months ago
3.9 kB
3
Indexable
Never
#include "GlobalPlacer.h"
#include "ExampleFunction.h"
#include "NumericalOptimizer.h"
#include <cstdlib>
#include <iostream>

GlobalPlacer::GlobalPlacer(wrapper::Placement &placement)
    : _placement(placement)
{
}

void GlobalPlacer::randomPlace()
{
    srand(0);
    double coreWidth = _placement.boundryRight() - _placement.boundryLeft();
    double coreHeight = _placement.boundryTop() - _placement.boundryBottom();
    for (size_t i = 0; i < _placement.numModules(); ++i)
    {
        if (_placement.module(i).isFixed())
            continue;

        double width = _placement.module(i).width();
        double height = _placement.module(i).height();
        double x = rand() % static_cast<int>(coreWidth - width) + _placement.boundryLeft();
        double y = rand() % static_cast<int>(coreHeight - height) + _placement.boundryBottom();
        _placement.module(i).setPosition(x, y);
    }
}

void GlobalPlacer::place()
{
    ///////////////////////////////////////////////////////////////////
    // The following example is only for analytical methods.
    // if you use other methods, you can skip and delete it directly.
    //////////////////////////////////////////////////////////////////
    randomPlace();
    vector<double> x(_placement.numModules() * 2); // solution vector, size: num_blocks*2
    for(size_t i = 0; i < _placement.numModules(); i++) {
        x[2 * i] = _placement.module(i).x();
        x[2 * i + 1] = _placement.module(i).y();
    }

    for(uint iteration = 0; iteration < 4; iteration++) {
        ExampleFunction ef(_placement, 100, 10 * (iteration + 1), 10); // require to define the object function and gradient function
                            // each 2 variables represent the X and Y dimensions of a block
        // x[0] = 100;          // initialize the solution vector
        // x[1] = 100;
        // x[2 * i] = modulei.x()
        // x[2 * i + 1] = modulei.y()
        cout << "beta: " << ef.beta << '\n';
        NumericalOptimizer no(ef);
        double stepSize = (ef.placement.boundryRight() - ef.placement.boundryLeft());
        no.setX(x);             // set initial solution
        no.setNumIteration(iteration == 0 ? 150 : 35); // user-specified parameter
        no.setStepSizeBound(stepSize); // user-specified parameter
        no.solve();             // Conjugate Gradient solver
        for(size_t i = 0; i < _placement.numModules(); i++) {
            if(_placement.module(i).isFixed()) {
                double boundL = _placement.boundryLeft();
                continue;
            }
            else {
                // cout << "place " << endl;
                _placement.module(i).setPosition(no.x(2 * i),no.x(2 * i + 1));
            }
            x[2 * i] = _placement.module(i).x();
            x[2 * i + 1] = _placement.module(i).y();
        }
    cout << "Current solution:\n";
    cout << "Objective: " << no.objective() << "\n";

    }

    ////////////////////////////////////////////////////////////////

    // An example of random placement implemented by TA.
    // If you want to use it, please uncomment the folllwing 1 line.

    /* @@@ TODO
     * 1. Understand above example and modify ExampleFunction.cpp to implement the analytical placement
     * 2. You can choose LSE or WA as the wirelength model, the former is easier to calculate the gradient
     * 3. For the bin density model, you could refer to the lecture notes
     * 4. You should first calculate the form of wirelength model and bin density model and the forms of their gradients ON YOUR OWN
     * 5. Replace the value of f in evaluateF() by the form like "f = alpha*WL() + beta*BinDensity()"
     * 6. Replace the form of g[] in evaluateG() by the form like "g = grad(WL()) + grad(BinDensity())"
     * 7. Set the initial vector x in place(), set step size, set #iteration, and call the solver like above example
     * */
}
Leave a Comment