Skip to content

Commit

Permalink
First non-working try to incorporate the new HPC model.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrino-meli authored and Ev3nt1ne committed Apr 9, 2024
1 parent ea87db3 commit 3300430
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 147 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Getting started
Proof of concept: main_poc.mlx



# Instalation
for osqp:
needs CMake (sudo apt install CMake)
needs gcc
Expand Down
248 changes: 101 additions & 147 deletions scripts/extract_hpc2mat.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
% TODO: change these path if you are on a different system
clear all;
addpath('../'); % add git repo base to search path
addpath('../Controllers'); % add git repo base to search path
basename = '../../src'; % directory were .mat files are saved to

experiments = ...
Expand Down Expand Up @@ -34,158 +35,111 @@
];

for values = experiments.'
% 1 Instantiate main Class:
hpc = hpc_lab;

% Rows and Columns
hpc.Nv = values(2); % Number of Processing Elements in each row
hpc.Nh = values(1); % Number of PE's in each column

% Core number
hpc.Nc = hpc.Nh*hpc.Nv; % Total Number of controlled PE's in thermal model

%% 1: only instantiate
Nh = values(1); % Number of Processing Elements in each row
Nv = values(2); % Number of PE's in each column
Nc = Nh*Nv; % Total Number of controlled PE's in thermal model
hpc = hpc_system(Nc,Nh,Nv);
hpc.Nhzn = values(3); % Control Horizon

%% 2: Define System Parameters: Init + Thermal
% Cores:
display(char(compose('Generating linear thermal model with Nh=%d, Nv=%d and horizon=%d',hpc.Nh, hpc.Nv, hpc.Nhzn)));
hpc % verbose output:

% Version of the Thermal Model
% hpc.thermal_model_ver = 0;
% Exponential Leakage
% hpc.exp_leakage = 1;
% Noise and Variation
%hpc.model_variation = 1;

% hpc.pw_gmean = 0.02;
% hpc.pw_gvar = 1;
% hpc.pw_glim = [-0.02 0.05];
% hpc.pw_3sigma_on3 = 0.05;
% hpc = hpc.create_core_pw_noise();
% hpc.measure_noise = 1;

% External Ambient Temperature
temp_amb = 25.0 + 273.15;

% Create Thermal Model:
hpc = hpc.create_thermal_model();

hpc.x_init = temp_amb*ones(hpc.Ns,1);

%% 3: Simulation setup

% SIMULATION TIME (in sec)
hpc.tsim = 2.0;
% Simulation Time-Step
hpc.Ts = 50e-6;
% Workload Quantum-step
hpc.quantum_us = 50;

% Controller Time-Step
hpc.Ts_ctrl = 500e-6;
% Input Time-Step
hpc.Ts_input = 1e-3;

% Probability for each WL
hpc.wl_prob = [0 2 3 2 2.5];
% minimal execution in us
hpc.wl_min_exec_us = [50e3/8 5e3 5e3 5e3 100e3];
% mean execution in us
hpc.wl_mean_exec_us = [150e3/8 10e3 10e3 10e3 200e3];

% hpc = hpc.generate_reference();

hpc.T_noise_max = 0.5;
hpc.F_discretization_step = 0.05;


%% 4 Controller

% Max Temperature
hpc.core_crit_temp = 85 + 273.15;
% Max Power
ts = ceil(hpc.tsim / hpc.Ts_input)+1;
hpc.tot_pw_budget = 450*ones(ts,1);
ts = ceil(ts/4);
hpc.tot_pw_budget(ts+1:2*ts) = 4*hpc.Nc;
hpc.tot_pw_budget(2*ts+1:3*ts) = 2*hpc.Nc;
hpc.tot_pw_budget(3*ts+1:3*ts+ceil(ts/2)) = 6*hpc.Nc;
hpc.tot_pw_budget(3*ts+ceil(ts/2)+1:end) = 8*hpc.Nc;

% TODO: ??? why
% TODO: any constraint change is only allowed to change the l, u vector
% not the A matrix -> otherwise numerical refactorization has to be done.
% TODO: but changes in l, u might infringe on recursive feasibility!
% Actually total power budget should be a soft constraint to avoid problems in that regard.
% TODO: insentive on setpoint should be given by the reference!
hpc.tot_pw_budget = hpc.tot_pw_budget/36*hpc.Nc;
% Delays
hpc.delay_F_mean = 1e-5; % Mean Frequency application Delay
hpc.delay_V_mean = 1e-5; % Mean Voltage application Delay

%hpc.frplot = 3.45 * ones(min(ceil(hpc.tsim / hpc.Ts_input)+1,(hpc.tsim/hpc.Ts_ctrl+1)), hpc.Nc);

%% MPC init:

% Robust Controls
robust = 1;

R_coeff = 10; %10;
R2_coeff = 100; %30; %10

hpc.R = R_coeff*eye(hpc.Nc);
hpc.R2 = zeros(hpc.Nc);

for v=1:hpc.vd
%Here I could create an accumulation thing that need to be
%optimized (e.g. reduced) that contains the deltaF among quadrant
hpc.R2 = hpc.R2 + hpc.VDom(:,v)*hpc.VDom(:,v)' / sum(hpc.VDom(:,v))^2 * R2_coeff;
end
%TODO: Assuming that the diagonal is full
hpc.R2(~eye(size(hpc.R2))) = hpc.R2(~eye(size(hpc.R2))) * (-1);
cores_per_dom = sum(hpc.VDom);
hpc.R2(logical(eye(size(hpc.R2)))) = hpc.R2(~~eye(size(hpc.R2))) .* hpc.VDom*cores_per_dom'; %here I need ~~ to converto to logical values

hpc.yMax = ones(hpc.Nout,1)*hpc.core_crit_temp;
% TODO: check that core power constraint is not unbounded
hpc.umin = hpc.core_min_power;
hpc.uMax = hpc.core_Max_power;
hpc.usum = [hpc.tot_pw_budget(1) ...
hpc.quad_pw_budget(1,:)];

hpc.mpc_robustness = 0.7;

%{
if robust == 1
hpc.Cty = hpc.find_unc_set(hpc.mpc_robustness*100)*hpc.C';
%hpc.Ctu = hpc.max_uncertainty(??,hpc.mpc_robustness*100)*ones(hpc.Nhzn, hpc.Ni_c);
hpc.Ctu = repelem(hpc.mpc_robustness* ...
abs(hpc.power_compute(hpc.F_Max*ones(hpc.Nc,1),hpc.V_Max*ones(hpc.Nc,1),ones(hpc.Nc,1)*hpc.core_crit_temp,[1 zeros(1,hpc.ipl-1)],1) ...
- hpc.power_compute(hpc.F_Max*ones(hpc.Nc,1),hpc.V_Max*ones(hpc.Nc,1),ones(hpc.Nc,1)*hpc.core_crit_temp,[zeros(1,hpc.ipl-1) 1],1) ), ...
1, hpc.Nhzn)';
%}
if robust == 1
hpc.Cty = 5 * ones(hpc.Nhzn, hpc.Nout);
hpc.Ctu = 0.3 * ones(hpc.Nhzn, hpc.Ni_c);
else
hpc.Cty = zeros(hpc.Nhzn, hpc.Nout);
hpc.Ctu = zeros(hpc.Nhzn, hpc.Ni_c);
end

%%
hpc.R2 = zeros(hpc.Nc);

hpc.exp_leakage = 0;
hpc.ctrl_MA = 0;
hpc.iterative_fv = ~hpc.ctrl_MA;

%% linear mpc
hpc = hpc.lin_mpc_setup();


% dump Quadratic Program matrix data for further optimization, code generation
% Number of Voltage Domains
hpc.vd = 3;

% How the cores are distributed per domain
%hpc.VDom = ... ;
% Alternatively
hpc.default_VDom_config();

% Default Floorplan and Parameter deviation vector
hpc.default_floorplan_config();
hpc.create_model_deviation();

%Decide simulation frequency (discrete timing)
hpc.Ts = 5e-5;

%Decide total simulation time
hpc.tsim = 2;

%Presence/Absence of sensor noise:
hpc.sensor_noise = 1;

%Thermal model version
hpc.model_ver = 0;

hpc.model_init();
%hpc.create_core_pw_noise();

% Initial Temperature condition
hpc.t_init = hpc.temp_amb*ones(hpc.Ns,1);

%Input maximum frequency
hpc.Ts_target;

tt = min(ceil(hpc.tsim / hpc.Ts_target)+1, (hpc.tsim/1e-4+1));

% Target Frequency Trajectory
hpc.frtrc = 3.45 * ones(tt, hpc.Nc);

%Target Power Budget
hpc.tot_pw_budget = 450/36*hpc.Nc*ones(tt,1);
tu = ceil(tt/4);
hpc.tot_pw_budget(tu+1:2*tu) = 2*hpc.Nc;
hpc.tot_pw_budget(2*tu+1:3*tu) = 5*hpc.Nc;
hpc.tot_pw_budget(3*tu+1:3*tu+ceil(tu/2)) = 3*hpc.Nc;
hpc.tot_pw_budget(3*tu+ceil(tu/2)+1:end) = 8*hpc.Nc;
hpc.quad_pw_budget = 450/36*hpc.Nc*ones(2,hpc.vd);

%Others, TODO
hpc.min_pw_red = 0.6;

%hpc.anteSimCheckTM();
%hpc.anteSimCheckLab();
%hpc.anteSimCheckPM();
%
% Changing Ts to improve the speed of simulation
%hpc.Ts = 250e-6;
%hpc.model_init();
%hpc.sim_tm_autonomous(tsim_aut)
hpc.Ts = 5e-5;
hpc.model_init();

% MPC
ctrl = cp_mpc();
ctrl.C = eye(hpc.Ns);

% Control Horizon
ctrl.Nhzn = values(3);

%Controller frequency (s)
ctrl.Ts_ctrl = 5e-3;

%Robust Margins for T and P
ctrl.Cty = zeros(ctrl.Nhzn, hpc.Nc);
ctrl.Ctu = zeros(ctrl.Nhzn, hpc.Nc);

%Reference Tracking Objective Matrix
R_coeff = 10;
ctrl.R = R_coeff*eye(hpc.Nc);

%Others
ctrl.R2 = zeros(hpc.Nc);
ctrl.Q = zeros(hpc.Ns);

%% 4 verbose
hpc
ctrl

ctrl.init_fnc(hpc,1);

%% 5 dump Quadratic Program matrix data for further optimization, code generation
filename = char(compose('%s/_HPC_%dx%d_H%d.mat',basename, hpc.Nh, hpc.Nv, hpc.Nhzn));
display(strcat('Saving model to file: ',filename));
ops = sdpsettings('verbose',1,'solver','osqp', 'usex0',0);
hpc.ylmp_constraints;
yalmip2mat(filename,hpc.ylmp_constraints,hpc.ylmp_objective,ops,hpc.ylmp_opt_variables,hpc.ylmp_opt_output);
%yalmip2mat(filename,hpc.ylmp_constraints,hpc.ylmp_objective,ops,hpc.ylmp_opt_variables,hpc.ylmp_opt_output);
%yalmip_model = export(constraints,hpcective,ops,opt_variables,opt_output);
%a = yalmip2osqp(yalmip_model);
%ops = sdpsettings('verbose',1,'solver','osqp', 'savesolverinput',1);
Expand Down

0 comments on commit 3300430

Please sign in to comment.