|
5 | 5 | import communication_helpers as ch |
6 | 6 |
|
7 | 7 | 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): |
9 | 9 |
|
10 | 10 | self.sim_content = sim_content |
11 | 11 | self.N_turns = sim_content.N_turns |
| 12 | + self.N_pieces_per_transfer = N_pieces_per_transfer |
12 | 13 |
|
13 | 14 | self.sim_content.ring_of_CPUs = self |
14 | 15 |
|
@@ -66,26 +67,32 @@ def run(self): |
66 | 67 | t_last_turn = time.mktime(time.localtime()) |
67 | 68 | while True: #(it will be stopped with a break) |
68 | 69 | 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)) |
76 | 82 |
|
77 | 83 | # 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) |
79 | 85 | if len(sendbuf) > self.N_buffer_float_size: |
80 | 86 | raise ValueError('Float buffer is too small!') |
81 | 87 | self.comm.Sendrecv(sendbuf, dest=0, sendtag=0, |
82 | 88 | 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) |
89 | 96 |
|
90 | 97 | # end of turn |
91 | 98 | if len(self.pieces_treated)==self.N_pieces: |
@@ -128,23 +135,24 @@ def run(self): |
128 | 135 |
|
129 | 136 | elif self.I_am_a_worker: |
130 | 137 | # initialization |
131 | | - piece_to_send = None |
| 138 | + list_of_buffers_to_send = [self.sim_content.piece_to_buffer(None)] |
132 | 139 |
|
133 | 140 | while True: |
134 | 141 |
|
135 | | - sendbuf = self.sim_content.piece_to_buffer(piece_to_send) |
| 142 | + sendbuf = ch.combine_float_buffers(list_of_buffers_to_send) |
136 | 143 | if len(sendbuf) > self.N_buffer_float_size: |
137 | 144 | raise ValueError('Float buffer is too small!') |
138 | 145 | self.comm.Sendrecv(sendbuf, dest=self.right, sendtag=self.right, |
139 | 146 | 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)) |
141 | 148 |
|
142 | 149 | # 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 |
145 | 153 |
|
146 | 154 | # 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) |
148 | 156 |
|
149 | 157 | # receive orders from the master |
150 | 158 | self.comm.Bcast(self.buf_int, self.master_id) |
|
0 commit comments