1 #!/usr/bin/env mpipython
4 # gpiv_series - Processes a set of numbered input data
6 # Copyright (C) 2008 Gerber van der Graaf
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #--------------------------------------------------------------------
26 # This version is MPI enabled for parallel processing
29 from Scientific
import MPI
30 communicator
= MPI
.world
.duplicate()
33 #----------- Command line arguments parser
35 from optparse
import OptionParser
37 usage
= "%prog [options] \"process\""
38 parser
= OptionParser(usage
)
39 parser
.add_option("-a", "--arg_n",
40 action
="store_true", dest
="arg_n", default
=False,
41 help="if the process needs the current number in its \
42 argument list instead of prepending/appending it to the \
43 filebase name, the number will be put before (-f) \
44 \"filename\" in the \"process\" string.")
45 parser
.add_option("-b", "--basename", type='string', dest
="basename",
46 help="File basename for reading", metavar
="FILE")
47 parser
.add_option("-e", "--ext", type='string', dest
="ext", metavar
="EXT",
48 help="add an extension after the file basename + number (without leading \".\")")
49 parser
.add_option("-f", "--first", type='int', dest
="first_nr", default
=0,
50 help="first numbered file (default: 0)", metavar
="N")
51 parser
.add_option("-l", "--last", type='int', dest
="last_nr", default
=0,
52 help="last numbered file(default: 0)", metavar
="N")
53 parser
.add_option("-i", "--incr", type='int', dest
="incr_nr", default
=1,
54 help="increment file number (default: 1)", metavar
="N")
55 parser
.add_option("-p", "--print",
56 action
="store_true", dest
="pri", default
=False,
57 help="prints process parameters/variables to stdout")
58 parser
.add_option("--pad", type='int', dest
="pad0", default
=0,
59 help="padding number with zero's (default: 0)", metavar
="N")
60 parser
.add_option("-n", "--none",
61 action
="store_true", dest
="none", default
=False,
62 help="suppresses real execution")
63 parser
.add_option("-x", "--prefix", action
="store_true", dest
="prefix", default
=False,
64 help="prefix numbering to file basename")
66 (options
, args
) = parser
.parse_args()
68 parser
.error("incorrect number of arguments")
74 #----------- Function definitions
76 def pri_date(msg
= "Time stamp at start of series processing:"):
80 msg -- message to be printed before time stamp
82 if options
.pri
== True:
92 """Counts number of digits from a number
95 nr -- number to be questioned
105 """Created a string for zero padding
108 nr -- number of zeros to be padded
111 for i
in range(0, nr
):
117 def compose_name_nr(nr
):
118 """Creates proper name from basename and number.
121 nr -- number of filename to be processed
124 ndig
=count_digits(nr
)
125 null_str
=pad0(options
.pad0
- ndig
)
126 nr_str
=null_str
+str(nr
)
132 name
=str(options
.basename
)
134 name
=nr_str
+str(options
.basename
)
137 name
=str(options
.basename
)
139 name
=str(options
.basename
)+nr_str
141 if str(options
.ext
) != "None":
142 name
=str(name
)+str(".")+str(options
.ext
)
147 def compose_cmd(name
, nr
):
148 """Creates proper command.
151 name -- complete filename
155 # Eventually, substitutes "-f" with: "nr -f"
156 command_tmp
=re
.sub("-f", str(nr
)+" -f", command
)
157 if command_tmp
!= command
:
158 command
= str(command_tmp
)+" "+str(name
)
160 command
= str(command_tmp
)+" "+str(nr
)+" "+str(name
)
162 command
=str(command
)+" "+str(name
)
167 def proc_series_par():
168 """Processes a series on identic numbered files in parallel
169 environment using mpipython.
173 # Total number of data and number of data per node
175 Nt
= (options
.last_nr
+1 - options
.first_nr
) / options
.incr_nr
176 Nn
= Nt
/ communicator
.size
179 # Bail out if number of nodes is larger than number of data
181 if communicator
.size
> Nt
:
182 print 'nprocs (',communicator
.size
,\
183 ') larger then number of data (',Nt
,")"
187 for i
in range(options
.first_nr
, options
.last_nr
+1, options
.incr_nr
):
188 for j
in range(0, communicator
.size
, 1):
189 if communicator
.rank
== j
:
190 if i
>= options
.first_nr
+ j
*Nn
*options
.incr_nr
:
191 if i
< options
.first_nr
+ (j
+1)*Nn
*options
.incr_nr
:
192 name_nr
= compose_name_nr(i
)
193 command
= compose_cmd(name_nr
, i
)
194 if options
.pri
== True: print "rank=",communicator
.rank
,command
195 elif options
.none
== True: print "rank=",communicator
.rank
,command
196 if options
.none
== False: os
.system(command
)
199 # The fraction of data left, if Nn is not an integer,
200 # is processed at the highest node
202 if j
== communicator
.size
- 1:
203 if i
>= options
.first_nr
+ (j
+1)*Nn
*options
.incr_nr
:
204 name_nr
= compose_name_nr(i
)
205 command
= compose_cmd(name_nr
, i
)
206 if options
.pri
== True: print "rank=",communicator
.rank
,command
207 elif options
.none
== True: print "rank=",communicator
.rank
,command
208 if options
.none
== False: os
.system(command
)
212 #----------- Calling functions
214 #if communicator.rank == 0:
215 # if options.pri == True: pri_date()
216 # elif options.none == True: pri_date()
220 #communicator.rank == 0:
221 # if options.pri == True: pri_date(msg = "Time stamp at end of series processing:")
222 # elif options.none == True: pri_date(msg = "Time stamp at end of series processing:")
225 #----------- That's all folks