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 ...@@ -11,7 +11,18 @@ import os
import re import re
class KnobConfiguration: 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): def __init__(self, raw_config):
'''
Args: raw_config = line of configuration file to parse
'''
line_as_lst = raw_config.strip().split() line_as_lst = raw_config.strip().split()
self.id = int(line_as_lst[0]) self.id = int(line_as_lst[0])
...@@ -22,56 +33,69 @@ class KnobConfiguration: ...@@ -22,56 +33,69 @@ class KnobConfiguration:
# Build the new function call # Build the new function call
self.new_func_call = "%s(%s)" % (self.modified_func_name, ", ".join(self.params)) 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)
def generate_source_code(table, dir_name, filename, source_name):
# Assumption: Original function call = the new function without the <approx><conv> '''
# Ex: tensorConvPerfCuda replaces tensorConv Generates source code for all configurations in the table for one original source
# TODO might be better if we just create a col in the table for this ... Args
def generate_source_code(table, filename, dir_name, source_name): 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") source_file = open(filename, "r")
for knob_config in table: for knob_config in table:
print("KNOB CONFIG: ", knob_config)
source_file.seek(0, 0) source_file.seek(0, 0)
new_file_contents = [] 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: for line in source_file:
# Handles case where 1 actual line of code is split into 2 lines # Handles case where 1 actual line of code is split into 2 lines
if len(line) >= 2 and not line.startswith("#") and \ if len(line) >= 2 and not line.startswith("#") and \
line.find(";") == -1: # Last char is always \n line.find(";") == -1: # Last char is always \n
curr_line += line complete_line += line
continue continue
curr_line += line complete_line += line
orig_func_ind = curr_line.find(knob_config.orig_func_name) orig_func_ind = complete_line.find(knob_config.orig_func_name)
if orig_func_ind != -1: if orig_func_ind != -1:
# Replace from an instance of origOp to first instance of ) after origOp # Replace from an instance of origOp to first instance of ) after origOp
# with the new function call
new_line = [] new_line = []
line_start_ind = 0 line_start_ind = 0
last_ind = 0 last_ind = 0
while orig_func_ind != -1: 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) new_line.append(knob_config.new_func_call)
line_start_ind = curr_line.find(")", orig_func_ind) + 1 line_start_ind = complete_line.find(")", orig_func_ind) + 1
orig_func_ind = curr_line.find(knob_config.orig_func_name, line_start_ind) orig_func_ind = complete_line.find(knob_config.orig_func_name, line_start_ind)
new_line.append(curr_line[line_start_ind : ])
new_line.append(complete_line[line_start_ind : ])
new_file_contents.append(''.join(new_line)) new_file_contents.append(''.join(new_line))
else: else:
new_file_contents.append(curr_line) new_file_contents.append(complete_line)
curr_line = "" complete_line = ""
new_filename = os.path.join(dir_name, "%s_%s.cc" % (source_name, knob_config.id)) new_filename = os.path.join(dir_name, "%s_%s.cc" % (source_name, knob_config.id))
new_file = open(new_filename, "w") new_file = open(new_filename, "w")
new_file.write(''.join(new_file_contents)) new_file.write(''.join(new_file_contents))
new_file.close() new_file.close()
source_file.close() source_file.close()
def generate_all_sources(table, orig_files_filename): 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") orig_files = open(orig_files_filename, "r")
for orig_filename in orig_files: for orig_filename in orig_files:
orig_filename = orig_filename.strip() orig_filename = orig_filename.strip()
...@@ -97,12 +121,15 @@ def generate_all_sources(table, orig_files_filename): ...@@ -97,12 +121,15 @@ def generate_all_sources(table, orig_files_filename):
print("GENERATING DIRECTORY: %s" % dir_name) print("GENERATING DIRECTORY: %s" % dir_name)
os.makedirs(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() orig_files.close()
def parse_table(table_filename): 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 # else: need to use a dict
table = [] table = []
table_file = open(table_filename, "r") table_file = open(table_filename, "r")
...@@ -123,10 +150,5 @@ if __name__ == "__main__": ...@@ -123,10 +150,5 @@ if __name__ == "__main__":
print("Run with --usage flag for more detailed information") print("Run with --usage flag for more detailed information")
exit(1) 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]) table = parse_table(sys.argv[1])
generate_all_sources(table, sys.argv[2]) 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