Skip to content

Commit 44d0891

Browse files
committed
Merge branch 'feature/multislicecomm'
2 parents 0b5b63f + c62574c commit 44d0891

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

communication_helpers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22
from PyHEADTAIL.particles.particles import Particles
33

44

5+
def combine_float_buffers(list_of_buffers):
6+
N_buffers = len(list_of_buffers)
7+
len_buffers = np.array(map(lambda seq: float(len(seq)), list_of_buffers))
8+
return np.concatenate([np.array([float(N_buffers)]), len_buffers]+list_of_buffers)
9+
10+
def split_float_buffers(megabuffer):
11+
i_mbuf = 0
12+
13+
N_buffers = int(megabuffer[0])
14+
i_mbuf += 1
15+
16+
len_buffers = megabuffer[i_mbuf:i_mbuf+N_buffers]
17+
i_mbuf += N_buffers
18+
19+
list_of_buffers = []
20+
for i_buf in xrange(N_buffers):
21+
lenbuf = int(len_buffers[i_buf])
22+
list_of_buffers.append(megabuffer[i_mbuf:i_mbuf+lenbuf])
23+
i_mbuf += lenbuf
24+
25+
return list_of_buffers
26+
27+
28+
29+
30+
531
def list_of_strings_2_buffer(strlist):
632
data = ''.join(map(lambda s:s+';', strlist))+'\n'
733
buf_to_send = np.atleast_1d(np.int_(np.array(map(ord, list(data)))))

ring_of_CPUs.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import communication_helpers as ch
66

77
class RingOfCPUs(object):
8-
def __init__(self, sim_content, single_CPU_mode = False):
8+
def __init__(self, sim_content, N_pieces_per_transfer=1, single_CPU_mode = False):
99

1010
self.sim_content = sim_content
1111
self.N_turns = sim_content.N_turns
12+
self.N_pieces_per_transfer = N_pieces_per_transfer
1213

1314
self.sim_content.ring_of_CPUs = self
1415

@@ -66,26 +67,32 @@ def run(self):
6667
t_last_turn = time.mktime(time.localtime())
6768
while True: #(it will be stopped with a break)
6869
orders_from_master = []
69-
# pop a piece
70-
try:
71-
piece_to_send = self.pieces_to_be_treated.pop() # pop starts for the last slices
72-
# (it is what we want, for the HEADTAIL
73-
# slice order convention, z = -beta*c*t)
74-
except IndexError:
75-
piece_to_send = None
70+
list_of_buffers_to_send = []
71+
72+
for _ in xrange(self.N_pieces_per_transfer):
73+
# pop a piece
74+
try:
75+
piece_to_send = self.pieces_to_be_treated.pop() # pop starts for the last slices
76+
# (it is what we want, for the HEADTAIL
77+
# slice order convention, z = -beta*c*t)
78+
except IndexError:
79+
piece_to_send = None
80+
81+
list_of_buffers_to_send.append(self.sim_content.piece_to_buffer(piece_to_send))
7682

7783
# send it to the first element of the ring and receive from the last
78-
sendbuf = self.sim_content.piece_to_buffer(piece_to_send)
84+
sendbuf = ch.combine_float_buffers(list_of_buffers_to_send)
7985
if len(sendbuf) > self.N_buffer_float_size:
8086
raise ValueError('Float buffer is too small!')
8187
self.comm.Sendrecv(sendbuf, dest=0, sendtag=0,
8288
recvbuf=self.buf_float, source=self.master_id-1, recvtag=self.myid)
83-
piece_received = self.sim_content.buffer_to_piece(self.buf_float)
84-
85-
# treat received piece
86-
if piece_received is not None:
87-
self.sim_content.treat_piece(piece_received)
88-
self.pieces_treated.append(piece_received)
89+
list_received_pieces = map(self.sim_content.buffer_to_piece, ch.split_float_buffers(self.buf_float))
90+
91+
# treat received pieces
92+
for piece_received in list_received_pieces:
93+
if piece_received is not None:
94+
self.sim_content.treat_piece(piece_received)
95+
self.pieces_treated.append(piece_received)
8996

9097
# end of turn
9198
if len(self.pieces_treated)==self.N_pieces:
@@ -128,23 +135,24 @@ def run(self):
128135

129136
elif self.I_am_a_worker:
130137
# initialization
131-
piece_to_send = None
138+
list_of_buffers_to_send = [self.sim_content.piece_to_buffer(None)]
132139

133140
while True:
134141

135-
sendbuf = self.sim_content.piece_to_buffer(piece_to_send)
142+
sendbuf = ch.combine_float_buffers(list_of_buffers_to_send)
136143
if len(sendbuf) > self.N_buffer_float_size:
137144
raise ValueError('Float buffer is too small!')
138145
self.comm.Sendrecv(sendbuf, dest=self.right, sendtag=self.right,
139146
recvbuf=self.buf_float, source=self.left, recvtag=self.myid)
140-
piece_received = self.sim_content.buffer_to_piece(self.buf_float)
147+
list_received_pieces = map(self.sim_content.buffer_to_piece, ch.split_float_buffers(self.buf_float))
141148

142149
# treat received piece
143-
if piece_received is not None:
144-
self.sim_content.treat_piece(piece_received)
150+
for piece_received in list_received_pieces:
151+
if piece_received is not None:
152+
self.sim_content.treat_piece(piece_received) #the elements of the list are being mutated
145153

146154
# prepare for next iteration
147-
piece_to_send = piece_received
155+
list_of_buffers_to_send = map(self.sim_content.piece_to_buffer, list_received_pieces)
148156

149157
# receive orders from the master
150158
self.comm.Bcast(self.buf_int, self.master_id)

test_physics/000_test_with_ecloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
from Simulation_with_eclouds import Simulation
99
simulation_content = Simulation()
1010

11-
myCPUring = RingOfCPUs(simulation_content)
11+
myCPUring = RingOfCPUs(simulation_content, N_pieces_per_transfer=5)
1212

1313
myCPUring.run()

test_ring_with_objects/000_test_without_ecloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
from Simulation import Simulation
99
simulation_content = Simulation()
1010

11-
myCPUring = RingOfCPUs(simulation_content)
11+
myCPUring = RingOfCPUs(simulation_content, N_pieces_per_transfer=5)
1212

1313
myCPUring.run()

test_ring_with_objects/002_test_with_ecloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
from Simulation_with_eclouds import Simulation
99
simulation_content = Simulation()
1010

11-
myCPUring = RingOfCPUs(simulation_content)
11+
myCPUring = RingOfCPUs(simulation_content, N_pieces_per_transfer=5)
1212

1313
myCPUring.run()

0 commit comments

Comments
 (0)