Download PS8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Architecture and
Assembly Language
Practical Session 8
Game of life
• Simulates Evolution of
two-dimensional matrix’s
cells.
• Each cell can be alive or
dead.
• A cell’s state in each
iteration (generation) is set
with accordance to its state
and its neighbors’ states.
Cell neighbors
Every cell has 8 neighbors
N1
N2
N3
N4
me
N5
N6
N7
N8
Even at the board edges
– cells of the first row are neighbors of the cells in the last row
– cells of the first column are neighbors of the cells in the last column
Game rules
If the cell is currently alive, then it will remain alive in the next generation if and only if
exactly 2 or 3 of its neighbors are currently alive. Otherwise it dies.
Examples:
N1
N2
N3
N1
N2
N3
N4
me
N5
N4
me
N5
N6
N7
N8
N6
N7
N8
N1
N2
N3
N4
me
N5
N6
N7
N8
me stays alive
me dies
A dead cell remains dead in the next generation, unless it has exactly 3 living neighbors.
Examples:
N1
N2
N3
N4
me
N5
N6
N7
N8
me comes alive
N1
N2
N3
N4
me
N5
N6
N7
N8
me stays dead
Organism age is the number of generation it was alive in a row. Maximum age is 9*.
*A cell does not die after age 9, it continues its life according to the game rules, but its age stays unchanged.
Input file
•
Each cell is given an initial state from input file.
1 1
1 1
1 1
1 1
•
1
1
1
alive cells are denoted by ‘1’ in input file
empty (dead) cells are denoted by space in input file
At each generation, a cell will determine its next state according to its former
state and its neighbors’ former states, using the game rules.
After calculation of the next state, each cell updates its state.
1 1
1 1
1 1
1 1
1
1
1
1 1
1 1
calculate
next state
1 1
1 1
1
1 1 1
1
2 2
2 2
update
next state
2 2
2 2
1 2 1
Implementation
Using the co-routine mechanism, with the
following co-routines:
–n cell instances (2-dimensional matrix)
– printer
– scheduler
Cell
• Cell can be alive(‘1’-’9’) or dead(‘ ‘)
• Cell executes a simple infinite loop:
1. Calculate next state using current state (of a cell
and its neighbors)
2. Update current state of to a new state
• After each of these two stages, the cell coroutine must resume the scheduler.
In other words, it loops (forever) over:
(stage 1) resume  (stage 2) resume
Scheduler
• implements simple round-robin algorithm
• void scheduler (int cycles)
– iterate cycles times over all cells
– after each K resumes of cell co-routines, resume the
printer (1) resume  (2) resume …
– right before exit the program, resume the printer
once more, and then terminate the process
Printer
• prints the entire “world” (the global array),
whenever it gets “time”
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
print
matrix
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations)
resume(cell (row, column))
if (time to resume printer)
resume (printer)
column=(column+1)%maximalNumberOfColumns
if(column ==0)
row=(row+1)%maximalNumberOfRows
if (row==0 && column==0)
numberOfGenerations+=0.5
Printer
Scheduler
Cell (0,0)
Cell (0,1)
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
calculate stage
calculate stage
calculate stage
update matrix
update matrix
update matrix
…
…
…
Cell (n-1, n-1)
…
Program’s flow
> ass3 <filename> <length> <width> <t> <K>
‒ <filename> - name of a file contain the initial state
of the game board. A series of size <width> of ‘ ‘ and ‘1’ in each
line. The file contain <length> lines.
‒ <t> - number of generations
‒ <K> - printing frequency
‒ your array dimensions will be <width>*<length>
Program’s flow
When invoked, and using the co-routines mechanism from class,
your application will:
– Set up: a state array, Length, Width, K, t
– Initiate all co-routines:
• each cell gets parameters i,j - its indices in the global array
• a scheduler gets t, K, Length, and Width as parameters
• a printer gets Length and Width as parameters
– Initiate an array CORS of pointers to:
cell0,0,…,celllength-1,width-1, scheduler, printer
– Transfer control to scheduler
Program’s flow
Each cell in CORS array will point directly to the top of the
stack of the corresponded co-routine.
You don’t have to implement the co-routine structure as presented in the
practical session.
CORS:
CO(0,0)
CO(0,1)
…
CO(n-1, n-1)
CoPrinter
CoScheduler
CO(i,j):
Function
Flags
SP(i,j)
CORS:
SP(0,0)
SP(0,1)
…
SP(n-1, n-1)
SP_CoPrinter
SP_CoScheduler
Related documents