- How to Program, Part I
- How to Program, Part II
- How to Program, Part III
- How to Program, Part IV
- How to Program, Part V
- exercises
- pyMPI tutorial
- Calculating PI, Part I
- Calculating PI, Part II
- Calculating PI, Part III
- Poogle - Web Search
- Mandelbrot Sets
- Mandelbrot, The Code
- Mandelbrot, The Images
- Conway's Life, Part I
- Life Code Listing
- Conway's Life, Part II
- MPI Life Code Listing
| MPI Life Code Listing
| file: lifem.py |
| 1 | import time |
| 2 | import sys |
| 3 | import mpi |
| 4 | |
| 5 | # define the board, so far so good.... |
| 6 | board = [ |
| 7 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], |
| 8 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0], |
| 9 | [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1], |
| 10 | [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1], |
| 11 | [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 12 | [1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0], |
| 13 | [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], |
| 14 | [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 15 | [0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], |
| 16 | [0,0,0,0,0,0,0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,0,0,0,0,0], |
| 42 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]] |
| 43 | |
| 44 | # split the board up into regions, each of size n rows |
| 45 | n = len(board)/mpi.size |
| 46 | |
| 47 | # subset of the board used for computation by a proc |
| 48 | ilo = mpi.rank*n |
| 49 | ihi = (mpi.rank+1)*n-1 |
| 50 | |
| 51 | # subset of the board used for computation by a proc + ghost zones |
| 52 | nlo = ilo-1 |
| 53 | nhi = ihi+1 |
| 54 | if mpi.rank == 0: # min rank |
| 55 | nlo = ilo |
| 56 | elif mpi.rank+1 == mpi.size: # max rank |
| 57 | nhi = ihi |
| 58 | |
| 59 | # print one row of the board |
| 60 | def prow(row): |
| 61 | str = "" |
| 62 | for e in row: |
| 63 | if e == 0: |
| 64 | str += ". "; |
| 65 | else: |
| 66 | str += "X "; |
| 67 | print str |
| 68 | |
| 69 | # print out the board |
| 70 | def pboard(board): |
| 71 | if mpi.rank == 0: |
| 72 | for nn in range(nhi,len(board)): |
| 73 | (row_num,row_data)=mpi.recv(mpi.ANY_SOURCE)[0] |
| 74 | board[row_num] = row_data |
| 75 | # magic to home the cursor |
| 76 | print "\033[H"; #\033[2J", |
| 77 | print "Printing Board:" |
| 78 | for row in board: |
| 79 | prow(row) |
| 80 | print |
| 81 | else: |
| 82 | for i in range(ilo,ihi+1): |
| 83 | mpi.send((i,board[i]),0) |
| 84 | mpi.barrier() |
| 85 | |
| 86 | pboard(board) |
| 87 | |
| 88 | # number of game steps to play |
| 89 | nsteps = 1000 |
| 90 | |
| 91 | # start playing |
| 92 | for step in range(nsteps): |
| 93 | # synchronize ghost zones... |
| 94 | if nlo < ilo: |
| 95 | mpi.send(board[ilo],mpi.rank-1) |
| 96 | if ihi < nhi: |
| 97 | mpi.send(board[ihi],mpi.rank+1) |
| 98 | if ihi < nhi: |
| 99 | board[nhi] = mpi.recv(mpi.rank+1)[0] |
| 100 | if nlo < ilo: |
| 101 | board[nlo] = mpi.recv(mpi.rank-1)[0] |
| 102 | |
| 103 | # create a new board with all zeros |
| 104 | nboard = [] |
| 105 | for k in range(len(board)): |
| 106 | nrow = [] |
| 107 | for j in range(len(board[k])): |
| 108 | nrow.append(0) |
| 109 | nboard.append(nrow) |
| 110 | |
| 111 | # iterate over the old board |
| 112 | for i in range(ilo,ihi+1): |
| 113 | for j in range(len(board[i])): |
| 114 | |
| 115 | # find nearest neighbors... |
| 116 | sum = 0 |
| 117 | for deli in range(-1,2): |
| 118 | for delj in range(-1,2): |
| 119 | if delj == 0 and deli == 0: |
| 120 | pass |
| 121 | elif i+deli < 0 or j+delj < 0: |
| 122 | pass |
| 123 | elif i+deli >= len(board) or j+delj >= len(board[i]): |
| 124 | pass |
| 125 | else: |
| 126 | sum = sum + board[i+deli][j+delj] |
| 127 | |
| 128 | # fill in squares on the new board |
| 129 | # rule to stay alive |
| 130 | if board[i][j]==1 and sum == 2 or sum == 3: |
| 131 | nboard[i][j] = 1 |
| 132 | |
| 133 | # rule for birth |
| 134 | elif board[i][j]==0 and sum == 3: |
| 135 | nboard[i][j] = 1 |
| 136 | |
| 137 | # stay empty / die |
| 138 | else: |
| 139 | nboard[i][j] = 0 |
| 140 | |
| 141 | # trade the old board for the new one |
| 142 | board = nboard |
| 143 | |
| 144 | # print out the current step (debug) |
| 145 | #if step < 100: |
| 146 | pboard(board) |
| 147 | |
| 148 | # print result |
| 149 | pboard(board) |
| |
| |