-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
executable file
·205 lines (166 loc) · 6.8 KB
/
train.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
import argparse
import os
import sys
import gym
import config
import copy
import time
import numpy as np
import subprocess
import config
parser = argparse.ArgumentParser(description="Run commands")
def new_tmux_cmd(session, name, cmd):
if isinstance(cmd, (list, tuple)):
cmd = " ".join(str(v) for v in cmd)
return name, "tmux send-keys -t {}:{} '{} && {}' Enter".format(session, name, 'source activate dhp_env', cmd)
def create_tmux_commands(session, logdir):
if config.mode in ['on_line']:
'''genrate game done dic for first run, so that latter is auto started by the programe'''
done_sinal_dic = []
worker_running = 0
for game_i in range(0,len(config.game_dic)):
for subjects_i in range(0,config.num_subjects):
done_sinal_dic += [[game_i,subjects_i]]
worker_running += 1
if worker_running >= config.num_workers_one_run:
breakout = True
break
if breakout:
break
'''clean the temp dir'''
from config import worker_done_signal_dir
subprocess.call(["rm", "-r", worker_done_signal_dir])
subprocess.call(["mkdir", "-p", worker_done_signal_dir])
while True:
try:
from config import worker_done_signal_dir, worker_done_signal_file
np.savez(worker_done_signal_dir+worker_done_signal_file,
done_sinal_dic=done_sinal_dic)
break
except Exception, e:
print(str(Exception)+": "+str(e))
time.sleep(1)
'''cmds for init the tmux session'''
cmds = [
"mkdir -p {}".format(logdir),
"tmux kill-session -t {}".format(session),
"tmux new-session -s {} -d".format(session),
]
return cmds
elif config.mode in ['off_line','data_processor']:
'''ps'''
base_cmd = [
'CUDA_VISIBLE_DEVICES=', sys.executable, 'worker.py',
'--log-dir', logdir, '--env-id', config.game_dic[0],
'--num-workers', str(config.num_games)]
cmds_map = [new_tmux_cmd(session, "ps", base_cmd + ["--job-name", "ps"])]
'''worker'''
for i in range(config.num_games):
base_cmd = [
'CUDA_VISIBLE_DEVICES=', sys.executable, 'worker.py',
'--log-dir', logdir,
'--env-id', config.game_dic[i],
'--num-workers', str(config.num_games)]
cmds_map += [new_tmux_cmd(
session,
"w-%d" % i,
base_cmd + ["--job-name", "worker", "--task", str(i)]
)]
windows = [v[0] for v in cmds_map]
cmds = [
"mkdir -p {}".format(logdir),
"tmux kill-session -t {}".format(session),
"tmux new-session -s {} -n {} -d".format(session, windows[0]),
]
for w in windows[1:]:
cmds += ["tmux new-window -t {} -n {}".format(session, w)]
cmds += ["sleep 1"]
for window, cmd in cmds_map:
cmds += [cmd]
return cmds
def create_tmux_commands_auto(session, logdir, worker_running, game_i_at, subject_i_at):
''''''
cmds_map = []
if (game_i_at>=len(config.game_dic)):
print('all done')
print(s)
base_cmd = [
'CUDA_VISIBLE_DEVICES=', sys.executable, 'worker.py',
'--log-dir', logdir, '--env-id', config.game_dic[game_i_at],
'--num-workers', str(1)]
cmds_map += [new_tmux_cmd(session, 'g-'+str(game_i_at)+'-s-'+str(subject_i_at)+'-ps', base_cmd + ["--job-name", "ps",
"--subject", str(subject_i_at)])]
base_cmd = [
'CUDA_VISIBLE_DEVICES=', sys.executable, 'worker.py',
'--log-dir', logdir,
'--env-id', config.game_dic[game_i_at],
'--num-workers', str(1)]
cmds_map += [new_tmux_cmd(session,
'g-'+str(game_i_at)+'-s-'+str(subject_i_at)+'-w-0',
base_cmd + ["--job-name", "worker",
"--task", str(0),
"--subject", str(subject_i_at)])]
'''created new worker, add worker_running'''
print('a pair of ps_worker for game '+config.game_dic[game_i_at]+' subject '+str(subject_i_at)+' is created.')
worker_running += 1
subject_i_at += 1
if subject_i_at >= config.num_subjects:
game_i_at += 1
subject_i_at = 0
worker_running +=1
'''see if cmd added'''
if len(cmds_map) > 0:
''''''
windows = [v[0] for v in cmds_map]
cmds = []
for w in windows:
cmds += ["tmux new-window -t {} -n {}".format(session, w)]
cmds += ["sleep 1"]
for window, cmd in cmds_map:
cmds += [cmd]
'''excute cmds'''
os.system("\n".join(cmds))
return worker_running, game_i_at, subject_i_at
def kill_a_pair_of_ps_worker_windows(session,game,subject):
print('a pair of ps_worker for game '+config.game_dic[game]+' subject '+str(subject)+' is being killed.')
'''ganerate cmds'''
cmds = []
cmds += ["tmux kill-window -t "+session+":"+"g-"+str(game)+"-s-"+str(subject)+"-w-"+str(0)]
cmds += ["tmux kill-window -t "+session+":"+"g-"+str(game)+"-s-"+str(subject)+"-ps"]
'''excute cmds'''
os.system("\n".join(cmds))
def run():
args = parser.parse_args()
session = "a3c"
cmds = create_tmux_commands(session, config.log_dir)
print("\n".join(cmds))
os.system("\n".join(cmds))
if config.mode is 'on_line':
try:
run_to = np.load(config.log_dir+'run_to.npz')['run_to']
game_i_at = run_to[0]
subject_i_at = run_to[1]
worker_running = run_to[2]
print('>>>>>Previous run_to found, init run_to:')
print('\t\tgame_i_at: '+str(game_i_at))
print('\t\tsubject_i_at: '+str(subject_i_at))
print('\t\tworker_running: '+str(worker_running))
except Exception, e:
worker_running = config.num_workers_one_run # this is fake to start the run
game_i_at=0
subject_i_at=0
print('>>>>>No previous run_to found, init run_to:')
print('\t\tgame_i_at: '+str(game_i_at))
print('\t\tsubject_i_at: '+str(subject_i_at))
print('\t\tworker_running: '+str(worker_running))
'''record run_to'''
while True:
try:
np.savez(config.log_dir+'run_to.npz',
run_to=[game_i_at,subject_i_at,worker_running])
break
except Exception, e:
print(str(Exception)+": "+str(e))
time.sleep(1)
if __name__ == "__main__":
run()