Skip to content

Commit 0b5b63f

Browse files
committed
Merge branch 'feature/safesinglecore'
2 parents 04dc9a8 + 2c01c79 commit 0b5b63f

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

ring_of_CPUs.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@
44

55
import communication_helpers as ch
66

7-
87
class RingOfCPUs(object):
9-
def __init__(self, sim_content):
8+
def __init__(self, sim_content, single_CPU_mode = False):
109

1110
self.sim_content = sim_content
1211
self.N_turns = sim_content.N_turns
1312

1413
self.sim_content.ring_of_CPUs = self
1514

16-
self.comm = MPI.COMM_WORLD
17-
15+
# check if user is forcing simulation mode
16+
if single_CPU_mode:
17+
print '\nSingle_CPU_forced_by_user!\n'
18+
self.comm = SingleCoreComminicator()
19+
else:
20+
from mpi4py import MPI
21+
self.comm = MPI.COMM_WORLD
22+
23+
#check if there is only one node
24+
if self.comm.Get_size()==1:
25+
#in case it is forced by user it will be rebound but there is no harm in that
26+
self.comm = SingleCoreComminicator()
27+
1828
# get info on the grid
1929
self.N_nodes = self.comm.Get_size()
2030
self.N_wkrs = self.N_nodes-1
@@ -23,7 +33,7 @@ def __init__(self, sim_content):
2333
self.I_am_a_worker = self.myid!=self.master_id
2434
self.I_am_the_master = not(self.I_am_a_worker)
2535

26-
# allocate buffers for communation
36+
# allocate buffers for communication
2737
self.N_buffer_float_size = 1000000
2838
self.buf_float = np.array(self.N_buffer_float_size*[0.])
2939
self.N_buffer_int_size = 100
@@ -147,6 +157,40 @@ def run(self):
147157
if 'stop' in orders_from_master:
148158
break
149159

160+
# # usage
161+
# from Simulation import Simulation
162+
# sim_content = Simulation()
163+
164+
# myring = RingOfCPUs(sim_content, N_turns)
165+
166+
# myring.run()
167+
168+
class SingleCoreComminicator(object):
169+
def __init__(self):
170+
print '\n\n\n'
171+
print '****************************************'
172+
print '*** Using single core MPI simulator! ***'
173+
print '****************************************'
174+
print '\n\n\n'
175+
176+
def Get_size(self):
177+
return 1
178+
179+
def Get_rank(self):
180+
return 0
181+
182+
def Barrier(self):
183+
pass
184+
185+
def Sendrecv(self, sendbuf, dest, sendtag, recvbuf, source, recvtag):
186+
if dest!=0 or sendtag!=0 or source!=-1 or recvtag!=0:
187+
raise ValueError('Input of Sendrecv not compatible with single core operation!!!')
188+
recvbuf[:len(sendbuf)]=sendbuf
189+
190+
def Bcast(self, buf, root=0):
191+
if root!=0:
192+
raise ValueError('Input of Bcast not compatible with single core operation!!!')
193+
#Does not really have to do anything
150194

151195
# # usage
152196
# from Simulation import Simulation

test_ring_with_objects/001_plot_results_from_000.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@
4646
twax[0].plot(epsx, '-g')
4747
axes[0].set_xlabel('Turns')
4848
axes[0].set_ylabel(r'$\sigma_x$')
49-
twax[0].set_ylabel(r'$\varepsilon_y$')
49+
twax[0].set_ylabel(r'$\varepsilon_x$')
5050
axes[1].plot(sy)
5151
twax[1].plot(epsy, '-g')
5252
axes[1].set_xlabel('Turns')
53-
axes[1].set_ylabel(r'$\sigma_x$')
53+
axes[1].set_ylabel(r'$\sigma_y$')
5454
twax[1].set_ylabel(r'$\varepsilon_y$')
5555
axes[2].plot(sz)
5656
twax[2].plot(epsz, '-g')
5757
axes[2].set_xlabel('Turns')
58-
axes[2].set_ylabel(r'$\sigma_x$')
59-
twax[2].set_ylabel(r'$\varepsilon_y$')
58+
axes[2].set_ylabel(r'$\sigma_z$')
59+
twax[2].set_ylabel(r'$\varepsilon_z$')
6060
axes[0].grid()
6161
axes[1].grid()
6262
axes[2].grid()

0 commit comments

Comments
 (0)