def in_rand_pgm(num_pgms=1, N=3): from gym_hls.envs.random_bm import get_random bms = get_random(N=num_pgms) length = 45 for i, bm in enumerate(bms): envs = [] pgm, files= bm print("Program: {}".format(pgm)) env_configs = {} env_configs['pgm'] = pgm env_configs['pgm_files'] = files env_configs['run_dir'] = 'run_'+pgm.replace(".c","") #env_configs['feature_type'] = 'act_hist' env_configs['verbose'] = True env_configs['log_results'] = True envs.append(Env(env_configs)) begin = time.time() (passes, timings, sample_size) = runInsertionN(envs, N, length=length, sort=True) end = time.time() print("Best individuals are: {}".format(passes[0])) print("Cycles: {}".format(timings[0])) compile_time =end - begin print("Compile Time: %d"%(int(compile_time))) fout.write("{}|{}|{}|{}|{}".format(pgm, timings[0], compile_time, sample_size, passes[0]))
def ga_rand_pgm(num_pgms=1): length = 45 fout = open("report_ga_random" + ".txt", "w") fout.write( "Benchmark | Cycle Counts | Algorithm Runtime (s)| Sample Sizes | Passes \n" ) from gym_hls.envs.random_bm import get_random bms = get_random(N=num_pgms) for i, bm in enumerate(bms): envs = [] pgm, files = bm print("Program: {}".format(pgm)) env_configs = {} env_configs['pgm'] = pgm env_configs['pgm_files'] = files env_configs['run_dir'] = 'run_' + pgm.replace(".c", "") #env_configs['feature_type'] = 'act_hist' env_configs['verbose'] = True env_configs['log_results'] = True envs.append(Env(env_configs)) i = i + 1 toolbox = setupGA(envs, length=length) begin = time.time() cycles, passes, sample_size = trainGA(toolbox) end = time.time() print("Best individuals are: {}".format(passes)) print("Cycles: {}".format(cycles[0])) compile_time = end - begin print("Compile Time: %d" % (int(compile_time))) fout.write("{}|{}|{}|{}|{}".format(pgm, cycles[0], compile_time, sample_size, passes))
#bms = bms[0:1] # for i, bm in enumerate(bms): # pgm, path = bm # env_configs = {} # env_configs['pgm'] = pgm # env_configs['pgm_dir'] = path # env_configs['run_dir'] = 'run_'+str(i) # env_configs['verbose'] = True # env_configs['log_results'] = True for i, bm in enumerate(bms): pgm, files = bm env_configs = {} env_configs['pgm'] = pgm env_configs['pgm_files'] = files env_configs['run_dir'] = 'run_' + pgm.replace(".c", "") #env_configs['feature_type'] = 'act_hist' env_configs['verbose'] = True env_configs['log_results'] = True print("Tune for {}".format(pgm)) GccFlagsTuner.main(env_configs, argparser.parse_args()) if __name__ == '__main__': from gym_hls.envs.random_bm import get_random num_pgms = 100 bms = get_random(N=num_pgms) #print(len(bms)) ot_test_pgm_group(bms, [16])
def getbaseline(bm_name='chstone', num_pgms=None, clang_opt=False): """ Examples : >>> print(getbaseline()) Benchmark|-O0|-O3|-O0 Runtime(s)|-O3 Runtime(s) {adpcm.c}|{X}|{XX}|{XXX}|{XXXX} {aes.c}|{X}|{XX}|{XXX}|{XXXX} {bf.c}|{X}|{XX}|{XXX}|{XXXX} {gsm.c}|{X}|{XX}|{XXX}|{XXXX} {mpeg2.c}|{X}|{XX}|{XXX}|{XXXX} {sha_driver.c}|{X}|{XX}|{XXX}|{XXXX} {aes.c}|{X}|{XX}|{XXX}|{XXXX} {bf.c}|{X}|{XX}|{XXX}|{XXXX} {dfadd.c}|{X}|{XX}|{XXX}|{XXXX} Args: bm_name (str, optional): bm_name is the name of benchmarks (series of benchmarks) to use. Defaults to chstone benchmarks num_pgms (int, optional): num_pgms is the number of programs to pick from a different benchmarks suite than chstone. Defaults to NONE which is 10 programs from random_bm(). Clang_opt (bool, optional): clang_opt should be set to True if you want to calculate the reward using the clang option. Otherwise, clang_opt should be set to False. Raises: The function terminates if the bm_name is not chstone or random. Returns: Prints the name of each program (benchmarks), wth its o0 and o3 cycles reward (the reward is higher if the compile time of this program is less than the other programs), nd o0 and o3 compile time. """ import time if bm_name == "chstone": from chstone_bm import get_chstone, get_others, get_all9 bms = get_all9() elif bm_name == "random": from gym_hls.envs.random_bm import get_random bms = get_random(N=num_pgms) else: raise print(len(bms)) fout = open("report_baseline" + ".txt", "w") fout.write("Benchmark|-O0|-O3|-O0 Runtime(s)|-O3 Runtime(s)\n") for bm in bms: if bm_name == "chstone": pgm, path = bm env_conf = {} env_conf['pgm'] = pgm env_conf['pgm_dir'] = path env_conf['run_dir'] = 'run_' + pgm.replace(".c", "") elif bm_name == "random": pgm, files = bm env_conf = {} env_conf['pgm'] = pgm env_conf['pgm_files'] = files env_conf['run_dir'] = 'run_' + pgm.replace(".c", "") else: raise env_conf['delete_run_dir'] = False env = HLSEnv(env_conf) try: begin = time.time() o0_cycle = -env.get_Ox_rewards(level=0, clang_opt=clang_opt) end = time.time() o0_compile_time = end - begin begin = time.time() o3_cycle = -env.get_Ox_rewards(level=3, clang_opt=clang_opt) end = time.time() o3_compile_time = end - begin env.__del__() fout.write("{}|{}|{}|{}|{}\n".format(pgm, o0_cycle, o3_cycle, o0_compile_time, o3_compile_time)) print("{}|{}|{}|{}|{}\n".format(pgm, o0_cycle, o3_cycle, o0_compile_time, o3_compile_time)) finally: env.__del__() del env import os os.system("rm -rf run_*") fout.close()
# Optimize blocksize of apps/mmm_block.cpp # # This is an extremely simplified version meant only for tutorials # from __future__ import print_function #import adddeps # fix sys.path from gym_hls.envs.hls_env import HLSEnv import pickle pgm_list = ['14404', '16326', '14899', '2468', '16788', '13968', '16610', '15868', '5219', '634', '16778', '6066', '3240', '14101', '6386', '5257', '428', '15800', '9233', '15381', '8771', '3751', '4242', '9528', '11220', '6975', '8885', '17974', '16163', '3858', '9685', '9601', '7623', '13346', '4881', '16925', '18679', '924', '4660', '18121', '12142', '573', '12460', '9548', '1788', '12825', '8617', '9293', '1941', '5855', '16257', '1254', '1897', '8254', '14420', '8630', '10700', '12757', '5375', '13086', '2217', '9578', '961', '14259', '12396', '13061', '17840', '8162', '15893', '11296', '1852', '16681', '4282', '5160', '1284', '9347', '7488', '18464', '14481', '15022', '13755', '6922', '2630', '4717', '12790', '16505', '16595', '11240', '10629', '16604', '13924', '1638', '4188', '14469', '6540', '11026', '8152', '16598', '1915', '15341', '471', '4926', '12092', '3449', '15266', '4567', '16510', '6118', '15427', '112', '15801', '3229', '12471', '3271', '16599', '11090', '16470', '10308', '9724', '8971', '15292', '15117', '6827', '9381', '18028', '4278', '16971', '1985', '12721', '16698', '9428', '12024', '7246', '1335', '7923', '13570', '11580', '16010', '10492', '10396', '13085', '17532', '14602', '16879', '8518', '13055', '1546', '12204', '15008', '5381', '14332', '4310', '2503', '659', '10929', '10641', '9695', '15443', '10567', '10864', '3418', '10995', '10343', '16413', '12412', '1318', '17189', '6613', '7', '9498', '14037', '5822', '13455', '6029', '9470', '10356', '8715', '18043', '7531', '9360', '16785', '15786', '14891', '12634', '1016', '10920', '13219', '9109', '16600', '5052', '16995', '3799', '17475', '2828', '7671', '6008', '3294', '2004', '5864', '13213', '7904', '3809', '6980', '5021', '3654', '6232', '8158', '1757', '9671', '16324', '5092', '12787', '3036', '13516', '3137', '13320', '10077', '1440', '5493', '15466', '4822', '14610', '5057', '15968', '1263', '18237', '1011', '8993', '3766', '2008', '5369', '8234', '11812', '6957', '689', '6598', '6449', '2616', '18618', '14266', '5422', '7007', '1033', '16220', '10992', '7468', '10008', '12706', '16845', '5446', '11345', '16385', '4284', '16398', '15289', '13129', '7686', '15860', '14384', '15379', '2574', '15081', '6964', '9309', '10707', '1099', '14485', '9980', '7988', '5975', '13201', '9743', '14447', '12397', '18067', '5709', '15509', '6421', '16355', '8308', '12202', '3728', '16998', '102', '14482', '18168', '5088', '15959', '12509', '1147', '15207', '11138', '5118', '15130', '12306', '16568', '4229', '2769', '15817', '11148', '2609', '18171', '11088', '2667', '1383', '7451', '18319', '8860', '13743', '744', '7187', '16553', '8222', '14623', '8052', '545', '5757', '827', '3314', '9567', '8364', '4697', '10454', '1220', '1806', '12857', '17852', '18622', '14437', '1551', '635', '11584', '15655', '577', '14407', '12052', '14815', '1867', '15601', '12550', '15962', '15153', '8411', '1903', '3248', '93', '16213', '7255', '15000', '4871', '17736', '17619', '9883', '13041', '8780', '13156', '15131', '10988', '16154', '3456', '13849', '13530', '14097', '17467', '12424', '12351', '9908', '1319', '10170', '12652', '17757', '16177', '6472', '17888', '11607', '9464', '5675', '13326', '13605', '16816', '4885', '2321', '1470', '14226', '3606', '6145', '11571', '9234', '201', '12552', '10759', '6889', '1836', '16212', '66', '17902', '10530', '5663', '18530', '3940', '11045', '2666', '9871', '7062', '6413', '14928', '5126', '16223', '15228', '6018', '5800', '16981', '15042', '259', '4141', '12270', '9783', '129', '18224', '4601', '1467', '2545', '17060', '12596', '7718', '12169', '16707', '9633', '16926', '3525', '3918', '14309', '7108', '13081', '8427', '217', '9354', '4031', '17066', '5007', '9364', '13981', '14072', '2572', '12616', '18163', '6501', '11612', '17574', '6425', '9546', '13547', '13485', '17419', '11482', '89', '18089', '18032', '774', '16047', '5799', '4719', '10061', '16963', '3184', '5882', '1823', '8616', '369', '6028', '9579', '17973', '4787', '1645', '5940', '13022', '5814', '15272', '10336', '18284', '18441', '12115', '6344', '9013', '4559', '3306', '9833', '5238', '11308', '5892', '13492', '15922', '7701', '1368', '11246', '10934', '14118', '16276', '2586', '4690', '4111', '18446', '6741', '2924', '11209', '4105', '18660', '16494', '9904', '1702', '754', '10422', '15874', '16339', '4794', '6480', '1264', '1883', '16318', '123', '7513', '6853', '2227', '7416', '9727', '11734', '11446', '11807', '10927', '261', '11519', '14461', '18233', '15635', '18117', '17662', '15627', '4233', '7824', '4579', '5075', '18114', '15279', '13786', '4480', '3557', '12015', '10721', '3999', '8268', '6703', '15641', '3744', '10018', '11568', '4643', '15221', '4776', '16416', '7296', '2997', '16564', '1597', '3634', '3285', '3190', '4928', '12814', '407', '14476', '11397', '11559', '5251', '8315', '13498', '9550', '1964', '1305', '14787', '10691', '18479', '3329', '13750', '3304', '3458', '14786', '14472', '13014', '8857', '860', '18044', '5548', '16088', '733', '18326', '5346', '15976', '3205', '11549', '5730', '3864', '13068', '17330', '13553', '2093', '5520', '13030', '6659', '15995', '16130', '13220', '15057', '11499', '3258', '8279', '15186', '18268', '6948', '8690', '5416', '7485', '16164', '935', '12217', '1552', '17982', '17991', '17496', '9596', '11619', '3125', '718', '13913', '1310', '9110', '3310', '5012', '8477', '8257', '113', '13397', '11785', '4393', '18317', '7441', '12516', '6171', '6748', '5769', '6463', '6185', '8759', '8963', '15538', '2223', '6755', '8770', '16585', '16758', '7241', '2180', '2893', '8849', '9357', '17055', '11227', '17096', '6266', '8073', '1756', '4386', '18506', '7593', '14311', '16075', '4555', '16446', '4451', '13193', '13651', '3182', '14655', '4063', '6244', '11494', '949', '18443', '11087', '10794', '14116', '6778', '2425', '8325', '1302', '10448', '4876', '287', '6270', '1882', '1624', '1521', '3481', '15773', '11600', '3579', '4728', '3693', '12714', '7630', '6866', '16126', '17223', '1790', '11436', '11741', '5811', '695', '6076', '9843', '306', '9012', '8655', '27', '2401', '2239', '15468', '9151', '8440', '3670', '15818', '12060', '12113', '4656', '16019', '13440', '5049', '2733', '8375', '1709', '17304', '14534', '15793', '12405', '12975', '6656', '12926', '17682', '4942', '10324', '1507', '7700', '5176', '10731', '9442', '4260', '11379', '10600', '15168', '2351', '16034', '12566', '3993', '16069', '16634', '546', '9327', '15473', '18072', '732', '4803', '9048', '16643', '16606', '15182', '3725', '12775', '16161', '12702', '14575', '18471', '7018', '9954', '682', '9221', '8970', '18381', '16501', '16577', '14161', '11329', '11015', '7235', '16886', '1107', '17109', '4041', '8071', '6051', '3135', '4017', '7899', '13813', '14886', '10716', '4438', '16206', '3003', '13917', '12999', '17511', '8984', '15946', '14647', '6716', '6067', '4305', '1164', '16320', '16301', '17012', '13770', '12642', '17827', '8123', '5968', '17054', '5860', '14652', '11483', '8976', '9162', '10798', '4060', '936', '16399', '12927', '10564', '16271', '12548', '13943', '16131', '1370', '3711', '7939', '15920', '17830', '614', '1538', '9195', '12129', '9947', '13552', '6274', '12733', '15333', '17296', '5724', '8567', '6321', '17344', '2127', '16093', '1914', '6275', '9387', '7267', '16187', '8814', '4577', '9239', '534', '13908', '5291', '4362', '6141', '6979', '16875', '3907', '18024', '14258', '5394', '13661', '3051', '18659', '18243', '2415', '15731', '3696', '18177', '17534', '17166', '16219', '8013', '7405', '1808', '10129', '8165', '11669', '12268', '16838', '11009', '180', '10158', '7885', '14720', '13606', '7450', '6645', '18521', '9592', '4269', '1634', '10577', '8240', '18045', '1079', '9297', '13652', '18265', '5870', '5970', '12476', '1995', '16227', '10678', '16619', '7797', '11750', '3246', '2409', '17696', '7706', '10768', '7194', '9702', '6398', '12863', '3022', '8366', '17050', '17292', '2398', '5412', '3652', '13306', '6971', '3835', '8725', '6038', '16894', '17090', '15398', '7176', '14545', '7744', '16178', '1973', '4952', '13102', '4099', '14827', '8642', '10244', '18436', '10528', '9866', '4729', '13387', '5887', '4862', '13272', '13754', '7634', '2913', '5583', '5183', '13646', '1080', '4290', '6054', '12322', '16081', '12477', '1658', '5585', '6251', '812', '6676', '14858', '10257', '16734', '11373', '2215', '14812', '15387', '3154', '6469', '13333', '13819', '88', '7932', '13847', '16012', '13408', '6024', '10617', '7527', '10961', '9438', '8516', '794', '1271', '16877', '2138', '4258', '10370', '14336', '12709', '16508', '3771', '18182', '17229', '2286', '17489', '16371', '14134', '4681', '313', '8092', '17293', '12921', '2363', '8645', '12463', '7956', '12811', '17040', '3971', '2608', '5867', '3141', '6996', '12261', '8238', '13154', '9859', '3275', '7332', '17036', '17947', '1299', '6454', '12731', '13033', '10976', '9142', '14479', '9690', '5243', '4544', '111', '12644', '3775', '7093', '3302', '193', '5562', '776', '13798', '10312', '16298', '8980', '6644', '3263', '7641', '16159', '12973', '10260', '4868', '11036', '2160', '656', '12035', '4079', '2942', '1444', '18269', '5054', '12241', '5485', '10029', '11753', '15172', '18478', '14653', '553', '3027', '1362', '4524', '12740', '12474', '9177', '316', '6213', '2525', '14853', '8919', '7046', '1910', '5518', '10925', '7935', '15531', '17539', '14454', '4161', '16670', '10335', '8872', '3753', '3620', '9558', '14843', '2132', '331', '8887', '5983', '1659', '8832', '14769', '4981', '15625', '7060', '23', '505', '1618', '10358', '3932', '15790', '11291', '38', '10744', '13861', '15812', '1848', '2343', '7637', '11579', '4083', '15267', '1281', '4368', '15768', '4531', '13625', '7786', '196', '6338', '13969', '8436', '2484', '3763', '3924', '13658', '4795', '5632', '15384', '9479', '5244', '11316', '17590', '6243', '18209', '11723', '3667', '3089', '5704', '14590', '3515', '9670', '16797', '16147', '16392', '4824', '12286', '11751', '12838', '1462', '7733', '4257', '5523', '1428', '7330', '15181', '9211', '3975', '920', '5586', '6147', '13189', '5854', '3460', '12161', '14874', '382', '11882', '8445', '5736', '12665', '6033', '7995', '7943', '9084', '10232', '14579', '8809', '3035', '13479', '6566', '12627', '4886', '10262', '452', '16625', '4899', '16627', '9405', '1868', '7469', '7708', '1969', '17133', '18614', '5341', '12141', '2914', '18415', '18278', '3100', '605', '8412', '3238', '1412', '6403', '12104', '3516', '7728', '8837', '8264', '11219', '8034', '16404', '7277', '4800', '905', '17158', '136', '5167', '8940', '16038', '1846', '18132', '17948', '1232', '7743', '6820', '16434', '724', '5726', '10780', '10684', '14032', '389', '10954', '3146', '5005', '15026', '15135', '9314', '18029', '9529', '9010', '16515', '15323', '8047', '1621', '5595', '16204', '18391', '17490', '10284', '8847', '13797', '9403', '4136', '13351', '16990', '9704', '7811', '8249', '13829', '16139', '16918', '1966', '464', '6830', '10055', '901', '3316', '17421', '8179', '6304', '12622', '7753', '4405', '14883', '14364', '11300', '14627', '3224', '13902', '9765', '17666', '15961', '358', '632', '4605', '815', '183', '6079', '4686', '4519', '15564', '6904', '14346', '3267', '7921', '18003', '16690', '4264', '15792', '12147', '5053', '12578', '5954', '9703', '13190', '11008', '12426', '1874', '17067', '14784', '3794', '15648', '11593', '5659', '17323', '10368', '4725', '13119', '12719', '8163', '11614', '5179', '8228', '18279', '903', '9841', '8232', '14337', '7293', '4931', '4297', '13983', '9657', '6681', '2759', '8318', '3692', '14569', '4629', '8211', '10526', '1889', '4750', '9069', '10169', '2200', '3566', '420', '2098', '3379', '3644', '1717', '16567', '14114', '5728', '12069', '14182', '15086', '9677', '16812', '13522', '488', '7764', '9987', '14224', '11794', '16156', '453', '1716', '9639', '7379', '4588', '13017', '17087', '1454', '1951', '13288', '7437', '12483', '11930', '168', '12821', '16046', '17300', '10475', '4603', '16464', '13841', '13384', '2798', '11907', '9991', '15255', '280', '12323', '16119', '7369', '4918', '981', '17946', '16952', '12587', '4782', '15485', '14441', '18553', '6385', '9938', '17862', '18109', '5215', '18097', '14849', '5226', '5201', '8654', '11707', '7308', '239', '9922', '6348', '2555', '9415', '17220', '11290', '5761', '5695', '7742', '13388', '11653', '9440', '3059', '14474', '5263', '14994', '8053', '16633', '4321', '12028', '9971', '16079', '16807', '14876', '6554', '12319', '14493', '11622', '8293', '8553', '15489', '14105', '10873', '6989', '15637', '17411', '18134', '12734', '12375', '13885', '10658', '12744', '9307', '10070', '17108', '1845', '4960', '6347', '8104', '12056', '12378', '3843', '5941', '540', '6137', '6139', '15749', '15032', '1694', '1578', '1406', '4186', '295', '18653', '12538', '1513', '3179', '18342', '10955', '7282', '8525', '1536', '14208', '13436', '12454', '12739', '10481', '996', '18538', '15558', '729', '2099', '7659', '17001', '16435', '3028', '13244', '9475', '11414', '1865', '4730', '5214', '1457', '18086', '10894', '10382', '4228', '12232', '6017', '3385', '16264', '8538', '6085', '15607', '5981', '8842', '5911', '3142', '12783', '14055', '10438', '3471', '9710', '9725', '5344', '235', '14466', '4181', '12254', '15890', '11942', '7893', '658', '9553', '715', '10074', '13523', '3355', '2015', '15682', '1627', '30', '15142', '15119', '14422', '14136', '6423', '12029', '16768', '6781', '10172', '1583', '17199', '2517', '14559', '865', '661', '15725', '10797', '6358', '6105', '17480', '12986', '11217', '92', '12338', '5995', '17234', '10307', '9135', '4270', '2704', '7679', '14457', '1278', '5992', '17961', '4204', '17449', '902', '11188', '5918', '5380'] if __name__ == '__main__': from gym_hls.envs.random_bm import get_random num_pgms = 1000 bms = get_random(N=num_pgms, pgm_list=pgm_list) #bms = bms[100:150] algo_pass = {} #algo_pass['autotuner'] = [18, 7, 37, 31, 38, 17, 2, 6, 23, 21, 24, 41, 19, 20, 33, 11, 44, 26, 4, 34, 27, 32, 8, 22, 36] #algo_pass['ga'] = [0, 1, 43, 1, 1, 0, 25, 1, 23, 16, 0, 0, 0, 33, 0, 18, 16, 0, 37, 0, 35, 0, 1, 0, 1, 14, 14, 0, 23, 1, 44, 0, 1, 33, 1, 1, 28, 0, 1, 0, 0, 0, 0, 1, 2] #algo_pass['greedy'] = [23, 20, 34, 11, 33, 10, 32] #algo_pass['greedy'] = [23, 20, 34, 11, 33, 10, 32, 9, 31, 8, 30, 7, 29, 6, 28, 5, 27] for algo, passes in algo_pass.items(): fout = open(algo+".csv", "w") for i, bm in enumerate(bms): pgm, files= bm env_configs = {} env_configs['pgm'] = pgm env_configs['pgm_files'] = files env_configs['run_dir'] = 'run_'+pgm.replace(".c","")
from gym_hls.envs.hls_env import HLSEnv from gym_hls.envs.hls_multi_env import HLSMultiEnv import os import pickle env_configs = {} import argparse NumSteps = 12 parser = argparse.ArgumentParser() parser.add_argument('--checkpoint_dir', '-cpd', type=str, required=True) parser.add_argument('--steps', '-s', type=int, default=NumSteps) args = parser.parse_args() from gym_hls.envs.random_bm import get_random bms = get_random() needed_config_for_rollout = {} for i, bm in enumerate(bms): pgm, files = bm env_configs["pgm"] = pgm env_configs["verbose"] = 'True' env_configs["pgm_files"] = files env_configs["run_dir"] = 'run_'+pgm.replace(".c","") env_configs["orig_and_normalize"] = 'True' env_configs["log_obs_reward"] = 'True' needed_config_for_rollout["env_config"] = env_configs #needed_config_for_rollout["model"] = {"use_lstm": True, "max_seq_len":5, "lstm_use_prev_action_reward":True} #needed_config_for_rollout["model"]= {"fcnet_hiddens":[256,256,256,256]} str_env_config = str(needed_config_for_rollout).replace("\'","\"") #print(str_env_config) print('-'*30) command = "rllib rollout {0} --run PPO --env HLS-v0 --steps {1} --no-render --config '{2}'".format(args. checkpoint_dir,args.steps,str_env_config)