Hi Sydney,

On Wed, 10 Jul 2019 at 16:45, Shall, Sydney via Tutor <tutor@python.org> wrote:
>
> I am a relative beginner.
>
> My program models cell reproduction. I have written a program that models 
> this and it works.
>
> Now I want to model a tissue with several types of cells. I did this by 
> simply rerunning the program with different inputs (cell characteristics). 
> But now I want to send and receive signals between the cells in each 
> population. This requires some sort of concurrent processing with halts at 
> appropriate points to pass and receive signals.

You say that this "requires some sort of concurrent processing" but I
think that you are mistaken there. I have a lot of experience in
mathematical modelling and dynamic simulation including some
multi-cell models and I have never personally come across a situation
where any significant benefit could be obtained from using concurrent
processing for different parts of a single simulation. Those
situations do exist but you haven't said anything to make me think
that yours is an exceptional case.

A simple way to do this (not the only way!) is something like:

# Some data structure that stores which cells are sending messages
messages_from_cells = {}

for t in range(num_timesteps):
    # Calculate new state of all cells based only on the old states of
all cells and messages.
    new_cells = {}
    for c in range(len(cells)):
        new_cells[c] = update_cell(cells[c], messages_from_cells)
    # Update all cells synchronously:
    cells = new_cells
    # Update messages based on new cell states:
    for c in range(len(cells)):
        messages_from_cells = update_messages(cells[c], messages_from_cells)

You just need to figure out a data structure (I've suggested a dict
above) that would somehow store what messages are being passed between
which cells. You can update each cell based on the current messages
and then update the messages ready for the next timestep.

Concurrent execution is not needed: I have simulated concurrency by
using two separate loops over the cells. The result is as if each cell
was updated concurrently. Another approach is that at each timestep
you can choose a cell randomly and update that one keeping all the
others constant. It really depends what kind of model you are using.

In a simulation context like this there are two different reasons why
you might conceivably want to use concurrent execution:

1. Your simulations are CPU-bound and are slow and you need to make
them run faster by using more cores.
2. Your simulation needs more memory then an individual computer has
and you need to run it over a cluster of many computers.

Python's multiprocessing module can help with the first problem: it
can theoretically make your simulations run faster. However it is hard
to actually achieve any speedup that way. Most likely there are other
ways to make your code run faster that are easier than using
concurrent execution and can deliver bigger gains. Multiprocessing
used well might make your code 10x faster but I will bet that there
are easier ways to make your code 100x faster.

Multiprocessing makes the second problem worse: it actually uses more
memory on each computer. To solve problem 2 is very hard but can be
done. I don't think either problem applies to you though.

There is a situation where I have used multiprocessing to make
simulations faster. In practice I rarely want to do just one
simulation; I want to do thousands with different parameters or
because they are stochastic and I want to average them. Running these
thousands of simulations can be made faster easily with
multiprocessing because it is an "embarrassingly parallel" problem.
You need to get your simulations working without multiprocessing first
though. This is a much easier way to solve problem 1 (in so far as
using more cores can help).

Side note: you've misspelt Professor in your email signature:

> Prodessor. Sydney Shall

--
Oscar
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to