File: series_mpi.py

package info (click to toggle)
gpivtools 0.6.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,472 kB
  • ctags: 528
  • sloc: ansic: 6,804; sh: 5,105; perl: 1,035; makefile: 299; python: 207
file content (226 lines) | stat: -rw-r--r-- 6,580 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env mpipython


#   gpiv_series - Processes a set of numbered input data

#   Copyright (C) 2008 Gerber van der Graaf

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  


#--------------------------------------------------------------------

#
# This version is MPI enabled for parallel processing
#
import os, re
from Scientific import MPI
communicator = MPI.world.duplicate()

#
#----------- Command line arguments parser
#
from optparse import OptionParser

usage = "%prog [options] \"process\""
parser = OptionParser(usage)
parser.add_option("-a", "--arg_n",
		  action="store_true", dest="arg_n", default=False,
                  help="if the process needs the current number in its \
		  argument list instead of prepending/appending it to the \
		  filebase name, the number will be put before (-f) \
		  \"filename\" in the \"process\" string.")
parser.add_option("-b", "--basename", type='string', dest="basename",
                  help="File basename for reading", metavar="FILE")
parser.add_option("-e", "--ext", type='string', dest="ext", metavar="EXT",
                  help="add an extension after the file basename + number (without leading \".\")")
parser.add_option("-f", "--first", type='int', dest="first_nr", default=0,
                  help="first numbered file (default: 0)", metavar="N")
parser.add_option("-l", "--last", type='int', dest="last_nr", default=0,
                  help="last numbered file(default: 0)", metavar="N")
parser.add_option("-i", "--incr", type='int', dest="incr_nr", default=1,
                  help="increment file number (default: 1)", metavar="N")
parser.add_option("-p", "--print",
		  action="store_true", dest="pri", default=False,
                  help="prints process parameters/variables to stdout")
parser.add_option("--pad", type='int', dest="pad0", default=0,
                  help="padding number with zero's (default: 0)", metavar="N")
parser.add_option("-n", "--none",
		  action="store_true", dest="none", default=False,
		  help="suppresses real execution")
parser.add_option("-x", "--prefix", action="store_true", dest="prefix", default=False,
                  help="prefix numbering to file basename")

(options, args) = parser.parse_args()
if len(args) != 1:
        parser.error("incorrect number of arguments")
else:
	process = args[0]


#
#----------- Function definitions
#
def pri_date(msg = "Time stamp at start of series processing:"):
	"""Prints time stamp.

	Keyword arguments:
	msg -- message to be printed before time stamp
	"""
	if options.pri == True:
		print msg
		os.system('date')
	elif options.none:
		print msg
		os.system('date')



def count_digits(nr):
	"""Counts number of digits from a number
	
	Keyword arguments:
	nr -- number to be questioned
	"""
	count=0
	while nr/10 !=0:
		nr=nr/10
		count=count+1
	return count


def pad0(nr):
	"""Created a string for zero padding
	
	Keyword arguments:
	nr -- number of zeros to be padded
	"""
	pd0=""
	for i in range(0, nr):
		pd0 = str(pd0)+"0"

	return pd0


def compose_name_nr(nr):
	"""Creates proper name from basename and number.

	Keyword arguments:
	nr -- number of filename to be processed
	"""
	if options.pad0 > 0:
		ndig=count_digits(nr)
		null_str=pad0(options.pad0 - ndig)
		nr_str=null_str+str(nr)
	else:
		nr_str=str(nr)
		
	if options.prefix:
		if options.arg_n:
			name=str(options.basename)
		else:
			name=nr_str+str(options.basename)
	else:
		if options.arg_n:
			name=str(options.basename)
		else:
			name=str(options.basename)+nr_str
			
	if str(options.ext) != "None":
		name=str(name)+str(".")+str(options.ext)	

	return(name)


def compose_cmd(name, nr):
	"""Creates proper command.

	Keyword arguments:
	name -- complete filename
	"""
	command=str(process)
	if options.arg_n:
		# Eventually, substitutes "-f" with: "nr -f"
		command_tmp=re.sub("-f", str(nr)+" -f", command)
		if command_tmp != command:
			command = str(command_tmp)+" "+str(name)
		else:
			command = str(command_tmp)+" "+str(nr)+" "+str(name)
	else:
		command=str(command)+" "+str(name)

	return command


def proc_series_par():
	"""Processes a series on identic numbered files in parallel
	environment using mpipython.

	"""
	#
	# Total number of data and number of data per node
	#
	Nt = (options.last_nr+1 - options.first_nr) / options.incr_nr
	Nn = Nt / communicator.size

	#
	# Bail out if number of nodes is larger than number of data
	#
	if communicator.size > Nt:
		print 'nprocs (',communicator.size,\
		      ') larger then number of data (',Nt,")"
		exit()
		

       	for i in range(options.first_nr, options.last_nr+1, options.incr_nr):
		for j in range(0, communicator.size, 1):
			if communicator.rank == j:
				if i >= options.first_nr + j*Nn*options.incr_nr:
					if i < options.first_nr + (j+1)*Nn*options.incr_nr:
						name_nr = compose_name_nr(i)
						command = compose_cmd(name_nr, i)
						if options.pri == True: print "rank=",communicator.rank,command
						elif options.none == True: print "rank=",communicator.rank,command
						if options.none == False: os.system(command)

				#
				# The fraction of data left, if Nn is not an integer,
				# is processed at the highest node
				#
				if j == communicator.size - 1:
					if i >=  options.first_nr + (j+1)*Nn*options.incr_nr:
						name_nr = compose_name_nr(i)
						command = compose_cmd(name_nr, i)
						if options.pri == True: print "rank=",communicator.rank,command
						elif options.none == True: print "rank=",communicator.rank,command
						if options.none == False: os.system(command)


#
#----------- Calling functions
#
#if communicator.rank == 0: 
#	if options.pri == True: pri_date()
#	elif options.none == True: pri_date()

proc_series_par()

#communicator.rank == 0:
#	if options.pri == True: pri_date(msg = "Time stamp at end of series processing:")	    
#	elif options.none == True: pri_date(msg = "Time stamp at end of series processing:")

#
#----------- That's all folks
#