forked from GamesCrafters/GamesmanMPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_launcher.py
executable file
·158 lines (129 loc) · 3.76 KB
/
solver_launcher.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
#!/usr/bin/env python3
from mpi4py import MPI
import imp
import argparse
import src.utils
parser = argparse.ArgumentParser()
parser.add_argument(
'game_file',
help='Game to solve for.'
)
parser.add_argument(
'--debug',
help='Enables or disables logging.',
action='store_true'
)
parser.add_argument(
'-sd',
'--statsdir',
help='Location to store statistics about game.',
action='store'
)
parser.add_argument(
'--custom',
help='Specifies custom file to modify provided game file.'
)
parser.add_argument(
'--init_pos',
help='Initial position to start at for the game. If none is specified, the default is used.'
)
args = parser.parse_args()
comm = MPI.COMM_WORLD
# May be later modified for debugging purposes.
# Because comm.send is read only we need to make a
# new variable.
isend = comm.isend
recv = comm.recv
abort = comm.Abort
# Load file and give it to each process.
game_module = imp.load_source('game_module', args.game_file)
src.utils.game_module = game_module
# Make sure every process has a copy of this.
comm.Barrier()
# Now it is safe to import the classes we need as everything
# has now been initialized correctly.
from src.game_state import GameState # NOQA
from src.new_job import Job # NOQA
from src.new_process import Process # NOQA
import src.debug # NOQA
def validate(mod):
try:
getattr(mod, 'initial_position')
getattr(mod, 'do_move')
getattr(mod, 'gen_moves')
getattr(mod, 'primitive')
except AttributeError as e:
print('Could not find method'), e.args[0]
raise
# Make sure the game is properly defined
validate(src.utils.game_module)
def load_custom(mod):
try:
custom = imp.load_source('custom', mod)
return custom
except AttributeError as e:
print('Custom file with new initial position not specified. '
'Using default initial position instead.')
return None
except FileNotFoundError as e:
print('Custom file was not found. '
'Using default initial position instead.')
return None
def load_init_pos(mod):
try:
init_pos = getattr(custom, mod)
return init_pos
except AttributeError as e:
print('Initial position was not found in custom file. '
'Using default initial position instead.')
return None
# Patch the game_module as necessary
if args.init_pos:
custom = load_custom(args.custom)
if custom != None:
init_pos = load_init_pos(args.init_pos)
if init_pos != None:
src.utils.game_module.initial_position = init_pos
# Make sure every process has a copy of this.
comm.Barrier()
# Now it is safe to import the classes we need as everything
# has now been initialized correctly.
from src.game_state import GameState # NOQA
from src.job import Job # NOQA
from src.process import Process # NOQA
import src.debug # NOQA
# For debugging with heapy.
if args.debug:
src.debug.init_debug(comm.Get_rank())
isend = src.debug.debug_send(comm.isend)
recv = src.debug.debug_recv(comm.recv)
abort = src.debug.debug_abort(comm.Abort)
initial_position = src.utils.game_module.initial_position()
process = Process(
comm.Get_rank(),
comm.Get_size(),
comm,
isend,
recv,
abort,
stats_dir=args.statsdir
)
if process.rank == process.root:
initial_gamestate = GameState(GameState.INITIAL_POS)
tup = (initial_gamestate.to_tuple())
# Uncomment for new_solver
initial_job = Job(
Job.LOOK_UP,
process.rank,
Job.INITIAL_JOB_ID,
tup
)
# initial_job = Job(
# Job.LOOK_UP,
# initial_gamestate,
# process.rank,
# Job.INITIAL_JOB_ID,
# )
process.work.put(initial_job)
process.run()
comm.Barrier()