Skip to content
Snippets Groups Projects
Commit 9b8631af authored by Elizabeth's avatar Elizabeth
Browse files

Added comments

parent 56da52d7
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,18 @@ import os
import re
class KnobConfiguration:
'''
Stores the configurations as well as other useful information for each knob configuration
Stores: id (may factor out if ids are guaranteed to start at 0/1 and be consecutive)
original function name
modified function name
new function parameters (knobs)
new function call (modified function name(knobs))
'''
def __init__(self, raw_config):
'''
Args: raw_config = line of configuration file to parse
'''
line_as_lst = raw_config.strip().split()
self.id = int(line_as_lst[0])
......@@ -22,56 +33,69 @@ class KnobConfiguration:
# Build the new function call
self.new_func_call = "%s(%s)" % (self.modified_func_name, ", ".join(self.params))
def __repr__(self):
return "%s %s %s %s %s" % (self.id, self.modified_func_name, self.params, self.orig_func_name, self.new_func_call)
# Assumption: Original function call = the new function without the <approx><conv>
# Ex: tensorConvPerfCuda replaces tensorConv
# TODO might be better if we just create a col in the table for this ...
def generate_source_code(table, filename, dir_name, source_name):
def generate_source_code(table, dir_name, filename, source_name):
'''
Generates source code for all configurations in the table for one original source
Args
table: List of KnobConfigurations
dir_name: Directory new sources should be placed in
filename: Filename of original source
source_name: Filename without the file extension (ex: foo/blah.cc --> blah)
'''
source_file = open(filename, "r")
for knob_config in table:
print("KNOB CONFIG: ", knob_config)
source_file.seek(0, 0)
new_file_contents = []
curr_line = ""
# Store complete line to handle cases where one line of code is split into two lines
complete_line = ""
for line in source_file:
# Handles case where 1 actual line of code is split into 2 lines
if len(line) >= 2 and not line.startswith("#") and \
line.find(";") == -1: # Last char is always \n
curr_line += line
complete_line += line
continue
curr_line += line
orig_func_ind = curr_line.find(knob_config.orig_func_name)
complete_line += line
orig_func_ind = complete_line.find(knob_config.orig_func_name)
if orig_func_ind != -1:
# Replace from an instance of origOp to first instance of ) after origOp
# with the new function call
new_line = []
line_start_ind = 0
last_ind = 0
while orig_func_ind != -1:
new_line.append(curr_line[line_start_ind : orig_func_ind])
new_line.append(complete_line[line_start_ind : orig_func_ind])
new_line.append(knob_config.new_func_call)
line_start_ind = curr_line.find(")", orig_func_ind) + 1
orig_func_ind = curr_line.find(knob_config.orig_func_name, line_start_ind)
new_line.append(curr_line[line_start_ind : ])
line_start_ind = complete_line.find(")", orig_func_ind) + 1
orig_func_ind = complete_line.find(knob_config.orig_func_name, line_start_ind)
new_line.append(complete_line[line_start_ind : ])
new_file_contents.append(''.join(new_line))
else:
new_file_contents.append(curr_line)
curr_line = ""
new_file_contents.append(complete_line)
complete_line = ""
new_filename = os.path.join(dir_name, "%s_%s.cc" % (source_name, knob_config.id))
new_file = open(new_filename, "w")
new_file.write(''.join(new_file_contents))
new_file.close()
source_file.close()
def generate_all_sources(table, orig_files_filename):
'''
Generates directories and source code for all original sources for all knob configurations
Args:
table: List of KnobConfiguration objects
orig_files_filename: Filename of file containing all original source names to generate new
sources for
'''
orig_files = open(orig_files_filename, "r")
for orig_filename in orig_files:
orig_filename = orig_filename.strip()
......@@ -97,12 +121,15 @@ def generate_all_sources(table, orig_files_filename):
print("GENERATING DIRECTORY: %s" % dir_name)
os.makedirs(dir_name)
generate_source_code(table, orig_filename, dir_name, source_name)
generate_source_code(table, dir_name, orig_filename, source_name)
orig_files.close()
def parse_table(table_filename):
# can we assume that the ids always start at 1 --> if so, can index by knobs
'''
Given the filename of a table, parses the table into a list of KnobConfigurations
'''
# Can we assume that the ids always start at 1 --> if so, can index by knobs
# else: need to use a dict
table = []
table_file = open(table_filename, "r")
......@@ -123,10 +150,5 @@ if __name__ == "__main__":
print("Run with --usage flag for more detailed information")
exit(1)
# How do we know what function names correspond to what files?
# Assume that for the given filenames, we replace all function names with the corresp func name --> then we can't run this for different functions, need to replace the modified func name
# Parse the table first to avoid having to reparse it for each file to change
table = parse_table(sys.argv[1])
generate_all_sources(table, sys.argv[2])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment