Skip to content
Snippets Groups Projects
Commit 591d1222 authored by mads's avatar mads
Browse files

some changes

parent dd1bd353
Branches
Tags
No related merge requests found
......@@ -171,7 +171,7 @@ class HTCLine(HTCContents):
("", "\t" + self.str_values())[bool(self.values)],
("", "\t" + self.comments)[bool(self.comments.strip())])
def str_values(self):
return " ".join([str(v) for v in self.values])
return " ".join([str(v).lower() for v in self.values])
def __getitem__(self, key):
return self.values[key]
......@@ -351,7 +351,10 @@ class HTCDefaults(object):
else:
mann.add_line('create_turb_parameters', [L, ae23, Gamma, seed, int(high_frq_compensation)], "L, alfaeps, gamma, seed, highfrq compensation")
if filenames is None:
filenames = ["./turb/turb_wsp%d_s%04d%s.bin" % (self.wind.wsp[0], seed, c) for c in ['u', 'v', 'w']]
fmt = "l%.1f_ae%.2f_g%.1f_h%d_%dx%dx%d_%.3fx%.2fx%.2f_s%04d%c.turb"
import numpy as np
dxyz = tuple(np.array(box_dimension) / no_grid_points)
filenames = ["./turb/" + fmt % ((L, ae23, Gamma, high_frq_compensation) + no_grid_points + dxyz + (seed, uvw)) for uvw in ['u', 'v', 'w']]
if isinstance(filenames, str):
filenames = ["./turb/%s_s%04d%s.bin" % (filenames, seed, c) for c in ['u', 'v', 'w']]
for filename, c in zip(filenames, ['u', 'v', 'w']):
......
......@@ -24,6 +24,14 @@ from copy import copy
class HTCFile(HTCContents, HTCDefaults):
"""Wrapper for HTC files
Examples:
---------
>>> htcfile = HTCFile('htc/test.htc')
>>> htcfile.wind.wsp = 10
>>> htcfile.save()
"""
filename = None
htc_inputfiles = []
......@@ -95,13 +103,16 @@ class HTCFile(HTCContents, HTCDefaults):
with open(filename, 'w', encoding='utf-8') as fid:
fid.write(str(self))
def set_name(self, name, folder="htc"):
def set_name(self, name, folder="htc/"):
if os.path.isabs(folder) is False and os.path.relpath(folder).startswith("htc" + os.path.sep):
folder = "./" + os.path.relpath(folder).replace("\\", "/")
self.filename = os.path.join(self.modelpath, folder, "%s.htc" % name).replace("\\", "/")
if 'simulation' in self and 'logfile' in self.simulation:
self.simulation.logfile = "./log/%s.log" % name
self.simulation.logfile = os.path.join(folder.replace("htc", "log", 1), "%s.log" % name).replace("\\", "/")
elif 'test_structure' in self and 'logfile' in self.test_structure:
self.test_structure.logfile = "./log/%s.log" % name
self.output.filename = "./res/%s" % name
self.test_structure.logfile = os.path.join(folder.replace("htc", "log", 1), "%s.log" % name).replace("\\", "/")
self.output.filename = os.path.join(folder.replace("htc", "res", 1), "%s" % name).replace("\\", "/")
def input_files(self):
files = self.htc_inputfiles
......
......@@ -181,4 +181,4 @@ class LogInfo(LogFile):
self.errors = []
def update_status(self):
pass
\ No newline at end of file
pass
This diff is collapsed.
......@@ -3,16 +3,22 @@ Created on 04/04/2016
@author: MMPE
'''
from wetb.utils.cluster_tools.ssh_client import SSHClient
from wetb.utils.cluster_tools import pbswrap
import multiprocessing
import threading
import psutil
from wetb.utils.cluster_tools import pbswrap
from wetb.utils.cluster_tools.ssh_client import SSHClient
class Resource(object):
def __init__(self, min_cpu, min_free):
self.min_cpu = min_cpu
self.min_free = min_free
self.acquired = 0
self.lock = threading.Lock()
def ok2submit(self):
"""Always ok to have min_cpu cpus and ok to have more if there are min_free free cpus"""
......@@ -25,6 +31,13 @@ class Resource(object):
else:
return False
def acquire(self):
with self.lock:
self.acquired += 1
def release(self):
with self.lock:
self.acquired -= 1
......@@ -32,35 +45,52 @@ class SSHPBSClusterResource(Resource, SSHClient):
def __init__(self, host, username, password, port, min_cpu, min_free):
Resource.__init__(self, min_cpu, min_free)
SSHClient.__init__(self, host, username, password, port=port)
self.lock = threading.Lock()
def new_ssh_connection(self):
return SSHClient(self.host, self.username, self.password, self.port)
def check_resources(self):
with self:
_, output, _ = self.execute('pbsnodes -l all')
pbsnodes, nodes = pbswrap.parse_pbsnode_lall(output.split("\n"))
with self.lock:
try:
with self:
_, output, _ = self.execute('pbsnodes -l all')
pbsnodes, nodes = pbswrap.parse_pbsnode_lall(output.split("\n"))
_, output, _ = self.execute('qstat -n1')
users, host, nodesload = pbswrap.parse_qstat_n1(output.split("\n"))
_, output, _ = self.execute('qstat -n1')
users, host, nodesload = pbswrap.parse_qstat_n1(output.split("\n"))
# if the user does not have any jobs, this will not exist
try:
cpu_user = users[self.username]['cpus']
cpu_user += users[self.username]['Q']
except KeyError:
cpu_user = 0
cpu_user = max(cpu_user, self.acquired)
cpu_free, nodeSum = pbswrap.count_cpus(users, host, pbsnodes)
return nodeSum['used_cpu'] + cpu_free, cpu_free, cpu_user
except IOError as e:
raise e
except:
raise EnvironmentError("check resources failed")
# if the user does not have any jobs, this will not exist
try:
cpu_user = users[self.username]['cpus']
cpu_user += users[self.username]['Q']
except KeyError:
cpu_user = 0
cpu_free, nodeSum = pbswrap.count_cpus(users, host, pbsnodes)
def jobids(self, jobname_prefix):
_, output, _ = self.execute('qstat -u %s' % self.username)
return [l.split()[0].split(".")[0] for l in output.split("\n")[5:] if l.strip() != "" and l.split()[3].startswith("h2l")]
return nodeSum['used_cpu'] + cpu_free, cpu_free, cpu_user
def stop_pbsjobs(self, jobids):
if not hasattr(jobids, "len"):
jobids = list(jobids)
self.execute("qdel %s" % (" ".join(jobids)))
class LocalResource(Resource):
def __init__(self, process_name):
N = max(1, multiprocessing.cpu_count() / 4)
Resource.__init__(self, N, N)
N = max(1, multiprocessing.cpu_count() / 2)
Resource.__init__(self, N, multiprocessing.cpu_count())
self.process_name = process_name
self.host = 'Localhost'
......@@ -68,10 +98,10 @@ class LocalResource(Resource):
def name(i):
try:
return psutil.Process(i).name
except psutil._error.AccessDenied:
except (psutil._error.AccessDenied, psutil._error.NoSuchProcess):
return ""
no_cpu = multiprocessing.cpu_count()
cpu_free = (1 - psutil.cpu_percent(.5) / 100) * no_cpu
cpu_free = no_cpu - self.acquired #(1 - psutil.cpu_percent(.5) / 100) * no_cpu
no_current_process = len([i for i in psutil.get_pid_list() if name(i).lower().startswith(self.process_name.lower())])
return no_cpu, cpu_free, no_current_process
......@@ -13,7 +13,7 @@ class SSHClient(object):
"A wrapper of paramiko.SSHClient"
TIMEOUT = 4
def __init__(self, host, username, password, port=22, key=None, passphrase=None):
def __init__(self, host, username, password=None, port=22, key=None, passphrase=None):
self.host = host
self.username = username
self.password = password
......@@ -33,6 +33,8 @@ class SSHClient(object):
self.connect()
def connect(self):
if self.password is None:
raise IOError("Password not set")
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(self.host, self.port, username=self.username, password=self.password, pkey=self.key, timeout=self.TIMEOUT)
......@@ -136,7 +138,8 @@ class SSHClient(object):
_, out, _ = self.execute(r'find %s -maxdepth 1 -type f -name "%s"' % (cwd, filepattern))
files = []
for file in out.strip().split("\n"):
files.append(file.strip())
if file != "":
files.append(file.strip())
return files
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment