Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
Add minor modifications
Browse files Browse the repository at this point in the history
- Add server files to run on python 2 (server_py2 and util_py2)
- Save the GA final population to file (pickled)
- Correct some bugs
  • Loading branch information
mdmfernandes committed Jul 25, 2018
1 parent 4961d2d commit aef0b59
Show file tree
Hide file tree
Showing 13 changed files with 563 additions and 71 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ ENV/
.mypy_cache/

# logs content
logs/*
logs/*

# todo
TODO_PROBLEMAS
4 changes: 4 additions & 0 deletions benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ Faraday 65nm
ind: 100
gen: 30
time: 5117.354s
4 changes: 2 additions & 2 deletions cadence/cadence.il
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ procedure( errorHandler(cid errorMessage)
;; @param {number} exitStatus - Server exit status
;;
procedure( exitHandler(cid exitStatus)
printf("Server has stopped with the exit code %d. I'm out!!!\n" exitStatus)
printf("Server has stopped with the exit code %d. I'm out!!!\n" exitStatus)
hiRegTimer("exit()", 10) ; exit Cadence
)

Expand All @@ -109,7 +109,7 @@ procedure( exitHandler(cid exitStatus)
procedure( startServer()
printf("[INFO] Starting server process...\n")
serverHasStarted = 0
cid = ipcBeginProcess("run_server.sh" "" 'requestHandler 'errorHandler 'exitHandler "")
cid = ipcBeginProcess("./run_server.sh" "" 'requestHandler 'errorHandler 'exitHandler "")
msg = "Python server has started!\n"
ipcWriteProcess(cid sprintf(nil "%d\n" strlen(msg)))
ipcWriteProcess(cid msg)
Expand Down
7 changes: 5 additions & 2 deletions cadence/run_server.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env sh

export server_path="/home/tarzan/Projects/paim/server/server.py"
export pyth="python3.6"
# TODO: IF python -V == 2,
#export server_path="/home/mdm.fernandes/Projects/paim/server/server_py2.py"

export server_path="/home/mdm.fernandes/Projects/paim/server/server.py"
export pyth="python"

export cmd="${pyth} ${server_path}"

Expand Down
24 changes: 14 additions & 10 deletions cadence/script/loadSimulator.ocn
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
setup(?numberNotation 'engineering)

;; Simulator
simulator( 'spectre )
design( "/home/tarzan/simulation/cs/spectre/schematic/netlist/netlist")
resultsDir( "/home/tarzan/simulation/cs/spectre/schematic" )
simulator( 'spectre )
design( "/home/mdm.fernandes/IC6_workspace/simulation/COMMON_SOURCE/spectre/schematic/netlist/netlist")
resultsDir( "/home/mdm.fernandes/IC6_workspace/simulation/COMMON_SOURCE/spectre/schematic" )


;; Model Files
modelFile(
'("/home/umc/130nm/cadence/CDB/umc13mmrf/../Models/Spectre/l130e-rf-v131-control.scs" "")
'("/home/umc/130nm/cadence/CDB/umc13mmrf/../Models/Spectre/L130E_MM_HS_MAIN_V151.lib.scs" "")
'("/home/umc/130nm/cadence/CDB/umc13mmrf/../Models/Spectre/L130E_MM_TRIPLEWELL_MAIN_V151.lib.scs" "")
'("/home/tarzan/Desktop/oppoint.scs" "")
modelFile(
'("/home/mdm.fernandes/IC6_workspace/nominal/spectre/nominalwrapper.scs" "")
)
definitionFile(
"models.scs"
)

;; Set simulations
analysis('ac ?start "10k" ?stop "1G" )
analysis('dc ?saveOppoint t )
analysis('ac ?start "10k" ?stop "1G" )
analysis('dc ?saveOppoint t )
envOption(
'analysisOrder list("dc" "ac")
)


temp( 27 )
30 changes: 22 additions & 8 deletions cadence/script/run.ocn
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ outf = outfile(out_path "w")
;; Run simulation
run()

;; Get results
POWER = -(VDC("/vdd") * IDC("/V1/PLUS"))
GAIN = ymax(dB20(VF("/net3")))
REG1 = pv("/M1" "region" ?result "dcOpInfo-info")
REG2 = pv("/M2" "region" ?result "dcOpInfo-info")
GBW = gainBwProd(VF("/net3"))
OS = (((1.2 - pv("/M1" "vdsat" ?result "dcOpInfo-info")) + pv("/M2" "vdsat" ?result "dcOpInfo-info")) - 0.1)

;; Get results
POWER = (- pv("V0" "pwr" ?result "dcOpInfo"))
GAIN = ymax(mag(v("/out" ?result "ac")))
REG2 = pv("M2.m1" "region" ?result "dcOpInfo")
REG1 = pv("M1.m1" "region" ?result "dcOpInfo")
GBW = gainBwProd(mag(v("/out" ?result "ac")))
POWER = (- pv("V0" "pwr" ?result "dcOpInfo"))
OS = 0.9

;; Print to screen (debug)
;printf( "%s\t%e\n", "POWER", POWER)
;printf( "%s\t%g\n", "GAIN", GAIN)
;printf( "%s\t%d\n", "REG1", REG1)
;printf( "%s\t%d\n", "REG2", REG2)
;if( GBW == nil then
; printf("NILLLLLLLLLLL\n")
;else
; printf( "%s\t%g\n", "GBW", GBW)
;)
;printf( "%s\t%g", "OS", OS)


;; Save results to file
fprintf( outf "%s\t%e\n", "POWER", POWER)
fprintf( outf "%s\t%g\n", "GAIN", GAIN)
Expand Down
10 changes: 5 additions & 5 deletions cadence/script/vars.ocn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
desVar( "IB" 100u )
desVar( "L" 240n )
desVar( "VBIAS" 600m )
desVar( "W1" 2u )
desVar( "W2" 6u )
desVar( "IB" 100u )
desVar( "L" 0.28 )
desVar( "VBIAS" 500m )
desVar( "W1" 2 )
desVar( "W2" 6 )
4 changes: 2 additions & 2 deletions cadence/start_cadence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Project name
export PROJECT_NAME="commonSource-tutorial"
# Project root directory
WORK_SPACE="/home/tarzan/Projects"
WORK_SPACE="/home/mdm.fernandes/Projects"

#############################################
# Do not change the code below! #
Expand Down Expand Up @@ -52,4 +52,4 @@ echo "**********************************************************************"
echo "* Starting Cadence *"
echo "**********************************************************************"
# Code to run Cadence and the script cadence.il
icfb -nograph -restore cadence.il
virtuoso -nograph -restore cadence.il
15 changes: 10 additions & 5 deletions paim/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def get_circuit_params_from_file():

# Genes de um individuo - W1, W2 (=WB), L (=L1=L2=LB), Ib, Vbias(=VGS1)
circuit_vars = {
'W1': (1e-6, 100e-6),
'W2': (3e-6, 100e-6),
'L': (120e-9, 4*120e-9),
'W1': (1, 100),
'W2': (3, 100),
'L': (140e-3, 4*140e-3),
'IB': (10e-6, 100e-6),
'VBIAS': (0.3, 1.0)
}
Expand Down Expand Up @@ -108,8 +108,8 @@ def main():
print(f"Circuit Variables: {circuit_vars.keys()}")

# Optimizer parameters (TODO: get from somewhere)
pop_size = 2
max_gen = 1
pop_size = 10
max_gen = 5

# Load the optimizer
paim = OptimizerNSGA2(objectives, constraints, circuit_vars, pop_size,
Expand All @@ -123,6 +123,11 @@ def main():
# Iterate over the dictionary and save to file
pickle.dump(logbook, f)

# Save fronts (pop of the last run) pickled to file
with open(f"../logs/fronts{current_time}.pickle", 'wb') as f:
# Iterate over the dictionary and save to file
pickle.dump(fronts, f)

# Read do logbook só para confirmar
# with open(f"../logs/logbook{current_time}.pickle", 'rb') as f:
# oi = pickle.load(f)
Expand Down
38 changes: 15 additions & 23 deletions paim/optimizer/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import array
import copy
import math
import random
from multiprocessing import Pool
import time

from deap import algorithms, base, creator, tools
from profilehooks import timecall

#from multiprocessing import Pool


# A class é só para ter tudo mais organizado...


Expand Down Expand Up @@ -141,16 +143,6 @@ def uniform(bound_low, bound_up):
# return penalty

# https://groups.google.com/forum/#!topic/deap-users/SSd_zZ4XinI

def teste(ola={"oi": 1, "coiso": 2}):
"""[summary]
Keyword Arguments:
ola {dict} -- [description] (default: {{"oi":1, "coiso":2}})
"""

pass

def eval_circuit(self, individual):
"""[summary]
Expand Down Expand Up @@ -195,7 +187,6 @@ def eval_circuit(self, individual):
fitnesses = [1000, -1000]
return tuple(fitnesses)

print("Simulation Sucessfull")
fitnesses = []

# Get the fitnesses
Expand All @@ -213,22 +204,20 @@ def eval_circuit(self, individual):

if self.constraints:
# Handling the contraints
pass # penalty = self.handle_constraints(sim_res)
else:
# Just a test... se não estiverem na saturação
if sim_res["REG1"] != 2 or sim_res["REG2"] != 2:
print("penalty...")
penalty = 0.1
else:
penalty = 1

# Multiply the fitness by the penalty
#fitnesses = [fit * penalty for fit in fitnesses]
#TODO: Talvez verificar se o penalty for para minimizar ou maximizar
# e atribuir o penalty de acordo
fitnesses = [(1/penalty) * fitnesses[0], penalty * fitnesses[1]]

#print(f"FITNESS: {fitnesses}")
#print(f"VARS: {individual}")
#print(f"RESULTS: {sim_res}")
print(f"FITNESS: {fitnesses}")
print(f"VARS: {individual}")
print(f"RESULTS: {sim_res}")
print("==================================================")

return tuple(fitnesses)

Expand Down Expand Up @@ -261,7 +250,6 @@ def ga_mu_plus_lambda(self, mu, lambda_, statistics,
registered in the toolbox. This algorithm uses the :func:`varOr`
variation.
"""

# Create the population
population = self.toolbox.population(n=self.pop_size)
population = self.toolbox.select(population, len(population))
Expand Down Expand Up @@ -292,6 +280,9 @@ def ga_mu_plus_lambda(self, mu, lambda_, statistics,

# Begin the generational process
for gen in range(1, self.max_gen + 1):
# Get current time
start_time = time.time()

# Vary the population
offspring = algorithms.varOr(population, self.toolbox, lambda_,
self.cx_prob, self.mut_prob)
Expand All @@ -314,7 +305,8 @@ def ga_mu_plus_lambda(self, mu, lambda_, statistics,
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose: # Only prints multiples of 5 gens (and not gen % 5)
# print(logbook.stream)
print(f"---- Gen: {gen} \t| # Evals: {len(invalid_ind)} -----")
gen_time = time.time() - start_time
print(f"-------- Gen: {gen} | # Evals: {len(invalid_ind)} | Time: {gen_time:.2f}s --------")

return population, logbook

Expand Down
25 changes: 12 additions & 13 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def __init__(self, cad_file, is_cadence=True):
# Uninitialized variables
self.conn = None

# Requests count
self.count = 0

def run(self):
"""Start the server."""
if self.is_cadence:
Expand Down Expand Up @@ -83,15 +86,13 @@ def run(self):
data = dict(data=addr)
self.send_data(data)

count = 0 # request number

while True:
# Receive request from the optimizer
req = self.recv_data()

if self.is_cadence:
# Process the optimizer request
expr = self.process_skill_request(req, count)
expr = self.process_skill_request(req)

if expr == 0:
break
Expand All @@ -109,9 +110,8 @@ def run(self):
res = self.recv_skill()

# Process the Cadence response
typ, obj = self.process_skill_response(
res, count)
count += 1 # Update request number
typ, obj = self.process_skill_response(res)
self.count += 1 # Update request number

self.send_data(dict(type=typ, data=obj))
else:
Expand Down Expand Up @@ -261,15 +261,14 @@ def send_debug(self, msg):
time.sleep(1)
self.send_warn(f"[Debug] {msg}")

def process_skill_request(self, req, count):
def process_skill_request(self, req):
"""Process a skill request from the optimizer.
Based on the given request object, returns the skill expression
to be evaluated by Cadence.
Arguments:
req {dict} -- request object
count {integer} -- request number
Returns:
expr {string or None} -- expression to be evaluated by Cadence
Expand All @@ -292,11 +291,11 @@ def process_skill_request(self, req, count):

elif typ == 'updateAndRun':
# Store circuit variables in file
vars_path = os.environ.get('VAR_PATH') + f"/vars{count}.ocn"
vars_path = os.environ.get('VAR_PATH') + f"/vars{self.count}.ocn"
store_vars_in_file(data, vars_path)
# Redefine the path of the simulation results file
# to be used by cadence
out_file = os.environ.get('RESULT_PATH') + f"/out{count}.txt"
out_file = os.environ.get('RESULT_PATH') + f"/out{self.count}.txt"

# create empty file
with open(out_file, 'w'):
Expand All @@ -307,7 +306,7 @@ def process_skill_request(self, req, count):
self.send_warn("Invalid object type sent from the client.")
return None

def process_skill_response(self, msg, count):
def process_skill_response(self, msg):
"""Process the skill response from Cadence.
Arguments:
Expand All @@ -325,9 +324,9 @@ def process_skill_response(self, msg, count):
elif "updateAndRun_OK" in msg:
typ = 'updateAndRun'
# TODO: Get simulation results
#res_path = os.environ.get('RESULT_PATH') + f'/out{count}.txt'
#res_path = os.environ.get('RESULT_PATH') + f'/out{self.count}.txt'
#res = get_results_from_file(res_path)
out_file = os.environ.get('RESULT_PATH') + f"/out{count}.txt"
out_file = os.environ.get('RESULT_PATH') + f"/out{self.count}.txt"

obj = get_results_from_file(out_file)

Expand Down
Loading

0 comments on commit aef0b59

Please sign in to comment.