forked from GamesCrafters/GamesmanMPI
/
solver_launcher.py
61 lines (47 loc) · 1.74 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
from mpi4py import MPI
import inspect
import logging
import imp
import argparse
import src.utils
parser = argparse.ArgumentParser()
parser.add_argument("game_file", help="game to solve for")
parser.add_argument("-np", "--numpy", help="optimize for numpy array usage",
action="store_true")
parser.add_argument("--debug", help="Enables or disables logging",
action="store_true")
args = parser.parse_args()
comm = MPI.COMM_WORLD
# 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
from src.job import Job
from src.process import Process
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)
# Set up our logging system
lvl = logging.CRITICAL
if args.debug:
lvl = logging.DEBUG
logging.basicConfig(filename='logs/solver_log' + str(comm.Get_rank()) + '.log', filemode='w', level=lvl)
initial_position = src.utils.game_module.initial_position()
process = Process(comm.Get_rank(), comm.Get_size(), comm, NP=args.numpy)
if process.rank == process.root:
initial_gamestate = GameState(GameState.INITIAL_POS)
initial_job = Job(Job.LOOK_UP, initial_gamestate, process.rank, Job.INITIAL_JOB_ID)
process.add_job(initial_job)
process.run()