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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Start of server.js
class Server {
constructor(ctx) {
this.ctx = ctx;
this._vars = {};
this._types = {};
this._commands = [ 'inputs',
'outputs',
'call',
'restore',
'store',
'free',
'clear',
'pause_profiling',
'unpause_profiling',
'report',
'rename'
];
}
_get_arg(args, i) {
if (i < args.length) {
return args[i];
} else {
throw 'Insufficient command args';
}
}
_get_entry_point(entry) {
if (entry in this.ctx.get_entry_points()) {
return this.ctx.get_entry_points()[entry];
} else {
throw "Unkown entry point: " + entry;
}
}
_check_var(vname) {
if (!(vname in this._vars)) {
throw 'Unknown variable: ' + vname;
}
}
_set_var(vname, v, t) {
this._vars[vname] = v;
this._types[vname] = t;
}
_get_type(vname) {
this._check_var(vname);
return this._types[vname];
}
_get_var(vname) {
this._check_var(vname);
return this._vars[vname];
}
_cmd_inputs(args) {
var entry = this._get_arg(args, 0);
var inputs = this._get_entry_point(entry)[1];
for (var i = 0; i < inputs.length; i++) {
console.log(inputs[i]);
}
}
_cmd_outputs(args) {
var entry = this._get_arg(args, 0);
var outputs = this._get_entry_point(entry)[2];
for (var i = 0; i < outputs.length; i++) {
console.log(outputs[i]);
}
}
_cmd_dummy(args) {
// pass
}
_cmd_free(args) {
for (var i = 0; i < args.length; i++) {
var vname = args[i];
this._check_var(vname);
delete this._vars[vname];
}
}
_cmd_rename(args) {
var oldname = this._get_arg(args, 0)
var newname = this._get_arg(args, 1)
if (newname in this._vars) {
throw "Variable already exists: " + newname;
}
this._vars[newname] = this._vars[oldname];
this._types[newname] = this._types[oldname];
delete this._vars[oldname];
delete this._types[oldname];
}
_cmd_call(args) {
var entry = this._get_entry_point(this._get_arg(args, 0));
var num_ins = entry[1].length;
var num_outs = entry[2].length;
var expected_len = 1 + num_outs + num_ins
if (args.length != expected_len) {
throw "Invalid argument count, expected " + expected_len
}
var out_vnames = args.slice(1, num_outs+1)
for (var i = 0; i < out_vnames.length; i++) {
var out_vname = out_vnames[i];
if (out_vname in this._vars) {
throw "Variable already exists: " + out_vname;
}
}
var in_vnames = args.slice(1+num_outs);
var ins = [];
for (var i = 0; i < in_vnames.length; i++) {
ins.push(this._get_var(in_vnames[i]));
}
// Call entry point function from string name
var bef = performance.now()*1000;
var vals = this.ctx[entry[0]].apply(this.ctx, ins);
var aft = performance.now()*1000;
if (num_outs == 1) {
this._set_var(out_vnames[0], vals, entry[2][0]);
} else {
for (var i = 0; i < out_vnames.length; i++) {
this._set_var(out_vnames[i], vals[i], entry[2][i]);
}
}
console.log("runtime: " + Math.round(aft-bef));
}
_cmd_store(args) {
var fname = this._get_arg(args, 0);
for (var i = 1; i < args.length; i++) {
var vname = args[i];
var value = this._get_var(vname);
var typ = this._get_type(vname);
var fs = require("fs");
var bin_val = construct_binary_value(value, typ);
fs.appendFileSync(fname, bin_val, 'binary')
}
}
fut_to_dim_typ(typ) {
var type = typ;
var count = 0;
while (type.substr(0, 2) == '[]') {
count = count + 1;
type = type.slice(2);
}
return [count, type];
}
_cmd_restore(args) {
if (args.length % 2 == 0) {
throw "Invalid argument count";
}
var fname = args[0];
var args = args.slice(1);
var as = args;
var reader = new Reader(fname);
while (as.length != 0) {
var vname = as[0];
var typename = as[1];
as = as.slice(2);
if (vname in this._vars) {
throw "Variable already exists: " + vname;
}
try {
var value = read_value(typename, reader);
if (typeof value == 'number' || typeof value == 'bigint') {
this._set_var(vname, value, typename);
} else {
// We are working with an array and need to create to convert [shape, arr] to futhark ptr
var shape= value[0];
var arr = value[1];
var dimtyp = this.fut_to_dim_typ(typename);
var dim = dimtyp[0];
var typ = dimtyp[1];
var arg_list = [arr, ...shape];
var fnam = "new_" + typ + "_" + dim + "d";
var ptr = this.ctx[fnam].apply(this.ctx, arg_list);
this._set_var(vname, ptr, typename);
}
} catch (err) {
var err_msg = "Failed to restore variable " + vname + ".\nPossibly malformed data in " + fname + ".\n" + err.toString();
throw err_msg;
}
}
skip_spaces(reader);
if (reader.get_buff().length != 0) {
throw "Expected EOF after reading values";
}
}
_process_line(line) {
// TODO make sure it splits on anywhite space
var words = line.split(" ");
if (words.length == 0) {
throw "Empty line";
} else {
var cmd = words[0];
var args = words.splice(1);
if (this._commands.includes(cmd)) {
switch (cmd) {
case 'inputs': this._cmd_inputs(args); break;
case 'outputs': this._cmd_outputs(args); break
case 'call': this._cmd_call(args); break
case 'restore': this._cmd_restore(args); break
case 'store': this._cmd_store(args); break
case 'free': this._cmd_free(args); break
case 'clear': this._cmd_dummy(args); break
case 'pause_profiling': this._cmd_dummy(args); break
case 'unpause_profiling': this._cmd_dummy(args); break
case 'report': this._cmd_dummy(args); break
case 'rename': this._cmd_rename(args); break
}
} else {
throw "Unknown command: " + cmd;
}
}
}
run() {
console.log('%%% OK'); // TODO figure out if flushing is neccesary for JS
const readline = require('readline');
const rl = readline.createInterface(process.stdin);
rl.on('line', (line) => {
if (line == "") {
rl.close();
}
try {
this._process_line(line);
console.log('%%% OK');
} catch (err) {
console.log('%%% FAILURE');
console.log(err);
console.log('%%% OK');
}
}).on('close', () => { process.exit(0); });
}
}
// End of server.js
|