summaryrefslogtreecommitdiff
path: root/generator/photon.py
diff options
context:
space:
mode:
Diffstat (limited to 'generator/photon.py')
-rw-r--r--generator/photon.py52
1 files changed, 27 insertions, 25 deletions
diff --git a/generator/photon.py b/generator/photon.py
index e656428..6c656b2 100644
--- a/generator/photon.py
+++ b/generator/photon.py
@@ -25,24 +25,29 @@ class G4GeneratorProcess(multiprocessing.Process):
photon_socket = context.socket(zmq.PUSH)
photon_socket.connect(self.photon_socket_address)
- # Signal with the photon socket that we are online and ready for messages
+ # Signal with the photon socket that we are online
+ # and ready for messages.
photon_socket.send('READY')
while True:
ev = vertex_socket.recv_pyobj()
- ev.photon_start = gen.generate_photons(ev)
+ ev.photons_beg = gen.generate_photons(ev.vertices)
+ #print 'type(ev.photons_beg) is %s' % type(ev.photons_beg)
photon_socket.send_pyobj(ev)
def partition(num, partitions):
- '''Generator that returns num//partitions, with the last item including the remainder.
-
- Useful for partitioning a number into mostly equal parts while preserving the sum.
-
- >>> list(partition(800, 3))
- [266, 266, 268]
- >>> sum(list(partition(800, 3)))
- 800
- '''
+ """Generator that returns num//partitions, with the last item including
+ the remainder.
+
+ Useful for partitioning a number into mostly equal parts while preserving
+ the sum.
+
+ Examples:
+ >>> list(partition(800, 3))
+ [266, 266, 268]
+ >>> sum(list(partition(800, 3)))
+ 800
+ """
step = num // partitions
for i in xrange(partitions):
if i < partitions - 1:
@@ -51,8 +56,8 @@ def partition(num, partitions):
yield step + (num % partitions)
def vertex_sender(vertex_iterator, vertex_socket):
- for ev in vertex_iterator:
- vertex_socket.send_pyobj(ev)
+ for vertex in vertex_iterator:
+ vertex_socket.send_pyobj(vertex)
def socket_iterator(nelements, socket):
for i in xrange(nelements):
@@ -66,8 +71,7 @@ class G4ParallelGenerator(object):
base_address = 'ipc:///tmp/chroma_'+str(uuid.uuid4())
self.vertex_address = base_address + '.vertex'
self.photon_address = base_address + '.photon'
- self.processes = [ G4GeneratorProcess(i, material, self.vertex_address, self.photon_address, seed=base_seed + i)
- for i in xrange(nprocesses) ]
+ self.processes = [ G4GeneratorProcess(i, material, self.vertex_address, self.photon_address, seed=base_seed + i) for i in xrange(nprocesses) ]
for p in self.processes:
p.start()
@@ -78,18 +82,16 @@ class G4ParallelGenerator(object):
self.photon_socket = self.zmq_context.socket(zmq.PULL)
self.photon_socket.bind(self.photon_address)
- # Verify everyone is running and connected to avoid sending all the events to one client
+ # Verify everyone is running and connected to avoid
+ # sending all the events to one client.
for i in xrange(nprocesses):
msg = self.photon_socket.recv()
assert msg == 'READY'
- def generate_events(self, nevents, vertex_iterator):
- # Doing this to avoid a deadlock caused by putting to one queue while getting from another
- limited_iterator = itertools.islice(vertex_iterator, nevents)
- sender_thread = threading.Thread(target=vertex_sender, args=(limited_iterator,
- self.vertex_socket))
+ def generate_events(self, vertex_iterator):
+ # Doing this to avoid a deadlock caused by putting to one queue
+ # while getting from another.
+ vertex_list = list(vertex_iterator)
+ sender_thread = threading.Thread(target=vertex_sender, args=(vertex_list, self.vertex_socket))
sender_thread.start()
- return socket_iterator(nevents, self.photon_socket)
-
-
-
+ return socket_iterator(len(vertex_list), self.photon_socket)