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
|
import { execFile, spawn } from 'child_process'
import interpreters from './interpreters'
import modules from './modules'
import kill from 'tree-kill'
import { remote } from './remote'
import { set_connected } from './rpc'
import uuidv1 from 'uuid/v1'
const idle_state = { status: 'IDLE', task: {} }
export const state = {
current_cpu_task: idle_state,
current_gpu_task: idle_state,
}
export function get_current_cpu_task(){
return state.current_cpu_task
}
export function get_current_gpu_task(){
return state.current_gpu_task
}
export function get_current_task(processor) {
if (processor === 'cpu') {
return state.current_cpu_task
} else {
return state.current_gpu_task
}
}
function serialize_task(t){
if (!t || t.status === 'IDLE') {
return {
status: 'IDLE',
}
}
return {
status: 'RUNNING',
task: t.task,
uuid: t.task.uuid,
pid: t.subprocess.pid,
}
}
function clear_task(is_gpu, task){
if (is_gpu) {
if (state.current_gpu_task.task && state.current_gpu_task.task.uuid === task.uuid) {
state.current_gpu_task = idle_state
}
} else {
if (state.current_cpu_task.task && state.current_cpu_task.task.uuid === task.uuid) {
state.current_cpu_task = idle_state
}
}
}
export function status () {
return {
cpu: serialize_task(state.current_cpu_task),
gpu: serialize_task(state.current_gpu_task),
}
}
export function build_params(module, task) {
const activity = module.activities[task.activity]
const interpreter = interpreters[activity.type]
let opt_params;
if (activity.build_params) {
opt_params = activity.build_params(task)
}
else {
const opt = task.opt
opt_params = Object.keys(opt).map(key => {
const flag = '--' + key.replace(/-/g, '_')
const value = opt[key]
if (value === 'true') {
return [flag]
}
return [flag, value]
}).reduce((acc, cur) => acc.concat(cur), [])
}
const params = (interpreter.params || [])
.concat([ activity.script ])
.concat(activity.params || [])
.concat(opt_params)
return {
activity,
interpreter,
params
}
}
export function run_system_command(cmd, cb) {
console.log('running system command:', cmd)
switch(cmd) {
case 'nvidia-smi':
case 'ps':
case 'uptime':
case 'w':
execFile(cmd, cb)
break
case 'df':
execFile('df', ['-h'], cb)
break
default:
cb({ error: 'no such command' })
break
}
}
export function run_task(task, preempt, watch){
const module = modules[task.module]
if (! module) return { type: 'error', error: "No such module: " + task.module }
const { activity, interpreter, params } = build_params(module, task)
if (! interpreter) return { type: 'error', error: "No such interpreter: " + activity.interpreter }
if (interpreter.gpu && state.current_gpu_task.status !== 'IDLE') {
if (preempt) {
console.log('preempting currently running GPU task')
terminate(state.current_gpu_task.subprocess)
} else {
console.log('already running GPU task :(', state.current_gpu_task.pid)
return { type: 'error', error: 'task already running on gpu' }
}
} else if (!interpreter.gpu && state.current_cpu_task.status !== 'IDLE') {
if (preempt) {
console.log('preempting currently running CPU task')
terminate(state.current_cpu_task.subprocess)
} else {
console.log('already running CPU task :(')
return { type: 'error', error: 'task already running on cpu' }
}
}
console.log('running task', task.activity)
console.log(module.cwd)
console.log(interpreter.cmd, params)
const subprocess = spawn(interpreter.cmd, params, {
cwd: module.cwd,
})
if (interpreter.gpu) {
state.current_gpu_task = {
subprocess, task, status: 'RUNNING'
}
}
else {
state.current_cpu_task = {
subprocess, task, status: 'RUNNING'
}
}
task.uuid = task.uuid || uuidv1()
const processor = task.processor = interpreter.gpu ? 'gpu' : 'cpu'
remote.emit('task_res', { type: 'task_begin', task })
watch && console.log("watching stdout..")
subprocess.stdout.on('data', data => {
watch && remote.emit('task_res', { type: 'stdout', processor, data: data.toString('utf8') })
})
subprocess.stderr.on('data', data => {
watch && remote.emit('task_res', { type: 'stderr', processor, data: data.toString('utf8') })
})
let finished = false
subprocess.on('error', (err) => {
if (finished) return
finished = true
console.log('task error', subprocess.exitCode, err)
clear_task(interpreter.gpu, task)
remote.emit('task_res', { type: 'task_error', task, err })
set_connected(false)
})
subprocess.on('close', () => {
if (finished) return
finished = true
console.log('task ended', subprocess.exitCode || '')
clear_task(interpreter.gpu, task)
remote.emit('task_res', { type: 'task_finish', task })
set_connected(false)
})
}
export function stop_task(task){
if (state.current_cpu_task.task.uuid === task.uuid) {
terminate(state.current_cpu_task.subprocess)
return { status: 'ok' }
} else if (state.current_gpu_task.task.uuid === task.uuid) {
terminate(state.current_gpu_task.subprocess)
return { status: 'ok' }
}
return { error: 'no such task' }
}
export function terminate(subprocess){
if (!subprocess) {
return
}
console.log('kill pid', subprocess.pid)
kill(subprocess.pid)
}
|