source: branches/numpy_misc/tools/acceptance_tests/test_pypar.py @ 6991

Last change on this file since 6991 was 6991, checked in by rwilson, 15 years ago

Initial commit of the cluster acceptance package.

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