| 1 | module main |
| 2 | |
| 3 | import benchmark |
| 4 | import sim |
| 5 | import sim.args as simargs |
| 6 | import sim.img |
| 7 | |
| 8 | fn main() { |
| 9 | args := simargs.parse_args()! as simargs.ParallelArgs |
| 10 | |
| 11 | img_settings := img.image_settings_from_grid(args.grid) |
| 12 | |
| 13 | width := img_settings.width |
| 14 | height := img_settings.height |
| 15 | total_pixels := width * height |
| 16 | |
| 17 | request_chan := chan &sim.SimRequest{cap: args.workers} |
| 18 | result_chan := chan &sim.SimResult{cap: args.workers} |
| 19 | |
| 20 | mut writer := img.ppm_writer_for_fname(args.filename, img_settings)! |
| 21 | mut image_writer := img.new_image_writer(mut writer, img_settings) |
| 22 | |
| 23 | mut workers := []thread{cap: args.workers} |
| 24 | mut bmark := benchmark.start() |
| 25 | |
| 26 | defer { |
| 27 | request_chan.close() |
| 28 | sim.log('Waiting for workers to finish') |
| 29 | workers.wait() |
| 30 | result_chan.close() |
| 31 | bmark.measure(@FN) |
| 32 | sim.log('Closing writer file') |
| 33 | writer.close() |
| 34 | sim.log('Done!') |
| 35 | } |
| 36 | |
| 37 | for id in 0 .. args.workers { |
| 38 | workers << spawn sim.sim_worker(id, request_chan, [result_chan]) |
| 39 | } |
| 40 | |
| 41 | mut x := 0 |
| 42 | mut y := 0 |
| 43 | mut request_index := 0 |
| 44 | |
| 45 | for { |
| 46 | // setup state conditions |
| 47 | position := sim.vector( |
| 48 | x: 0.1 * ((f64(x) - 0.5 * f64(width - 1)) / f64(width - 1)) |
| 49 | y: 0.1 * ((f64(y) - 0.5 * f64(height - 1)) / f64(height - 1)) |
| 50 | z: 0.0 |
| 51 | ) |
| 52 | velocity := sim.vector(x: 0, y: 0, z: 0) |
| 53 | |
| 54 | mut state := sim.new_state( |
| 55 | position: position |
| 56 | velocity: velocity |
| 57 | ) |
| 58 | |
| 59 | state.satisfy_rope_constraint(args.params) |
| 60 | request := &sim.SimRequest{ |
| 61 | id: request_index |
| 62 | state: state |
| 63 | params: args.params |
| 64 | } |
| 65 | select { |
| 66 | result := <-result_chan { |
| 67 | image_writer.handle(result) or { break } |
| 68 | } |
| 69 | else { |
| 70 | if request.id == total_pixels { |
| 71 | continue |
| 72 | } |
| 73 | request_chan <- request |
| 74 | x++ |
| 75 | if x == width { |
| 76 | x = 0 |
| 77 | y++ |
| 78 | sim.log('y: ${y + 1}') |
| 79 | } |
| 80 | request_index++ |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |