source: anuga_core/source/pypar-numeric/testpypar.py @ 5779

Last change on this file since 5779 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: 29.1 KB
Line 
1#!/usr/bin/env python
2# Test of MPI module 'pypar' for Python
3#
4# Run as
5#   python testpypar.py
6# or
7#   mpirun -np 2 testpypar.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, GPC FEB 2002
13
14try:
15  import Numeric
16except:
17  raise 'Module Numeric must be present to run pypar'
18
19
20#print "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
35#print "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    # Long integer arrays
64    #
65    A = Numeric.array(range(N)).astype('l')
66    B = Numeric.zeros(N)   
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 long integer arrays OK"
72
73    # Real arrays
74    #
75    A = Numeric.array(range(N)).astype('f')
76    B = Numeric.zeros(N).astype('f')   
77    pypar.raw_send(A,1)
78    pypar.raw_receive(B,numproc-1)
79   
80    assert Numeric.allclose(A, B)   
81    print "Raw communication of numeric real arrays OK"
82
83    # Complex arrays
84    #
85    A = Numeric.array(range(N)).astype('D')
86    A += 3j
87    B = Numeric.zeros(N).astype('D')
88    pypar.raw_send(A, 1)
89    B = pypar.raw_receive(B, numproc-1)
90
91    assert Numeric.allclose(A, B)   
92    print "Raw communication of numeric complex arrays OK"
93
94    # Strings (< 256 characters)
95    #
96    A = "and now to something completely different !"
97    B = " "*len(A)
98    pypar.raw_send(A,1)
99    B, status = pypar.raw_receive(B,numproc-1,return_status=True)
100
101    assert A == B
102    print "Raw communication of strings OK"
103   
104    # A more general structure
105    #
106    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
107    B = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]   
108    pypar.raw_send(A,1)
109    B, status = pypar.raw_receive(B,numproc-1, return_status=True)
110
111    assert A == B
112    print "Raw communication of general structures OK"
113   
114  else: 
115    # Integers
116    #
117    X = Numeric.zeros(N)
118    pypar.raw_receive(X, myid-1) 
119    pypar.raw_send(X, (myid+1)%numproc)
120 
121    # Long integers
122    #
123    X = Numeric.zeros(N).astype('l')
124    pypar.raw_receive(X, myid-1) 
125    pypar.raw_send(X, (myid+1)%numproc)   
126
127    # Floats
128    #
129    X = Numeric.zeros(N).astype('f')
130    pypar.raw_receive(X, myid-1) 
131    pypar.raw_send(X, (myid+1)%numproc)   
132
133    # Complex
134    #
135    X = Numeric.zeros(N).astype('D')
136    X = pypar.raw_receive(X, myid-1) 
137    pypar.raw_send(X, (myid+1)%numproc)   
138
139    # Strings
140    #
141    X = " "*256
142    pypar.raw_receive(X, myid-1) 
143    pypar.raw_send(X.strip(), (myid+1)%numproc)   
144
145    # General
146    #
147    X = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]
148    X = pypar.raw_receive(X, myid-1) 
149    pypar.raw_send(X, (myid+1)%numproc)   
150   
151
152
153
154
155  #Test (raw communication of) multi dimensional arrays
156
157  M = 13  #Number of elements in dim 1
158  N = 17  #Number of elements in higher dims
159 
160  if myid == 0:
161    # 2D real arrays
162    #
163    A = Numeric.array(range(M*N)).astype('f')
164    B = Numeric.zeros(M*N).astype('f')
165
166    A = Numeric.reshape(A, (M,N))
167    B = Numeric.reshape(B, (M,N))   
168   
169    pypar.raw_send(A,1)
170    pypar.raw_receive(B,numproc-1)
171   
172    assert Numeric.allclose(A, B)   
173    print "Raw communication of 2D real arrays OK"
174
175    # 2D complex arrays
176    #
177    A = Numeric.array(range(M*N)).astype('D')
178    B = Numeric.zeros(M*N).astype('D')
179
180    A = Numeric.reshape(A, (M,N))
181    B = Numeric.reshape(B, (M,N))   
182
183    pypar.raw_send(A,1)
184    pypar.raw_receive(B,numproc-1)
185   
186    assert Numeric.allclose(A, B)   
187    print "Raw communication of 2D complex arrays OK"
188
189    # 3D real arrays
190    #
191    A = Numeric.array(range(M*N*N)).astype('f')
192    B = Numeric.zeros(M*N*N).astype('f')
193
194    A = Numeric.reshape(A, (M,N,N))
195    B = Numeric.reshape(B, (M,N,N))   
196   
197    pypar.raw_send(A,1)
198    pypar.raw_receive(B,numproc-1)
199   
200    assert Numeric.allclose(A, B)   
201    print "Raw communication of 3D real real arrays OK"
202
203    # 4D real arrays
204    #
205    A = Numeric.array(range(M*N*N*M)).astype('f')
206    B = Numeric.zeros(M*N*N*M).astype('f')
207
208    A = Numeric.reshape(A, (M,N,N,M))
209    B = Numeric.reshape(B, (M,N,N,M))   
210   
211    pypar.raw_send(A,1)
212    pypar.raw_receive(B,numproc-1)
213   
214    assert Numeric.allclose(A, B)   
215    print "Raw communication of 4D real real arrays OK"
216
217    # 5D real arrays
218    #
219    A = Numeric.array(range(M*N*2*N*M)).astype('f')
220    B = Numeric.zeros(M*N*2*N*M).astype('f')
221
222    A = Numeric.reshape(A, (M,N,2,N,M))
223    B = Numeric.reshape(B, (M,N,2,N,M))   
224   
225    pypar.raw_send(A,1)
226    pypar.raw_receive(B,numproc-1)
227   
228    assert Numeric.allclose(A, B)   
229    print "Raw communication of 5D real real arrays OK"
230
231    # 5D double arrays
232    #
233    A = Numeric.array(range(M*N*2*N*M)).astype('d')
234    B = Numeric.zeros(M*N*2*N*M).astype('d')
235
236    A = Numeric.reshape(A, (M,N,2,N,M))
237    B = Numeric.reshape(B, (M,N,2,N,M))   
238   
239    pypar.raw_send(A,1)
240    pypar.raw_receive(B,numproc-1)
241   
242    assert Numeric.allclose(A, B)   
243    print "Raw communication of 5D double arrays OK"
244
245    # 5D complex arrays
246    #
247    A = Numeric.array(range(M*N*2*N*M)).astype('D')
248    B = Numeric.zeros(M*N*2*N*M).astype('D')
249
250    A = Numeric.reshape(A, (M,N,2,N,M))
251    B = Numeric.reshape(B, (M,N,2,N,M))   
252   
253    pypar.raw_send(A,1)
254    pypar.raw_receive(B,numproc-1)
255   
256    assert Numeric.allclose(A, B)   
257    print "Raw communication of 5D complex arrays OK"
258  else: 
259    # 2D real arrays
260    #
261    X = Numeric.zeros(M*N).astype('f')
262    X = Numeric.reshape(X, (M,N))
263   
264    pypar.raw_receive(X, myid-1) 
265    pypar.raw_send(X, (myid+1)%numproc)
266 
267    # 2D complex arrays
268    #
269    X = Numeric.zeros(M*N).astype('D')
270    X = Numeric.reshape(X, (M,N))
271
272    X = pypar.raw_receive(X, myid-1)
273    pypar.raw_send(X, (myid+1)%numproc)
274 
275    # 3D real arrays
276    #
277    X = Numeric.zeros(M*N*N).astype('f')
278    X = Numeric.reshape(X, (M,N,N))
279   
280    pypar.raw_receive(X, myid-1) 
281    pypar.raw_send(X, (myid+1)%numproc)
282
283    # 4D real arrays
284    #
285    X = Numeric.zeros(M*N*N*M).astype('f')
286    X = Numeric.reshape(X, (M,N,N,M))
287   
288    pypar.raw_receive(X, myid-1) 
289    pypar.raw_send(X, (myid+1)%numproc)
290
291    # 5D real arrays
292    #
293    X = Numeric.zeros(M*N*2*N*M).astype('f')
294    X = Numeric.reshape(X, (M,N,2,N,M))
295   
296    pypar.raw_receive(X, myid-1) 
297    pypar.raw_send(X, (myid+1)%numproc)
298
299    # 5D double arrays
300    #
301    X = Numeric.zeros(M*N*2*N*M).astype('d')
302    X = Numeric.reshape(X, (M,N,2,N,M))
303   
304    pypar.raw_receive(X, myid-1) 
305    pypar.raw_send(X, (myid+1)%numproc)
306
307    # 5D complex arrays
308    #
309    X = Numeric.zeros(M*N*2*N*M).astype('D')
310    X = Numeric.reshape(X, (M,N,2,N,M))
311   
312    pypar.raw_receive(X, myid-1) 
313    pypar.raw_send(X, (myid+1)%numproc)
314
315  # Test easy communication  - without buffers (arrays, strings and general)
316  #
317  N = 17 #Number of elements
318 
319  if myid == 0:
320    # Integer arrays
321    #
322    A = Numeric.array(range(N))
323
324    pypar.send(A,1)
325    B = pypar.receive(numproc-1)
326   
327
328    assert Numeric.allclose(A, B)
329    print "Simplified communication of numeric integer arrays OK"
330
331    # Long integer arrays
332    #
333    A = Numeric.array(range(N)).astype('l')
334    pypar.send(A,1)
335    B=pypar.receive(numproc-1)
336   
337    assert Numeric.allclose(A, B)   
338    print "Simplified communication of long integer real arrays OK"
339
340    # Real arrays
341    #
342    A = Numeric.array(range(N)).astype('f')
343    pypar.send(A,1)
344    B=pypar.receive(numproc-1)
345   
346    assert Numeric.allclose(A, B)   
347    print "Simplified communication of numeric real arrays OK"
348
349    # Complex arrays
350    #
351    A = Numeric.array(range(N)).astype('D')
352    A += 3j
353    pypar.send(A,1)
354    B=pypar.receive(numproc-1)
355
356    assert Numeric.allclose(A, B)   
357    print "Simplified communication of numeric complex arrays OK"
358
359
360    # Strings
361    #
362    A = "and now to something completely different !"
363    pypar.send(A,1)
364    B=pypar.receive(numproc-1)
365   
366    assert A == B
367    print "Simplified communication of strings OK"
368   
369    # A more general structure
370    #
371    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
372    pypar.send(A,1)
373    B = pypar.receive(numproc-1)
374   
375    assert A == B
376    print "Simplified communication of general structures OK"
377   
378  else: 
379    # Integers
380    #
381    X=pypar.receive(myid-1) 
382    pypar.send(X, (myid+1)%numproc)
383 
384    # Long integers
385    #
386    X=pypar.receive(myid-1) 
387    pypar.send(X, (myid+1)%numproc)   
388
389    # Floats
390    #
391    X=pypar.receive(myid-1) 
392    pypar.send(X, (myid+1)%numproc)   
393
394    # Complex
395    #
396    X=pypar.receive(myid-1) 
397    pypar.send(X, (myid+1)%numproc)   
398
399    # Strings
400    #
401    X=pypar.receive(myid-1) 
402    pypar.send(X, (myid+1)%numproc)   
403
404    # General
405    #
406    X = pypar.receive(myid-1) 
407    pypar.send(X, (myid+1)%numproc)   
408
409
410
411
412   
413  #Test (easy communication of) multi dimensional arrays
414
415  M = 13  #Number of elements in dim 1
416  N = 17  #Number of elements in higher dims
417 
418  if myid == 0:
419    # 2D real arrays
420    #
421    A = Numeric.array(range(M*N)).astype('f')
422
423    A = Numeric.reshape(A, (M,N))
424   
425    pypar.send(A,1)
426    B = pypar.receive(numproc-1)
427   
428    assert Numeric.allclose(A, B)   
429    print "Simplified communication of 2D real arrays OK"
430
431    # 3D real arrays
432    #
433    A = Numeric.array(range(M*N*N)).astype('f')
434    A = Numeric.reshape(A, (M,N,N))
435   
436    pypar.send(A,1)
437    B=pypar.receive(numproc-1)
438   
439    assert Numeric.allclose(A, B)   
440    print "Simplified communication of 3D real arrays OK"
441
442    # 4D real arrays
443    #
444    A = Numeric.array(range(M*N*N*M)).astype('f')
445    A = Numeric.reshape(A, (M,N,N,M))
446   
447    pypar.send(A,1)
448    B=pypar.receive(numproc-1)
449   
450    assert Numeric.allclose(A, B)   
451    print "Simplified communication of 4D real arrays OK"
452
453    # 5D real arrays
454    #
455    A = Numeric.array(range(M*N*2*N*M)).astype('f')
456    A = Numeric.reshape(A, (M,N,2,N,M))
457   
458    pypar.send(A,1)
459    B=pypar.receive(numproc-1)
460   
461    assert Numeric.allclose(A, B)   
462    print "Simplified communication of 5D real arrays OK"
463
464    # 5D double arrays
465    #
466    A = Numeric.array(range(M*N*2*N*M)).astype('d')
467    A = Numeric.reshape(A, (M,N,2,N,M))
468   
469    pypar.send(A,1)
470    B=pypar.receive(numproc-1)
471   
472    assert Numeric.allclose(A, B)   
473    print "Simplified communication of 5D double arrays OK"
474
475    # 5D complex arrays
476    #
477    A = Numeric.array(range(M*N*2*N*M)).astype('D')
478    A = Numeric.reshape(A, (M,N,2,N,M))
479   
480    pypar.send(A,1)
481    B=pypar.receive(numproc-1)
482   
483    assert Numeric.allclose(A, B)   
484    print "Simplified communication of 5D complex real arrays OK"
485
486  else: 
487    # 2D real arrays
488    #
489   
490    X = pypar.receive(myid-1) 
491    pypar.send(X, (myid+1)%numproc)
492 
493    # 3D real arrays
494    #
495    X=pypar.receive(myid-1) 
496    pypar.send(X, (myid+1)%numproc)
497
498    # 4D real arrays
499    #
500    X = pypar.receive(myid-1) 
501    pypar.send(X, (myid+1)%numproc)
502
503    # 5D real arrays
504    #
505    X=pypar.receive(myid-1) 
506    pypar.send(X, (myid+1)%numproc)
507
508    # 5D double arrays
509    #
510    X=pypar.receive(myid-1) 
511    pypar.send(X, (myid+1)%numproc)
512
513    # 5D complex arrays
514    #
515    X=pypar.receive(myid-1) 
516    pypar.send(X, (myid+1)%numproc)
517
518  # Test broadcast  - with buffers (arrays, strings and general)
519  #
520     
521  testString = ('test' + str(myid)).ljust(10)  #Buffers must have the same length on all procs!
522  pypar.bcast(testString, 0)
523  assert testString.strip() == 'test0'
524 
525  testString = ('test' + str(myid)).ljust(10)  #Buffers must have the same length on all procs!
526  pypar.bcast(testString, numproc-1)
527  assert testString.strip() == 'test' + str(numproc-1)
528 
529  if myid == 0:
530    print "Broadcast communication of strings OK"
531
532  #################################################### 
533  N = 17 #Number of elements
534  testArray = myid * Numeric.array(range(N))
535  pypar.bcast(testArray, 1)
536  assert Numeric.allclose(testArray, 1 * testArray)
537 
538  if myid == 0:   
539    print "Broadcast communication of numeric integer array OK"
540
541
542  testArray = myid * Numeric.array(range(N)).astype('f')
543  pypar.bcast(testArray, 1)
544  assert Numeric.allclose(testArray, 1 * testArray)
545  if myid == 0:
546    print "Broadcast communication of numeric real array OK"
547
548
549  M = 13
550  testArray = myid * Numeric.array(range(M*N)).astype('f')
551  testArray = Numeric.reshape(testArray, (M,N)) 
552  pypar.bcast(testArray, 1)
553  assert Numeric.allclose(testArray, 1 * testArray)
554  if myid == 0:
555    print "Broadcast communication of 2D numeric real array OK"
556
557  testArray = myid * Numeric.array(range(M*2*N)).astype('f')
558  testArray = Numeric.reshape(testArray, (M,2,N)) 
559  pypar.bcast(testArray, 1)
560  assert Numeric.allclose(testArray, 1 * testArray)
561  if myid == 0:
562    print "Broadcast communication of 3D numeric real array OK"
563
564  testArray = myid * Numeric.array(range(M*2*N)).astype('D')
565  testArray = Numeric.reshape(testArray, (M,2,N)) 
566  pypar.bcast(testArray, 1)
567  assert Numeric.allclose(testArray, 1 * testArray)
568  if myid == 0:
569    print "Broadcast communication of 3D numeric complex array OK"
570
571   
572  testGeneral = ['ABC', myid, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
573 
574  testGeneral = pypar.bcast(testGeneral, 1)
575 
576  assert testGeneral ==  ['ABC', 1, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])], 'Should have been %s' %testGeneral
577 
578  if myid == 0:
579    print "Broadcast communication of general structures OK"
580 
581
582
583
584  # Test scatter  - with/without buffers (arrays, strings)
585  #
586  N = 16 #Number of elements
587
588  NP = N/numproc
589 
590  testString = 'ABCDEFGHIJKLMNOP'  #Length = 16
591  X = ' '*NP
592
593  #print 'P%d: s=%s, r=%s' %(myid, testString, X)
594  #pypar.raw_scatter(testString, X, 2)
595  #Y = pypar.scatter(testString, 2)
596
597
598  #assert X==Y, 'X=%s, Y=%s' %(X,Y)
599  #assert Y == testString[myid*NP:(myid+1)*NP]
600  #assert X == testString[myid*NP:(myid+1)*NP]
601
602  #if myid == 0:   
603  #  print "Scatter communication of strings OK"
604   
605
606  #Scatter Arrays
607  testArray = Numeric.array(range(N))
608  X = Numeric.zeros(NP)
609  pypar.raw_scatter(testArray, X, 0)
610  Y = pypar.scatter(testArray, 0)
611
612  assert Numeric.allclose(X, Y) 
613  assert Numeric.allclose(X, testArray[myid*NP:(myid+1)*NP])
614  assert Numeric.allclose(Y, testArray[myid*NP:(myid+1)*NP])   
615
616  if myid == 0:
617    print "Scatter communication of numeric integer array OK"
618
619
620  testArray = Numeric.array(range(N)).astype('f')
621  X = Numeric.zeros(NP).astype('f')
622  pypar.raw_scatter(testArray, X, 0)
623   
624  Y = pypar.scatter(testArray, 0)
625
626  assert Numeric.allclose(X, Y) 
627  assert Numeric.allclose(X, testArray[myid*NP:(myid+1)*NP])
628  assert Numeric.allclose(Y, testArray[myid*NP:(myid+1)*NP])   
629
630  if myid == 0:
631    print "Scatter communication of numeric real arrays OK"
632  #else:
633  #  print X, testArray, Y
634  #  assert Numeric.allclose(X, Y)
635
636  testArray = Numeric.array(range(N)).astype('D')
637  X = Numeric.zeros(NP).astype('D')
638  pypar.raw_scatter(testArray, X, 0)
639   
640  Y = pypar.scatter(testArray, 0)
641
642  assert Numeric.allclose(X, Y) 
643  assert Numeric.allclose(X, testArray[myid*NP:(myid+1)*NP])
644  assert Numeric.allclose(Y, testArray[myid*NP:(myid+1)*NP])   
645
646  if myid == 0:
647    print "Scatter communication of numeric complex array OK"
648
649
650###################################################
651#FIXME: 2D scatter doesn't work yet   
652#   M = 16
653#   N = 13
654#   MP = M/numproc
655 
656#   testArray = Numeric.array(range(M*N)).astype('D')
657#   testArray = Numeric.reshape(testArray, (M,N))   
658#   X = Numeric.zeros(MP*N).astype('D')
659#   X = Numeric.reshape(X, (MP,N))
660 
661#   pypar.raw_scatter(testArray, X, 0)
662#   Y = pypar.scatter(testArray, 0)
663 
664#   assert Numeric.allclose(X, Y) 
665#   assert Numeric.allclose(X, testArray[myid*MP:(myid+1)*MP,:])
666#   assert Numeric.allclose(Y, testArray[myid*MP:(myid+1)*MP,:])   
667
668#   if myid == 0:
669#     print "Scatter communication of 2D numeric complex array OK"
670
671
672
673  # Test gather  - with/without buffers (arrays, strings)
674  #
675  N = 17 #Number of elements
676     
677  testString = 'AB'
678  X = '_'*(len(testString)*numproc)    #Blanks caused errors when numproc >= 6
679  pypar.raw_gather(testString, X, 0)
680
681  Y =  pypar.gather(testString, 0) 
682 
683  if myid == 0:
684    assert X == 'AB' * numproc
685    assert Y == 'AB' * numproc
686    print "Gather communication of strings OK"
687 
688
689  testArray = Numeric.array(range(N))
690  X = Numeric.zeros(N*numproc)
691  pypar.raw_gather(testArray, X, 0)
692
693  Y = pypar.gather(testArray, 0)
694 
695  if myid == 0:
696    for i in range(numproc):       
697      assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)])
698
699    assert Numeric.allclose(X, Y)
700    print "Gather communication of numeric integer array OK"
701   
702   
703  testArray = Numeric.array(range(N)).astype('f')
704  X = Numeric.zeros(N*numproc).astype('f')
705  pypar.raw_gather(testArray, X, 0)
706 
707  Y = pypar.gather(testArray, 0)
708  if myid == 0:
709    for i in range(numproc):       
710      assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)])
711    assert Numeric.allclose(X, Y)     
712    print "Gather communication of numeric real array OK"
713   
714 
715  testArray = Numeric.array(range(N)).astype('D')
716  X = Numeric.zeros(N*numproc).astype('D')
717  pypar.raw_gather(testArray, X, 0)
718 
719  Y = pypar.gather(testArray, 0)
720  if myid == 0:
721    for i in range(numproc):       
722      assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)])
723    assert Numeric.allclose(X, Y)     
724    print "Gather communication of numeric complex arrays OK"
725
726  M = 13 
727  testArray = Numeric.array(range(M*N)).astype('D')
728  testArray = Numeric.reshape(testArray, (M,N))
729  X = Numeric.zeros(M*N*numproc).astype('D')
730  X = Numeric.reshape(X, (M*numproc,N))
731 
732  pypar.raw_gather(testArray, X, 0)
733 
734  Y = pypar.gather(testArray, 0)
735  if myid == 0:
736    for i in range(numproc):       
737      assert Numeric.allclose(testArray, X[(i * M): ((i+1)*M), :])
738    assert Numeric.allclose(X, Y)     
739    print "Gather communication of 2D numeric complex arrays OK"
740   
741 
742  ########################################################
743  # Test reduce
744  #######################################################
745  N = 17 #Number of elements
746 
747  # Create one (different) array on each processor
748  #   
749  testArray = Numeric.array(range(N)) * (myid+1)
750  #print testArray
751  X = Numeric.zeros(N) # Buffer for results
752
753  pypar.raw_reduce(testArray, X, pypar.SUM, 0)
754  if myid == 0:
755    Y = Numeric.zeros(N)
756    for i in range(numproc):
757      Y = Y+Numeric.array(range(N))*(i+1)   
758    #print X
759    #print Y 
760    assert Numeric.allclose(X, Y)
761    print "Raw reduce using pypar.SUM OK"
762       
763  pypar.raw_reduce(testArray, X, pypar.MAX, 0, 0)
764  if myid == 0:
765    Y = Numeric.array(range(N))*numproc
766    assert Numeric.allclose(X, Y)
767    print "Raw reduce using pypar.MAX OK"
768
769  pypar.raw_reduce(testArray, X, pypar.MIN, 0, 0)
770  if myid == 0:
771    Y = Numeric.array(range(N))
772    assert Numeric.allclose(X, Y)
773    print "Raw reduce using pypar.MIN OK"
774   
775  if numproc <= 20:
776    testArray_float = testArray.astype('f')  #Make room for big results
777    X_float = X.astype('f')
778    pypar.raw_reduce(testArray_float, X_float, pypar.PROD, 0, 0)
779    if myid == 0:
780      Y = Numeric.ones(N).astype('f')   
781      for i in range(numproc):
782        Y = Y*Numeric.array(range(N))*(i+1)   
783      #print X_float
784      #print Y 
785      assert Numeric.allclose(X_float, Y)
786      print "Raw reduce using pypar.PROD OK"
787  else:
788    if myid == 0:
789      print "Skipping product-reduce - try again with numproc < 20"   
790
791  pypar.raw_reduce(testArray, X, pypar.LAND, 0, 0)
792  if myid == 0: 
793    Y = Numeric.ones(N)   
794    for i in range(numproc):
795      Y = Numeric.logical_and(Y, Numeric.array(range(N))*(i+1)) 
796    assert Numeric.allclose(X, Y)
797    print "Raw reduce using pypar.LAND OK"   
798   
799  pypar.raw_reduce(testArray, X, pypar.BAND, 0, 0)
800  if myid == 0:
801    Y = Numeric.ones(N)*255  #Neutral element for &   
802    for i in range(numproc):
803      Y = Numeric.bitwise_and(Y, Numeric.array(range(N))*(i+1))
804    assert Numeric.allclose(X, Y)
805    print "Raw reduce using pypar.BAND OK"   
806
807  pypar.raw_reduce(testArray, X, pypar.LOR, 0, 0)
808  if myid == 0: 
809    Y = Numeric.zeros(N)   
810    for i in range(numproc):
811      Y = Numeric.logical_or(Y, Numeric.array(range(N))*(i+1)) 
812    assert Numeric.allclose(X, Y)
813    print "Raw reduce using pypar.LOR OK"   
814 
815  pypar.raw_reduce(testArray, X, pypar.BOR, 0, 0)
816  if myid == 0:
817    Y = Numeric.zeros(N)   #Neutral element for |   
818    for i in range(numproc):
819      Y = Numeric.bitwise_or(Y, Numeric.array(range(N))*(i+1))
820    assert Numeric.allclose(X, Y)
821    print "Raw reduce using pypar.BOR OK"   
822
823  pypar.raw_reduce(testArray, X, pypar.LXOR, 0, 0)
824  if myid == 0: 
825    Y = Numeric.zeros(N)   
826    for i in range(numproc):
827      Y = Numeric.logical_xor(Y, Numeric.array(range(N))*(i+1)) 
828    assert Numeric.allclose(X, Y)
829    print "Raw reduce using pypar.LXOR OK"   
830
831  pypar.raw_reduce(testArray, X, pypar.BXOR, 0, 0)
832  if myid == 0:
833    Y = Numeric.zeros(N)   #Neutral element for xor ?   
834    for i in range(numproc):
835      Y = Numeric.bitwise_xor(Y, Numeric.array(range(N))*(i+1))
836    assert Numeric.allclose(X, Y)
837    print "Raw reduce using pypar.BXOR OK"   
838
839  # NOT YET SUPPORTED
840 
841  #pypar.raw_reduce(testArray, X, N, pypar.MAXLOC, 0, 0) 
842  #if myid == 0:
843  #  print 'MAXLOC', X
844  #pypar.raw_reduce(testArray, X, N, pypar.MINLOC, 0, 0)
845  #if myid == 0:
846  #  print 'MINLOC', X
847 
848  #
849  #  FIXME
850  # Don't know how to test this (not available on all MPI systems)
851  #
852  #pypar.raw_reduce(testArray, X, N, pypar.REPLACE, 0, 0)
853  #if myid == 0:
854  #  print 'REPLACE', X
855
856
857
858  # Test status block (simple communication)
859  N = 17 
860  if myid == 0:
861    # Integer arrays
862    #
863    A = Numeric.array(range(N))
864
865    pypar.send(A,1)
866    B, status = pypar.receive(numproc-1, return_status = True)
867
868    #print status
869
870    sz = A.itemsize()
871    assert Numeric.allclose(A, B)
872    assert len(B) == status.length, 'Reported length == %d should be %d'\
873           %(status.length, len(B))
874    assert status.size == sz, 'Reported size == %d should be %d'\
875           %(status.size, sz)
876    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
877           %(status.tag, pypar.default_tag)
878    assert status.error == 0
879    assert status.source == numproc-1, 'Reported source == %d should be %d'\
880           %(status.source, numproc-1)
881
882    print "Status object (numeric integer arrays) OK"
883           
884    # Real arrays
885    #
886    A = Numeric.array(range(N)).astype('f')
887    pypar.send(A,1)
888    B, status = pypar.receive(numproc-1, return_status = True)   
889
890    sz = A.itemsize()   
891    assert Numeric.allclose(A, B)
892    assert len(B) == status.length, 'Reported length == %d should be %d'\
893           %(status.length, len(B))
894    assert status.size == sz, 'Reported size == %d should be %d'\
895           %(status.size, sz)
896    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
897           %(status.tag, pypar.default_tag)
898    assert status.error == 0
899    assert status.source == numproc-1, 'Reported source == %d should be %d'\
900           %(status.source, numproc-1)
901   
902    print "Status object (numeric real arrays) OK"
903
904    # Strings
905    #
906    A = "and now to something completely different !"
907    pypar.send(A,1)
908    B, status = pypar.receive(numproc-1, return_status = True)       
909
910    sz = 1 #Characters are one byte long
911    assert A == B
912    assert len(B) == status.length, 'Reported length == %d should be %d'\
913           %(status.length, len(B))
914    assert status.size == sz, 'Reported size == %d should be %d'\
915           %(status.size, sz)
916    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
917           %(status.tag, pypar.default_tag)
918    assert status.error == 0
919    assert status.source == numproc-1, 'Reported source == %d should be %d'\
920           %(status.source, numproc-1)
921
922    print "Status object (strings) OK"
923   
924    # A more general structure
925    #
926    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
927    pypar.send(A,1)
928    B, status = pypar.receive(numproc-1, return_status = True)           
929   
930    assert A == B
931
932    #Length is the number of characters needed to encode the structure
933    #Can't think of a test.
934
935    sz = 1
936    assert status.size == sz, 'Reported size == %d should be %d'\
937           %(status.size, sz)
938    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
939           %(status.tag, pypar.default_tag)
940    assert status.error == 0
941    assert status.source == numproc-1, 'Reported source == %d should be %d'\
942           %(status.source, numproc-1)
943
944    print "Status object (general structures) OK"
945   
946  else: 
947    # Integers
948    #
949    X=pypar.receive(myid-1) 
950    pypar.send(X, (myid+1)%numproc)
951 
952    # Floats
953    #
954    X=pypar.receive(myid-1) 
955    pypar.send(X, (myid+1)%numproc)   
956
957    # Strings
958    #
959    X=pypar.receive(myid-1) 
960    pypar.send(X, (myid+1)%numproc)   
961
962    # General
963    #
964    X = pypar.receive(myid-1) 
965    pypar.send(X, (myid+1)%numproc)   
966
967
968
969
970
971
972
973
974  # Test status block (raw communication)
975  N = 17 #Number of elements
976  if myid == 0:
977    # Integer arrays
978    #
979    A = Numeric.array(range(N))
980    B = Numeric.zeros(N)   
981    pypar.raw_send(A,1)
982    B, status = pypar.raw_receive(B,numproc-1,return_status=True)
983   
984    assert Numeric.allclose(A, B)
985
986    sz = A.itemsize()
987    assert Numeric.allclose(A, B)
988    assert len(B) == status.length, 'Reported length == %d should be %d'\
989           %(status.length, len(B))
990    assert status.size == sz, 'Reported size == %d should be %d'\
991           %(status.size, sz)
992    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
993           %(status.tag, pypar.default_tag)
994    assert status.error == 0
995    assert status.source == numproc-1, 'Reported source == %d should be %d'\
996           %(status.source, numproc-1)
997
998    print "Status object (raw numeric integer arrays) OK"
999
1000
1001    # Real arrays
1002    #
1003    A = Numeric.array(range(N)).astype('f')
1004    B = Numeric.zeros(N).astype('f')   
1005    pypar.raw_send(A,1)
1006    B, status = pypar.raw_receive(B,numproc-1,return_status=True)   
1007   
1008    assert Numeric.allclose(A, B)
1009    sz = A.itemsize()
1010    assert Numeric.allclose(A, B)
1011    assert len(B) == status.length, 'Reported length == %d should be %d'\
1012           %(status.length, len(B))
1013    assert status.size == sz, 'Reported size == %d should be %d'\
1014           %(status.size, sz)
1015    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
1016           %(status.tag, pypar.default_tag)
1017    assert status.error == 0
1018    assert status.source == numproc-1, 'Reported source == %d should be %d'\
1019           %(status.source, numproc-1)
1020
1021    print "Status object (raw numeric real arrays) OK"
1022
1023    # Strings (< 256 characters)
1024    #
1025    A = "and now to something completely different !"
1026    B = " "*len(A)
1027    pypar.raw_send(A,1)
1028    B, status = pypar.raw_receive(B,numproc-1,return_status=True)
1029
1030
1031    sz = 1 #Characters are one byte long
1032    assert A == B
1033    assert len(B) == status.length, 'Reported length == %d should be %d'\
1034           %(status.length, len(B))
1035    assert status.size == sz, 'Reported size == %d should be %d'\
1036           %(status.size, sz)
1037    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
1038           %(status.tag, pypar.default_tag)
1039    assert status.error == 0
1040    assert status.source == numproc-1, 'Reported source == %d should be %d'\
1041           %(status.source, numproc-1)
1042
1043    print "Status object (raw strings) OK"
1044   
1045
1046   
1047    # A more general structure
1048    #
1049    A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])]
1050    B = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]   
1051    pypar.raw_send(A,1)
1052    B, status = pypar.raw_receive(B,numproc-1, return_status=True)
1053
1054
1055    assert A == B
1056    sz = 1
1057    assert status.size == sz, 'Reported size == %d should be %d'\
1058           %(status.size, sz)
1059    assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\
1060           %(status.tag, pypar.default_tag)
1061    assert status.error == 0
1062    assert status.source == numproc-1, 'Reported source == %d should be %d'\
1063           %(status.source, numproc-1)
1064
1065   
1066    print "Status object (raw general structures) OK"
1067   
1068   
1069  else: 
1070    # Integers
1071    #
1072    X = Numeric.zeros(N)
1073    pypar.raw_receive(X, myid-1) 
1074    pypar.raw_send(X, (myid+1)%numproc)
1075 
1076    # Floats
1077    #
1078    X = Numeric.zeros(N).astype('f')
1079    pypar.raw_receive(X, myid-1) 
1080    pypar.raw_send(X, (myid+1)%numproc)   
1081
1082   
1083    # Strings
1084    #
1085    X = " "*256
1086    X = pypar.raw_receive(X, myid-1) 
1087    pypar.raw_send(X.strip(), (myid+1)%numproc)   
1088
1089    # General
1090    #
1091    X = ['   ', (0,0,0.0), {0: '     '}, Numeric.zeros(2).astype('f')]
1092    X = pypar.raw_receive(X, myid-1) 
1093    pypar.raw_send(X, (myid+1)%numproc)   
1094   
1095
1096
1097pypar.Finalize()
1098
1099
1100
1101
Note: See TracBrowser for help on using the repository browser.