h.fadvance() stops python threading, why?

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
rth
Posts: 37
Joined: Thu Jun 21, 2012 4:47 pm

h.fadvance() stops python threading, why?

Post by rth » Tue May 09, 2017 4:35 pm

Hi all,

I have a bit strange problem with using threading Python module with NEURON.
I run NEURON -- Release 7.4 (1370:16a7055d4a86) 2015-11-09 from Python 2.7.12 (default, Nov 19 2016, 06:48:10) script under Linux 4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux.

Sorry, probably too much details in previous post.

Here very simple code to show the problem.

Code: Select all

import threading, time, sys
from datetime import datetime
from neuron import h

buf = []

def VerySlowProcess():
	global buf
	while len(buf) > 0: 
		#Print time and a firs element of the buffer
		now = datetime.now()
		sys.stderr.write( "Time:{}.{}.{} BUFx:{}\n".format(now.hour, now.minute, now.second, buf[0]) )
		#Delete is!
		del buf[0]
		time.sleep(1)



#Fill the buffer
buf += range(100)

#Create a thread
dthread = threading.Thread(target=VerySlowProcess)
#Start it
dthread.start()

#now let's wait a bit to see that tread works well
sys.stderr.write( "Time to sleep\n" )
time.sleep(5)
sys.stderr.write( "Ready to go\n" )

#creates a 100 cables to keep neuron busy
c = [ h.Section() for x in xrange(100) ]
for n in c:
	n.nseg = 20
	n.L    = 700
	n.insert("hh")

#Now run a simulation and watch the treading
h.finitialize()
h.fcurrent()
h.frecord_init()
sys.stderr.write( "Run a simulation\n" )
while h.t < 500. : h.fadvance()
sys.stderr.write( "Finished\n" )

Result looks like this

Code: Select all

NEURON -- Release 7.4 (1370:16a7055d4a86) 2015-11-09
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits

Time to sleep
Time:9.46.59 BUFx:0
Time:9.47.0 BUFx:1
Time:9.47.1 BUFx:2
Time:9.47.2 BUFx:3
Time:9.47.3 BUFx:4
Time:9.47.4 BUFx:5
Ready to go
Run a simulation
Finished
Time:9.47.21 BUFx:6
Time:9.47.22 BUFx:7
Time:9.47.23 BUFx:8
Time:9.47.24 BUFx:9
Time:9.47.25 BUFx:10
Time:9.47.26 BUFx:11
Time:9.47.27 BUFx:12
Time:9.47.28 BUFx:13

hines
Site Admin
Posts: 1449
Joined: Wed May 18, 2005 3:32 pm

Re: h.fadvance() stops python threading, why?

Post by hines » Thu May 11, 2017 6:46 pm

I believe this is normal and expected behavior. When fadvance is called, Python gives up the global interpeter lock and gets it back when fadvance returns.
Python does not (as far as I am aware) support simultaneously executing threads. One will naturally get this kind of parallelism with MPI where each rank is a separate process.

rth
Posts: 37
Joined: Thu Jun 21, 2012 4:47 pm

Re: h.fadvance() stops python threading, why?

Post by rth » Thu May 11, 2017 7:17 pm

Thank you, for reply.
Well, it isn't fortune. I'm trying to run a quite long simulation (minutes) and of course memory couldn't hold all generated data. So the idea was to run a simulation for one second, copy all vectors into python and resume a simulation, while python saves data on disk. It isn't good idea to idle all processes for saving on disk (quite slow operation), right? MPI as well as python pipes can separate a processes but overhead for using them seems bigger than just memory copying.

Post Reply