-
Notifications
You must be signed in to change notification settings - Fork 84
/
prun.py
executable file
·176 lines (161 loc) · 7.17 KB
/
prun.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
#! /usr/bin/env python
#
# Copyright (c) 2014, Douban Inc.
# All rights reserved.
#
# Distributed under the BSD License. Check out the LICENSE file for full text.
#
# Paracel - A distributed optimization framework with parameter server.
#
# Downloading
# git clone https://github.com/douban/paracel.git
#
# Authors: Hong Wu <[email protected]>
#
try:
from optparse import OptionParser
except ImportError as e:
print e
print 'optparse module required'
exit(0)
import os
import sys
import socket
import random
import subprocess
import logging
logging.basicConfig(filename='paracelrun_log', format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
PARACEL_INSTALL_PREFIX = os.path.dirname(os.path.abspath(__file__))
def paracelrun_cpp_proxy(n_srv, init_port):
'''Get the ports from started parameter server procs'''
from subprocess import Popen, PIPE
cmd_lst = [os.path.join(PARACEL_INSTALL_PREFIX, 'bin/paracelrun_cpp_proxy --nsrv'), str(n_srv), '--init_port', str(init_port)]
cmd = ' '.join(cmd_lst)
logger.info(cmd)
p = Popen(cmd.split(), stdin=PIPE, stdout=PIPE)
return p.stdout.readline()
def get_free_port():
''''Get available port in os'''
def is_avaliable(port):
cmd = 'netstat -tuln | grep LISTEN | cut -f 2 -d :'
tmp = os.popen(cmd)
content = tmp.read()
content = content.strip('\n').split('0.0.0.0')
plst = [item.strip('\n').strip(' ') for item in content]
while '' in plst:
plst.remove('')
plst = [int(item) for item in plst]
tmp.close()
if port in plst:
return False
else:
return True
port = random.randint(10000, 65535)
while not is_avaliable(port):
port = random.randint(10000, 65535)
return port
def init_starter(method, mem_limit, ppn, hostfile, group):
'''Assemble commands for running paracel programs'''
starter = ''
if not hostfile:
hostfile = '~/.mpi/large.18'
if method == 'mesos':
if group:
starter = '%s/mrun -m %s -p %s -g %s -n ' % (PARACEL_INSTALL_PREFIX, mem_limit, ppn, group)
else:
starter = '%s/mrun -m %s -p %s -n ' % (PARACEL_INSTALL_PREFIX, mem_limit, ppn)
elif method == 'mpi':
starter = 'mpirun --hostfile %s -n ' % hostfile
elif method == 'local':
starter = 'mpirun -n '
else:
print 'method %s not supported.' % method
sys.exit(1)
return starter
if __name__ == '__main__':
optpar = OptionParser()
optpar.add_option('-p', '--snum', default=1,
action='store', type='int', dest='parasrv_num',
help='number of parameter servers')
optpar.add_option('--m_server',
action='store', type='string', dest='method_server',
help='running method for parameter servers. If not given, set with the same value of -m or --method', metavar='local | mesos | mpi')
optpar.add_option('--ppn_server',
action='store', type='int', dest='ppn_server',
help='mesos case: procs number per node of parameter servers. If not given, set with the same value of --ppn')
optpar.add_option('--mem_limit_server',
action='store', type='int', dest='mem_limit_server',
help='mesos case: memory size of each task in parameter servers. If not given, set with the same value of --mem_limit')
optpar.add_option('--hostfile_server',
action='store', type='string', dest='hostfile_server',
help='mpi case: hostfile for mpirun of parameter servers. If not given, set with the same value of --hostfile')
optpar.add_option('-w', '--wnum', default=1,
action='store', type='int', dest='worker_num',
help='number of workers for learning')
optpar.add_option('-m', '--method', default='local',
action='store', type='string', dest='method',
help='running method for workers', metavar='local | mesos | mpi')
optpar.add_option('--ppn', default=4,
action='store', type='int', dest='ppn',
help='mesos case: procs number per node for workers')
optpar.add_option('--mem_limit', default=200,
action='store', type='int', dest='mem_limit',
help='mesos case: memory size of each task of workers')
optpar.add_option('--hostfile',
action='store', type='string', dest='hostfile',
help='mpi case: hostfile for mpirun for workers')
optpar.add_option('-c', '--cfg_file',
action='store', type='string', dest='config',
help='config file in json fmt, for alg usage')
optpar.add_option('-g', '--group', default='',
action='store', type='string', dest='worker_group',
help='mesos case: which group for worker to run')
optpar.add_option('--group_server',
action='store', type='string', dest='server_group',
help='mesos case: which group for server to run')
(options, args) = optpar.parse_args()
nsrv = 1
nworker = 1
if options.parasrv_num:
nsrv = options.parasrv_num
if options.worker_num:
nworker = options.worker_num
if not options.method_server:
options.method_server = options.method
if not options.ppn_server:
options.ppn_server = options.ppn
if not options.mem_limit_server:
options.mem_limit_server = options.mem_limit
if not options.hostfile_server:
options.hostfile_server = options.hostfile
server_starter = init_starter(options.method_server,
str(options.mem_limit_server),
str(options.ppn_server),
options.hostfile_server,
options.server_group)
worker_starter = init_starter(options.method,
str(options.mem_limit),
str(options.ppn),
options.hostfile,
options.worker_group)
#initport = random.randint(30000, 65000)
#initport = get_free_port()
initport = 11777
start_parasrv_cmd_lst = [server_starter, str(nsrv), os.path.join(PARACEL_INSTALL_PREFIX, 'bin/start_server --start_host'), socket.gethostname(), ' --init_port', str(initport)]
start_parasrv_cmd = ' '.join(start_parasrv_cmd_lst)
logger.info(start_parasrv_cmd)
procs = subprocess.Popen(start_parasrv_cmd, shell=True, preexec_fn=os.setpgrp)
try:
serverinfo = paracelrun_cpp_proxy(nsrv, initport)
entry_cmd = ''
if args:
entry_cmd = ' '.join(args)
alg_cmd_lst = [worker_starter, str(nworker), entry_cmd, '--server_info', serverinfo, '--cfg_file', options.config]
alg_cmd = ' '.join(alg_cmd_lst)
logger.info(alg_cmd)
os.system(alg_cmd)
os.killpg(procs.pid, 9)
except Exception as e:
logger.exception(e)
os.killpg(procs.pid, 9)