[85] | 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, GPC FEB 2002 |
---|
| 13 | |
---|
[2949] | 14 | import os, sys |
---|
| 15 | print 'PATH:', os.getenv('PATH') |
---|
| 16 | print 'PYTHONPATH:', os.getenv('PYTHONPATH') |
---|
| 17 | print 'Version:', sys.version |
---|
[85] | 18 | try: |
---|
| 19 | import Numeric |
---|
[2949] | 20 | except Exception, e: |
---|
| 21 | msg = 'Module Numeric must be present to run pypar: %s' %e |
---|
| 22 | raise msg |
---|
[85] | 23 | |
---|
| 24 | |
---|
| 25 | print "Importing pypar" |
---|
| 26 | import pypar |
---|
| 27 | methods = dir(pypar) |
---|
| 28 | assert 'Abort' in methods |
---|
| 29 | assert 'Finalize' in methods |
---|
| 30 | assert 'Get_processor_name' in methods |
---|
| 31 | assert 'Wtime' in methods |
---|
| 32 | assert 'rank' in methods |
---|
| 33 | assert 'raw_receive' in methods |
---|
| 34 | assert 'raw_send' in methods |
---|
| 35 | assert 'receive' in methods |
---|
| 36 | assert 'send' in methods |
---|
| 37 | assert 'bcast' in methods |
---|
| 38 | assert 'size' in methods |
---|
| 39 | |
---|
| 40 | print "Module pypar imported OK" |
---|
| 41 | #pypar.Barrier() |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | myid = pypar.rank() |
---|
| 45 | numproc = pypar.size() |
---|
| 46 | node = pypar.Get_processor_name() |
---|
| 47 | |
---|
| 48 | print "I am processor %d of %d on node %s" %(myid, numproc, node) |
---|
| 49 | pypar.Barrier() |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | if numproc > 1: |
---|
| 53 | # Test simple raw communication (arrays, strings and general) |
---|
| 54 | # |
---|
| 55 | N = 17 #Number of elements |
---|
| 56 | |
---|
| 57 | if myid == 0: |
---|
| 58 | # Integer arrays |
---|
| 59 | # |
---|
| 60 | A = Numeric.array(range(N)) |
---|
| 61 | B = Numeric.zeros(N) |
---|
| 62 | pypar.raw_send(A,1) |
---|
| 63 | pypar.raw_receive(B,numproc-1) |
---|
| 64 | |
---|
| 65 | assert Numeric.allclose(A, B) |
---|
| 66 | print "Raw communication of numeric integer arrays OK" |
---|
| 67 | |
---|
| 68 | # Real arrays |
---|
| 69 | # |
---|
| 70 | A = Numeric.array(range(N)).astype('f') |
---|
| 71 | B = Numeric.zeros(N).astype('f') |
---|
| 72 | pypar.raw_send(A,1) |
---|
| 73 | pypar.raw_receive(B,numproc-1) |
---|
| 74 | |
---|
| 75 | assert Numeric.allclose(A, B) |
---|
| 76 | print "Raw communication of numeric real arrays OK" |
---|
| 77 | |
---|
| 78 | # Strings (< 256 characters) |
---|
| 79 | # |
---|
| 80 | A = "and now to something completely different !" |
---|
| 81 | B = " "*len(A) |
---|
| 82 | pypar.raw_send(A,1) |
---|
| 83 | pypar.raw_receive(B,numproc-1) |
---|
| 84 | |
---|
| 85 | assert A == B |
---|
| 86 | print "Raw communication of strings OK" |
---|
| 87 | |
---|
| 88 | # A more general structure |
---|
| 89 | # |
---|
| 90 | A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 91 | B = [' ', (0,0,0.0), {0: ' '}, Numeric.zeros(2).astype('f')] |
---|
| 92 | pypar.raw_send(A,1) |
---|
| 93 | B = pypar.raw_receive(B,numproc-1) |
---|
| 94 | |
---|
| 95 | assert A == B |
---|
| 96 | print "Raw communication of general structures OK" |
---|
| 97 | |
---|
| 98 | else: |
---|
| 99 | # Integers |
---|
| 100 | # |
---|
| 101 | X = Numeric.zeros(N) |
---|
| 102 | pypar.raw_receive(X, myid-1) |
---|
| 103 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 104 | |
---|
| 105 | # Floats |
---|
| 106 | # |
---|
| 107 | X = Numeric.zeros(N).astype('f') |
---|
| 108 | pypar.raw_receive(X, myid-1) |
---|
| 109 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 110 | |
---|
| 111 | # Strings |
---|
| 112 | # |
---|
| 113 | X = " "*256 |
---|
| 114 | pypar.raw_receive(X, myid-1) |
---|
| 115 | pypar.raw_send(X.strip(), (myid+1)%numproc) |
---|
| 116 | |
---|
| 117 | # General |
---|
| 118 | # |
---|
| 119 | X = [' ', (0,0,0.0), {0: ' '}, Numeric.zeros(2).astype('f')] |
---|
| 120 | X = pypar.raw_receive(X, myid-1) |
---|
| 121 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | # Test easy communication - without buffers (arrays, strings and general) |
---|
| 125 | # |
---|
| 126 | N = 17 #Number of elements |
---|
| 127 | |
---|
| 128 | if myid == 0: |
---|
| 129 | # Integer arrays |
---|
| 130 | # |
---|
| 131 | A = Numeric.array(range(N)) |
---|
| 132 | |
---|
| 133 | pypar.send(A,1) |
---|
| 134 | B = pypar.receive(numproc-1) |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | assert Numeric.allclose(A, B) |
---|
| 138 | print "Simplified communication of numeric integer arrays OK" |
---|
| 139 | |
---|
| 140 | # Real arrays |
---|
| 141 | # |
---|
| 142 | A = Numeric.array(range(N)).astype('f') |
---|
| 143 | pypar.send(A,1) |
---|
| 144 | B=pypar.receive(numproc-1) |
---|
| 145 | |
---|
| 146 | assert Numeric.allclose(A, B) |
---|
| 147 | print "Simplified communication of numeric real arrays OK" |
---|
| 148 | |
---|
| 149 | # Strings |
---|
| 150 | # |
---|
| 151 | A = "and now to something completely different !" |
---|
| 152 | pypar.send(A,1) |
---|
| 153 | B=pypar.receive(numproc-1) |
---|
| 154 | |
---|
| 155 | assert A == B |
---|
| 156 | print "Simplified communication of strings OK" |
---|
| 157 | |
---|
| 158 | # A more general structure |
---|
| 159 | # |
---|
| 160 | A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 161 | pypar.send(A,1) |
---|
| 162 | B = pypar.receive(numproc-1) |
---|
| 163 | |
---|
| 164 | assert A == B |
---|
| 165 | print "Simplified communication of general structures OK" |
---|
| 166 | |
---|
| 167 | else: |
---|
| 168 | # Integers |
---|
| 169 | # |
---|
| 170 | X=pypar.receive(myid-1) |
---|
| 171 | pypar.send(X, (myid+1)%numproc) |
---|
| 172 | |
---|
| 173 | # Floats |
---|
| 174 | # |
---|
| 175 | X=pypar.receive(myid-1) |
---|
| 176 | pypar.send(X, (myid+1)%numproc) |
---|
| 177 | |
---|
| 178 | # Strings |
---|
| 179 | # |
---|
| 180 | X=pypar.receive(myid-1) |
---|
| 181 | pypar.send(X, (myid+1)%numproc) |
---|
| 182 | |
---|
| 183 | # General |
---|
| 184 | # |
---|
| 185 | X = pypar.receive(myid-1) |
---|
| 186 | pypar.send(X, (myid+1)%numproc) |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | # Test broadcast - with buffers (arrays, strings and general) |
---|
| 190 | # |
---|
| 191 | N = 17 #Number of elements |
---|
| 192 | |
---|
| 193 | testString = 'test' + str(myid) |
---|
| 194 | print testString |
---|
| 195 | pypar.bcast(testString, 0) |
---|
| 196 | assert testString == 'test0' |
---|
| 197 | |
---|
| 198 | testString = 'test' + str(myid) |
---|
| 199 | pypar.bcast(testString, numproc-1) |
---|
| 200 | assert testString == 'test' + str(numproc-1) |
---|
| 201 | |
---|
| 202 | if myid == 0: |
---|
| 203 | print "Broadcast communication of strings OK" |
---|
| 204 | |
---|
| 205 | testArray = myid * Numeric.array(range(N)) |
---|
| 206 | pypar.bcast(testArray, 1) |
---|
| 207 | assert Numeric.allclose(testArray, 1 * testArray) |
---|
| 208 | |
---|
| 209 | if myid == 0: |
---|
| 210 | print "Broadcast communication of numeric integer array OK" |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | testArray = myid * Numeric.array(range(N)).astype('f') |
---|
| 214 | pypar.bcast(testArray, 1) |
---|
| 215 | assert Numeric.allclose(testArray, 1 * testArray) |
---|
| 216 | |
---|
| 217 | if myid == 0: |
---|
| 218 | print "Broadcast communication of numeric real array OK" |
---|
| 219 | |
---|
| 220 | testGeneral = ['ABC', myid, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 221 | |
---|
| 222 | testGeneral = pypar.bcast(testGeneral, 1) |
---|
| 223 | |
---|
| 224 | assert testGeneral == ['ABC', 1, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 225 | |
---|
| 226 | if myid == 0: |
---|
| 227 | print "Broadcast communication of general structures OK" |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | |
---|