def stop(points_old, config, is_all, pids, force): # @todo: distinguere fra processi 'data' e 'test' # si fa con un argomento config che viene da args.is_data from os import environ, system, popen import pickle from lib.utils import find_running, point_dir, config_dir points_run, sim_info = find_running() points_run = [x[0] for x in points_run if x[1] == config] # if config or not is_all: # if not config: # config = 'test' # points_run = [x[0] for x in points_run if x[1] == config] # else: # points_run = [x[0] for x in points_run] running_pids = [ps_i[2] for ps_i in sim_info] if pids: for pid in pids: if pid in running_pids or force: ps_out = popen('ps -fu ' + environ['USER']).read().split('\n') for line in ps_out[1:-1]: infos = line.split() ps_pid = int(infos[1]) ps_ppid = int(infos[2]) if ps_pid == pid or ps_ppid == pid: if ps_ppid != 1: system('kill ' + str(ps_ppid)) system('kill ' + str(ps_pid)) else: print(f'Invalid PID: {pid}, ' + 'there is no running simulation with that one.') return try: with open(config_dir(config) + '/pstop.pickle', 'rb') as stop_file: points_stopped = pickle.load(stop_file) except FileNotFoundError: points_stopped = [] l_aux = [] for Point in points_stopped: if Point in points_run: l_aux += [Point] points_stopped = l_aux points_stopping = [] points_notstop = [] for Point in points_old: if Point in points_run and Point not in points_stopped: points_stopped += [Point] points_stopping += [Point] from os import system from time import time from datetime import datetime sf = '%d-%m-%Y %H:%M:%S' system('echo ' + datetime.fromtimestamp(time()).strftime(sf) + ' > ' + config_dir(config) + '/' + point_dir(Point) + '/stop') # forse '.stop' anzichè 'stop' else: points_notstop += [Point] with open(config_dir(config) + '/pstop.pickle', 'wb') as stop_file: pickle.dump(points_stopped, stop_file) if len(points_stopping) > 0: print("Simulations for following (λ, β) just stopped: ", points_stopping) if len(points_notstop) > 0 and not is_all: print("Simulations for following (λ, β) were not running: ", points_notstop)
def show_state(configs, full_show=False): # @todo: add support for the other platforms # @todo: for clusters: add 'pending' state import pickle import json from os import environ, popen from platform import node from datetime import datetime from time import time from lib.utils import find_running, point_dir, config_dir from lib.platforms import get_ps_out if not type(configs) == list: configs = [configs] # if node() == 'Paperopoli' or node() == 'fis-delia.unipi.it': ps_out = get_ps_out() # else: # ps_out = [] # print("This platform is still not supported") # return empty = len(['' for line in ps_out if ' bin/CDT_2D-Lambda' in line]) == 0 if len(ps_out) > 1 and not empty: print(' LAMBDA-λ BETA-β TIME STATUS CONFIG', end='') if full_show == '0': print() elif full_show == '1': print(' RUN_ID PID PPID') elif full_show == '2': print(' LIN_HIST START DISK US.') points_run_all, sim_all = find_running() configs += ['-'] d = {} for config in configs: points_run_list = [] sim_list = [] for i in range(len(sim_all)): try: if points_run_all[i][1] == config: points_run_list += [points_run_all[i]] sim_list += [sim_all[i]] except IndexError: # It's possible that find_running can't retrieve the config # for some simulations, these will be manged after pass d[config] = points_run_list, sim_list # for i in range(len(sim_all)): # if len(points_run_all[i]) == 1: # points_run_list += [points_run_all[i] + ['-']] # sim_list += [sim_all[i]] # d['-'] = points_run_list, sim_list else: print("There are no running simulations currently.") return for config in configs: try: if config == '-': raise FileNotFoundError with open(config_dir(config) + '/pstop.pickle', 'rb') as stop_file: points_stopped = pickle.load(stop_file) except FileNotFoundError: points_stopped = [] if len(ps_out) > 1 and not empty: points_run_list, sim_list = d[config] for i in range(0, len(sim_list)): points_run = points_run_list[i] sim = sim_list[i] Point = points_run[0] l_conf = points_run[1] if Point in points_stopped: state = 'killed' else: state = 'running' print(r_f(str(Point[0]), 9), r_f(str(Point[1]), 9), sim[0].rjust(11), ' ', state.ljust(10), l_conf.ljust(11), end='') if full_show == '0': print() elif full_show == '1': print(sim[1].rjust(4), ' ', sim[2].ljust(8), sim[3].ljust(8)) elif full_show == '2': if config != '-': Point_dir = config_dir(config) + '/' + \ point_dir(Point) + '/' with open(Point_dir + 'state.json', 'r') as state_file: state = json.load(state_file) lin_hist = state['linear-history'] start = state['start_time'] disk_us = popen('du -hs ' + Point_dir).read().split()[0] else: lin_hist = '-' start = '-' disk_us = '-' print( str(lin_hist).ljust(9), start[-8:].ljust(10), disk_us.rjust(8)) points_run = [x[0] for x in points_run_list if x[1] == config] l_aux = [] for Point in points_stopped: if Point in points_run: l_aux += [Point] points_stopped = l_aux else: points_stopped = [] if config != '-': with open(config_dir(config) + '/pstop.pickle', 'wb') as stop_file: pickle.dump(points_stopped, stop_file) if len(ps_out) > 1 and not empty: clock = datetime.fromtimestamp(time()).strftime('%H:%M:%S') print('\n [CLOCK: ' + clock + ']')
def plot(lambdas_old, config): from matplotlib.pyplot import figure, show from numpy import loadtxt from lib.utils import find_running lambdas_run, _ = find_running() lambdas_run = [x[0] for x in lambdas_run if x[1] == config] color_cycle = [ 'xkcd:carmine', 'xkcd:teal', 'xkcd:peach', 'xkcd:mustard', 'xkcd:cerulean' ] n_col = len(color_cycle) canvases = [] props = [] i = -1 for Lambda in lambdas_old: i += 1 vol_file = ('output/' + config + '/Lambda' + str(Lambda) + '/history/volumes.txt') indices, volumes = loadtxt(vol_file, unpack=True) fig = figure() ax = fig.add_subplot(111) props += [{ 'fig': fig, 'ax': ax, 'skip': len(volumes), 'vol_file': vol_file }] canvases += [fig.canvas] fig.set_size_inches(7, 5) ax.plot(indices, volumes, color=color_cycle[i % n_col]) if Lambda in lambdas_run: run_t = ', running' else: run_t = '' ax.set_title('λ = ' + str(Lambda) + run_t) def on_key(event): i = canvases.index(event.canvas) p = props[i] if event.key == 'f5': try: ind_aux, vol_aux = loadtxt(p['vol_file'], unpack=True, skiprows=p['skip']) except ValueError: ind_aux = [] vol_aux = [] try: if len(vol_aux) > 10: props[i]['skip'] += len(vol_aux) p['ax'].plot(ind_aux, vol_aux, color=color_cycle[i % n_col]) p['fig'].canvas.draw() except TypeError: pass fig.canvas.mpl_connect('key_press_event', on_key) show()
def launch(lambdas_old, lambdas_new, config, linear_history, time, steps, force, time_length): """Output analysis for CDT_2D simulation. attempts_str = str(attempts) Descrizione... Parameters ---------- p1 : tipo descrizione del primo parametro p1 p2 : tipo descrizione del secondo parametro p2 p3 : tipo, optional descrizione del terzo parametro opzionale p3 Returns ------- tipo descrizione del tipo di ritorno Raises ------ Exception descrizione dell'eccezione lanciata """ from os import mkdir, chdir, system, getcwd, scandir from shutil import copyfile from re import split from platform import node import json from subprocess import Popen from lib.utils import find_running from lib.utils import authorization_request lambdas_run, sim_info = find_running() lambdas_run = [x[0] for x in lambdas_run if x[1] == config] lambdas_old_auth = [] lambdas_req_run = [] for Lambda in lambdas_old: if Lambda not in lambdas_run: if not config == 'test' and not force: what_to_do = "to rerun simulation" authorized = authorization_request(what_to_do, Lambda) else: authorized = True if authorized: lambdas_old_auth += [Lambda] else: lambdas_req_run += [Lambda] lambdas = lambdas_old_auth + lambdas_new if len(lambdas_new) > 0: print("New simulations will be launched for following λ: ", lambdas_new) if len(lambdas_old_auth) > 0: print("Old simulations will be rerunned for following λ: ", lambdas_old_auth) if len(lambdas_req_run) > 0: print("Simulations for following λ were already running: ", lambdas_req_run) if len(lambdas) > 0: print() project_folder = getcwd() for Lambda in lambdas: chdir(project_folder + '/output/' + config) dir_name = "Lambda" + str(Lambda) launch_script_name = 'launch_' + str(Lambda) + '.py' make_script_name = 'make_' + str(Lambda) + '.py' if Lambda in lambdas_old: with open(dir_name + "/state.json", "r+") as state_file: state = json.load(state_file) if state['is_thermalized']: print('(λ = ' + str(Lambda) + ') Ha già finito idiota!') # @todo da migliorare continue if state['last_run_succesful']: run_num = state['run_done'] + 1 else: print('(λ = ' + str(Lambda) + ') Problem in the last run') continue checkpoints = [x.name for x in scandir(dir_name + "/checkpoint") \ if (split('_|\.|run', x.name)[1] == str(run_num - 1) and x.name[-4:] != '.tmp')] # nell'ordinamento devo sostituire i '.' con le '~', o in generale # un carattere che venga dopo '_', altrimenti 'run1.1_...' viene # prima di 'run1_...' checkpoints.sort(key=lambda s: s.replace('.', '~')) last_check = checkpoints[-1] else: mkdir(dir_name) mkdir(dir_name + "/checkpoint") mkdir(dir_name + "/history") mkdir(dir_name + "/bin") copyfile('../../lib/launch_script.py', dir_name + '/' + launch_script_name) copyfile('../../lib/make_script.py', dir_name + '/' + make_script_name) run_num = 1 last_check = None # devo farlo qui perché prima non sono sicuro che dir_name esista # ('mkdir(dir_name)') chdir(dir_name) if run_num > 1: from lib.tools import recovery_history recovery_history() # ricongiungo le due variabili perché è ancora facile # distinguerle dall'ultimo carattere if steps != '0': end_condition = steps else: end_condition = time if type(time_length) == list: time_length = time_length[0] debug_flag = 'false' arguments = [ run_num, Lambda, time_length, end_condition, debug_flag, last_check, linear_history ] arg_str = '' for x in arguments: arg_str += ' ' + str(x) if (node() == 'Paperopoli'): make_script = Popen( ["python3", make_script_name, str(run_num), str(Lambda)]) make_script.wait() system('nohup python3 $PWD/' + launch_script_name + arg_str + ' &') elif (node() == 'fis-delia.unipi.it'): make_script = Popen( ["python36", make_script_name, str(run_num), str(Lambda)]) make_script.wait() system('nohup python36 $PWD/' + launch_script_name + arg_str + ' &') elif (node() == 'gridui3.pi.infn.it'): print('support for grid still missing') # make_script = Popen(["python3", make_script_name, str(run_num), # str(Lambda)]) # make_script.wait() # system('bsub -q local -o stdout.txt -e stderr.txt -J ' + \ # dir_name + ' $PWD/' + launch_script_name + arg_str) elif (node()[0:4] == 'r000'): print('support for marconi still missing') else: raise NameError('Node not recognized (known nodes in data.py)')
def show_state(configs, full_show=False): # @todo: add support for the other platforms # @todo: for clusters: add 'pending' state import pickle, json from os import environ, popen from platform import node from datetime import datetime from time import time from lib.utils import find_running if not type(configs) == list: configs = [configs] if node() == 'Paperopoli' or node() == 'fis-delia.unipi.it': ps_out = popen('ps -fu ' + environ['USER']).read().split('\n') else: ps_out = [] print("This platform is still not supported") empty = len([1 for line in ps_out if 'CDT_2D-Lambda' in line]) == 0 if len(ps_out) > 1 and not empty: print(' LAMBDA TIME STATUS CONFIG', end='') if full_show == '0': print() elif full_show == '1': print(' RUN_ID PID PPID') elif full_show == '2': print(' LIN_HIST START DISK US.') lambdas_run_all, sim_all = find_running() d = {} for config in configs: lambdas_run_list = [] sim_list = [] for i in range(0, len(sim_all)): if lambdas_run_all[i][1] == config: lambdas_run_list += [lambdas_run_all[i]] sim_list += [sim_all[i]] d[config] = lambdas_run_list, sim_list else: print("There are no running simulations currently.") for config in configs: try: with open('output/' + config + '/pstop.pickle', 'rb') as stop_file: lambdas_stopped = pickle.load(stop_file) except FileNotFoundError: lambdas_stopped = [] if len(ps_out) > 1 and not empty: lambdas_run_list, sim_list = d[config] for i in range(0, len(sim_list)): lambdas_run = lambdas_run_list[i] sim = sim_list[i] Lambda = lambdas_run[0] l_conf = lambdas_run[1] if Lambda in lambdas_stopped: state = 'killed' else: state = 'running' print(str(Lambda).rjust(9), sim[0].rjust(11), ' ', state.ljust(10), l_conf.ljust(11), end='') if full_show == '0': print() elif full_show == '1': print(sim[1].rjust(4), ' ', sim[2].ljust(8), sim[3].ljust(8)) elif full_show == '2': Lambda_dir = 'output/' + config + '/Lambda' + \ str(Lambda) + '/' with open(Lambda_dir + 'state.json', 'r') as state_file: state = json.load(state_file) lin_hist = state['linear-history'] start = state['start_time'] disk_us = popen('du -hs ' + Lambda_dir).read().split()[0] print( str(lin_hist).ljust(9), start[-8:].ljust(10), disk_us.rjust(8)) lambdas_run = [x[0] for x in lambdas_run_list if x[1] == config] l_aux = [] for Lambda in lambdas_stopped: if Lambda in lambdas_run: l_aux += [Lambda] lambdas_stopped = l_aux else: lambdas_stopped = [] with open('output/' + config + '/pstop.pickle', 'wb') as stop_file: pickle.dump(lambdas_stopped, stop_file) if len(ps_out) > 1 and not empty: clock = datetime.fromtimestamp(time()).strftime('%H:%M:%S') print('\n [CLOCK: ' + clock + ']')