trophia / sna1

A model of the SNA1 fishery
2 stars 2 forks source link

parallel code #32

Open Craig44 opened 6 years ago

Craig44 commented 6 years ago

Whilst I am deep in the code I was wondering if we could brainstorm ideas on how best to parallelise the code. There is a statement in the read me "IBMs are inherently easy to parrallelize and thus take advantage of multi-core and multi-machine computing environments." Could we use this thread to elaborate on the hows? of this statement

Craig44 commented 6 years ago

I found the quote that helps "the fates of all individual fish are independent it is relatively straight forward to parallelise this aspect of the code"

nokome commented 6 years ago

Hi, sorry for the slow response.

In theory, parallelism should be quite straightforward: you just create several threads (say number of cores +1) and have each thread operate on parts of the fish population. See this code for how I have done it before for an IBM: https://github.com/trophia/indi/blob/master/indi/fishes.hpp#L94

The speed benefits will be greater for large populations. When pops are small the cost of setting up threads is high relative to the speed up of parrallel execution.

Craig44 commented 6 years ago

Yeah I started to go down that track, but have gone towards using Boost asio functionality which allows us to have a pool of threads that I will make available the entire run time. That means we can easily outtask other functions (regional fishing) to the pool with out the overhead of creating destroying locally scoped threads. Will test some code today to see if it is any good.