Debian Codegraph @

rts/c/server.h

raw
1 // Start of server.h.
2
3 // Forward declarations of things that we technically don't know until
4 // the application header file is included, but which we need.
5 struct futhark_context_config;
6 struct futhark_context;
7 char *futhark_context_get_error(struct futhark_context *ctx);
8 int futhark_context_sync(struct futhark_context *ctx);
9 int futhark_context_clear_caches(struct futhark_context *ctx);
10 int futhark_context_config_set_tuning_param(struct futhark_context_config *cfg,
11 const char *param_name,
12 size_t new_value);
13 int futhark_get_tuning_param_count(void);
14 const char* futhark_get_tuning_param_name(int i);
15 const char* futhark_get_tuning_param_class(int i);
16
17 typedef int (*restore_fn)(const void*, FILE *, struct futhark_context*, void*);
18 typedef void (*store_fn)(const void*, FILE *, struct futhark_context*, void*);
19 typedef int (*free_fn)(const void*, struct futhark_context*, void*);
20 typedef int (*project_fn)(struct futhark_context*, void*, const void*);
21 typedef int (*new_fn)(struct futhark_context*, void**, const void*[]);
22
23 struct field {
24 const char *name;
25 const struct type *type;
26 project_fn project;
27 };
28
29 struct record {
30 int num_fields;
31 const struct field* fields;
32 new_fn new;
33 };
34
35 struct type {
36 const char *name;
37 restore_fn restore;
38 store_fn store;
39 free_fn free;
40 const void *aux;
41 const struct record *record;
42 };
43
44 int free_scalar(const void *aux, struct futhark_context *ctx, void *p) {
45 (void)aux;
46 (void)ctx;
47 (void)p;
48 // Nothing to do.
49 return 0;
50 }
51
52 #define DEF_SCALAR_TYPE(T) \
53 int restore_##T(const void *aux, FILE *f, \
54 struct futhark_context *ctx, void *p) { \
55 (void)aux; \
56 (void)ctx; \
57 return read_scalar(f, &T##_info, p); \
58 } \
59 \
60 void store_##T(const void *aux, FILE *f, \
61 struct futhark_context *ctx, void *p) { \
62 (void)aux; \
63 (void)ctx; \
64 write_scalar(f, 1, &T##_info, p); \
65 } \
66 \
67 struct type type_##T = \
68 { .name = #T, \
69 .restore = restore_##T, \
70 .store = store_##T, \
71 .free = free_scalar \
72 } \
73
74 DEF_SCALAR_TYPE(i8);
75 DEF_SCALAR_TYPE(i16);
76 DEF_SCALAR_TYPE(i32);
77 DEF_SCALAR_TYPE(i64);
78 DEF_SCALAR_TYPE(u8);
79 DEF_SCALAR_TYPE(u16);
80 DEF_SCALAR_TYPE(u32);
81 DEF_SCALAR_TYPE(u64);
82 DEF_SCALAR_TYPE(f16);
83 DEF_SCALAR_TYPE(f32);
84 DEF_SCALAR_TYPE(f64);
85 DEF_SCALAR_TYPE(bool);
86
87 struct value {
88 const struct type *type;
89 union {
90 void *v_ptr;
91 int8_t v_i8;
92 int16_t v_i16;
93 int32_t v_i32;
94 int64_t v_i64;
95
96 uint8_t v_u8;
97 uint16_t v_u16;
98 uint32_t v_u32;
99 uint64_t v_u64;
100
101 uint16_t v_f16;
102 float v_f32;
103 double v_f64;
104
105 bool v_bool;
106 } value;
107 };
108
109 void* value_ptr(struct value *v) {
110 if (v->type == &type_i8) {
111 return &v->value.v_i8;
112 }
113 if (v->type == &type_i16) {
114 return &v->value.v_i16;
115 }
116 if (v->type == &type_i32) {
117 return &v->value.v_i32;
118 }
119 if (v->type == &type_i64) {
120 return &v->value.v_i64;
121 }
122 if (v->type == &type_u8) {
123 return &v->value.v_u8;
124 }
125 if (v->type == &type_u16) {
126 return &v->value.v_u16;
127 }
128 if (v->type == &type_u32) {
129 return &v->value.v_u32;
130 }
131 if (v->type == &type_u64) {
132 return &v->value.v_u64;
133 }
134 if (v->type == &type_f16) {
135 return &v->value.v_f16;
136 }
137 if (v->type == &type_f32) {
138 return &v->value.v_f32;
139 }
140 if (v->type == &type_f64) {
141 return &v->value.v_f64;
142 }
143 if (v->type == &type_bool) {
144 return &v->value.v_bool;
145 }
146 return &v->value.v_ptr;
147 }
148
149 struct variable {
150 // NULL name indicates free slot. Name is owned by this struct.
151 char *name;
152 struct value value;
153 };
154
155 typedef int (*entry_point_fn)(struct futhark_context*, void**, void**);
156
157 struct entry_point {
158 const char *name;
159 entry_point_fn f;
160 const char** tuning_params;
161 const struct type **out_types;
162 bool *out_unique;
163 const struct type **in_types;
164 bool *in_unique;
165 };
166
167 int entry_num_ins(struct entry_point *e) {
168 int count = 0;
169 while (e->in_types[count]) {
170 count++;
171 }
172 return count;
173 }
174
175 int entry_num_outs(struct entry_point *e) {
176 int count = 0;
177 while (e->out_types[count]) {
178 count++;
179 }
180 return count;
181 }
182
183 struct futhark_prog {
184 // Last entry point identified by NULL name.
185 struct entry_point *entry_points;
186 // Last type identified by NULL name.
187 const struct type **types;
188 };
189
190 struct server_state {
191 struct futhark_prog prog;
192 struct futhark_context_config *cfg;
193 struct futhark_context *ctx;
194 int variables_capacity;
195 struct variable *variables;
196 };
197
198 struct variable* get_variable(struct server_state *s,
199 const char *name) {
200 for (int i = 0; i < s->variables_capacity; i++) {
201 if (s->variables[i].name != NULL &&
202 strcmp(s->variables[i].name, name) == 0) {
203 return &s->variables[i];
204 }
205 }
206
207 return NULL;
208 }
209
210 struct variable* create_variable(struct server_state *s,
211 const char *name,
212 const struct type *type) {
213 int found = -1;
214 for (int i = 0; i < s->variables_capacity; i++) {
215 if (found == -1 && s->variables[i].name == NULL) {
216 found = i;
217 } else if (s->variables[i].name != NULL &&
218 strcmp(s->variables[i].name, name) == 0) {
219 return NULL;
220 }
221 }
222
223 if (found != -1) {
224 // Found a free spot.
225 s->variables[found].name = strdup(name);
226 s->variables[found].value.type = type;
227 return &s->variables[found];
228 }
229
230 // Need to grow the buffer.
231 found = s->variables_capacity;
232 s->variables_capacity *= 2;
233 s->variables = realloc(s->variables,
234 s->variables_capacity * sizeof(struct variable));
235
236 s->variables[found].name = strdup(name);
237 s->variables[found].value.type = type;
238
239 for (int i = found+1; i < s->variables_capacity; i++) {
240 s->variables[i].name = NULL;
241 }
242
243 return &s->variables[found];
244 }
245
246 void drop_variable(struct variable *v) {
247 free(v->name);
248 v->name = NULL;
249 }
250
251 int arg_exists(const char *args[], int i) {
252 return args[i] != NULL;
253 }
254
255 const char* get_arg(const char *args[], int i) {
256 if (!arg_exists(args, i)) {
257 futhark_panic(1, "Insufficient command args.\n");
258 }
259 return args[i];
260 }
261
262 const struct type* get_type(struct server_state *s, const char *name) {
263 for (int i = 0; s->prog.types[i]; i++) {
264 if (strcmp(s->prog.types[i]->name, name) == 0) {
265 return s->prog.types[i];
266 }
267 }
268
269 futhark_panic(1, "Unknown type %s\n", name);
270 return NULL;
271 }
272
273 struct entry_point* get_entry_point(struct server_state *s, const char *name) {
274 for (int i = 0; s->prog.entry_points[i].name; i++) {
275 if (strcmp(s->prog.entry_points[i].name, name) == 0) {
276 return &s->prog.entry_points[i];
277 }
278 }
279
280 return NULL;
281 }
282
283 // Print the command-done marker, indicating that we are ready for
284 // more input.
285 void ok(void) {
286 printf("%%%%%% OK\n");
287 fflush(stdout);
288 }
289
290 // Print the failure marker. Output is now an error message until the
291 // next ok().
292 void failure(void) {
293 printf("%%%%%% FAILURE\n");
294 }
295
296 void error_check(struct server_state *s, int err) {
297 if (err != 0) {
298 failure();
299 char *error = futhark_context_get_error(s->ctx);
300 if (error != NULL) {
301 puts(error);
302 }
303 free(error);
304 }
305 }
306
307 void cmd_call(struct server_state *s, const char *args[]) {
308 const char *name = get_arg(args, 0);
309
310 struct entry_point *e = get_entry_point(s, name);
311
312 if (e == NULL) {
313 failure();
314 printf("Unknown entry point: %s\n", name);
315 return;
316 }
317
318 int num_outs = entry_num_outs(e);
319 int num_ins = entry_num_ins(e);
320 // +1 to avoid zero-size arrays, which is UB.
321 void* outs[num_outs+1];
322 void* ins[num_ins+1];
323
324 for (int i = 0; i < num_ins; i++) {
325 const char *in_name = get_arg(args, 1+num_outs+i);
326 struct variable *v = get_variable(s, in_name);
327 if (v == NULL) {
328 failure();
329 printf("Unknown variable: %s\n", in_name);
330 return;
331 }
332 if (v->value.type != e->in_types[i]) {
333 failure();
334 printf("Wrong input type. Expected %s, got %s.\n",
335 e->in_types[i]->name, v->value.type->name);
336 return;
337 }
338 ins[i] = value_ptr(&v->value);
339 }
340
341 for (int i = 0; i < num_outs; i++) {
342 const char *out_name = get_arg(args, 1+i);
343 struct variable *v = create_variable(s, out_name, e->out_types[i]);
344 if (v == NULL) {
345 failure();
346 printf("Variable already exists: %s\n", out_name);
347 return;
348 }
349 outs[i] = value_ptr(&v->value);
350 }
351
352 int64_t t_start = get_wall_time();
353 int err = e->f(s->ctx, outs, ins);
354 err |= futhark_context_sync(s->ctx);
355 int64_t t_end = get_wall_time();
356 long long int elapsed_usec = t_end - t_start;
357 printf("runtime: %lld\n", elapsed_usec);
358
359 error_check(s, err);
360 if (err != 0) {
361 // Need to uncreate the output variables, which would otherwise be left
362 // in an uninitialised state.
363 for (int i = 0; i < num_outs; i++) {
364 const char *out_name = get_arg(args, 1+i);
365 struct variable *v = get_variable(s, out_name);
366 if (v) {
367 drop_variable(v);
368 }
369 }
370 }
371 }
372
373 void cmd_restore(struct server_state *s, const char *args[]) {
374 const char *fname = get_arg(args, 0);
375
376 FILE *f = fopen(fname, "rb");
377 if (f == NULL) {
378 failure();
379 printf("Failed to open %s: %s\n", fname, strerror(errno));
380 return;
381 }
382
383 int bad = 0;
384 int values = 0;
385 for (int i = 1; arg_exists(args, i); i+=2, values++) {
386 const char *vname = get_arg(args, i);
387 const char *type = get_arg(args, i+1);
388
389 const struct type *t = get_type(s, type);
390 struct variable *v = create_variable(s, vname, t);
391
392 if (v == NULL) {
393 bad = 1;
394 failure();
395 printf("Variable already exists: %s\n", vname);
396 break;
397 }
398
399 errno = 0;
400 if (t->restore(t->aux, f, s->ctx, value_ptr(&v->value)) != 0) {
401 bad = 1;
402 failure();
403 printf("Failed to restore variable %s.\n"
404 "Possibly malformed data in %s (errno: %s)\n",
405 vname, fname, strerror(errno));
406 drop_variable(v);
407 break;
408 }
409 }
410
411 if (!bad && end_of_input(f) != 0) {
412 failure();
413 printf("Expected EOF after reading %d values from %s\n",
414 values, fname);
415 }
416
417 fclose(f);
418
419 if (!bad) {
420 int err = futhark_context_sync(s->ctx);
421 error_check(s, err);
422 }
423 }
424
425 void cmd_store(struct server_state *s, const char *args[]) {
426 const char *fname = get_arg(args, 0);
427
428 FILE *f = fopen(fname, "wb");
429 if (f == NULL) {
430 failure();
431 printf("Failed to open %s: %s\n", fname, strerror(errno));
432 } else {
433 for (int i = 1; arg_exists(args, i); i++) {
434 const char *vname = get_arg(args, i);
435 struct variable *v = get_variable(s, vname);
436
437 if (v == NULL) {
438 failure();
439 printf("Unknown variable: %s\n", vname);
440 return;
441 }
442
443 const struct type *t = v->value.type;
444 t->store(t->aux, f, s->ctx, value_ptr(&v->value));
445 }
446 fclose(f);
447 }
448 }
449
450 void cmd_free(struct server_state *s, const char *args[]) {
451 for (int i = 0; arg_exists(args, i); i++) {
452 const char *name = get_arg(args, i);
453 struct variable *v = get_variable(s, name);
454
455 if (v == NULL) {
456 failure();
457 printf("Unknown variable: %s\n", name);
458 return;
459 }
460
461 const struct type *t = v->value.type;
462
463 int err = t->free(t->aux, s->ctx, value_ptr(&v->value));
464 error_check(s, err);
465 drop_variable(v);
466 }
467 }
468
469 void cmd_rename(struct server_state *s, const char *args[]) {
470 const char *oldname = get_arg(args, 0);
471 const char *newname = get_arg(args, 1);
472 struct variable *old = get_variable(s, oldname);
473 struct variable *new = get_variable(s, newname);
474
475 if (old == NULL) {
476 failure();
477 printf("Unknown variable: %s\n", oldname);
478 return;
479 }
480
481 if (new != NULL) {
482 failure();
483 printf("Variable already exists: %s\n", newname);
484 return;
485 }
486
487 free(old->name);
488 old->name = strdup(newname);
489 }
490
491 void cmd_inputs(struct server_state *s, const char *args[]) {
492 const char *name = get_arg(args, 0);
493 struct entry_point *e = get_entry_point(s, name);
494
495 if (e == NULL) {
496 failure();
497 printf("Unknown entry point: %s\n", name);
498 return;
499 }
500
501 int num_ins = entry_num_ins(e);
502 for (int i = 0; i < num_ins; i++) {
503 if (e->in_unique[i]) {
504 putchar('*');
505 }
506 puts(e->in_types[i]->name);
507 }
508 }
509
510 void cmd_outputs(struct server_state *s, const char *args[]) {
511 const char *name = get_arg(args, 0);
512 struct entry_point *e = get_entry_point(s, name);
513
514 if (e == NULL) {
515 failure();
516 printf("Unknown entry point: %s\n", name);
517 return;
518 }
519
520 int num_outs = entry_num_outs(e);
521 for (int i = 0; i < num_outs; i++) {
522 if (e->out_unique[i]) {
523 putchar('*');
524 }
525 puts(e->out_types[i]->name);
526 }
527 }
528
529 void cmd_clear(struct server_state *s, const char *args[]) {
530 (void)args;
531 int err = 0;
532 for (int i = 0; i < s->variables_capacity; i++) {
533 struct variable *v = &s->variables[i];
534 if (v->name != NULL) {
535 err |= v->value.type->free(v->value.type->aux, s->ctx, value_ptr(&v->value));
536 drop_variable(v);
537 }
538 }
539 err |= futhark_context_clear_caches(s->ctx);
540 error_check(s, err);
541 }
542
543 void cmd_pause_profiling(struct server_state *s, const char *args[]) {
544 (void)args;
545 futhark_context_pause_profiling(s->ctx);
546 }
547
548 void cmd_unpause_profiling(struct server_state *s, const char *args[]) {
549 (void)args;
550 futhark_context_unpause_profiling(s->ctx);
551 }
552
553 void cmd_report(struct server_state *s, const char *args[]) {
554 (void)args;
555 char *report = futhark_context_report(s->ctx);
556 if (report) {
557 puts(report);
558 } else {
559 failure();
560 report = futhark_context_get_error(s->ctx);
561 if (report) {
562 puts(report);
563 } else {
564 puts("Failed to produce profiling report.\n");
565 }
566 }
567 free(report);
568 }
569
570 void cmd_set_tuning_param(struct server_state *s, const char *args[]) {
571 const char *param = get_arg(args, 0);
572 const char *val_s = get_arg(args, 1);
573 size_t val = atol(val_s);
574 int err = futhark_context_config_set_tuning_param(s->cfg, param, val);
575
576 error_check(s, err);
577
578 if (err != 0) {
579 printf("Failed to set tuning parameter %s to %ld\n", param, (long)val);
580 }
581 }
582
583 void cmd_tuning_params(struct server_state *s, const char *args[]) {
584 const char *name = get_arg(args, 0);
585 struct entry_point *e = get_entry_point(s, name);
586
587 if (e == NULL) {
588 failure();
589 printf("Unknown entry point: %s\n", name);
590 return;
591 }
592
593 const char **params = e->tuning_params;
594 for (int i = 0; params[i] != NULL; i++) {
595 printf("%s\n", params[i]);
596 }
597 }
598
599 void cmd_tuning_param_class(struct server_state *s, const char *args[]) {
600 (void)s;
601 const char *param = get_arg(args, 0);
602
603 int n = futhark_get_tuning_param_count();
604
605 for (int i = 0; i < n; i++) {
606 if (strcmp(futhark_get_tuning_param_name(i), param) == 0) {
607 printf("%s\n", futhark_get_tuning_param_class(i));
608 return;
609 }
610 }
611
612 failure();
613 printf("Unknown tuning parameter: %s\n", param);
614 }
615
616 void cmd_fields(struct server_state *s, const char *args[]) {
617 const char *type = get_arg(args, 0);
618 const struct type *t = get_type(s, type);
619 const struct record *r = t->record;
620
621 if (r == NULL) {
622 failure();
623 printf("Not a record type\n");
624 return;
625 }
626
627 for (int i = 0; i < r->num_fields; i++) {
628 const struct field f = r->fields[i];
629 printf("%s %s\n", f.name, f.type->name);
630 }
631 }
632
633 void cmd_project(struct server_state *s, const char *args[]) {
634 const char *to_name = get_arg(args, 0);
635 const char *from_name = get_arg(args, 1);
636 const char *field_name = get_arg(args, 2);
637
638 struct variable *from = get_variable(s, from_name);
639
640 if (from == NULL) {
641 failure();
642 printf("Unknown variable: %s\n", from_name);
643 return;
644 }
645
646 const struct type *from_type = from->value.type;
647 const struct record *r = from_type->record;
648
649 if (r == NULL) {
650 failure();
651 printf("Not a record type\n");
652 return;
653 }
654
655 const struct field *field = NULL;
656 for (int i = 0; i < r->num_fields; i++) {
657 if (strcmp(r->fields[i].name, field_name) == 0) {
658 field = &r->fields[i];
659 break;
660 }
661 }
662
663 if (field == NULL) {
664 failure();
665 printf("No such field\n");
666 }
667
668 struct variable *to = create_variable(s, to_name, field->type);
669
670 if (to == NULL) {
671 failure();
672 printf("Variable already exists: %s\n", to_name);
673 return;
674 }
675
676 field->project(s->ctx, value_ptr(&to->value), from->value.value.v_ptr);
677 }
678
679 void cmd_new(struct server_state *s, const char *args[]) {
680 const char *to_name = get_arg(args, 0);
681 const char *type_name = get_arg(args, 1);
682 const struct type *type = get_type(s, type_name);
683 struct variable *to = create_variable(s, to_name, type);
684
685 if (to == NULL) {
686 failure();
687 printf("Variable already exists: %s\n", to_name);
688 return;
689 }
690
691 const struct record* r = type->record;
692
693 if (r == NULL) {
694 failure();
695 printf("Not a record type\n");
696 return;
697 }
698
699 int num_args = 0;
700 for (int i = 2; arg_exists(args, i); i++) {
701 num_args++;
702 }
703
704 if (num_args != r->num_fields) {
705 failure();
706 printf("%d fields expected but %d values provided.\n", num_args, r->num_fields);
707 return;
708 }
709
710 const void** value_ptrs = alloca(num_args * sizeof(void*));
711
712 for (int i = 0; i < num_args; i++) {
713 struct variable* v = get_variable(s, args[2+i]);
714
715 if (v == NULL) {
716 failure();
717 printf("Unknown variable: %s\n", args[2+i]);
718 return;
719 }
720
721 if (strcmp(v->value.type->name, r->fields[i].type->name) != 0) {
722 failure();
723 printf("Field %s mismatch: expected type %s, got %s\n",
724 r->fields[i].name, r->fields[i].type->name, v->value.type->name);
725 return;
726 }
727
728 value_ptrs[i] = value_ptr(&v->value);
729 }
730
731 r->new(s->ctx, value_ptr(&to->value), value_ptrs);
732 }
733
734 void cmd_entry_points(struct server_state *s, const char *args[]) {
735 (void)args;
736 for (int i = 0; s->prog.entry_points[i].name; i++) {
737 puts(s->prog.entry_points[i].name);
738 }
739 }
740
741 void cmd_types(struct server_state *s, const char *args[]) {
742 (void)args;
743 for (int i = 0; s->prog.types[i] != NULL; i++) {
744 puts(s->prog.types[i]->name);
745 }
746 }
747
748 char *next_word(char **line) {
749 char *p = *line;
750
751 while (isspace(*p)) {
752 p++;
753 }
754
755 if (*p == 0) {
756 return NULL;
757 }
758
759 if (*p == '"') {
760 char *save = p+1;
761 // Skip ahead till closing quote.
762 p++;
763
764 while (*p && *p != '"') {
765 p++;
766 }
767
768 if (*p == '"') {
769 *p = 0;
770 *line = p+1;
771 return save;
772 } else {
773 return NULL;
774 }
775 } else {
776 char *save = p;
777 // Skip ahead till next whitespace.
778
779 while (*p && !isspace(*p)) {
780 p++;
781 }
782
783 if (*p) {
784 *p = 0;
785 *line = p+1;
786 } else {
787 *line = p;
788 }
789 return save;
790 }
791 }
792
793 void process_line(struct server_state *s, char *line) {
794 int max_num_tokens = 1000;
795 const char* tokens[max_num_tokens];
796 int num_tokens = 0;
797
798 while ((tokens[num_tokens] = next_word(&line)) != NULL) {
799 num_tokens++;
800 if (num_tokens == max_num_tokens) {
801 futhark_panic(1, "Line too long.\n");
802 }
803 }
804
805 const char *command = tokens[0];
806
807 if (command == NULL) {
808 failure();
809 printf("Empty line\n");
810 } else if (strcmp(command, "call") == 0) {
811 cmd_call(s, tokens+1);
812 } else if (strcmp(command, "restore") == 0) {
813 cmd_restore(s, tokens+1);
814 } else if (strcmp(command, "store") == 0) {
815 cmd_store(s, tokens+1);
816 } else if (strcmp(command, "free") == 0) {
817 cmd_free(s, tokens+1);
818 } else if (strcmp(command, "rename") == 0) {
819 cmd_rename(s, tokens+1);
820 } else if (strcmp(command, "inputs") == 0) {
821 cmd_inputs(s, tokens+1);
822 } else if (strcmp(command, "outputs") == 0) {
823 cmd_outputs(s, tokens+1);
824 } else if (strcmp(command, "clear") == 0) {
825 cmd_clear(s, tokens+1);
826 } else if (strcmp(command, "pause_profiling") == 0) {
827 cmd_pause_profiling(s, tokens+1);
828 } else if (strcmp(command, "unpause_profiling") == 0) {
829 cmd_unpause_profiling(s, tokens+1);
830 } else if (strcmp(command, "report") == 0) {
831 cmd_report(s, tokens+1);
832 } else if (strcmp(command, "set_tuning_param") == 0) {
833 cmd_set_tuning_param(s, tokens+1);
834 } else if (strcmp(command, "tuning_params") == 0) {
835 cmd_tuning_params(s, tokens+1);
836 } else if (strcmp(command, "tuning_param_class") == 0) {
837 cmd_tuning_param_class(s, tokens+1);
838 } else if (strcmp(command, "fields") == 0) {
839 cmd_fields(s, tokens+1);
840 } else if (strcmp(command, "new") == 0) {
841 cmd_new(s, tokens+1);
842 } else if (strcmp(command, "project") == 0) {
843 cmd_project(s, tokens+1);
844 } else if (strcmp(command, "entry_points") == 0) {
845 cmd_entry_points(s, tokens+1);
846 } else if (strcmp(command, "types") == 0) {
847 cmd_types(s, tokens+1);
848 } else {
849 futhark_panic(1, "Unknown command: %s\n", command);
850 }
851 }
852
853 void run_server(struct futhark_prog *prog,
854 struct futhark_context_config *cfg,
855 struct futhark_context *ctx) {
856 char *line = NULL;
857 size_t buflen = 0;
858 ssize_t linelen;
859
860 struct server_state s = {
861 .cfg = cfg,
862 .ctx = ctx,
863 .variables_capacity = 100,
864 .prog = *prog
865 };
866
867 s.variables = malloc(s.variables_capacity * sizeof(struct variable));
868
869 for (int i = 0; i < s.variables_capacity; i++) {
870 s.variables[i].name = NULL;
871 }
872
873 ok();
874 while ((linelen = getline(&line, &buflen, stdin)) > 0) {
875 process_line(&s, line);
876 ok();
877 }
878
879 free(s.variables);
880 free(line);
881 }
882
883 // The aux struct lets us write generic method implementations without
884 // code duplication.
885
886 typedef void* (*array_new_fn)(struct futhark_context *, const void*, const int64_t*);
887 typedef const int64_t* (*array_shape_fn)(struct futhark_context*, void*);
888 typedef int (*array_values_fn)(struct futhark_context*, void*, void*);
889 typedef int (*array_free_fn)(struct futhark_context*, void*);
890
891 struct array_aux {
892 int rank;
893 const struct primtype_info_t* info;
894 const char *name;
895 array_new_fn new;
896 array_shape_fn shape;
897 array_values_fn values;
898 array_free_fn free;
899 };
900
901 int restore_array(const struct array_aux *aux, FILE *f,
902 struct futhark_context *ctx, void *p) {
903 void *data = NULL;
904 int64_t shape[aux->rank];
905 if (read_array(f, aux->info, &data, shape, aux->rank) != 0) {
906 return 1;
907 }
908
909 void *arr = aux->new(ctx, data, shape);
910 if (arr == NULL) {
911 return 1;
912 }
913 int err = futhark_context_sync(ctx);
914 *(void**)p = arr;
915 free(data);
916 return err;
917 }
918
919 void store_array(const struct array_aux *aux, FILE *f,
920 struct futhark_context *ctx, void *p) {
921 void *arr = *(void**)p;
922 const int64_t *shape = aux->shape(ctx, arr);
923 int64_t size = sizeof(aux->info->size);
924 for (int i = 0; i < aux->rank; i++) {
925 size *= shape[i];
926 }
927 int32_t *data = malloc(size);
928 assert(aux->values(ctx, arr, data) == 0);
929 assert(futhark_context_sync(ctx) == 0);
930 assert(write_array(f, 1, aux->info, data, shape, aux->rank) == 0);
931 free(data);
932 }
933
934 int free_array(const struct array_aux *aux,
935 struct futhark_context *ctx, void *p) {
936 void *arr = *(void**)p;
937 return aux->free(ctx, arr);
938 }
939
940 typedef void* (*opaque_restore_fn)(struct futhark_context*, void*);
941 typedef int (*opaque_store_fn)(struct futhark_context*, const void*, void **, size_t *);
942 typedef int (*opaque_free_fn)(struct futhark_context*, void*);
943
944 struct opaque_aux {
945 opaque_restore_fn restore;
946 opaque_store_fn store;
947 opaque_free_fn free;
948 };
949
950 int restore_opaque(const struct opaque_aux *aux, FILE *f,
951 struct futhark_context *ctx, void *p) {
952 // We have a problem: we need to load data from 'f', since the
953 // restore function takes a pointer, but we don't know how much we
954 // need (and cannot possibly). So we do something hacky: we read
955 // *all* of the file, pass all of the data to the restore function
956 // (which doesn't care if there's extra at the end), then we compute
957 // how much space the the object actually takes in serialised form
958 // and rewind the file to that position. The only downside is more IO.
959 size_t start = ftell(f);
960 size_t size;
961 char *bytes = fslurp_file(f, &size);
962 void *obj = aux->restore(ctx, bytes);
963 free(bytes);
964 if (obj != NULL) {
965 *(void**)p = obj;
966 size_t obj_size;
967 (void)aux->store(ctx, obj, NULL, &obj_size);
968 fseek(f, start+obj_size, SEEK_SET);
969 return 0;
970 } else {
971 fseek(f, start, SEEK_SET);
972 return 1;
973 }
974 }
975
976 void store_opaque(const struct opaque_aux *aux, FILE *f,
977 struct futhark_context *ctx, void *p) {
978 void *obj = *(void**)p;
979 size_t obj_size;
980 void *data = NULL;
981 (void)aux->store(ctx, obj, &data, &obj_size);
982 assert(futhark_context_sync(ctx) == 0);
983 fwrite(data, sizeof(char), obj_size, f);
984 free(data);
985 }
986
987 int free_opaque(const struct opaque_aux *aux,
988 struct futhark_context *ctx, void *p) {
989 void *obj = *(void**)p;
990 return aux->free(ctx, obj);
991 }
992
993 // End of server.h.