| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import os |
| 4 |
import stat |
| 5 |
import sys |
| 6 |
import re |
| 7 |
from optparse import OptionParser |
| 8 |
|
| 9 |
from gpaw.utilities.hardware import ComputeCluster |
| 10 |
|
| 11 |
defaults = { 'err' : None, |
| 12 |
'mail' : None, |
| 13 |
'mem' : None, |
| 14 |
'name' : None, |
| 15 |
'depth' : 1, |
| 16 |
'cores' : 2, |
| 17 |
'out' : None, |
| 18 |
'arch' : 'pbs', |
| 19 |
'script': 'run.py', |
| 20 |
'time' : 86400, # one day in seconds |
| 21 |
'wd' : None, |
| 22 |
'queue' : None, |
| 23 |
'smt' : False, |
| 24 |
} |
| 25 |
|
| 26 |
platforms = { 'aix5' : 'loadleveler' } |
| 27 |
|
| 28 |
env = {'GPAW_MAIL' : None, |
| 29 |
'MODULEPATH' : None, |
| 30 |
'LOADEDMODULES' : None, |
| 31 |
} |
| 32 |
|
| 33 |
set = defaults |
| 34 |
|
| 35 |
#...................................................... |
| 36 |
# functions |
| 37 |
|
| 38 |
def s_from_dhms(time): |
| 39 |
"""return seconds from dhms""" |
| 40 |
dhms_s = { 's' : 1, 'm' : 60, 'h' : 3600, 'd' : 86400 } |
| 41 |
time = time.lower() |
| 42 |
word_list = re.findall('\d*[^\d]*',time) |
| 43 |
seconds=0 |
| 44 |
for word in word_list: |
| 45 |
if word != '': |
| 46 |
sec = 1 |
| 47 |
for t in list(dhms_s.keys()): |
| 48 |
nw = word.replace(t,'') |
| 49 |
if nw != word: |
| 50 |
sec = dhms_s[t] |
| 51 |
word = nw |
| 52 |
break |
| 53 |
try: |
| 54 |
seconds += int(word) * sec |
| 55 |
except: |
| 56 |
raise RuntimeError('unknown format in timestring ' + time) |
| 57 |
return seconds |
| 58 |
|
| 59 |
def minutes(secs): |
| 60 |
return int(secs // 60) |
| 61 |
|
| 62 |
def unique_name(name): |
| 63 |
import string |
| 64 |
letters = list(string.letters) |
| 65 |
n = name |
| 66 |
while os.path.exists(n): |
| 67 |
n = name + letters.pop(0) |
| 68 |
return n |
| 69 |
|
| 70 |
# handle command line options |
| 71 |
|
| 72 |
parser = OptionParser(usage='%prog [options] [script ncores]') |
| 73 |
parser.add_option("-m", "--mail", dest='mail', |
| 74 |
help='Where to send an email about starting/ending of the job (def: read from environment variable GPAW_MAIL)') |
| 75 |
parser.add_option("-M", "--Memory", action='count', default=None, |
| 76 |
help='request large memory cores (host specific)') |
| 77 |
parser.add_option("-n", "--name", dest='name', |
| 78 |
help='Name of the job (def: name of parent directory)') |
| 79 |
parser.add_option("-d", "--depth", dest='depth', |
| 80 |
help='depth of directories for naming (def: 1)') |
| 81 |
parser.add_option("-o", "--outfile", dest='outfile', |
| 82 |
help='Name of the output file (def: script.out)') |
| 83 |
parser.add_option("-p", "--parameters", dest='parameters', |
| 84 |
help='Parameters to give to the script (def: empty)') |
| 85 |
parser.add_option("-a", "--arch", dest='arch', |
| 86 |
help='architecture (def: try to guess)') |
| 87 |
parser.add_option("-t", "--time", dest='time', |
| 88 |
help='Time (def: 86400=1140m=24h=1d=one day)') |
| 89 |
parser.add_option("-q", "--queue", dest='queue', |
| 90 |
help='queue to use (host specific)') |
| 91 |
parser.add_option("-s", "--smt", dest='smt', action='count', |
| 92 |
help='Simultaneous Multi-Threading (host specific)') |
| 93 |
opt, args = parser.parse_args() |
| 94 |
##print "opt=",opt |
| 95 |
##print "args=",args |
| 96 |
|
| 97 |
if opt.mail: |
| 98 |
set['mail'] = str(opt.mail) |
| 99 |
|
| 100 |
if opt.Memory is not None: |
| 101 |
set['mem'] = True |
| 102 |
|
| 103 |
if opt.name: |
| 104 |
set['name'] = str(opt.name) |
| 105 |
|
| 106 |
if opt.depth: |
| 107 |
set['depth'] = int(opt.depth) |
| 108 |
|
| 109 |
if opt.outfile: |
| 110 |
set['out'] = str(opt.outfile) |
| 111 |
|
| 112 |
if opt.parameters: |
| 113 |
set['parameters'] = str(opt.parameters) |
| 114 |
|
| 115 |
if opt.time: |
| 116 |
set['time'] = s_from_dhms(opt.time) |
| 117 |
|
| 118 |
if opt.smt: |
| 119 |
set['smt'] = True |
| 120 |
|
| 121 |
if opt.queue: |
| 122 |
set['queue'] = str(opt.queue) |
| 123 |
|
| 124 |
if len(args): |
| 125 |
set['script'] = args[0] |
| 126 |
if len(args) > 1: |
| 127 |
try: |
| 128 |
set['cores'] = int(args[1]) |
| 129 |
except ValueError: |
| 130 |
raise ValueError('Number of cores must be integer. ' + |
| 131 |
'See gpaw-runscript -h') |
| 132 |
|
| 133 |
# ............................................................ |
| 134 |
|
| 135 |
try: |
| 136 |
cc = ComputeCluster(opt.arch) |
| 137 |
except Exception as ex: |
| 138 |
raise |
| 139 |
print(ex.message, end='') |
| 140 |
sys.exit() |
| 141 |
|
| 142 |
print('using', cc.arch) |
| 143 |
|
| 144 |
# ............................................................ |
| 145 |
|
| 146 |
if set['mail'] is None and 'GPAW_MAIL' in os.environ: |
| 147 |
set['mail'] = str(os.environ['GPAW_MAIL']) |
| 148 |
|
| 149 |
parameter_ext = '' |
| 150 |
if 'parameters' in set: |
| 151 |
parameters = set['parameters'] |
| 152 |
for replace in [' ', os.sep]: |
| 153 |
parameters = parameters.replace(replace, '_') |
| 154 |
parameter_ext += '_' + parameters |
| 155 |
|
| 156 |
# set output files |
| 157 |
if set['out'] is None: |
| 158 |
set['out'] = set['script'] + parameter_ext + ".out" |
| 159 |
if set['err'] is None: |
| 160 |
set['err'] = set['script'] + parameter_ext + ".err" |
| 161 |
|
| 162 |
# get the name from current working directory |
| 163 |
if set['wd'] is None: |
| 164 |
set['wd'] = os.getcwd() |
| 165 |
if set['name'] is None: |
| 166 |
nl = os.getcwd().split('/')[-set['depth']:] |
| 167 |
name = nl[0] |
| 168 |
for string in nl[1:]: |
| 169 |
name += '_' + string |
| 170 |
# avoid beginning with a number |
| 171 |
if name[0].isdigit(): |
| 172 |
name = 'j' + name |
| 173 |
set['name'] = name + parameter_ext |
| 174 |
|
| 175 |
print(cc.write(**set), 'written') |
| 176 |
|
| 177 |
|
| 178 |
|