44
55import communication_helpers as ch
66
7-
87class 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 '\n Single_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
0 commit comments