Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
QKDNetJournal/threaded_data.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
202 lines (170 sloc)
5.51 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from data_gathering import embedTNs, print_save_data | |
import concurrent.futures | |
from Simulator import main as sim_main | |
from Simulator import * | |
import logging | |
# import threading | |
import time | |
import math | |
import redirect | |
# import contextlib | |
import sys | |
import os | |
#Default Statistics | |
size = 7 #to embed | |
full_size = 9 | |
decoherence = .02 | |
dec_range = [0,.01,.02,.03,.04,.05,.075,.10] | |
L = 1 | |
P = 10**(-.15*L/10) | |
BSM = .85 | |
num_iter = int(1e6) | |
#TNs | |
TN_labels = ["Central", "OffCenter", "Diagonal", "NotEqual","CentralAndOne"] | |
# NoTN = [0, size*size-1] # A and B | |
Central = [0, int((size*size-1)/2), size*size-1] # A and B and center | |
OffCenter = [0, int((size*size-1)/2) - size-1, size*size-1] # Off Center | |
# Corner = [0, size-1, size*(size-1), size*size-1] # A and corners and B | |
Diagonal = [0, math.floor(size/3)*(size+1),size*size-1-math.floor(size/3)*(size+1), size*size-1] # Diagonal | |
NotDiag = [0, math.floor(size/3)*(size+1)-size-1,size*size-1-math.floor(size/3)*(size+1), size*size-1] # Not Diagonal | |
CentralAndOne = [0, int((size*size-1)/2)-size-1,int((size*size-1)/2), size*size-1] # A and B and center | |
TN_list = [Central, OffCenter, Diagonal, NotDiag,CentralAndOne] | |
TN_list = [embedTNs(size, x) for x in TN_list] | |
# exit(0) | |
# Variable | |
sys.argv.append("N/A") | |
CAD = False | |
Segmenting = False | |
if True: | |
if sys.argv[1] == "0": | |
Global = True | |
Balancing = True | |
Surplus = True | |
#Segmenting = False | |
elif sys.argv[1] == "1": | |
Global = True | |
Balancing = True | |
Surplus = False | |
#Segmenting = True | |
elif sys.argv[1] == "2": | |
Global = True | |
Balancing = False | |
Surplus = True | |
#Segmenting = False | |
elif sys.argv[1] == "3": | |
Global = True | |
Balancing = False | |
Surplus = False | |
#Segmenting = True | |
elif sys.argv[1] == "4": | |
Global = False | |
Balancing = True | |
Surplus = True | |
#Segmenting = False | |
elif sys.argv[1] == "5": | |
Global = False | |
Balancing = True | |
Surplus = False | |
#Segmenting = True | |
elif sys.argv[1] == "6": | |
Global = False | |
Balancing = False | |
Surplus = True | |
#Segmenting = False | |
elif sys.argv[1] == "7": | |
Global = False | |
Balancing = False | |
Surplus = False | |
#Segmenting = True | |
else: | |
print("Default, arg didn't work!") | |
Global = True | |
Balancing = True | |
Surplus = True | |
#Segmenting = True | |
# exit(0) | |
else: | |
print("Default, arg didn't work!") | |
Global = True | |
CAD = True | |
Segmenting = True | |
if (Surplus and not Balancing): | |
print("Usless case, exitting") | |
exit(0) | |
# Balancing = False | |
output_prefix = f'{"Surplus" if Surplus else ""}{"No" if not Balancing else ""}Balancing{"No" if not CAD else ""}CAD{"No" if not Segmenting else ""}Seg{"Local" if not Global else "Global"}Info' | |
def generate_data_CAD_POOL_Dec(): | |
data = {} | |
# for dec_idx in range(len(dec_range)): | |
# threads.append(threading.Thread(target = thread_function, args = (,), kwargs= (name = TN_labels[TN_idx],))) | |
# x = threading.Thread(target=thread_function, kwargs=(name=TN_labels[TN_idx,)) | |
threads = [] | |
tot_points = len(TN_labels*len(dec_range)) | |
# if False and "y" not in input(f"this function will use 10 workers at a time to generate {tot_points} data points, ok? (y/n)"): | |
# print("ok, exiting") | |
# exit(0) | |
thrd_num = 0 | |
with concurrent.futures.ThreadPoolExecutor(max_workers=40) as executor: | |
for TN_idx in range(len(TN_labels)): | |
data[TN_labels[TN_idx]] = {} | |
for dec_idx in range(len(dec_range)): | |
v = dec_range[dec_idx] | |
thrd_num +=1 | |
logging.info(f"Main: Creating thread #{thrd_num}/{tot_points}: {TN_labels[TN_idx]}, dec = {v} starting") | |
future = executor.submit(thread_CADPOOL,thrd_num,tot_points, TN_idx, v) | |
# logging.info(f"Main: created thread {thrd_num}/{tot_points}") | |
threads.append((future,TN_labels[TN_idx], v)) | |
# break | |
# break | |
# break | |
# break | |
for thread in threads: | |
data[thread[1]][thread[2]] = thread[0].result() | |
print("Main completed") | |
return data | |
def thread_CADPOOL(thrd_num,tot_points, TN_idx, dec): | |
logging.info(f"Proc {sys.argv[1]} Thread #{thrd_num}/{tot_points}: {TN_idx} v={dec} started") | |
print(TN_list[TN_idx]) | |
# with nostdout(): | |
filename = TN_labels[TN_idx] | |
try: | |
os.mkdir(f"logs/") | |
except FileExistsError as e: | |
pass | |
try: | |
os.mkdir(f"logs/{output_prefix}/") | |
except FileExistsError as e: | |
pass | |
try: | |
os.mkdir(f"logs/{output_prefix}/{filename}/") | |
except FileExistsError as e: | |
pass | |
with open(f"logs/{output_prefix}/{filename}/v={dec}.log","w") as f: | |
redirect.redirect(f) | |
try: | |
data = sim_main(num_iter, full_size, TN_list[TN_idx], P, BSM, dec, glob = Global, CAD_flag = CAD, balance_flag = Balancing, segment = Segmenting, surplus_flag=Surplus) | |
finally: | |
redirect.stop_redirect() | |
redirect.stop_redirect() | |
logging.info(f"Proc {sys.argv[1]} Thread #{thrd_num}/{tot_points}: {TN_idx} v={dec} completed") | |
return data[0] | |
if __name__ == "__main__": | |
sys.argv.append(1) | |
format = "%(asctime)s: %(message)s" | |
redirect.enable_proxy() | |
logging.basicConfig(format=format, level=logging.INFO, | |
datefmt="%H:%M:%S") | |
# with False: #nostdout(): | |
data = generate_data_CAD_POOL_Dec() | |
data_str = "Data for {} iterations, {} in {} Grid, L = {} km, Q = {}, E = {}, Pz = {}, Global Info = {}, Balancing = {} CAD = {}, Segmenting = {}, Surplus = {}"\ | |
.format(num_iter, size, full_size, L, BSM, "var", .5, Global if Global else "{}, Smart = {}".format(Global, not True), Balancing, CAD, Segmenting, Surplus) | |
print(data_str) | |
print("returned:", data) | |
data_array = [data[key] for key in data] | |
labels = [key for key in data] | |
try: | |
os.mkdir(f"data/") | |
except FileExistsError as e: | |
pass | |
with open(f"data/{output_prefix}.csv", "w") as f: | |
print_save_data(data_array, labels, data_str, "D", f) | |