mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: improve the pendulum simulation, with several modes and diagrams (#13446)
This commit is contained in:

committed by
GitHub

parent
a74d28ae5f
commit
4391ae563d
50
examples/pendulum-simulation/parallel_with_iw.v
Normal file
50
examples/pendulum-simulation/parallel_with_iw.v
Normal file
@ -0,0 +1,50 @@
|
||||
module main
|
||||
|
||||
import benchmark
|
||||
import sim
|
||||
import sim.args as simargs
|
||||
import sim.img
|
||||
|
||||
fn main() {
|
||||
args := simargs.parse_args(extra_workers: 1) ? as simargs.ParallelArgs
|
||||
|
||||
img_settings := img.image_settings_from_grid(args.grid)
|
||||
|
||||
request_chan := chan &sim.SimRequest{cap: args.workers}
|
||||
result_chan := chan &sim.SimResult{cap: args.workers}
|
||||
|
||||
mut writer := img.ppm_writer_for_fname(args.filename, img_settings) ?
|
||||
|
||||
mut workers := []thread{cap: args.workers + 1}
|
||||
mut bmark := benchmark.start()
|
||||
|
||||
defer {
|
||||
image_worker := workers.pop()
|
||||
request_chan.close()
|
||||
sim.log('Waiting for workers to finish')
|
||||
workers.wait()
|
||||
result_chan.close()
|
||||
sim.log('Waiting for image writer to finish')
|
||||
image_worker.wait()
|
||||
sim.log('Workers finished!')
|
||||
bmark.measure(@FN)
|
||||
sim.log('Closing writer file')
|
||||
writer.close()
|
||||
sim.log('Done!')
|
||||
}
|
||||
|
||||
for id in 0 .. args.workers {
|
||||
workers << go sim.sim_worker(id, request_chan, [result_chan])
|
||||
}
|
||||
|
||||
workers << go img.image_worker(mut writer, result_chan, img_settings)
|
||||
|
||||
handle_request := fn [request_chan] (request &sim.SimRequest) ? {
|
||||
request_chan <- request
|
||||
}
|
||||
|
||||
sim.run(args.params,
|
||||
grid: args.grid
|
||||
on_request: sim.SimRequestHandler(handle_request)
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user