[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 | |
---|
| 14 | try: |
---|
| 15 | import Numeric |
---|
| 16 | except: |
---|
| 17 | raise 'Module Numeric must be present to run pypar' |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | print "Importing pypar" |
---|
| 21 | import pypar |
---|
| 22 | methods = dir(pypar) |
---|
| 23 | assert 'Abort' in methods |
---|
| 24 | assert 'Finalize' in methods |
---|
| 25 | assert 'Get_processor_name' in methods |
---|
| 26 | assert 'Wtime' in methods |
---|
| 27 | assert 'rank' in methods |
---|
| 28 | assert 'raw_receive' in methods |
---|
| 29 | assert 'raw_send' in methods |
---|
| 30 | assert 'receive' in methods |
---|
| 31 | assert 'send' in methods |
---|
| 32 | assert 'bcast' in methods |
---|
| 33 | assert 'size' in methods |
---|
| 34 | |
---|
| 35 | print "Module pypar imported OK" |
---|
| 36 | #pypar.Barrier() |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | myid = pypar.rank() |
---|
| 40 | numproc = pypar.size() |
---|
| 41 | node = pypar.Get_processor_name() |
---|
| 42 | |
---|
| 43 | print "I am processor %d of %d on node %s" %(myid, numproc, node) |
---|
| 44 | pypar.Barrier() |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | if 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 | print testString |
---|
| 190 | pypar.bcast(testString, 0) |
---|
| 191 | assert testString == 'test0' |
---|
| 192 | |
---|
| 193 | testString = 'test' + str(myid) |
---|
| 194 | pypar.bcast(testString, numproc-1) |
---|
| 195 | assert testString == 'test' + str(numproc-1) |
---|
| 196 | |
---|
| 197 | if myid == 0: |
---|
| 198 | print "Broadcast communication of strings OK" |
---|
| 199 | |
---|
| 200 | testArray = myid * Numeric.array(range(N)) |
---|
| 201 | pypar.bcast(testArray, 1) |
---|
| 202 | assert Numeric.allclose(testArray, 1 * testArray) |
---|
| 203 | |
---|
| 204 | if myid == 0: |
---|
| 205 | print "Broadcast communication of numeric integer array OK" |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | testArray = myid * Numeric.array(range(N)).astype('f') |
---|
| 209 | pypar.bcast(testArray, 1) |
---|
| 210 | assert Numeric.allclose(testArray, 1 * testArray) |
---|
| 211 | |
---|
| 212 | if myid == 0: |
---|
| 213 | print "Broadcast communication of numeric real array OK" |
---|
| 214 | |
---|
| 215 | testGeneral = ['ABC', myid, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 216 | |
---|
| 217 | testGeneral = pypar.bcast(testGeneral, 1) |
---|
| 218 | |
---|
| 219 | assert testGeneral == ['ABC', 1, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 220 | |
---|
| 221 | if myid == 0: |
---|
| 222 | print "Broadcast communication of general structures OK" |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | # Test scatter - with/without buffers (arrays, strings) |
---|
| 230 | # |
---|
| 231 | N = 17 #Number of elements |
---|
| 232 | |
---|
| 233 | testString = 'test' + str(myid) |
---|
| 234 | s_size = 1 |
---|
| 235 | X = ' '*s_size |
---|
| 236 | pypar.raw_scatter(testString, s_size, X, 0) |
---|
| 237 | |
---|
| 238 | Y = pypar.scatter(testString, s_size, 0) |
---|
| 239 | |
---|
| 240 | if myid == 0: |
---|
| 241 | assert X == 't' |
---|
| 242 | assert Y == 't' |
---|
| 243 | print "Scatter communication of strings OK" |
---|
| 244 | |
---|
| 245 | testArray = Numeric.array(range(N)) |
---|
| 246 | s_size = 1 |
---|
| 247 | X = Numeric.zeros(s_size) |
---|
| 248 | pypar.raw_scatter(testArray, s_size, X, 0) |
---|
| 249 | |
---|
| 250 | Y = pypar.scatter(testArray, s_size, 0) |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | if myid == 0: |
---|
| 254 | assert X == [0] |
---|
| 255 | assert Y == [0] |
---|
| 256 | print "Scatter communication of numeric integer array OK" |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | testArray = Numeric.array(range(N)).astype('f') |
---|
| 260 | s_size = 1 |
---|
| 261 | X = Numeric.zeros(s_size).astype('f') |
---|
| 262 | pypar.raw_scatter(testArray, s_size, X, 0) |
---|
| 263 | |
---|
| 264 | Y = pypar.scatter(testArray, s_size, 0) |
---|
| 265 | |
---|
| 266 | if myid == 0: |
---|
| 267 | assert X == [0.0] |
---|
| 268 | assert Y == [0.0] |
---|
| 269 | print "Scatter communication of numeric real array OK" |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|