1 / 35

Practical Session 9

Simulates the evolution of a two-dimensional hexagonal matrix's cells based on specific rules. Each cell's state in each generation depends on its state and its neighbors' states. Implementing co-routine mechanisms for cell instances, printer, and scheduler to manage the simulation flow efficiently.

lrussell
Download Presentation

Practical Session 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Architecture and Assembly Language Practical Session 9

  2. Game of life You can see the simulator here. …

  3. Game of life • Simulates Evolution of two-dimensional hexagonal matrix’s cells. • Eachcellcan be aliveordead. • A cell’s state in each iteration (generation) is set with accordance to its state and its neighbors’ states.

  4. Game rules If the cell is currently alive, then it will remain alive in the next generation if and only if exactly 3 or 4 of its neighbors are currently alive. Otherwise it dies. Examples: me me stays alive me stays alive me me me A deadcell remains dead in the next generation, unless it has exactly 2 living neighbors. Example: me comes alive me me

  5. Cell neighbors 1 2 me 3 6 4 5 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 4 6 5 2 3 1

  6. Input file alive cells are denoted by ‘1’ in input file • Each cell is given an initial statefrom input file. dead cells are denoted by ‘0’ 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. calculate next state updatenext state v x x v v x x v v x x v

  7. Cell neighbors 1 2 1 2 or me 3 me 6 3 6 4 5 4 5

  8. Implementation Using the co-routine mechanism, with the following co-routines: • n cell instances (2-dimensional matrix) • a printer • a scheduler

  9. A Cell • Cell can be alive(‘1’) or dead(‘0‘) • Cell executes a simple infinite loop: • Calculate next state using current state (of a cell and its neighbors) • Update current state of to a new state • After each of these two stages, the cellco-routine must resume the scheduler. In other words, it loops (forever) over: (stage 1) resume  (stage 2) resume

  10. A scheduler • implements simple round-robin algorithm • void scheduler (int cycles) • iterate cycles times over all cells • after each P 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 The printer • Printsthe entire “world” (the global array), whenever it gets “time”.

  11. 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) … Cell (n-1, n-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 … … …

  12. 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) … Cell (n-1, n-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 … … …

  13. 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) … Cell (n-1, n-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 … … …

  14. 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) … Cell (n-1, n-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 … … …

  15. 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) … Cell (n-1, n-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 … … …

  16. 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) … Cell (n-1, n-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 … … …

  17. 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) … Cell (n-1, n-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 … … …

  18. 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) … Cell (n-1, n-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 … … …

  19. 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) … Cell (n-1, n-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 … … …

  20. 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) … Cell (n-1, n-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 … … …

  21. 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) … Cell (n-1, n-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 … … …

  22. 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) … Cell (n-1, n-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 … … …

  23. 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) … Cell (n-1, n-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 … … …

  24. 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) … Cell (n-1, n-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 … … …

  25. 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) … Cell (n-1, n-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 … … …

  26. 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) … Cell (n-1, n-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 … … …

  27. 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) … Cell (n-1, n-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 … … …

  28. 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) … Cell (n-1, n-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 … … …

  29. 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) … Cell (n-1, n-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 … … …

  30. 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) … Cell (n-1, n-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 … … …

  31. 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) … Cell (n-1, n-1) print matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix … … …

  32. 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) … Cell (n-1, n-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 … … …

  33. Program’s flow > ass3 <filename> <length> <width> <g> <P> • <filename> - name of a file contain the initial state of the game board. A series of size <2*width> of ‘0 ‘ and ‘1’ in each line, separated by space. The file contain <length> lines. • <g> - number of generations • <P> - printing frequency • your array dimensions will be <width>*<length>

  34. Program’s flow When invoked, and using the co-routines mechanism from class, your application will: • Set up: a state array, Length, Width, P, g • Initiate all co-routines: • each cell gets parameters i,j - its indices in the global array • a scheduler gets g, P, Length, and Width as parameters • a printer gets Length and Width as parameters • Initiate an array CORSof pointers to: cell0,0,…,celllength-1,width-1, scheduler, printer  • Transfer control to scheduler

  35. Program’s flow You may build cell in CORS array will point directly to the top of the stack of the corresponded co-routine. CORS: SP_CoPrinter SP_CoScheduler SP(0,0) SP(0,1) … SP(i, j) … CORS: CoPrinter CoScheduler CO(0,0) CO(0,1) … CO(i, j) … CO(i,j): Function Flags SP(i,j)

More Related