-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathring_of_CPUs.py
221 lines (172 loc) · 7.15 KB
/
ring_of_CPUs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
import communication_helpers as ch
class RingOfCPUs(object):
def __init__(self, sim_content, N_pieces_per_transfer=1, single_CPU_mode = False, comm=None):
self.sim_content = sim_content
self.N_turns = sim_content.N_turns
self.N_pieces_per_transfer = N_pieces_per_transfer
if hasattr(sim_content, 'N_pieces_per_transfer'):
self.N_pieces_per_transfer = N_pieces_per_transfer
print 'N_pieces_per_transfer = ', N_pieces_per_transfer
self.sim_content.ring_of_CPUs = self
# choice of the communicator
if single_CPU_mode:
print '\nSingle CPU forced by user!\n'
self.comm = SingleCoreComminicator()
elif comm is not None:
print '\nMultiprocessing using communicator provided as argument.\n'
self.comm = comm
else:
print '\nMultiprocessing via MPI.'
from mpi4py import MPI
self.comm = MPI.COMM_WORLD
#check if there is only one node
if self.comm.Get_size()==1:
#in case it is forced by user it will be rebound but there is no harm in that
self.comm = SingleCoreComminicator()
# get info on the grid
self.N_nodes = self.comm.Get_size()
self.N_wkrs = self.N_nodes-1
self.master_id = self.N_nodes-1
self.myid = self.comm.Get_rank()
self.I_am_a_worker = self.myid!=self.master_id
self.I_am_the_master = not(self.I_am_a_worker)
# allocate buffers for communication
self.N_buffer_float_size = 1000000
self.buf_float = np.array(self.N_buffer_float_size*[0.])
self.N_buffer_int_size = 100
self.buf_int = np.array(self.N_buffer_int_size*[0])
self.sim_content.init_all()
self.comm.Barrier() # only for stdoutp
if self.I_am_the_master:
self.pieces_to_be_treated = self.sim_content.init_master()
self.N_pieces = len(self.pieces_to_be_treated)
self.pieces_treated = []
self.i_turn = 0
self.piece_to_send = None
elif self.I_am_a_worker:
self.sim_content.init_worker()
# Identify CPUs on my left and my right
if self.myid==0:
self.left = self.master_id
else:
self.left = self.myid-1
self.right = self.myid+1
self.comm.Barrier() # wait that all are done with the init
def run(self):
if self.I_am_the_master:
with open('logfile.txt', 'a+') as fid:
import socket
fid.writelines(['Running on %s\n'%socket.gethostname()])
import time
t_last_turn = time.mktime(time.localtime())
while True: #(it will be stopped with a break)
orders_from_master = []
list_of_buffers_to_send = []
for _ in xrange(self.N_pieces_per_transfer):
# pop a piece
try:
piece_to_send = self.pieces_to_be_treated.pop() # pop starts for the last slices
# (it is what we want, for the HEADTAIL
# slice order convention, z = -beta*c*t)
except IndexError:
piece_to_send = None
list_of_buffers_to_send.append(self.sim_content.piece_to_buffer(piece_to_send))
# send it to the first element of the ring and receive from the last
sendbuf = ch.combine_float_buffers(list_of_buffers_to_send)
if len(sendbuf) > self.N_buffer_float_size:
raise ValueError('Float buffer is too small!')
self.comm.Sendrecv(sendbuf, dest=0, sendtag=0,
recvbuf=self.buf_float, source=self.master_id-1, recvtag=self.myid)
list_received_pieces = map(self.sim_content.buffer_to_piece, ch.split_float_buffers(self.buf_float))
# treat received pieces
for piece_received in list_received_pieces:
if piece_received is not None:
self.sim_content.treat_piece(piece_received)
self.pieces_treated.append(piece_received)
# end of turn
if len(self.pieces_treated)==self.N_pieces:
self.pieces_treated = self.pieces_treated[::-1] #restore the original order
# perform global operations and reslice
orders_to_pass, new_pieces_to_be_treated = \
self.sim_content.finalize_turn_on_master(self.pieces_treated)
orders_from_master += orders_to_pass
t_now = time.mktime(time.localtime())
print 'Turn %d, %d s'%(self.i_turn,t_now-t_last_turn)
with open('logfile.txt', 'a+') as fid:
fid.writelines(['Turn %d, %d s\n'%(self.i_turn,t_now-t_last_turn)])
t_last_turn = t_now
# prepare next turn
self.pieces_to_be_treated = new_pieces_to_be_treated
self.N_pieces = len(self.pieces_to_be_treated)
self.pieces_treated = []
self.i_turn+=1
# check if stop is needed
if self.i_turn == self.N_turns: orders_from_master.append('stop')
# send orders
buforders = ch.list_of_strings_2_buffer(orders_from_master)
if len(buforders) > self.N_buffer_int_size:
raise ValueError('Int buffer is too small!')
self.comm.Bcast(buforders, self.master_id)
#execute orders from master (the master executes its own orders :D)
self.sim_content.execute_orders_from_master(orders_from_master)
# check if simulation has to be ended
if 'stop' in orders_from_master:
break
# finalize simulation (savings etc.)
self.sim_content.finalize_simulation()
elif self.I_am_a_worker:
# initialization
list_of_buffers_to_send = [self.sim_content.piece_to_buffer(None)]
while True:
sendbuf = ch.combine_float_buffers(list_of_buffers_to_send)
if len(sendbuf) > self.N_buffer_float_size:
raise ValueError('Float buffer is too small!')
self.comm.Sendrecv(sendbuf, dest=self.right, sendtag=self.right,
recvbuf=self.buf_float, source=self.left, recvtag=self.myid)
list_received_pieces = map(self.sim_content.buffer_to_piece, ch.split_float_buffers(self.buf_float))
# treat received piece
for piece_received in list_received_pieces:
if piece_received is not None:
self.sim_content.treat_piece(piece_received) #the elements of the list are being mutated
# prepare for next iteration
list_of_buffers_to_send = map(self.sim_content.piece_to_buffer, list_received_pieces)
# receive orders from the master
self.comm.Bcast(self.buf_int, self.master_id)
orders_from_master = ch.buffer_2_list_of_strings(self.buf_int)
#execute orders from master
self.sim_content.execute_orders_from_master(orders_from_master)
# check if simulation has to be ended
if 'stop' in orders_from_master:
break
# # usage
# from Simulation import Simulation
# sim_content = Simulation()
# myring = RingOfCPUs(sim_content, N_turns)
# myring.run()
class SingleCoreComminicator(object):
def __init__(self):
print '\n\n\n'
print '****************************************'
print '*** Using single core MPI simulator! ***'
print '****************************************'
print '\n\n\n'
def Get_size(self):
return 1
def Get_rank(self):
return 0
def Barrier(self):
pass
def Sendrecv(self, sendbuf, dest, sendtag, recvbuf, source, recvtag):
if dest!=0 or sendtag!=0 or source!=-1 or recvtag!=0:
raise ValueError('Input of Sendrecv not compatible with single core operation!!!')
recvbuf[:len(sendbuf)]=sendbuf
def Bcast(self, buf, root=0):
if root!=0:
raise ValueError('Input of Bcast not compatible with single core operation!!!')
#Does not really have to do anything
# # usage
# from Simulation import Simulation
# sim_content = Simulation()
# myring = RingOfCPUs(sim_content, N_turns)
# myring.run()