#!pip install ANNarchy
Parallel simulations
This example demonstrates the use of parallel_run()
to simulate the same network multiple times in parallel.
We start by creating the Izhikevich pulse-coupled network defined in Izhikevich.ipynb.
import numpy as np
import ANNarchy as ann
ann.clear()
ANNarchy 4.8 (4.8.2) on darwin (posix).
# Create the whole population
= ann.Population(geometry=1000, neuron=ann.Izhikevich)
P
# Create the excitatory population
= P[:800]
Exc = np.random.random(800)
re = 5.0
Exc.noise = 0.02
Exc.a = 0.2
Exc.b = -65.0 + 15.0 * re**2
Exc.c = 8.0 - 6.0 * re**2
Exc.d = -65.0
Exc.v = Exc.v * Exc.b
Exc.u
# Create the Inh population
= P[800:]
Inh = np.random.random(200)
ri = 2.0
Inh.noise = 0.02 + 0.08 * ri
Inh.a = 0.25 - 0.05 * ri
Inh.b = -65.0
Inh.c = 2.0
Inh.d = -65.0
Inh.v = Inh.v * Inh.b
Inh.u
# Create the projections
= ann.Projection(Exc, P, 'exc')
proj_exc = ann.Projection(Inh, P, 'inh')
proj_inh
=ann.Uniform(0.0, 0.5))
proj_exc.connect_all_to_all(weights=ann.Uniform(0.0, 1.0))
proj_inh.connect_all_to_all(weights
# Create a spike monitor
= ann.Monitor(P, 'spike')
M
compile() ann.
Compiling ... OK
We define a simulation method that re-initializes the network, runs a simulation and returns a raster plot.
The simulation method must take an index as first argument and a Network
instance as second one.
def run_network(idx, net):
# Retrieve subpopulations
= net.get(P)
P_local = P_local[:800]
Exc = P_local[800:]
Inh # Randomize initialization
= np.random.random(800)
re = -65.0 + 15.0 * re**2
Exc.c = 8.0 - 6.0 * re**2
Exc.d = np.random.random(200)
ri = 2.0
Inh.noise = 0.02 + 0.08 * ri
Inh.a = 0.25 - 0.05 * ri
Inh.b = Inh.v * Inh.b
Inh.u # Simulate
1000.)
net.simulate(# Recordings
= net.get(M).raster_plot()
t, n return t, n
parallel_run()
uses the multiprocessing
module to start parallel processes. On Linux, it should work directly, but there is an issue on OSX. Since Python 3.8, the ‘spawn’ method is the default way to start processes, but it does not work on MacOS. The following cell should fix the issue, but it should only be ran once.
import platform
if platform.system() == "Darwin":
import multiprocessing as mp
'fork') mp.set_start_method(
We can now call parallel_run()
to simulate 8 identical but differently initialized networks. The first call runs the simulations sequentially, while the second is in parallel.
We finally plot the raster plots of the two first simulations.
# Run four identical simulations sequentially
= ann.parallel_run(method=run_network, number=8, measure_time=True, sequential=True)
vals
# Run four identical simulations in parallel
= ann.parallel_run(method=run_network, number=8, measure_time=True)
vals
# Data analysis
= vals[0]
t1, n1 = vals[1]
t2, n2
import matplotlib.pyplot as plt
=(15, 8))
plt.figure(figsize121)
plt.subplot('.')
plt.plot(t1, n1, 122)
plt.subplot('.')
plt.plot(t2, n2, plt.show()
Running 8 networks sequentially took: 1.3628458976745605
Running 8 networks in parallel took: 0.4673430919647217