$treeview $search $mathjax $extrastylesheet
artdaq_mpich_plugin
v1_00_06a
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #!/usr/bin/env python 00002 00003 import sys 00004 import os 00005 00006 USAGE="\ 00007 Usage: %s [options] <num dect and src> <num sink> <qsize> <run#>\n\ 00008 Examples: %s 5 5\n\ 00009 %s 4 4 --nodes=grunt{1-5} --ddnodes=1 # 1 node sending to 4\n\ 00010 %s 4 4 --nodes=grunt{1-5} --ddnodes=4 # 4 nodes sending to 1\n\ 00011 %s 5 5 --nodes=grunt{1-5} --ddnodes=0 # \n\ 00012 %s 5 5 --nodes=grunt{1-5} # same as above\ 00013 "%((os.path.basename(sys.argv[0]),)*6) 00014 00015 00016 00017 00018 ############################################################################### 00019 00020 g_opt={'tlvlmsk':0} 00021 if sys.version_info[0] == 2: exec( 'trc_one=1L' ) 00022 else: exec( 'trc_one=1' ) 00023 00024 00025 def TRACE( lvl, fmt_s, *args ): 00026 import socket,time,os 00027 global g_thisnode 00028 # how is g_opt working w/o "global" declaration? - must default if read 00029 if g_opt['tlvlmsk'] & (trc_one<<lvl): 00030 if g_thisnode == None: g_thisnode=NodeInfo() 00031 fo = open( "%s.%s.trc"%(g_thisnode.hostnames_l[0],os.getenv('RGANG_MACH_ID')), "a+" ) 00032 fd = fo.fileno() 00033 os.write( fd,'%.2f:%s:%s:%d:%s\n'%(time.time(),socket.gethostname(), 00034 g_thisnode.mach_idx,lvl,fmt_s%args)) 00035 fo.close() 00036 # TRACE 00037 00038 00039 ############################################################################### 00040 # General Regular Expression class that allows for: 00041 # xx = Re( re ) 00042 # ... 00043 # if xx.search( line ): 00044 # yy = xx.match_obj.group( x ) 00045 # 00046 00047 class Re: 00048 import re 00049 ## Create an instance of the regular expression 00050 def __init__( self, reg_ex=None,flags=0 ): 00051 ## The compiled version of the regex 00052 if reg_ex: self.compiled = self.re.compile(reg_ex,flags) 00053 ## Match object representing all matches for the regex 00054 self.match_obj=None 00055 ## Find matches for the regular expression 00056 def search(self,arg1,string=None): 00057 if string: self.match_obj = self.re.search(arg1, string) 00058 else: self.match_obj = self.compiled.search( arg1 ) 00059 return self.match_obj 00060 # Re 00061 00062 re_numeric = Re( r"([0-9a-f]+)-((0x{0,1}){0,1}[0-9a-f]+)" ) # the "r" prefix --> use Python's raw string notation 00063 re_1alpha = Re( r"([a-zA-Z])-([a-zA-Z])" ) # the "r" prefix --> use Python's raw string notation 00064 re_hex = Re( r"^[0-9a-f]+" ) 00065 00066 00067 00068 def findall_expands( ss ): 00069 result = []; result_idx = 0; brace_lvl = 0 00070 for cc in ss: 00071 if cc == '{': 00072 brace_lvl = brace_lvl + 1 00073 if brace_lvl == 1: result.append('') 00074 if brace_lvl > 0: result[result_idx] = result[result_idx] + cc 00075 if cc == '}': 00076 brace_lvl = brace_lvl - 1 00077 if brace_lvl == 0: result_idx = result_idx + 1 00078 if brace_lvl != 0: result.pop() 00079 return result 00080 # findall_expands 00081 00082 00083 def numeric_expand( ss_l ): 00084 ret = [] 00085 for sss in ss_l: 00086 # single alpha check 1st so {a-f} is not mistaken for 00087 # integer (not hex) numeric expand 00088 if re_1alpha.search( sss ): 00089 start = re_1alpha.match_obj.group(1) 00090 end = re_1alpha.match_obj.group(2) 00091 end = chr(ord(end)+1) 00092 while start != end: 00093 ret.append( start ) 00094 start = chr(ord(start)+1) 00095 elif re_numeric.search( sss ): 00096 start = re_numeric.match_obj.group(1) 00097 end = re_numeric.match_obj.group(2) 00098 bb = re_numeric.match_obj.group(3) 00099 if bb == None: 00100 for num in range(int(start),eval(end)+1): 00101 ret.append( '%0*d'%(len(start),num) ) 00102 elif bb == '0': 00103 for num in range(eval('0%s'%(start,)),eval(end)+1): 00104 ret.append( '%0*o'%(len(start),num) ) 00105 elif bb == '0x': 00106 for num in range(eval('0x%s'%(start,)),eval(end)+1): 00107 ret.append( '%0*x'%(len(start),num) ) 00108 else: ret.append( sss ) 00109 TRACE( 28, 'numeric_expand returning %s', ret ) 00110 return ret 00111 # numeric_expand 00112 00113 def expand( ss ): 00114 import string 00115 import re 00116 TRACE( 29, 'expand(%s)', ss ) 00117 ssIn = ss 00118 try: 00119 placeholder_idx = 0 00120 expands = findall_expands( ss ) 00121 if not expands: return ss.split(',') 00122 exp_result = [] 00123 for exp in expands: 00124 ss = ss.replace( exp, '<%d>'%(placeholder_idx,), 1 ) 00125 placeholder_idx = placeholder_idx + 1 00126 placeholder_idx = 0 00127 for sss in ss.split(','): 00128 TRACE( 30, 'expand sss=%s of ss=%s', sss, ss ) 00129 place_holders = re.findall( '<[0-9]+>', sss ) 00130 for idx in range(len(place_holders)): 00131 p_holder = '<%d>'%(placeholder_idx+idx,) 00132 expanding = expand( expands[placeholder_idx+idx][1:-1] ) #Recursive call 00133 expanding = numeric_expand( expanding ) 00134 result = [] 00135 for ssss in sss.split(','): 00136 holder_idx = ssss.find(p_holder) 00137 if holder_idx != -1: 00138 pre = ssss[:holder_idx] 00139 post= ssss[holder_idx+len(p_holder):] 00140 for expanded in expanding: 00141 result.append( pre+expanded+post ) 00142 sss = ','.join(result) 00143 exp_result = exp_result + sss.split(',') 00144 placeholder_idx = placeholder_idx + len(place_holders) 00145 except: # any 00146 TRACE( 31, 'except - expand' ) 00147 exc, value, tb = sys.exc_info() 00148 sys.stderr.write('Error expanding node list "%s": %s: %s\n'%(ssIn,exc,value) ) 00149 sys.stderr.write('Prehaps an invalid decimal/octal/hex digit\n' ) 00150 sys.stderr.write('remember: in the \'{seq1-seq2}\' syntax, seq2\n' ) 00151 sys.stderr.write('can begin with \'0x\' to force hex or \'0\' to\n' ) 00152 sys.stderr.write('force octal\n' ) 00153 if g_opt['tlvlmsk']: 00154 for ln in traceback.format_exception( exc, value, tb ): 00155 sys.stderr.write(ln) 00156 sys.exit(1) 00157 00158 return exp_result 00159 # expand, numeric_expand, findall_expands 00160 00161 00162 def build_quoted_str( args ): 00163 import string # join 00164 quoted_args=[] 00165 for arg in args: 00166 if repr(arg)[0] == "'": quoted_args.append( "'%s'"%(arg,) ) 00167 else: quoted_args.append( '"%s"'%(arg,) ) 00168 return ' '.join( quoted_args ) 00169 # build_quoted_str 00170 00171 00172 00173 def main(): 00174 import getopt 00175 art_args='' 00176 if sys.argv.count('--'): 00177 art_args = build_quoted_str( sys.argv[sys.argv.index('--' ) + 1:] ) 00178 pass 00179 long_opt_spec=["help","ddnodes=","nodes="] 00180 try: 00181 opts, args = getopt.gnu_getopt(sys.argv[1:], "h", long_opt_spec) 00182 except getopt.GetoptError, err: 00183 # print help information and exit: 00184 print str(err) # will print something like "option -a not recognized" 00185 print(USAGE) 00186 sys.exit(2) 00187 ddnodes=0 00188 nodes=[] 00189 for o, a in opts: 00190 if o in ("-h", "--help"): 00191 usage() 00192 sys.exit() 00193 elif o in ("--ddnodes",): 00194 ddnodes = int(a,0) 00195 elif o in ("--nodes",): 00196 nodes = expand(a) 00197 else: 00198 assert False, "unhandled option" 00199 00200 00201 if len(args) < 4: print(USAGE); sys.exit() 00202 00203 num_det = int( args[0],0 ) 00204 num_sink = int( args[1],0 ) 00205 00206 builder=os.popen("which builder 2>/dev/null").readline() 00207 if not builder: 00208 print("Error: builder executable not found.") 00209 sys.exit(3) 00210 pass 00211 00212 print("opts=%s args=%s nodes=%s"%(opts,args,nodes)) 00213 rc = 0 00214 if nodes: 00215 if ddnodes >= len(nodes): 00216 print("invalid configuration. ddnodes must be < nodes") 00217 sys.exit(4) 00218 pass 00219 # create a nodes file... The number of lines should be == num_det+num_src+num_sink 00220 00221 nodes_file="/tmp/nodes%s.txt"%(os.getpid(),) 00222 fo=open(nodes_file,'w') 00223 00224 # 1st set of ranks is for "detector" nodes 00225 node_idx=0 00226 for xx in range(num_det): 00227 fo.write( "%s\n"%(nodes[node_idx],) ); node_idx += 1 00228 if node_idx == len(nodes) or node_idx == ddnodes: node_idx=0 00229 pass 00230 if ddnodes: nodes=nodes[ddnodes:] 00231 00232 node_idx=0 # dect and src get paired up 00233 00234 # last set of ranks is for "sink" nodes 00235 for xx in range(num_sink): 00236 fo.write( "%s\n"%(nodes[node_idx],) ); node_idx += 1 00237 if node_idx == len(nodes): node_idx=0 00238 pass 00239 00240 fo.close() 00241 00242 os.system("cat %s"%(nodes_file,)) 00243 #os.remove( nodes_file ) 00244 00245 cmd ="mpirun_rsh -rsh -hostfile %s "%(nodes_file,) 00246 cmd+="-n %d "%(num_det+num_sink,) 00247 cmd+=" FHICL_FILE_PATH=\"$FHICL_FILE_PATH\" " 00248 cmd+=builder[:-1] 00249 if art_args: cmd+=" %s"%(art_args,) 00250 print( "executing cmd: %s"%(cmd,) ) 00251 os.system(cmd) 00252 else: 00253 cmd ="mpirun -n %d "%(num_det+num_sink,) 00254 cmd+=builder[:-1] 00255 if art_args: cmd+=" %s"%(art_args,) 00256 print( "executing cmd: %s"%(cmd,) ) 00257 rc = (os.system( cmd ) >> 8) 00258 print "return status of %s is: %d\n" %(cmd, rc) 00259 00260 print "return status is (really!) : %d\n" %(rc,) 00261 sys.exit(rc) 00262 00263 00264 if __name__ == "__main__": 00265 main()