[85] | 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 | 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 | # 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 | |
---|
| 565 | testGeneral = ['ABC', myid, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 566 | |
---|
| 567 | testGeneral = pypar.bcast(testGeneral, 1) |
---|
| 568 | |
---|
| 569 | assert testGeneral == ['ABC', 1, (1,2,3), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 570 | |
---|
| 571 | if myid == 0: |
---|
| 572 | print "Broadcast communication of general structures OK" |
---|
| 573 | |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | |
---|
| 579 | # Test scatter - with/without buffers (arrays, strings) |
---|
| 580 | # |
---|
| 581 | N = 17 #Number of elements |
---|
| 582 | |
---|
| 583 | testString = 'test' + str(myid) |
---|
| 584 | X = ' '*len(testString) |
---|
| 585 | pypar.raw_scatter(testString, X, 0) |
---|
| 586 | |
---|
| 587 | Y = pypar.scatter(testString, 0) |
---|
| 588 | |
---|
| 589 | if myid == 0: |
---|
| 590 | assert X == testString |
---|
| 591 | assert Y == testString |
---|
| 592 | print "Scatter communication of strings OK" |
---|
| 593 | |
---|
| 594 | testArray = Numeric.array(range(N)) |
---|
| 595 | X = Numeric.zeros(N) |
---|
| 596 | pypar.raw_scatter(testArray, X, 0) |
---|
| 597 | |
---|
| 598 | Y = pypar.scatter(testArray, 0) |
---|
| 599 | |
---|
| 600 | if myid == 0: |
---|
| 601 | assert Numeric.allclose(testArray, Y) |
---|
| 602 | assert Numeric.allclose(X, Y) |
---|
| 603 | print "Scatter communication of numeric integer array OK" |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | testArray = Numeric.array(range(N)).astype('f') |
---|
| 607 | X = Numeric.zeros(N).astype('f') |
---|
| 608 | pypar.raw_scatter(testArray, X, 0) |
---|
| 609 | |
---|
| 610 | Y = pypar.scatter(testArray, 0) |
---|
| 611 | |
---|
| 612 | if myid == 0: |
---|
| 613 | assert Numeric.allclose(testArray, Y) |
---|
| 614 | assert Numeric.allclose(X, Y) |
---|
| 615 | print "Scatter communication of numeric real array OK" |
---|
| 616 | |
---|
| 617 | |
---|
| 618 | testArray = Numeric.array(range(N)).astype('D') |
---|
| 619 | X = Numeric.zeros(N).astype('D') |
---|
| 620 | pypar.raw_scatter(testArray, X, 0) |
---|
| 621 | |
---|
| 622 | Y = pypar.scatter(testArray, 0) |
---|
| 623 | if myid == 0: |
---|
| 624 | assert Numeric.allclose(testArray, Y) |
---|
| 625 | assert Numeric.allclose(X, Y) |
---|
| 626 | print "Scatter communication of numeric complex array OK" |
---|
| 627 | |
---|
| 628 | M = 13 |
---|
| 629 | testArray = Numeric.array(range(M*N)).astype('D') |
---|
| 630 | testArray = Numeric.reshape(testArray, (M,N)) |
---|
| 631 | X = Numeric.zeros(M*N).astype('D') |
---|
| 632 | X = Numeric.reshape(X, (M,N)) |
---|
| 633 | |
---|
| 634 | pypar.raw_scatter(testArray, X, 0) |
---|
| 635 | |
---|
| 636 | Y = pypar.scatter(testArray, 0) |
---|
| 637 | |
---|
| 638 | if myid == 0: |
---|
| 639 | assert Numeric.allclose(testArray, Y) |
---|
| 640 | assert Numeric.allclose(X, Y) |
---|
| 641 | print "Scatter communication of 2D numeric complex array OK" |
---|
| 642 | |
---|
| 643 | |
---|
| 644 | |
---|
| 645 | # Test gather - with/without buffers (arrays, strings) |
---|
| 646 | # |
---|
| 647 | N = 17 #Number of elements |
---|
| 648 | |
---|
| 649 | testString = 'AB' |
---|
| 650 | X = '_'*(len(testString)*numproc) #Blanks caused errors when numproc >= 6 |
---|
| 651 | pypar.raw_gather(testString, X, 0) |
---|
| 652 | |
---|
| 653 | Y = pypar.gather(testString, 0) |
---|
| 654 | |
---|
| 655 | if myid == 0: |
---|
| 656 | assert X == 'AB' * numproc |
---|
| 657 | assert Y == 'AB' * numproc |
---|
| 658 | print "Gather communication of strings OK" |
---|
| 659 | |
---|
| 660 | |
---|
| 661 | testArray = Numeric.array(range(N)) |
---|
| 662 | X = Numeric.zeros(N*numproc) |
---|
| 663 | pypar.raw_gather(testArray, X, 0) |
---|
| 664 | |
---|
| 665 | Y = pypar.gather(testArray, 0) |
---|
| 666 | |
---|
| 667 | if myid == 0: |
---|
| 668 | for i in range(numproc): |
---|
| 669 | assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)]) |
---|
| 670 | |
---|
| 671 | assert Numeric.allclose(X, Y) |
---|
| 672 | print "Gather communication of numeric integer array OK" |
---|
| 673 | |
---|
| 674 | |
---|
| 675 | testArray = Numeric.array(range(N)).astype('f') |
---|
| 676 | X = Numeric.zeros(N*numproc).astype('f') |
---|
| 677 | pypar.raw_gather(testArray, X, 0) |
---|
| 678 | |
---|
| 679 | Y = pypar.gather(testArray, 0) |
---|
| 680 | if myid == 0: |
---|
| 681 | for i in range(numproc): |
---|
| 682 | assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)]) |
---|
| 683 | assert Numeric.allclose(X, Y) |
---|
| 684 | print "Gather communication of numeric real array OK" |
---|
| 685 | |
---|
| 686 | |
---|
| 687 | testArray = Numeric.array(range(N)).astype('D') |
---|
| 688 | X = Numeric.zeros(N*numproc).astype('D') |
---|
| 689 | pypar.raw_gather(testArray, X, 0) |
---|
| 690 | |
---|
| 691 | Y = pypar.gather(testArray, 0) |
---|
| 692 | if myid == 0: |
---|
| 693 | for i in range(numproc): |
---|
| 694 | assert Numeric.allclose(testArray, X[(i * N): ((i+1)*N)]) |
---|
| 695 | assert Numeric.allclose(X, Y) |
---|
| 696 | print "Gather communication of numeric complex arrays OK" |
---|
| 697 | |
---|
| 698 | M = 13 |
---|
| 699 | testArray = Numeric.array(range(M*N)).astype('D') |
---|
| 700 | testArray = Numeric.reshape(testArray, (M,N)) |
---|
| 701 | X = Numeric.zeros(M*N*numproc).astype('D') |
---|
| 702 | X = Numeric.reshape(X, (M*numproc,N)) |
---|
| 703 | |
---|
| 704 | pypar.raw_gather(testArray, X, 0) |
---|
| 705 | |
---|
| 706 | Y = pypar.gather(testArray, 0) |
---|
| 707 | if myid == 0: |
---|
| 708 | for i in range(numproc): |
---|
| 709 | assert Numeric.allclose(testArray, X[(i * M): ((i+1)*M), :]) |
---|
| 710 | assert Numeric.allclose(X, Y) |
---|
| 711 | print "Gather communication of 2D numeric complex arrays OK" |
---|
| 712 | |
---|
| 713 | |
---|
| 714 | ######################################################## |
---|
| 715 | # Test reduce |
---|
| 716 | ####################################################### |
---|
| 717 | N = 17 #Number of elements |
---|
| 718 | |
---|
| 719 | # Create one (different) array on each processor |
---|
| 720 | # |
---|
| 721 | testArray = Numeric.array(range(N)) * (myid+1) |
---|
| 722 | #print testArray |
---|
| 723 | X = Numeric.zeros(N) # Buffer for results |
---|
| 724 | |
---|
| 725 | pypar.raw_reduce(testArray, X, pypar.SUM, 0) |
---|
| 726 | if myid == 0: |
---|
| 727 | Y = Numeric.zeros(N) |
---|
| 728 | for i in range(numproc): |
---|
| 729 | Y = Y+Numeric.array(range(N))*(i+1) |
---|
| 730 | #print X |
---|
| 731 | #print Y |
---|
| 732 | assert Numeric.allclose(X, Y) |
---|
| 733 | print "Raw reduce using pypar.SUM OK" |
---|
| 734 | |
---|
| 735 | pypar.raw_reduce(testArray, X, pypar.MAX, 0, 0) |
---|
| 736 | if myid == 0: |
---|
| 737 | Y = Numeric.array(range(N))*numproc |
---|
| 738 | assert Numeric.allclose(X, Y) |
---|
| 739 | print "Raw reduce using pypar.MAX OK" |
---|
| 740 | |
---|
| 741 | pypar.raw_reduce(testArray, X, pypar.MIN, 0, 0) |
---|
| 742 | if myid == 0: |
---|
| 743 | Y = Numeric.array(range(N)) |
---|
| 744 | assert Numeric.allclose(X, Y) |
---|
| 745 | print "Raw reduce using pypar.MIN OK" |
---|
| 746 | |
---|
| 747 | if numproc <= 20: |
---|
| 748 | testArray_float = testArray.astype('f') #Make room for big results |
---|
| 749 | X_float = X.astype('f') |
---|
| 750 | pypar.raw_reduce(testArray_float, X_float, pypar.PROD, 0, 0) |
---|
| 751 | if myid == 0: |
---|
| 752 | Y = Numeric.ones(N).astype('f') |
---|
| 753 | for i in range(numproc): |
---|
| 754 | Y = Y*Numeric.array(range(N))*(i+1) |
---|
| 755 | #print X_float |
---|
| 756 | #print Y |
---|
| 757 | assert Numeric.allclose(X_float, Y) |
---|
| 758 | print "Raw reduce using pypar.PROD OK" |
---|
| 759 | else: |
---|
| 760 | if myid == 0: |
---|
| 761 | print "Skipping product-reduce - try again with numproc < 20" |
---|
| 762 | |
---|
| 763 | pypar.raw_reduce(testArray, X, pypar.LAND, 0, 0) |
---|
| 764 | if myid == 0: |
---|
| 765 | Y = Numeric.ones(N) |
---|
| 766 | for i in range(numproc): |
---|
| 767 | Y = Numeric.logical_and(Y, Numeric.array(range(N))*(i+1)) |
---|
| 768 | assert Numeric.allclose(X, Y) |
---|
| 769 | print "Raw reduce using pypar.LAND OK" |
---|
| 770 | |
---|
| 771 | pypar.raw_reduce(testArray, X, pypar.BAND, 0, 0) |
---|
| 772 | if myid == 0: |
---|
| 773 | Y = Numeric.ones(N)*255 #Neutral element for & |
---|
| 774 | for i in range(numproc): |
---|
| 775 | Y = Numeric.bitwise_and(Y, Numeric.array(range(N))*(i+1)) |
---|
| 776 | assert Numeric.allclose(X, Y) |
---|
| 777 | print "Raw reduce using pypar.BAND OK" |
---|
| 778 | |
---|
| 779 | pypar.raw_reduce(testArray, X, pypar.LOR, 0, 0) |
---|
| 780 | if myid == 0: |
---|
| 781 | Y = Numeric.zeros(N) |
---|
| 782 | for i in range(numproc): |
---|
| 783 | Y = Numeric.logical_or(Y, Numeric.array(range(N))*(i+1)) |
---|
| 784 | assert Numeric.allclose(X, Y) |
---|
| 785 | print "Raw reduce using pypar.LOR OK" |
---|
| 786 | |
---|
| 787 | pypar.raw_reduce(testArray, X, pypar.BOR, 0, 0) |
---|
| 788 | if myid == 0: |
---|
| 789 | Y = Numeric.zeros(N) #Neutral element for | |
---|
| 790 | for i in range(numproc): |
---|
| 791 | Y = Numeric.bitwise_or(Y, Numeric.array(range(N))*(i+1)) |
---|
| 792 | assert Numeric.allclose(X, Y) |
---|
| 793 | print "Raw reduce using pypar.BOR OK" |
---|
| 794 | |
---|
| 795 | pypar.raw_reduce(testArray, X, pypar.LXOR, 0, 0) |
---|
| 796 | if myid == 0: |
---|
| 797 | Y = Numeric.zeros(N) |
---|
| 798 | for i in range(numproc): |
---|
| 799 | Y = Numeric.logical_xor(Y, Numeric.array(range(N))*(i+1)) |
---|
| 800 | assert Numeric.allclose(X, Y) |
---|
| 801 | print "Raw reduce using pypar.LXOR OK" |
---|
| 802 | |
---|
| 803 | pypar.raw_reduce(testArray, X, pypar.BXOR, 0, 0) |
---|
| 804 | if myid == 0: |
---|
| 805 | Y = Numeric.zeros(N) #Neutral element for xor ? |
---|
| 806 | for i in range(numproc): |
---|
| 807 | Y = Numeric.bitwise_xor(Y, Numeric.array(range(N))*(i+1)) |
---|
| 808 | assert Numeric.allclose(X, Y) |
---|
| 809 | print "Raw reduce using pypar.BXOR OK" |
---|
| 810 | |
---|
| 811 | # NOT YET SUPPORTED |
---|
| 812 | # |
---|
| 813 | #pypar.raw_reduce(testArray, X, N, pypar.MAXLOC, 0, 0) |
---|
| 814 | #if myid == 0: |
---|
| 815 | # print 'MAXLOC', X |
---|
| 816 | #pypar.raw_reduce(testArray, X, N, pypar.MINLOC, 0, 0) |
---|
| 817 | #if myid == 0: |
---|
| 818 | # print 'MINLOC', X |
---|
| 819 | |
---|
| 820 | # |
---|
| 821 | # FIXME |
---|
| 822 | # Don't know how to test this (not available on all MPI systems) |
---|
| 823 | # |
---|
| 824 | #pypar.raw_reduce(testArray, X, N, pypar.REPLACE, 0, 0) |
---|
| 825 | #if myid == 0: |
---|
| 826 | # print 'REPLACE', X |
---|
| 827 | |
---|
| 828 | |
---|
| 829 | |
---|
| 830 | # Test status block (simple communication) |
---|
| 831 | N = 17 |
---|
| 832 | if myid == 0: |
---|
| 833 | # Integer arrays |
---|
| 834 | # |
---|
| 835 | A = Numeric.array(range(N)) |
---|
| 836 | |
---|
| 837 | pypar.send(A,1) |
---|
| 838 | B, status = pypar.receive(numproc-1, return_status = True) |
---|
| 839 | |
---|
| 840 | #print status |
---|
| 841 | |
---|
| 842 | sz = A.itemsize() |
---|
| 843 | assert Numeric.allclose(A, B) |
---|
| 844 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 845 | %(status.length, len(B)) |
---|
| 846 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 847 | %(status.size, sz) |
---|
| 848 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 849 | %(status.tag, pypar.default_tag) |
---|
| 850 | assert status.error == 0 |
---|
| 851 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 852 | %(status.source, numproc-1) |
---|
| 853 | |
---|
| 854 | print "Status object (numeric integer arrays) OK" |
---|
| 855 | |
---|
| 856 | # Real arrays |
---|
| 857 | # |
---|
| 858 | A = Numeric.array(range(N)).astype('f') |
---|
| 859 | pypar.send(A,1) |
---|
| 860 | B, status = pypar.receive(numproc-1, return_status = True) |
---|
| 861 | |
---|
| 862 | sz = A.itemsize() |
---|
| 863 | assert Numeric.allclose(A, B) |
---|
| 864 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 865 | %(status.length, len(B)) |
---|
| 866 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 867 | %(status.size, sz) |
---|
| 868 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 869 | %(status.tag, pypar.default_tag) |
---|
| 870 | assert status.error == 0 |
---|
| 871 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 872 | %(status.source, numproc-1) |
---|
| 873 | |
---|
| 874 | print "Status object (numeric real arrays) OK" |
---|
| 875 | |
---|
| 876 | # Strings |
---|
| 877 | # |
---|
| 878 | A = "and now to something completely different !" |
---|
| 879 | pypar.send(A,1) |
---|
| 880 | B, status = pypar.receive(numproc-1, return_status = True) |
---|
| 881 | |
---|
| 882 | sz = 1 #Characters are one byte long |
---|
| 883 | assert A == B |
---|
| 884 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 885 | %(status.length, len(B)) |
---|
| 886 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 887 | %(status.size, sz) |
---|
| 888 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 889 | %(status.tag, pypar.default_tag) |
---|
| 890 | assert status.error == 0 |
---|
| 891 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 892 | %(status.source, numproc-1) |
---|
| 893 | |
---|
| 894 | print "Status object (strings) OK" |
---|
| 895 | |
---|
| 896 | # A more general structure |
---|
| 897 | # |
---|
| 898 | A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 899 | pypar.send(A,1) |
---|
| 900 | B, status = pypar.receive(numproc-1, return_status = True) |
---|
| 901 | |
---|
| 902 | assert A == B |
---|
| 903 | |
---|
| 904 | #Length is the number of characters needed to encode the structure |
---|
| 905 | #Can't think of a test. |
---|
| 906 | |
---|
| 907 | sz = 1 |
---|
| 908 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 909 | %(status.size, sz) |
---|
| 910 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 911 | %(status.tag, pypar.default_tag) |
---|
| 912 | assert status.error == 0 |
---|
| 913 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 914 | %(status.source, numproc-1) |
---|
| 915 | |
---|
| 916 | print "Status object (general structures) OK" |
---|
| 917 | |
---|
| 918 | else: |
---|
| 919 | # Integers |
---|
| 920 | # |
---|
| 921 | X=pypar.receive(myid-1) |
---|
| 922 | pypar.send(X, (myid+1)%numproc) |
---|
| 923 | |
---|
| 924 | # Floats |
---|
| 925 | # |
---|
| 926 | X=pypar.receive(myid-1) |
---|
| 927 | pypar.send(X, (myid+1)%numproc) |
---|
| 928 | |
---|
| 929 | # Strings |
---|
| 930 | # |
---|
| 931 | X=pypar.receive(myid-1) |
---|
| 932 | pypar.send(X, (myid+1)%numproc) |
---|
| 933 | |
---|
| 934 | # General |
---|
| 935 | # |
---|
| 936 | X = pypar.receive(myid-1) |
---|
| 937 | pypar.send(X, (myid+1)%numproc) |
---|
| 938 | |
---|
| 939 | |
---|
| 940 | |
---|
| 941 | |
---|
| 942 | |
---|
| 943 | |
---|
| 944 | |
---|
| 945 | |
---|
| 946 | # Test status block (raw communication) |
---|
| 947 | N = 17 #Number of elements |
---|
| 948 | if myid == 0: |
---|
| 949 | # Integer arrays |
---|
| 950 | # |
---|
| 951 | A = Numeric.array(range(N)) |
---|
| 952 | B = Numeric.zeros(N) |
---|
| 953 | pypar.raw_send(A,1) |
---|
| 954 | B, status = pypar.raw_receive(B,numproc-1,return_status=True) |
---|
| 955 | |
---|
| 956 | assert Numeric.allclose(A, B) |
---|
| 957 | |
---|
| 958 | sz = A.itemsize() |
---|
| 959 | assert Numeric.allclose(A, B) |
---|
| 960 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 961 | %(status.length, len(B)) |
---|
| 962 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 963 | %(status.size, sz) |
---|
| 964 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 965 | %(status.tag, pypar.default_tag) |
---|
| 966 | assert status.error == 0 |
---|
| 967 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 968 | %(status.source, numproc-1) |
---|
| 969 | |
---|
| 970 | print "Status object (raw numeric integer arrays) OK" |
---|
| 971 | |
---|
| 972 | |
---|
| 973 | # Real arrays |
---|
| 974 | # |
---|
| 975 | A = Numeric.array(range(N)).astype('f') |
---|
| 976 | B = Numeric.zeros(N).astype('f') |
---|
| 977 | pypar.raw_send(A,1) |
---|
| 978 | B, status = pypar.raw_receive(B,numproc-1,return_status=True) |
---|
| 979 | |
---|
| 980 | assert Numeric.allclose(A, B) |
---|
| 981 | sz = A.itemsize() |
---|
| 982 | assert Numeric.allclose(A, B) |
---|
| 983 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 984 | %(status.length, len(B)) |
---|
| 985 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 986 | %(status.size, sz) |
---|
| 987 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 988 | %(status.tag, pypar.default_tag) |
---|
| 989 | assert status.error == 0 |
---|
| 990 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 991 | %(status.source, numproc-1) |
---|
| 992 | |
---|
| 993 | print "Status object (raw numeric real arrays) OK" |
---|
| 994 | |
---|
| 995 | # Strings (< 256 characters) |
---|
| 996 | # |
---|
| 997 | A = "and now to something completely different !" |
---|
| 998 | B = " "*len(A) |
---|
| 999 | pypar.raw_send(A,1) |
---|
| 1000 | B, status = pypar.raw_receive(B,numproc-1,return_status=True) |
---|
| 1001 | |
---|
| 1002 | |
---|
| 1003 | sz = 1 #Characters are one byte long |
---|
| 1004 | assert A == B |
---|
| 1005 | assert len(B) == status.length, 'Reported length == %d should be %d'\ |
---|
| 1006 | %(status.length, len(B)) |
---|
| 1007 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 1008 | %(status.size, sz) |
---|
| 1009 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 1010 | %(status.tag, pypar.default_tag) |
---|
| 1011 | assert status.error == 0 |
---|
| 1012 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 1013 | %(status.source, numproc-1) |
---|
| 1014 | |
---|
| 1015 | print "Status object (raw strings) OK" |
---|
| 1016 | |
---|
| 1017 | |
---|
| 1018 | |
---|
| 1019 | # A more general structure |
---|
| 1020 | # |
---|
| 1021 | A = ['ABC', (1,2,3.14), {8: 'Monty'}, Numeric.array([13.45, 1.2])] |
---|
| 1022 | B = [' ', (0,0,0.0), {0: ' '}, Numeric.zeros(2).astype('f')] |
---|
| 1023 | pypar.raw_send(A,1) |
---|
| 1024 | B, status = pypar.raw_receive(B,numproc-1, return_status=True) |
---|
| 1025 | |
---|
| 1026 | |
---|
| 1027 | assert A == B |
---|
| 1028 | sz = 1 |
---|
| 1029 | assert status.size == sz, 'Reported size == %d should be %d'\ |
---|
| 1030 | %(status.size, sz) |
---|
| 1031 | assert status.tag == pypar.default_tag, 'Reported tag == %d should be %d'\ |
---|
| 1032 | %(status.tag, pypar.default_tag) |
---|
| 1033 | assert status.error == 0 |
---|
| 1034 | assert status.source == numproc-1, 'Reported source == %d should be %d'\ |
---|
| 1035 | %(status.source, numproc-1) |
---|
| 1036 | |
---|
| 1037 | |
---|
| 1038 | print "Status object (raw general structures) OK" |
---|
| 1039 | |
---|
| 1040 | |
---|
| 1041 | else: |
---|
| 1042 | # Integers |
---|
| 1043 | # |
---|
| 1044 | X = Numeric.zeros(N) |
---|
| 1045 | pypar.raw_receive(X, myid-1) |
---|
| 1046 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 1047 | |
---|
| 1048 | # Floats |
---|
| 1049 | # |
---|
| 1050 | X = Numeric.zeros(N).astype('f') |
---|
| 1051 | pypar.raw_receive(X, myid-1) |
---|
| 1052 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 1053 | |
---|
| 1054 | |
---|
| 1055 | # Strings |
---|
| 1056 | # |
---|
| 1057 | X = " "*256 |
---|
| 1058 | X = pypar.raw_receive(X, myid-1) |
---|
| 1059 | pypar.raw_send(X.strip(), (myid+1)%numproc) |
---|
| 1060 | |
---|
| 1061 | # General |
---|
| 1062 | # |
---|
| 1063 | X = [' ', (0,0,0.0), {0: ' '}, Numeric.zeros(2).astype('f')] |
---|
| 1064 | X = pypar.raw_receive(X, myid-1) |
---|
| 1065 | pypar.raw_send(X, (myid+1)%numproc) |
---|
| 1066 | |
---|
| 1067 | |
---|
| 1068 | |
---|
| 1069 | pypar.Finalize() |
---|
| 1070 | |
---|
| 1071 | |
---|
| 1072 | |
---|
| 1073 | |
---|