<reponame>rf3nster/thesis-test-code
# Mixed NoC Simulation Run Script
# Written by <NAME>, May 10/2021
# Runs Modelsim instances, concatenates CSVs into a single Excel sheet

# Imports
import subprocess
import sys
import os
import re
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment

# Create workbook
wb = Workbook()
ws1 = wb.active
# Grab injection rates from file
injrates_file = open ('injectionrates.dat','r')
injrates = injrates_file.readlines()

# Remove trailing linebreaks
injrates = [s.replace("\n","") for s in injrates]
del injrates_file
# Parameter grabbing time

# Get X Size of Network
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
x_size = params_file.readlines()[47]
x_size = re.sub("[^0-9]", "", x_size)

# Get Y Size of Network
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
y_size = params_file.readlines()[49]
y_size = re.sub("[^0-9]", "", y_size)

# Get quantity of packets
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
packet_qty = params_file.readlines()[41]
packet_qty = re.sub("[^0-9]", "", packet_qty)

# Get number of ticks per period
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
period_size = params_file.readlines()[43]
period_size = re.sub("[^0-9]", "", period_size)

# Clean up
del params_file

print("**** Preparing Environment *****")    
try:
    os.mkdir("csv_data")    
except OSError:
    print ("... Creation of the csv data directory failed or already exists")
# Clean simulation env
print("**** Cleaning Environment *****")
dir_name = os.getcwd()+ "/csv_data"
# Clean CSVs
current_csv_files = os.listdir(dir_name)
for item in current_csv_files:
    if item.endswith(".csv"):
        os.remove(os.path.join(dir_name, item))
dir_name = os.getcwd()
current_csv_files = os.listdir(dir_name) 
for item in current_csv_files:
    if item.endswith(".csv"):
        os.remove(os.path.join(dir_name, item))
del current_csv_files
# Clean XLSXs
current_csv_files = os.listdir(dir_name) 
for item in current_csv_files:
    if item.endswith(".xlsx"):
        os.remove(os.path.join(dir_name, item))
del current_csv_files
# Clean WLFs
current_wlf_files = os.listdir(dir_name)
for item in current_wlf_files:
    if item.endswith(".wlf"):
        os.remove(os.path.join(dir_name, item))


# PACKETS_PER_PERIOD
print("===== Test Parameters =====")
print("X Size of Network: ", x_size)
print("Y Size of Network: ", y_size)
print("Number of Packets to Inject: ",packet_qty)
print("Number of Ticks Per Period: ",period_size)
print("Number of injection rates to test: ", len(injrates))
input("Press any key to continue...")

for runcount  in range(len(injrates)):
    print ("===== Run number",runcount, "with injection rate of", injrates[runcount]+" ======")
    # Get current value
    curr_number_of_packets = int(float(injrates[runcount]) * float(packet_qty))
    # Modify the parameters file with new injection rate
    params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
    params_data = params_file.readlines()
    params_data[45] = '    constant PACKETS_PER_PERIOD : integer := '+str(curr_number_of_packets)+ ";\n"
    params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd','w')
    params_file.writelines(params_data)
    params_file.close() 
    # Launch vsim
    print("... Running Modelsim")
    subprocess.call( ["vsim","-c",  "-do", "do ./thesis-test-code/do_files/test/noc_3x3_rand_mixed.do"], cwd=os.getcwd(), stdout=subprocess.DEVNULL,  stderr=subprocess.DEVNULL)
    print("... Concatenating Results")
    # Concatenate accurate results
    results_write_file_name = "results_acc_"+str(injrates[runcount])+".csv"
    results_write_file = open('./csv_data/'+results_write_file_name,'a')
    for x_coord in range (int(x_size)):
        for y_coord in range (int(y_size)):
            result_read_file_name = "results_acc_"+str(x_coord)+"_"+str(y_coord)+".csv"
            results_read_file = open(result_read_file_name)
            results_write_file.writelines(results_read_file.readlines())

    # Concatenate approx results
    results_write_file_name = "results_apx_"+str(injrates[runcount])+".csv"
    results_write_file = open('./csv_data/'+results_write_file_name,'a')
    for x_coord in range (int(x_size)):
        for y_coord in range (int(y_size)):
            result_read_file_name = "results_apx_"+str(x_coord)+"_"+str(y_coord)+".csv"
            results_read_file = open(result_read_file_name)
            results_write_file.writelines(results_read_file.readlines())        
    # Clean up objects that are no longer used
    del results_write_file_name
    del results_write_file
    del result_read_file_name
    del results_read_file
    # Delete remaining CSVs
    dir_name = os.getcwd()
    # Clean CSVs
    current_csv_files = os.listdir(dir_name)
    for item in current_csv_files:
        if item.endswith(".csv"):
            os.remove(os.path.join(dir_name, item))
    del current_csv_files
    # Clean WLFs
    current_wlf_files = os.listdir(dir_name)
    for item in current_wlf_files:
        if item.endswith(".wlf"):
            os.remove(os.path.join(dir_name, item))

# Init lists for averages
acc_averages = []
apx_averages = []
print("==== Creating Excel Workbook ====")
# Create workbook
print("... Adding Data to Book")
wb = Workbook(write_only=True)

for injrates_cnt in range(len(injrates)):
    
    # Start with Accurate
    ws1 = wb.create_sheet("IR = "+str(injrates[injrates_cnt]+" , Acc"))
    # Add header
    ws1.append(["Packet Sent At", "Packet Received At", "Received X Co-Ord", "Received Y Co-Ord", "Delta CC"])

    # Grab CSV file
    csv_results_file = open('./csv_data/results_acc_'+(injrates[injrates_cnt].strip())+'.csv', 'r')
    # Traverse the CSV file
    line = csv_results_file.readline()
    cnt = 0

    while line:

        # Strip string of newline
        line = line.split(',')
        line = [i.replace('\n','') for i in line]
        # Convert to int
        integer_map = map(int, line)
        line = list(integer_map)
        # Add subtraction
        line.append("=B"+str(cnt+2)+"-A"+str(cnt+2))
        # Append to sheet
        ws1.append(line)

        # Go to new line
        line = csv_results_file.readline()
        cnt += 1

    # Add Average
    ws1.append(["Average","","","","=AVERAGE(E2:E"+str(cnt+1)+")"])
    # Append average to list
    acc_averages.append("E"+str(cnt+2))

    # Now approximate
    ws1 = wb.create_sheet("IR = "+str(injrates[injrates_cnt]+" , Apx"))
    # Add header
    ws1.append(["Packet Sent At", "Packet Received At", "Received X Co-Ord", "Received Y Co-Ord", "Delta CC"])

    # Grab CSV file
    csv_results_file = open('./csv_data/results_apx_'+(injrates[injrates_cnt].strip())+'.csv', 'r')
    # Traverse the CSV file
    line = csv_results_file.readline()
    cnt = 0

    while line:

        # Strip string of newline
        line = line.split(',')
        line = [i.replace('\n','') for i in line]
        # Convert to int
        integer_map = map(int, line)
        line = list(integer_map)
        # Add subtraction
        line.append("=B"+str(cnt+2)+"-A"+str(cnt+2))
        # Append to sheet
        ws1.append(line)

        # Go to new line
        line = csv_results_file.readline()
        cnt += 1

    # Add Average
    ws1.append(["Average","","","","=AVERAGE(E2:E"+str(cnt+1)+")"])
    # Append average to list
    apx_averages.append("E"+str(cnt+2))

# Create Sheet with Results
# Create Sheet with Results
print("... Creating Results Sheet")
ws1 = wb.create_sheet("Results and System Info")
# System Info
ws1.append(["System Info"])
ws1.append(["X Size", x_size])
ws1.append(["Y Size", y_size])
ws1.append(["Number of Injected Packets Per Node", packet_qty])
ws1.append(["Number of Ticks Per Period: ",period_size])
# Accurate Results
ws1.append(["Accurate Results"])
ws1.append(["Injection Rate", "Average Number of Cycles in Transit"])
# Add Averages to sheet
for avg_cnt in range(len(acc_averages)):
    ws1.append([str(injrates[avg_cnt]), "='IR = "+str(injrates[avg_cnt])+" , Acc'!"+acc_averages[avg_cnt]])
# Approximate Results
ws1.append(["Approximate Results"])
ws1.append(["Injection Rate", "Average Number of Cycles in Transit"])
# Add Averages to sheet
for avg_cnt in range(len(apx_averages)):
    ws1.append([str(injrates[avg_cnt]), "='IR = "+str(injrates[avg_cnt])+" , Apx'!"+apx_averages[avg_cnt]])
wb.save (filename = "results_mixed.xlsx")    

# Mixed NoC Simulation Run Script
# Written by <NAME>, May 10/2021
# Runs Modelsim instances, concatenates CSVs into a single Excel sheet

# Imports
import subprocess
import sys
import os
import re
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.styles import Border, Side, PatternFill, Font, GradientFill, Alignment

# Create workbook
wb = Workbook()
ws1 = wb.active
# Grab injection rates from file
injrates_file = open ('injectionrates.dat','r')
injrates = injrates_file.readlines()

# Remove trailing linebreaks
injrates = [s.replace("\n","") for s in injrates]
del injrates_file
# Parameter grabbing time

# Get X Size of Network
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
x_size = params_file.readlines()[47]
x_size = re.sub("[^0-9]", "", x_size)

# Get Y Size of Network
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
y_size = params_file.readlines()[49]
y_size = re.sub("[^0-9]", "", y_size)

# Get quantity of packets
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
packet_qty = params_file.readlines()[41]
packet_qty = re.sub("[^0-9]", "", packet_qty)

# Get number of ticks per period
params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
period_size = params_file.readlines()[43]
period_size = re.sub("[^0-9]", "", period_size)

# Clean up
del params_file

print("**** Preparing Environment *****")    
try:
    os.mkdir("csv_data")    
except OSError:
    print ("... Creation of the csv data directory failed or already exists")
# Clean simulation env
print("**** Cleaning Environment *****")
dir_name = os.getcwd()+ "/csv_data"
# Clean CSVs
current_csv_files = os.listdir(dir_name)
for item in current_csv_files:
    if item.endswith(".csv"):
        os.remove(os.path.join(dir_name, item))
dir_name = os.getcwd()
current_csv_files = os.listdir(dir_name) 
for item in current_csv_files:
    if item.endswith(".csv"):
        os.remove(os.path.join(dir_name, item))
del current_csv_files
# Clean XLSXs
current_csv_files = os.listdir(dir_name) 
for item in current_csv_files:
    if item.endswith(".xlsx"):
        os.remove(os.path.join(dir_name, item))
del current_csv_files
# Clean WLFs
current_wlf_files = os.listdir(dir_name)
for item in current_wlf_files:
    if item.endswith(".wlf"):
        os.remove(os.path.join(dir_name, item))


# PACKETS_PER_PERIOD
print("===== Test Parameters =====")
print("X Size of Network: ", x_size)
print("Y Size of Network: ", y_size)
print("Number of Packets to Inject: ",packet_qty)
print("Number of Ticks Per Period: ",period_size)
print("Number of injection rates to test: ", len(injrates))
input("Press any key to continue...")

for runcount  in range(len(injrates)):
    print ("===== Run number",runcount, "with injection rate of", injrates[runcount]+" ======")
    # Get current value
    curr_number_of_packets = int(float(injrates[runcount]) * float(packet_qty))
    # Modify the parameters file with new injection rate
    params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd')
    params_data = params_file.readlines()
    params_data[45] = '    constant PACKETS_PER_PERIOD : integer := '+str(curr_number_of_packets)+ ";\n"
    params_file = open('./thesis-test-code/src/shared/noc_parameterspkg.vhd','w')
    params_file.writelines(params_data)
    params_file.close() 
    # Launch vsim
    print("... Running Modelsim")
    subprocess.call( ["vsim","-c",  "-do", "do ./thesis-test-code/do_files/test/noc_3x3_rand_mixed.do"], cwd=os.getcwd(), stdout=subprocess.DEVNULL,  stderr=subprocess.DEVNULL)
    print("... Concatenating Results")
    # Concatenate accurate results
    results_write_file_name = "results_acc_"+str(injrates[runcount])+".csv"
    results_write_file = open('./csv_data/'+results_write_file_name,'a')
    for x_coord in range (int(x_size)):
        for y_coord in range (int(y_size)):
            result_read_file_name = "results_acc_"+str(x_coord)+"_"+str(y_coord)+".csv"
            results_read_file = open(result_read_file_name)
            results_write_file.writelines(results_read_file.readlines())

    # Concatenate approx results
    results_write_file_name = "results_apx_"+str(injrates[runcount])+".csv"
    results_write_file = open('./csv_data/'+results_write_file_name,'a')
    for x_coord in range (int(x_size)):
        for y_coord in range (int(y_size)):
            result_read_file_name = "results_apx_"+str(x_coord)+"_"+str(y_coord)+".csv"
            results_read_file = open(result_read_file_name)
            results_write_file.writelines(results_read_file.readlines())        
    # Clean up objects that are no longer used
    del results_write_file_name
    del results_write_file
    del result_read_file_name
    del results_read_file
    # Delete remaining CSVs
    dir_name = os.getcwd()
    # Clean CSVs
    current_csv_files = os.listdir(dir_name)
    for item in current_csv_files:
        if item.endswith(".csv"):
            os.remove(os.path.join(dir_name, item))
    del current_csv_files
    # Clean WLFs
    current_wlf_files = os.listdir(dir_name)
    for item in current_wlf_files:
        if item.endswith(".wlf"):
            os.remove(os.path.join(dir_name, item))

# Init lists for averages
acc_averages = []
apx_averages = []
print("==== Creating Excel Workbook ====")
# Create workbook
print("... Adding Data to Book")
wb = Workbook(write_only=True)

for injrates_cnt in range(len(injrates)):
    
    # Start with Accurate
    ws1 = wb.create_sheet("IR = "+str(injrates[injrates_cnt]+" , Acc"))
    # Add header
    ws1.append(["Packet Sent At", "Packet Received At", "Received X Co-Ord", "Received Y Co-Ord", "Delta CC"])

    # Grab CSV file
    csv_results_file = open('./csv_data/results_acc_'+(injrates[injrates_cnt].strip())+'.csv', 'r')
    # Traverse the CSV file
    line = csv_results_file.readline()
    cnt = 0

    while line:

        # Strip string of newline
        line = line.split(',')
        line = [i.replace('\n','') for i in line]
        # Convert to int
        integer_map = map(int, line)
        line = list(integer_map)
        # Add subtraction
        line.append("=B"+str(cnt+2)+"-A"+str(cnt+2))
        # Append to sheet
        ws1.append(line)

        # Go to new line
        line = csv_results_file.readline()
        cnt += 1

    # Add Average
    ws1.append(["Average","","","","=AVERAGE(E2:E"+str(cnt+1)+")"])
    # Append average to list
    acc_averages.append("E"+str(cnt+2))

    # Now approximate
    ws1 = wb.create_sheet("IR = "+str(injrates[injrates_cnt]+" , Apx"))
    # Add header
    ws1.append(["Packet Sent At", "Packet Received At", "Received X Co-Ord", "Received Y Co-Ord", "Delta CC"])

    # Grab CSV file
    csv_results_file = open('./csv_data/results_apx_'+(injrates[injrates_cnt].strip())+'.csv', 'r')
    # Traverse the CSV file
    line = csv_results_file.readline()
    cnt = 0

    while line:

        # Strip string of newline
        line = line.split(',')
        line = [i.replace('\n','') for i in line]
        # Convert to int
        integer_map = map(int, line)
        line = list(integer_map)
        # Add subtraction
        line.append("=B"+str(cnt+2)+"-A"+str(cnt+2))
        # Append to sheet
        ws1.append(line)

        # Go to new line
        line = csv_results_file.readline()
        cnt += 1

    # Add Average
    ws1.append(["Average","","","","=AVERAGE(E2:E"+str(cnt+1)+")"])
    # Append average to list
    apx_averages.append("E"+str(cnt+2))

# Create Sheet with Results
# Create Sheet with Results
print("... Creating Results Sheet")
ws1 = wb.create_sheet("Results and System Info")
# System Info
ws1.append(["System Info"])
ws1.append(["X Size", x_size])
ws1.append(["Y Size", y_size])
ws1.append(["Number of Injected Packets Per Node", packet_qty])
ws1.append(["Number of Ticks Per Period: ",period_size])
# Accurate Results
ws1.append(["Accurate Results"])
ws1.append(["Injection Rate", "Average Number of Cycles in Transit"])
# Add Averages to sheet
for avg_cnt in range(len(acc_averages)):
    ws1.append([str(injrates[avg_cnt]), "='IR = "+str(injrates[avg_cnt])+" , Acc'!"+acc_averages[avg_cnt]])
# Approximate Results
ws1.append(["Approximate Results"])
ws1.append(["Injection Rate", "Average Number of Cycles in Transit"])
# Add Averages to sheet
for avg_cnt in range(len(apx_averages)):
    ws1.append([str(injrates[avg_cnt]), "='IR = "+str(injrates[avg_cnt])+" , Apx'!"+apx_averages[avg_cnt]])
wb.save (filename = "results_mixed.xlsx")
# Mixed NoC Simulation Run Script # Written by <NAME>, May 10/2021 # Runs Modelsim instances, concatenates CSVs into a single Excel sheet # Imports # Create workbook # Grab injection rates from file # Remove trailing linebreaks # Parameter grabbing time # Get X Size of Network # Get Y Size of Network # Get quantity of packets # Get number of ticks per period # Clean up # Clean simulation env # Clean CSVs # Clean XLSXs # Clean WLFs # PACKETS_PER_PERIOD # Get current value # Modify the parameters file with new injection rate # Launch vsim # Concatenate accurate results # Concatenate approx results # Clean up objects that are no longer used # Delete remaining CSVs # Clean CSVs # Clean WLFs # Init lists for averages # Create workbook # Start with Accurate # Add header # Grab CSV file # Traverse the CSV file # Strip string of newline # Convert to int # Add subtraction # Append to sheet # Go to new line # Add Average # Append average to list # Now approximate # Add header # Grab CSV file # Traverse the CSV file # Strip string of newline # Convert to int # Add subtraction # Append to sheet # Go to new line # Add Average # Append average to list # Create Sheet with Results # Create Sheet with Results # System Info # Accurate Results # Add Averages to sheet # Approximate Results # Add Averages to sheet