Using pc.broadcast with python

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

Moderator: hines

Post Reply
neuromau
Posts: 97
Joined: Mon Apr 20, 2009 7:02 pm

Using pc.broadcast with python

Post by neuromau »

I am having trouble using the pc.broadcast() function correctly in Python. Here is my file mwe.py:

Code: Select all

from mpi4py import MPI
from neuron import h

MyString = "oldName"
print "The original string is: " + MyString	

pc = h.ParallelContext()

pc.barrier()
if int(pc.id()) == 0:
	MyString="newNameTest"
	print "Host 0 set the string to: " + MyString	

pc.barrier()
pc.broadcast(MyString, float(0)) # I've tried 0, float(0), int(0)
pc.barrier()
print "Host " + `int(pc.id())` + " now thinks the string is: " + MyString
And this is what it produces:

Code: Select all

munchkin:ringpy mariannebezaire$ mpiexec -n 4 python mwe.py 
numprocs=4
NEURON -- VERSION 7.5 (1455:c206cbee3a71) 2016-08-11
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2016
See http://neuron.yale.edu/neuron/credits

The original string is: oldName
The original string is: oldName
The original string is: oldName
The original string is: oldName
Host 0 set the string to: newNameTest
Host 3 now thinks the string is: oldName
Host 1 now thinks the string is: oldName
Host 2 now thinks the string is: oldName
Host 0 now thinks the string is: newNameTest
munchkin:ringpy mariannebezaire$ 
This is how I configured NEURON and the test parallel py script works fine:
>>> h.nrnversion(6)
" '--prefix=/Applications/NEURON-7.4/nrn' '--with-iv=/Applications/NEURON-7.4/iv' '--with-nrnpython=dynamic' '--with-paranrn=dynamic' 'PYLIB=-lpython' 'PYLIBLINK=-lpython'"
What am I doing wrong? Thanks!
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Using pc.broadcast with python

Post by hines »

Apologies, The documentation https://www.neuron.yale.edu/neuron/stat ... .broadcast
HOC syntax reads
pc.broadcast(strdef, root)
and it should be
pc.broadcast(&strdef, root)
That is call by reference arg instead of the default call by value. To implement this in python, one has to use a hoc reference object so the contents can be changed. ie.
broatcast_str.py

Code: Select all

from neuron import h
pc = h.ParallelContext()
rank = int(pc.id())

s = 'hello'
print("%d %s" % (rank, s))

if rank == 0:
  s = 'goodbye'

sref = h.ref(s)
pc.broadcast(sref, 0)
s = sref[0]

print("%d %s" % (rank, s))

pc.barrier()
h.quit()
produces

Code: Select all

mpiexec -n 4 nrniv -mpi -python broadcast_str.py
numprocs=4
NEURON -- ...
0 hello
1 hello
2 hello
3 hello
0 goodbye
1 goodbye
2 goodbye
3 goodbye
neuromau
Posts: 97
Joined: Mon Apr 20, 2009 7:02 pm

Re: Using pc.broadcast with python

Post by neuromau »

Ah, thank you Michael!
Post Reply