| 1 |
# Start of server.py |
| 2 |
|
| 3 |
import sys |
| 4 |
import time |
| 5 |
import shlex # For string splitting |
| 6 |
|
| 7 |
|
| 8 |
class Server: |
| 9 |
def __init__(self, ctx): |
| 10 |
self._ctx = ctx |
| 11 |
self._vars = {} |
| 12 |
|
| 13 |
class Failure(BaseException): |
| 14 |
def __init__(self, msg): |
| 15 |
self.msg = msg |
| 16 |
|
| 17 |
def _get_arg(self, args, i): |
| 18 |
if i < len(args): |
| 19 |
return args[i] |
| 20 |
else: |
| 21 |
raise self.Failure("Insufficient command args") |
| 22 |
|
| 23 |
def _get_entry_point(self, entry): |
| 24 |
if entry in self._ctx.entry_points: |
| 25 |
return self._ctx.entry_points[entry] |
| 26 |
else: |
| 27 |
raise self.Failure("Unknown entry point: %s" % entry) |
| 28 |
|
| 29 |
def _check_var(self, vname): |
| 30 |
if not vname in self._vars: |
| 31 |
raise self.Failure("Unknown variable: %s" % vname) |
| 32 |
|
| 33 |
def _check_new_var(self, vname): |
| 34 |
if vname in self._vars: |
| 35 |
raise self.Failure("Variable already exists: %s" % vname) |
| 36 |
|
| 37 |
def _get_var(self, vname): |
| 38 |
self._check_var(vname) |
| 39 |
return self._vars[vname] |
| 40 |
|
| 41 |
def _cmd_inputs(self, args): |
| 42 |
entry = self._get_arg(args, 0) |
| 43 |
for t in self._get_entry_point(entry)[1]: |
| 44 |
print(t) |
| 45 |
|
| 46 |
def _cmd_outputs(self, args): |
| 47 |
entry = self._get_arg(args, 0) |
| 48 |
for t in self._get_entry_point(entry)[2]: |
| 49 |
print(t) |
| 50 |
|
| 51 |
def _cmd_dummy(self, args): |
| 52 |
pass |
| 53 |
|
| 54 |
def _cmd_free(self, args): |
| 55 |
for vname in args: |
| 56 |
self._check_var(vname) |
| 57 |
del self._vars[vname] |
| 58 |
|
| 59 |
def _cmd_rename(self, args): |
| 60 |
oldname = self._get_arg(args, 0) |
| 61 |
newname = self._get_arg(args, 1) |
| 62 |
self._check_var(oldname) |
| 63 |
self._check_new_var(newname) |
| 64 |
self._vars[newname] = self._vars[oldname] |
| 65 |
del self._vars[oldname] |
| 66 |
|
| 67 |
def _cmd_call(self, args): |
| 68 |
entry = self._get_entry_point(self._get_arg(args, 0)) |
| 69 |
entry_fname = entry[0] |
| 70 |
num_ins = len(entry[1]) |
| 71 |
num_outs = len(entry[2]) |
| 72 |
exp_len = 1 + num_outs + num_ins |
| 73 |
|
| 74 |
if len(args) != exp_len: |
| 75 |
raise self.Failure("Invalid argument count, expected %d" % exp_len) |
| 76 |
|
| 77 |
out_vnames = args[1 : num_outs + 1] |
| 78 |
|
| 79 |
for out_vname in out_vnames: |
| 80 |
self._check_new_var(out_vname) |
| 81 |
|
| 82 |
in_vnames = args[1 + num_outs :] |
| 83 |
ins = [self._get_var(in_vname) for in_vname in in_vnames] |
| 84 |
|
| 85 |
try: |
| 86 |
(runtime, vals) = getattr(self._ctx, entry_fname)(*ins) |
| 87 |
except Exception as e: |
| 88 |
raise self.Failure(str(e)) |
| 89 |
|
| 90 |
print("runtime: %d" % runtime) |
| 91 |
|
| 92 |
if num_outs == 1: |
| 93 |
self._vars[out_vnames[0]] = vals |
| 94 |
else: |
| 95 |
for out_vname, val in zip(out_vnames, vals): |
| 96 |
self._vars[out_vname] = val |
| 97 |
|
| 98 |
def _store_val(self, f, value): |
| 99 |
# In case we are using the PyOpenCL backend, we first |
| 100 |
# need to convert OpenCL arrays to ordinary NumPy |
| 101 |
# arrays. We do this in a nasty way. |
| 102 |
if isinstance(value, opaque): |
| 103 |
for component in value.data: |
| 104 |
self._store_val(f, component) |
| 105 |
elif ( |
| 106 |
isinstance(value, np.number) |
| 107 |
or isinstance(value, bool) |
| 108 |
or isinstance(value, np.bool_) |
| 109 |
or isinstance(value, np.ndarray) |
| 110 |
): |
| 111 |
# Ordinary NumPy value. |
| 112 |
f.write(construct_binary_value(value)) |
| 113 |
else: |
| 114 |
# Assuming PyOpenCL array. |
| 115 |
f.write(construct_binary_value(value.get())) |
| 116 |
|
| 117 |
def _cmd_store(self, args): |
| 118 |
fname = self._get_arg(args, 0) |
| 119 |
|
| 120 |
with open(fname, "wb") as f: |
| 121 |
for i in range(1, len(args)): |
| 122 |
self._store_val(f, self._get_var(args[i])) |
| 123 |
|
| 124 |
def _restore_val(self, reader, typename): |
| 125 |
if typename in self._ctx.opaques: |
| 126 |
vs = [] |
| 127 |
for t in self._ctx.opaques[typename]: |
| 128 |
vs += [read_value(t, reader)] |
| 129 |
return opaque(typename, *vs) |
| 130 |
else: |
| 131 |
return read_value(typename, reader) |
| 132 |
|
| 133 |
def _cmd_restore(self, args): |
| 134 |
if len(args) % 2 == 0: |
| 135 |
raise self.Failure("Invalid argument count") |
| 136 |
|
| 137 |
fname = args[0] |
| 138 |
args = args[1:] |
| 139 |
|
| 140 |
with open(fname, "rb") as f: |
| 141 |
reader = ReaderInput(f) |
| 142 |
while args != []: |
| 143 |
vname = args[0] |
| 144 |
typename = args[1] |
| 145 |
args = args[2:] |
| 146 |
|
| 147 |
if vname in self._vars: |
| 148 |
raise self.Failure("Variable already exists: %s" % vname) |
| 149 |
|
| 150 |
try: |
| 151 |
self._vars[vname] = self._restore_val(reader, typename) |
| 152 |
except ValueError: |
| 153 |
raise self.Failure( |
| 154 |
"Failed to restore variable %s.\n" |
| 155 |
"Possibly malformed data in %s.\n" % (vname, fname) |
| 156 |
) |
| 157 |
|
| 158 |
skip_spaces(reader) |
| 159 |
if reader.get_char() != b"": |
| 160 |
raise self.Failure("Expected EOF after reading values") |
| 161 |
|
| 162 |
def _cmd_types(self, args): |
| 163 |
for k in self._ctx.opaques.keys(): |
| 164 |
print(k) |
| 165 |
|
| 166 |
def _cmd_entry_points(self, args): |
| 167 |
for k in self._ctx.entry_points.keys(): |
| 168 |
print(k) |
| 169 |
|
| 170 |
_commands = { |
| 171 |
"inputs": _cmd_inputs, |
| 172 |
"outputs": _cmd_outputs, |
| 173 |
"call": _cmd_call, |
| 174 |
"restore": _cmd_restore, |
| 175 |
"store": _cmd_store, |
| 176 |
"free": _cmd_free, |
| 177 |
"rename": _cmd_rename, |
| 178 |
"clear": _cmd_dummy, |
| 179 |
"pause_profiling": _cmd_dummy, |
| 180 |
"unpause_profiling": _cmd_dummy, |
| 181 |
"report": _cmd_dummy, |
| 182 |
"types": _cmd_types, |
| 183 |
"entry_points": _cmd_entry_points, |
| 184 |
} |
| 185 |
|
| 186 |
def _process_line(self, line): |
| 187 |
lex = shlex.shlex(line) |
| 188 |
lex.quotes = '"' |
| 189 |
lex.whitespace_split = True |
| 190 |
lex.commenters = "" |
| 191 |
words = list(lex) |
| 192 |
if words == []: |
| 193 |
raise self.Failure("Empty line") |
| 194 |
else: |
| 195 |
cmd = words[0] |
| 196 |
args = words[1:] |
| 197 |
if cmd in self._commands: |
| 198 |
self._commands[cmd](self, args) |
| 199 |
else: |
| 200 |
raise self.Failure("Unknown command: %s" % cmd) |
| 201 |
|
| 202 |
def run(self): |
| 203 |
while True: |
| 204 |
print("%%% OK", flush=True) |
| 205 |
line = sys.stdin.readline() |
| 206 |
if line == "": |
| 207 |
return |
| 208 |
try: |
| 209 |
self._process_line(line) |
| 210 |
except self.Failure as e: |
| 211 |
print("%%% FAILURE") |
| 212 |
print(e.msg) |
| 213 |
|
| 214 |
|
| 215 |
# End of server.py |