source: branches/source_numpy_conversion/pypar-numeric/contrib/testpypar.py @ 7248

Last change on this file since 7248 was 5779, checked in by steve, 16 years ago

Added the old version of pypar which works with Numeric. Necessary for parallel code until we move anuga to numpy (and then we can use pypar as distribute via sourceforge).

File size: 8.8 KB
Line 
1#!/usr/bin/env python
2# Test of MPI module 'pypar' for Python
3#
4# Run as
5#   python pypartest.py
6# or
7#   mpirun -np 2 pypartest.py
8# (perhaps try number of processors more than 2)
9#
10# To verify bandwidth of your architecture please run pytiming (and ctiming)
11#
12# OMN, FEB 2002
13
14try:
15  import Numeric
16except:
17  raise 'Module Numeric must be present to run pypar'
18
19
20print "Importing pypar"
21import pypar
22methods = dir(pypar)
23assert 'Abort' in methods
24assert 'Finalize' in methods
25assert 'Get_processor_name' in methods
26assert 'Wtime' in methods
27assert 'rank' in methods
28assert 'raw_receive' in methods
29assert 'raw_send' in methods
30assert 'receive' in methods
31assert 'send' in methods
32assert 'bcast' in methods
33assert 'size' in methods
34
35print "Module pypar imported OK"
36#pypar.Barrier()
37
38
39myid =    pypar.rank()
40numproc = pypar.size()
41node =    pypar.Get_processor_name()
42
43print "I am processor %d of %d on node %s" %(myid, numproc, node)
44pypar.Barrier()
45
46
47if numproc > 1:
48  # Test simple raw communication (arrays, strings and general)
49  #
50  N = 17 #Number of elements
51 
52  if myid == 0:
53    # Integer arrays
54    #
55    A = Numeric.array(range(N))
56    B = Numeric.zeros(N)   
57    pypar.raw_send(A,1)
58    pypar.raw_receive(B,numproc-1)
59   
60    assert Numeric.allclose(A, B)
61    print "Raw communication of numeric integer arrays OK"
62
63    # Real arrays
64    #
65    A = Numeric.array(range(N)).astype('f')
66    B = Numeric.zeros(N).astype('f')   
67    pypar.raw_send(A,1)
68    pypar.raw_receive(B,numproc-1)
69   
70    assert Numeric.allclose(A, B)   
71    print "Raw communication of numeric real arrays OK"
72
73    # Strings (< 256 characters)
74    #
75    A = "and now to something completely different !"
76    B = " "*len(A)
77    pypar.raw_send(A,1)
78    pypar.raw_receive(B,numproc-1)
79   
80    assert A == B
81    print "Raw communication of strings OK"
82   
83    # A more general structure
84    #
85    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
86    B = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]   
87    pypar.raw_send(A,1)
88    B = pypar.raw_receive(B,numproc-1)
89   
90    assert A == B
91    print "Raw communication of general structures OK"
92   
93  else: 
94    # Integers
95    #
96    X = Numeric.zeros(N)
97    pypar.raw_receive(X, myid-1) 
98    pypar.raw_send(X, (myid+1)%numproc)
99 
100    # Floats
101    #
102    X = Numeric.zeros(N).astype('f')
103    pypar.raw_receive(X, myid-1) 
104    pypar.raw_send(X, (myid+1)%numproc)   
105
106    # Strings
107    #
108    X = " "*256
109    pypar.raw_receive(X, myid-1) 
110    pypar.raw_send(X.strip(), (myid+1)%numproc)   
111
112    # General
113    #
114    X = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]
115    X = pypar.raw_receive(X, myid-1) 
116    pypar.raw_send(X, (myid+1)%numproc)   
117   
118
119  # Test easy communication  - without buffers (arrays, strings and general)
120  #
121  N = 17 #Number of elements
122 
123  if myid == 0:
124    # Integer arrays
125    #
126    A = Numeric.array(range(N))
127
128    pypar.send(A,1)
129    B = pypar.receive(numproc-1)
130   
131
132    assert Numeric.allclose(A, B)
133    print "Simplified communication of numeric integer arrays OK"
134
135    # Real arrays
136    #
137    A = Numeric.array(range(N)).astype('f')
138    pypar.send(A,1)
139    B=pypar.receive(numproc-1)
140   
141    assert Numeric.allclose(A, B)   
142    print "Simplified communication of numeric real arrays OK"
143
144    # Strings
145    #
146    A = "and now to something completely different !"
147    pypar.send(A,1)
148    B=pypar.receive(numproc-1)
149   
150    assert A == B
151    print "Simplified communication of strings OK"
152   
153    # A more general structure
154    #
155    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
156    pypar.send(A,1)
157    B = pypar.receive(numproc-1)
158   
159    assert A == B
160    print "Simplified communication of general structures OK"
161   
162  else: 
163    # Integers
164    #
165    X=pypar.receive(myid-1) 
166    pypar.send(X, (myid+1)%numproc)
167 
168    # Floats
169    #
170    X=pypar.receive(myid-1) 
171    pypar.send(X, (myid+1)%numproc)   
172
173    # Strings
174    #
175    X=pypar.receive(myid-1) 
176    pypar.send(X, (myid+1)%numproc)   
177
178    # General
179    #
180    X = pypar.receive(myid-1) 
181    pypar.send(X, (myid+1)%numproc)   
182
183
184  # Test broadcast  - with buffers (arrays, strings and general)
185  #
186  N = 17 #Number of elements
187     
188  testString = 'test' + str(myid)
189  pypar.bcast(testString, 0)
190  assert testString == 'test0'
191 
192  testString = 'test' + str(myid)
193  pypar.bcast(testString, numproc-1)
194  assert testString == 'test' + str(numproc-1)
195 
196  if myid == 0:
197    print "Broadcast communication of strings OK"
198 
199  testArray = myid * Numeric.array(range(N))
200  pypar.bcast(testArray, 1)
201  assert Numeric.allclose(testArray, 1 * testArray)
202 
203  if myid == 0:   
204    print "Broadcast communication of numeric integer array OK"
205
206
207  testArray = myid * Numeric.array(range(N)).astype('f')
208  pypar.bcast(testArray, 1)
209  assert Numeric.allclose(testArray, 1 * testArray)
210     
211  if myid == 0:
212    print "Broadcast communication of numeric real array OK"
213   
214  testGeneral = ['ABC', myid, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
215 
216  testGeneral = pypar.bcast(testGeneral, 1)
217 
218  assert testGeneral ==  ['ABC', 1, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
219 
220  if myid == 0:
221    print "Broadcast communication of general structures OK"
222 
223
224
225
226
227
228  # Test scatter  - with/without buffers (arrays, strings)
229  #
230  N = 17 #Number of elements
231     
232  testString = 'test' + str(myid)
233  s_size = 1   
234  X = ' '*s_size
235  pypar.raw_scatter(testString, s_size, X, 0)
236 
237  Y = pypar.scatter(testString, s_size, 0)
238     
239  if myid == 0:
240    assert X == 't'
241    assert Y == 't'
242    print "Scatter communication of strings OK"
243
244  testArray = Numeric.array(range(N))
245  s_size = 1   
246  X = Numeric.zeros(s_size)
247  pypar.raw_scatter(testArray, s_size, X, 0)
248 
249  Y = pypar.scatter(testArray, s_size, 0)
250 
251
252  if myid == 0:
253    assert X == [0]
254    assert Y == [0]
255    print "Scatter communication of numeric integer array OK"
256
257
258  testArray = Numeric.array(range(N)).astype('f')
259  s_size = 1   
260  X = Numeric.zeros(s_size).astype('f')
261  pypar.raw_scatter(testArray, s_size, X, 0)
262   
263  Y = pypar.scatter(testArray, s_size, 0)
264 
265  if myid == 0:
266    assert X == [0.0]
267    assert Y == [0.0]
268    print "Scatter communication of numeric real array OK"
269
270
271
272  # Test gather  - with/without buffers (arrays, strings)
273  #
274  N = 17 #Number of elements
275     
276  testString = 'AB'
277  s_size = 2 # to help test
278  X = ' '*(s_size*numproc)
279  pypar.raw_gather(testString, s_size, X, 0, 0)
280
281  if myid == 0:
282    assert X == 'AB' * numproc
283
284  Y =  pypar.gather(testString, s_size, 0) 
285  #print myid, Y
286 
287  if myid == 0:
288    assert X == 'AB' * numproc
289    assert Y == 'AB' * numproc
290    print "Gather communication of strings OK"
291 
292
293  testArray = Numeric.array(range(N))
294  s_size = N   
295  X = Numeric.zeros(s_size*numproc)
296  pypar.raw_gather(testArray, s_size, X, 0, 0)
297
298  Y = pypar.gather(testArray, s_size, 0)
299 
300  if myid == 0:
301    for i in range(numproc):       
302      assert Numeric.allclose(testArray, X[(i * s_size): ((i+1)*s_size)])
303    print "Gather communication of numeric integer array OK"
304   
305   
306  testArray = Numeric.array(range(N)).astype('f')
307  s_size = N   
308  X = Numeric.zeros(s_size*numproc).astype('f')
309  pypar.raw_gather(testArray, s_size, X, 0, 0)
310 
311  Y = pypar.gather(testArray, s_size, 0)
312   
313  if myid == 0:
314    for i in range(numproc):       
315      assert Numeric.allclose(testArray, X[(i * s_size): ((i+1)*s_size)])
316    print "Gather communication of numeric real array OK"
317   
318
319  # Test reduce  - with/without buffers (arrays, strings)
320  #
321  N = 17 #Number of elements
322     
323  testArray = Numeric.array(range(N))
324  s_size = N # to help test
325  X = Numeric.zeros(s_size)
326  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_SUM, 0, 0)
327  if myid == 0:
328    print 'SUM', X
329  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_MAX, 0, 0)
330  if myid == 0:
331    print 'MAX', X
332  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_MIN, 0, 0)
333  if myid == 0:
334    print 'MIN', X
335  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_PROD, 0, 0)
336  if myid == 0:
337    print 'PROD', X
338  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_LAND, 0, 0)
339  if myid == 0:
340    print 'LAND', X
341  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_BAND, 0, 0)
342  if myid == 0:
343    print 'BAND', X
344  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_LOR, 0, 0)
345  if myid == 0:
346    print 'LOR', X
347  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_BOR, 0, 0)
348  if myid == 0:
349    print 'BOR', X
350  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_LXOR, 0, 0)
351  if myid == 0:
352    print 'LXOR', X
353  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_BXOR, 0, 0)
354  if myid == 0:
355    print 'BXOR', X
356  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_MAXLOC, 0, 0) 
357  if myid == 0:
358    print 'MAXLOC', X
359  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_MINLOC, 0, 0)
360  if myid == 0:
361    print 'MINLOC', X
362  pypar.raw_reduce(testArray, X, s_size, pypar.mpi_REPLACE, 0, 0)
363  if myid == 0:
364    print 'REPLACE', X
Note: See TracBrowser for help on using the repository browser.