-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from thesps/fp_emulation
Backend agreement
- Loading branch information
Showing
7 changed files
with
218 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .writer import write, auto_config, sim_compile, decision_function, build, Simulators | ||
simulator = Simulators.xsim | ||
from conifer.backends.vhdl.writer import write, auto_config, sim_compile, decision_function, build | ||
from conifer.backends.vhdl.simulators import Modelsim, GHDL, Xsim | ||
simulator = Xsim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _compile_sim(simulator, odir): | ||
logger.info(f'Compiling simulation for {simulator.__name__.lower()} simulator') | ||
logger.debug(f'Compiling simulation with command "{simulator._compile_cmd}"') | ||
cwd = os.getcwd() | ||
os.chdir(odir) | ||
success = os.system(simulator._compile_cmd) | ||
os.chdir(cwd) | ||
if(success > 0): | ||
logger.error(f"'sim_compile' failed, check {simulator.__name__.lower()}_compile.log") | ||
return success == 0 | ||
|
||
def _run_sim(simulator, odir): | ||
logger.info(f'Running simulation for {simulator.__name__.lower()} simulator') | ||
logger.debug(f'Running simulation with command "{simulator._run_cmd}"') | ||
cwd = os.getcwd() | ||
os.chdir(odir) | ||
success = os.system(simulator._run_cmd) | ||
os.chdir(cwd) | ||
if(success > 0): | ||
logger.error(f"'sim_compile' failed, check {simulator.__name__.lower()}.log") | ||
return success == 0 | ||
|
||
class Modelsim: | ||
_compile_cmd = 'sh modelsim_compile.sh > modelsim_compile.log' | ||
_run_cmd = 'vsim -c -do "vsim -L BDT -L xil_defaultlib xil_defaultlib.testbench; run -all; quit -f" > vsim.log' | ||
|
||
def write_scripts(outputdir, filedir, n_classes): | ||
f = open(os.path.join(filedir,'./scripts/modelsim_compile.sh'),'r') | ||
fout = open(f'{outputdir}/modelsim_compile.sh','w') | ||
for line in f.readlines(): | ||
if 'insert arrays' in line: | ||
for i in range(n_classes): | ||
newline = f'vcom -2008 -work BDT ./firmware/Arrays{i}.vhd\n' | ||
fout.write(newline) | ||
else: | ||
fout.write(line) | ||
f.close() | ||
fout.close() | ||
|
||
f = open(f'{outputdir}/test.tcl', 'w') | ||
f.write('vsim -L BDT -L xil_defaultlib xil_defaultlib.testbench\n') | ||
f.write('run 100 ns\n') | ||
f.write('quit -f\n') | ||
f.close() | ||
|
||
def compile(odir): | ||
return _compile_sim(Modelsim, odir) | ||
|
||
def run_sim(odir): | ||
return _run_sim(Modelsim, odir) | ||
|
||
class GHDL: | ||
_compile_cmd = 'sh ghdl_compile.sh > ghdl_compile.log' | ||
_run_cmd = 'ghdl -r --std=08 --work=xil_defaultlib testbench > ghdl.log' | ||
def write_scripts(outputdir, filedir, n_classes): | ||
f = open(os.path.join(filedir, './scripts/ghdl_compile.sh'), 'r') | ||
fout = open(f'{outputdir}/ghdl_compile.sh', 'w') | ||
for line in f.readlines(): | ||
if 'insert arrays' in line: | ||
for i in range(n_classes): | ||
newline = f'ghdl -a --std=08 --work=BDT ./firmware/Arrays{i}.vhd\n' | ||
fout.write(newline) | ||
else: | ||
fout.write(line) | ||
f.close() | ||
fout.close() | ||
|
||
def compile(odir): | ||
return _compile_sim(GHDL, odir) | ||
|
||
def run_sim(odir): | ||
return _run_sim(GHDL, odir) | ||
|
||
class Xsim: | ||
_compile_cmd = 'sh xsim_compile.sh > xsim_compile.log' | ||
_run_cmd = 'xsim -R bdt_tb > xsim.log' | ||
def write_scripts(outputdir, filedir, n_classes): | ||
f = open(os.path.join(filedir, './scripts/xsim_compile.sh'), 'r') | ||
fout = open(f'{outputdir}/xsim_compile.sh', 'w') | ||
for line in f.readlines(): | ||
if 'insert arrays' in line: | ||
for i in range(n_classes): | ||
newline = f'xvhdl -2008 -work BDT ./firmware/Arrays{i}.vhd\n' | ||
fout.write(newline) | ||
else: | ||
fout.write(line) | ||
f.close() | ||
fout.close() | ||
|
||
def compile(odir): | ||
return _compile_sim(Xsim, odir) | ||
|
||
def run_sim(odir): | ||
return _run_sim(Xsim, odir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from conifer.utils.misc import _ap_include, _json_include | ||
from .fixed_point import FixedPointConverter | ||
from conifer.utils.misc import _ap_include, _json_include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
class FixedPointConverter: | ||
''' | ||
A Python wrapper around ap_fixed types to easily emulate the correct number representations | ||
''' | ||
|
||
def __init__(self, type_string): | ||
''' | ||
Construct the FixedPointConverter. Compiles the c++ library to use for conversions | ||
args: | ||
type_string : string for the ap_ type, e.g. ap_fixed<16,6,AP_RND,AP_SAT> | ||
''' | ||
logger.info(f'Constructing converter for {type_string}') | ||
self.type_string = type_string | ||
self.sani_type = type_string.replace('<','_').replace('>','').replace(',','_') | ||
filedir = os.path.dirname(os.path.abspath(__file__)) | ||
cpp_filedir = f"./.fp_converter_{self.sani_type}" | ||
cpp_filename = cpp_filedir + f'/{self.sani_type}.cpp' | ||
os.makedirs(cpp_filedir, exist_ok=True) | ||
|
||
fin = open(f'{filedir}/fixed_point_conversions.cpp', 'r') | ||
fout = open(cpp_filename, 'w') | ||
for line in fin.readlines(): | ||
newline = line | ||
if '// conifer insert typedef' in line: | ||
newline = f"typedef {type_string} T;\n" | ||
fout.write(newline) | ||
fin.close() | ||
fout.close() | ||
|
||
curr_dir = os.getcwd() | ||
os.chdir(cpp_filedir) | ||
cmd = f"g++ -O3 -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) -I/cvmfs/cms.cern.ch/slc7_amd64_gcc900/external/hls/2019.08/include/ {self.sani_type}.cpp -o {self.sani_type}.so" | ||
logger.debug(f'Compiling with command {cmd}') | ||
try: | ||
ret_val = os.system(cmd) | ||
if ret_val != 0: | ||
raise Exception(f'Failed to compile FixedPointConverter {self.sani_type}.cpp') | ||
finally: | ||
os.chdir(curr_dir) | ||
|
||
os.chdir(cpp_filedir) | ||
logger.debug(f'Importing compiled module {self.sani_type}.so') | ||
try: | ||
import importlib.util | ||
spec = importlib.util.spec_from_file_location('fixed_point', f'{self.sani_type}.so') | ||
self.lib = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(self.lib) | ||
except ImportError: | ||
os.chdir(curr_dir) | ||
raise Exception("Can't import pybind11 bridge, is it compiled?") | ||
os.chdir(curr_dir) | ||
|
||
def to_int(self, x): | ||
return self.lib.to_int(x) | ||
|
||
def to_double(self, x): | ||
return self.lib.to_double(x) | ||
|
||
def from_int(self, x): | ||
return self.lib.from_int(x) |
Oops, something went wrong.