def __init__(self): # getting parameters from makefile vsim_args_str = os.environ['VSIM_ARGS'] self.toplevel = os.environ['TOPLEVEL'] #vsim_args_str = "-gI=352 -gO=16 -gdelay=5 -gDA=2 -gDB=3" vsim_args_m = re.finditer(r"-g([^=]+)=([^ -]+)", vsim_args_str, re.MULTILINE) for matchNum, match in enumerate(vsim_args_m, start=1): setattr(self, match.group(1), int(match.group(2))) #generating simulation models self.SU = SortingUtils() self.net_sets = ( I, O, self.presort_in_sets, self.used_out_set, self.nonsorted_out_set) = self.SU.get_muctpi_opt_sets( self.I) if self.toplevel == 'work.csn_sort_88_64': self.net_sets_88 = self.SU.get_muctpi_opt_sets(88) [self.list_of_pairs_88, net] = self.SU.get_opt_net(gen_plots=False, net_sets=self.net_sets_88) self.net_sets_64 = self.SU.get_muctpi_opt_sets(64) [self.list_of_pairs_64, net] = self.SU.get_opt_net(gen_plots=False, net_sets=self.net_sets_64) elif self.toplevel == 'work.csn_sort_v2': SORTING_PATH = os.environ['SORTING_PATH'] if SORTING_PATH: path = SORTING_PATH+'/in/pickle/I352O016_alhajbaddar22_R_16_oddevenmerge_R_16.pickle' else: path = '../../../in/pickle/I352O016_alhajbaddar22_R_16_oddevenmerge_R_16.pickle' df = pd.read_pickle(path) [self.list_of_pairs, net] = [df['pairs'][0], df['net'][0]] else: [self.list_of_pairs, net] = self.SU.get_opt_net(gen_plots=False, net_sets=self.net_sets)
def __init__(self, I, O, method, generate_plot=False, plot_masked_pairs=True, figsize=None, title=None): self.SU = SortingUtils() self.I = I self.O = O self.N = 1000 self.ptlen = 4 self.idxlen = 9 self.roilen = 8 self.flagslen = 4 self.title = title if figsize is None: self.figsize = (4 * 46.8, 4 * 33.1) self.method = method self.generate_plot = generate_plot self.remove_masked_pairs = not plot_masked_pairs if 'pickle' in self.method: df = pd.read_pickle( '../../in/pickle/{method:s}'.format(method=method)) [self.list_of_pairs, self.net] = [df['pairs'][0], df['net'][0]] else: net_sets = self.SU.get_net_opt_sets(I=self.I, O=self.O, pI=None, nO=None) [self.list_of_pairs, self.net] = self.SU.get_opt_net(gen_plots=self.generate_plot, net_sets=net_sets, method=self.method) self.dependence_test_352() # creating header self.create_header() # Creating Stimullus self.gen_muon() # Computing expected result self.py_net_sort_muon() self.create_test()
def __init__(self, I, O, method, generate_plot=False, plot_masked_pairs=True, figsize=None, title=None): self.SU = SortingUtils() self.I = I self.O = O self.title = title if figsize is None: self.figsize = (4 * 46.8, 4 * 33.1) self.method = method self.generate_plot = generate_plot self.remove_masked_pairs = not plot_masked_pairs self.R_range = range(1, np.math.ceil(I // O) + 1) self.topology_cols = [ 'R', 'ceilI_R', 'method', 'cs', 'ds', 'Ecs', 'ceilLgR', 'R-1', 'Ecm', 'Edm', 'Ec', 'Ed' ] self.topology_df = pd.DataFrame(dtype=int) self.get_merge_dict()
def plotter(): SU = SortingUtils() df = pd.read_pickle( '../../out/pickle/I352O016_alhajbaddar22_R_16_oddevenmerge_R_16.pickle' ) [pairs, net] = [df['pairs'][0], df['net'][0]] netv2 = {'method': 'muctpi', 'I': 352, 'O': 16, 'net': net} plotnetv2 = SU.to_plotnet(netv2) plotnet3v2 = SU.to_plotnet_triple(plotnetv2) print(plotnetv2) SU.plot(plotnet3v2, figsize=(4 * 46.8, 4 * 33.1))
from SortingUtils import SortingUtils import pandas as pd SU = SortingUtils() SU.plot_masked_filename_fmt = '../../out/pdf/plot_I{i:03d}_O{o:03d}_{m:s}_thesis.pdf' ################### # mergesort N= 8 ################### N = 8 method = 'merge-exchange' list_of_pairs2 = SU.generate_net_pairs(N, method) # finding the stages netv2 = SU.to_stages(list_of_pairs2) # fixing the order, relevant only for plotting it net = [[(0, 4), (1, 5), (2, 6), (3, 7)], [(0, 2), (1, 3), (4, 6), (5, 7)], [(2, 4), (3, 5)], [(0, 1), (2, 3), (4, 5), (6, 7)], [(1, 4), (3, 6)], [(1, 2), (3, 4), (5, 6)]] netv2['net'] = net # creating plotnet object (adding substages) plotnetv2 = SU.to_plotnet(netv2) # creating plotnet3 (adding a third parameter for each comparison) plotnet3v2 = SU.to_plotnet_triple(plotnetv2) #SU.inline_fmt = ['$x_{i:d}$'.format(i=i+1) for i in range(N)] SU.plot(plotnet3v2) ################### # mergesort N= 8 ################### N = 8 method = 'oddevenmerge'
class SortingTopology: def __init__(self, I, O, method, generate_plot=False, plot_masked_pairs=True, figsize=None, title=None): self.SU = SortingUtils() self.I = I self.O = O self.title = title if figsize is None: self.figsize = (4 * 46.8, 4 * 33.1) self.method = method self.generate_plot = generate_plot self.remove_masked_pairs = not plot_masked_pairs self.R_range = range(1, np.math.ceil(I // O) + 1) self.topology_cols = [ 'R', 'ceilI_R', 'method', 'cs', 'ds', 'Ecs', 'ceilLgR', 'R-1', 'Ecm', 'Edm', 'Ec', 'Ed' ] self.topology_df = pd.DataFrame(dtype=int) self.get_merge_dict() #self.get_topology_df() #print(self.topology_df[self.topology_cols]) #ST.topology_df[ST.topology_cols] def get_net(self, I, O, method): net_sets = self.SU.get_net_opt_sets(I=I, O=O, pI=None, nO=None) [list_of_pairs, net] = self.SU.get_opt_net(gen_plots=False, net_sets=net_sets, method=method) return [list_of_pairs, net] def get_merge_dict(self, method='oddevenmerge'): if self.I == self.O: Im = Om = self.I else: Im = 2 * self.O Om = self.O # getting masked net net_sets = self.SU.get_net_opt_sets(I=Im, O=Om, pI=None, nO=None) masked_plotnet3v2 = self.SU.generate_opt_masked_net(*net_sets, method) masked_pairs = self.SU.to_list_of_pairs( masked_plotnet3v2, remove_masked=self.remove_masked_pairs) # plot it anyways self.SU.plot(masked_plotnet3v2) # getting optimized net [opt_list_of_pairs, opt_net] = self.get_net(I=Im, O=self.O, method=method) self.merge_dict = { 'pairs': opt_list_of_pairs, 'net': opt_net, 'masked_pairs': masked_pairs, 'masked_net': masked_plotnet3v2, 'method': method, 'cm': len(opt_list_of_pairs), 'dm': len(opt_net), 'Im': Im, 'Om': Om } # saving pickle df = pd.DataFrame() df = df.append({ 'pairs': masked_pairs, 'net': opt_net }, ignore_index=True) df.to_pickle('../../out/pickle/I{I:03d}O{O:03d}_{m:s}.pickle'.format( I=Im, O=Om, m=method)) def get_topology_df(self): for R in self.R_range: print('Working with R={R:d}'.format(R=R)) ceil_I_R = np.math.ceil(self.I / R) method = self.SU.get_method(ceil_I_R, self.method) [list_of_pairs, net] = self.get_net(ceil_I_R, self.O, method) cs = len(list_of_pairs) ds = len(net) Ecs = R * cs ceilLgR = np.math.ceil(np.math.log(R, 2)) R_1 = R - 1 Ecm = R_1 * self.merge_dict['cm'] Edm = ceilLgR * self.merge_dict['dm'] self.topology_df = self.topology_df.append( { 'R': np.int64(R), 'ceilI_R': ceil_I_R, 'method': method, 'cs': cs, 'ds': ds, 'Ecs': Ecs, 'ceilLgR': ceilLgR, 'R-1': R_1, 'Ecm': Ecm, 'Edm': Edm, 'Ec': Ecs + Ecm, 'Ed': ds + Edm }, ignore_index=True) for i in [ 'R', 'ceilI_R', 'cs', 'ds', 'Ecs', 'ceilLgR', 'R-1', 'Ecm', 'Edm', 'Ec', 'Ed' ]: self.topology_df[i] = self.topology_df[i].astype(int) self.topology_df[self.topology_cols].to_excel('topology.xlsx') def remap_pair(self, pair, mymap): # this way keep mask if exist new_pair = pair.copy() new_pair[0] = mymap[pair[0]] new_pair[1] = mymap[pair[1]] return new_pair def remap_list_of_pairs(self, list_of_pairs, mymap): new_list_of_pairs = [] for pair in list_of_pairs: new_list_of_pairs.append(self.remap_pair(pair, mymap)) return new_list_of_pairs def interleave_list_of_list_of_pairs(self, list_of_list_of_pairs): return [*sum(zip(*list_of_list_of_pairs), ())] def test_method(self): l = [[(1, 2, 0), (3, 4, 1)], [(5, 6, 0), (7, 8, 0)], [(9, 10, 1), (11, 12, 1)]] print(self.interleave_list_of_list_of_pairs(l)) def generate_R_sort_net(self, R): # finding the integer ceiling value of I/R ceil_I_R = np.math.ceil(self.I / R) # getting the appropriate optimization settings net_sets = self.SU.get_net_opt_sets(I=ceil_I_R, O=self.O, pI=None, nO=None) # getting net with masked comparisons plotnet3v2 = self.SU.generate_opt_masked_net(*net_sets, method=self.method) self.SU.plot(plotnet3v2) # converting net to list of pairs list_of_pairs = self.SU.to_list_of_pairs( plotnet3v2, remove_masked=self.remove_masked_pairs) # generating vhdl net = self.SU.to_stages(list_of_pairs) self.SU.generate_vhdl_pkg(net, ceil_I_R, filename='../../out/vhd/csn_sort') # saving pickle df = pd.DataFrame() df = df.append({'pairs': list_of_pairs, 'net': net}, ignore_index=True) df.to_pickle('../../out/pickle/I{I:03d}O{O:03d}_{m:s}.pickle'.format( I=ceil_I_R, O=self.O, m=plotnet3v2['method'])) ### # replicating net ### list_of_list_of_pairs = [] for r in range(R): mymap = list(range(r * ceil_I_R, (r + 1) * ceil_I_R)) list_of_list_of_pairs.append( self.remap_list_of_pairs(list_of_pairs, mymap)) # interleaving list of pairs to a single list new_list_of_pairs = self.interleave_list_of_list_of_pairs( list_of_list_of_pairs) # adding metadata new_list_of_pairsv2 = { 'method': '{m:s}_R_{R:d}'.format(m=plotnet3v2['method'], R=R), 'I': self.I, 'O': self.O * R, 'pairs': new_list_of_pairs } # finding the stages netv2 = self.SU.to_stages(new_list_of_pairsv2) # creating plotnet object (adding substages) plotnetv2 = self.SU.to_plotnet(netv2) if self.remove_masked_pairs: plotnetv2 = self.SU.to_plotnet_triple(plotnetv2) return [new_list_of_pairsv2, netv2, plotnetv2] def is_power_2(self, n): return ((n & (n - 1) == 0) and n != 0) def generate_R_merge_net(self, R): merge_pairs = self.merge_dict['masked_pairs'] # generating vhdl opt_net = self.SU.to_stages(merge_pairs) self.SU.generate_vhdl_pkg(opt_net, self.merge_dict['Im'], filename='../../out/vhd/csn_merge') print(self.merge_dict['Im']) print(opt_net) # replicating merge network merge_tree_pairs = [] net = [] # define donly if R is power of two if self.is_power_2(R): I_R = self.I // R O = self.O for L in range(int(np.math.log2(R))): level_pairs = [] for r in range(0, R >> L, 2): # computing first and second range os input of merge net first_range = range(r * (I_R << L), r * (I_R << L) + O) second_range = range((r + 1) * (I_R << L), (r + 1) * (I_R << L) + O) mymap = list(first_range) + list(second_range) # remapping net to new input mapping level_pairs.append( self.remap_list_of_pairs(merge_pairs, mymap)) #print(L, r) # interleaving pairs interleaved_pairs = self.interleave_list_of_list_of_pairs( level_pairs) #print(interleaved_pairs) # getting net for the current level net.extend(self.SU.to_stages(interleaved_pairs)) #print(netv2) merge_tree_pairs.extend( self.interleave_list_of_list_of_pairs(level_pairs)) # adding metadata new_list_of_pairsv2 = { 'method': '{m:s}_R_{R:d}'.format(m=self.merge_dict['method'], R=R), 'I': self.I, 'O': self.O, 'pairs': merge_tree_pairs } netv2 = { 'method': '{m:s}_R_{R:d}'.format(m=self.merge_dict['method'], R=R), 'I': self.I, 'O': self.O, 'net': net } # finding the stages #netv2 = self.SU.to_stages(new_list_of_pairsv2) # creating plotnet object (adding substages) plotnetv2 = self.SU.to_plotnet(netv2) if self.remove_masked_pairs: plotnetv2 = self.SU.to_plotnet_triple(plotnetv2) return [new_list_of_pairsv2, netv2, plotnetv2] def generate_R_net(self, R): # generating the sorting net (first stage) [sort_list_of_pairsv2, sort_netv2, sort_plotnetv2] = self.generate_R_sort_net(R) # plotting it anyaways #self.SU.plot(sort_plotnetv2) # validation if it is a single net if R == 1: net_sets = self.SU.get_net_opt_sets(I=sort_list_of_pairsv2['I'], O=sort_list_of_pairsv2['O'], pI=None, nO=None) self.SU.list_of_pairs_validation( net_sets=net_sets, list_of_pairsv2=sort_list_of_pairsv2, N=1000) # generating merging tree net [merge_list_of_pairsv2, merge_netv2, merge_plotnetv2] = self.generate_R_merge_net(R) # plotting it anyaways #self.SU.plot(merge_plotnetv2) # net net = [] net.extend(sort_netv2['net']) net.extend(merge_netv2['net']) netv2 = { 'method': '{ms:s}_{m:s}'.format(ms=sort_netv2['method'], m=merge_netv2['method']), 'I': self.I, 'O': self.O, 'net': net } #sorting net for readability netv2 = self.SU.sort_net(netv2) plotnetv2 = self.SU.to_plotnet(netv2) if self.remove_masked_pairs: plotnetv2 = self.SU.to_plotnet_triple(plotnetv2) #self.SU.print_plotnet(plotnetv2['plotnet']) if self.generate_plot: self.SU.plot(plotnetv2, figsize=self.figsize, title=self.title) # optimizing pairs opt_pairs = self.SU.to_list_of_pairs(plotnetv2, remove_masked=True) # validating pairs net_sets = self.SU.get_net_opt_sets(I=self.I, O=self.O, pI=None, nO=None) self.SU.list_of_pairs_validation(net_sets=net_sets, list_of_pairsv2=opt_pairs, N=100) # generating vhdl files c = len(opt_pairs) net = self.SU.to_stages(opt_pairs) d = len(net) self.SU.generate_vhdl_pkg(net, self.I) for i in range(1, d + 1): self.SU.get_stages_cfg(d, i) print( 'Generated network with {d:d} levels and {c:d} comparisons'.format( c=c, d=d)) # saving pickle df = pd.DataFrame() df = df.append({ 'pairs': opt_pairs, 'net': netv2['net'] }, ignore_index=True) df.to_pickle('../../out/pickle/I{I:03d}O{O:03d}_{m:s}.pickle'.format( I=self.I, O=self.O, m=netv2['method']))
class SortingHLS(SortingUtils): def __init__(self, I, O, method, generate_plot=False, plot_masked_pairs=True, figsize=None, title=None): self.SU = SortingUtils() self.I = I self.O = O self.N = 1000 self.ptlen = 4 self.idxlen = 9 self.roilen = 8 self.flagslen = 4 self.title = title if figsize is None: self.figsize = (4 * 46.8, 4 * 33.1) self.method = method self.generate_plot = generate_plot self.remove_masked_pairs = not plot_masked_pairs if 'pickle' in self.method: df = pd.read_pickle( '../../in/pickle/{method:s}'.format(method=method)) [self.list_of_pairs, self.net] = [df['pairs'][0], df['net'][0]] else: net_sets = self.SU.get_net_opt_sets(I=self.I, O=self.O, pI=None, nO=None) [self.list_of_pairs, self.net] = self.SU.get_opt_net(gen_plots=self.generate_plot, net_sets=net_sets, method=self.method) self.dependence_test_352() # creating header self.create_header() # Creating Stimullus self.gen_muon() # Computing expected result self.py_net_sort_muon() self.create_test() def create_header(self): with open('../../in/cpp/pairs_template.h') as content_file: template = content_file.read() template = template.replace('<HEADER_NAME>', 'PAIRS_{0:d}_{1:d}'.format(self.I, self.O)) template = template.replace('<I>', '{0:d}'.format(self.I)) template = template.replace('<O>', '{0:d}'.format(self.O)) template = template.replace('<NP>', '{0:d}'.format(len(self.list_of_pairs))) pairs_str = ['{{{0:d},{1:d}}}'.format(*p) for p in self.list_of_pairs] pairs_out = ',\n'.join(pairs_str) template = template.replace('<PAIRS>', pairs_out) with open('../../out/cpp/pairs_{0:d}_{1:d}.h'.format(self.I, self.O), 'w') as content_file: content_file.write(template) def create_test(self): lines = [] for i in range(self.N): lines.append(' '.join([ '{pt:d} {roi:d} {flags:d}'.format( pt=self.muon_cand[i][_]['pt'], roi=self.muon_cand[i][_]['roi'], flags=self.muon_cand[i][_]['flags']) for _ in range(self.I) ]) + ' ' + ' '.join([ '{id:d} {pt:d} {roi:d} {flags:d}'.format( id=self.py_net_sorted_muon[i][_]['idx'], pt=self.py_net_sorted_muon[i][_]['pt'], roi=self.py_net_sorted_muon[i][_]['roi'], flags=self.py_net_sorted_muon[i][_]['flags']) for _ in range(self.O) ])) str = '\n'.join(lines) with open('../../out/dat/test_{0:d}_{1:d}.dat'.format(self.I, self.O), 'w') as content_file: content_file.write(str) def gen_muon(self): self.muon_cand = [] for i in range(self.N): cand = [] for j in range(self.I): cand.append({ 'pt': random.randint(0, -1 + 2**self.ptlen), 'idx': j, 'roi': random.randint(0, -1 + 2**self.roilen), 'flags': random.randint(0, -1 + 2**self.flagslen), }) self.muon_cand.append(cand) pd.DataFrame(self.muon_cand).to_csv( '../../out/csv/stim_{0:d}_{1:d}.csv'.format(self.I, self.O)) def py_net_sort_muon(self): # copying list self.py_net_sorted_muon = copy.deepcopy(self.muon_cand) for i in range(self.N): for j in self.list_of_pairs: self.compare_and_swap(self.py_net_sorted_muon[i], *j, key=lambda k: k['pt']) self.py_net_sorted_muon[i] = self.py_net_sorted_muon[i][0:self.O] pd.DataFrame(self.muon_cand).to_csv( '../../out/csv/expec_{0:d}_{1:d}.csv'.format(self.I, self.O)) def dependence_test_352(self): print(len(self.net)) self.test_pairs = [] for i in range(16): members = list(range(i * 22, (i + 1) * 22)) for j in range(12): for p in self.net[j]: if p[0] in members: self.test_pairs.append(p) print(len(self.test_pairs)) for j in range(12, 32): for p in self.net[j]: self.test_pairs.append(p) self.list_of_pairs = self.test_pairs print(len(self.test_pairs))
from SortingUtils import SortingUtils import numpy as np import pandas as pd import matplotlib.pyplot as plt from gen_size_depth import * SU = SortingUtils() generate_df = True Nf = 512 plotstep = 32 #for plot only step = 1 # for points after 128 nrange = (list(range(2, 128)) + list(range(128, Nf + 1, step))) if generate_df: df = pd.DataFrame() cfgs = [] cfgs.append({'method': 'oddevenp2', 'iopt': True, 'bottomup': True}) cfgs.append({'method': 'oddevenp2', 'iopt': True, 'bottomup': False}) cfgs.append({'method': 'bitonicp2', 'iopt': True, 'bottomup': True}) cfgs.append({'method': 'bitonicp2', 'iopt': True, 'bottomup': False}) cfgs.append({'method': 'merge-exchange', 'iopt': False, 'bottomup': None}) for I in nrange:
from SortingUtils import SortingUtils import pandas as pd SU = SortingUtils() ################### # odd 32-16 ################### if False: N = 32 O = 16 method = 'oddevenmerge' plotnet3v2 = SU.generate_opt_masked_net(N, O, presort_in_sets=(set()), used_out_set=set(range(O)), nonsorted_out_set=None, method=method) list_of_pairs = SU.to_list_of_pairs(plotnet3v2, remove_masked=True) list_of_pairsv2 = { 'method': plotnet3v2['method'], 'I': N, 'O': O, 'pairs': list_of_pairs } #list_of_pairs2['pairs'].pop() result = SU.zeroone_validation(list_of_pairsv2) ###################