diff --git a/bin/lustre-mt/main.ml b/bin/lustre-mt/main.ml index d9766a7cd8f96cbd524091babdf3c3c571e60c0d..b0081614041e5726215385b4dd209871b3e7775a 100644 --- a/bin/lustre-mt/main.ml +++ b/bin/lustre-mt/main.ml @@ -4,123 +4,22 @@ open Printf open Types - -let variables = ref [];; -let channels = ref [];; -let main = {name = ""; ctx_type = ""; ctx_new = ""; var_in = []; var_out = []; ch_in = []; ch_out = []};; -let nodes = ref [];; -let instances = ref [];; - - -(** saves the data from the Yaml parser into references of type Types.t *) - -(* converts a `Float list into a int list *) -let (intlist : Yaml.value list -> int list) = - fun l -> - let (yv_to_int : Yaml.value -> int) = fun y -> - match y with - |`Float f -> int_of_float f - |_ -> assert false - in List.map yv_to_int l -;; - -let (save_variable_attributes : Types.variable -> (string * Yaml.value) -> unit) = - fun v (key, value) -> - match key, value with - |"id", `Float _f -> () - |"name", `String s -> v.name <- s - |"type", `String s -> v.var_type <- s - |_ -> assert false - -let (save_variables : Yaml.value -> unit) = fun x -> - let v = {name = ""; var_type = ""}::[] - in - match x with - |`O l -> - List.iter (save_variable_attributes (List.hd v)) l; - variables := !variables @ v - |_ -> assert false - - -let (save_channels : Yaml.value -> unit) = fun x -> - match x with - |`Float f -> channels := !channels @ [int_of_float f] - |_ -> assert false - - -let (save_main : (string * Yaml.value) -> unit) = - fun (key, value) -> - match key, value with - |"name", `String s -> main.name <- s - |"ctx_type", `String s -> main.ctx_type <- s - |"ctx_new", `String s -> main.ctx_new <- s - |"var_in", `A l -> main.var_in <- List.map (List.nth !variables) (intlist l) - |"var_out", `A l -> main.var_out <- List.map (List.nth !variables) (intlist l) - |"ch_in", `A l -> main.ch_in <- intlist l - |"ch_out", `A l -> main.ch_out <- intlist l - |_ -> assert false - - -let (save_node_attributes : Types.node -> (string * Yaml.value) -> unit) = - fun n (key, value) -> - match key, value with - |"id", `Float _f -> () - |"file_name", `String s -> n.file_name <- s - |"fct_name", `String s -> n.fct_name <- s - |"ctx", `Bool b -> n.ctx <- b - |"ctx_tab", `String s -> n.ctx_tab <- s - |_ -> assert false - -let (save_nodes : Yaml.value -> unit) = fun x -> - let n = {file_name = ""; fct_name = ""; ctx = false; ctx_tab = ""}::[] in - match x with - |`O l -> - List.iter (save_node_attributes (List.hd n)) l; - nodes := !nodes @ n - |_ -> assert false - - -let (save_instance_attributes : Types.instance -> (string * Yaml.value) -> unit) = - fun i (key, value) -> - match key, value with - |"id", `Float f -> i.id <- int_of_float f - |"node", `Float f -> i.node <- List.nth !nodes (int_of_float f) - |"var_in", `A l -> i.var_in <- List.map (List.nth !variables) (intlist l) - |"var_out", `A l -> i.var_out <- List.map (List.nth !variables) (intlist l) - |"ch_in", `A l -> i.ch_in <- intlist l - |"ch_out", `A l -> i.ch_out <- intlist l - |_ -> assert false - -let (save_instances : Yaml.value -> unit) = fun x -> - let i = {id = 0; node = List.nth !nodes 0; var_in = []; var_out = []; ch_in = []; ch_out = []}::[] - in - match x with - |`O l -> - List.iter (save_instance_attributes (List.hd i)) l; - instances := !instances @ i - |_ -> assert false - - -let (save_data2 : (string * Yaml.value) -> unit) = - fun (key, value) -> - match key, value with - |"variables", `A l -> List.iter save_variables l - |"channels" , `A l -> List.iter save_channels l - |"main" , `O l -> List.iter save_main l - |"nodes", `A l -> List.iter save_nodes l - |"instances", `A l -> List.iter save_instances l - |_ -> () - -let (save_data : Yaml.value -> unit) = fun x -> - match x with - |`O l -> List.iter save_data2 l - |_ -> assert false - - +open Parser let yaml_file = ref "" - +(* let print_var_inputs var name = +* fprintf cfile "void %s_TASK_setin_%s(%s_TASK_type tD, %s %s) {\n" name var.name name var.var_type var.name; +* fprintf cfile "%s_TASK_struct* ts = (%s_TASK_struct*) tD;\n" name; +* fprintf cfile "ts->ctx.%s = %s;\n" var.name var.name; +* fprintf cfile "}\n\n"; +* in +* let print_var_outputs var name = +* fprintf cfile "void %s_TASK_getout_%s(%s_TASK_type tD, %s* p%s) {\n" name var.name name var.var_type var.name; +* fprintf cfile "%s_TASK_struct* ts = (%s_TASK_struct*) tD;\n" name; +* fprintf cfile "*p%s = ts->ctx.%s;\n" var.name var.name; +* fprintf cfile "}\n\n"; +* in *) let main () = if (Array.length Sys.argv) <= 1 then ( Arg.usage MainArgs.speclist MainArgs.usage; flush stdout; exit 2 @@ -131,22 +30,21 @@ let main () = | e -> print_string (Printexc.to_string e); flush stdout; exit 2 ); let yaml = Yaml_unix.of_file_exn Fpath.(v !yaml_file) in - save_data yaml; + Parser.get_data yaml; (* creates the .c file *) - let cfile = open_out (main.name ^ "_pthread.c") in + let cfile = open_out (!Parser.name ^ "_thread.c") in (* includes *) fprintf cfile "#include <stdio.h>\n"; - fprintf cfile "#include <stdlib.h>\n"; - fprintf cfile "#include <string.h>\n"; - fprintf cfile "#include <stdbool.h>\n"; fprintf cfile "#include <pthread.h>\n"; fprintf cfile "#include <semaphore.h>\n"; fprintf cfile "#include <errno.h>\n"; - fprintf cfile "#include \"%s.h\"\n" main.name; - fprintf cfile "#include \"%s_loop_io.h\"\n" main.name; + fprintf cfile "#include <time.h>\n"; + fprintf cfile "#include <sys/time.h>\n"; + + fprintf cfile "#include \"%s.h\"\n" !Parser.name; fprintf cfile "\n"; (* semaphores macro *) @@ -158,86 +56,121 @@ let main () = fprintf cfile "#define SEM_SIGNAL(sem) sem_post(&(sem))\n"; fprintf cfile "\n"; - (* variables declaration *) - fprintf cfile "/* Declare variables */\n"; - List.iter (fun x -> fprintf cfile "%s %s;\n" x.var_type x.name) !variables; - fprintf cfile "\n"; - - (* semaphores declaration *) - fprintf cfile "/* Declare semaphores */\n"; - List.iter (fun x -> fprintf cfile "sem_t channel%i;\n" x) !channels; - fprintf cfile "\n"; - (* ctx declaration *) - fprintf cfile "/* Declare context */\n"; - fprintf cfile "%s* ctx;\n" main.ctx_type; - fprintf cfile "\n"; - (* instance loops *) - fprintf cfile "/* Instance loops */\n"; - let print_instance_loop instance = - fprintf cfile "void loop_%s%i() {\n" instance.node.file_name instance.id; - fprintf cfile " while(true) {\n"; - - List.iter (fprintf cfile " SEM_WAIT(channel%i);\n") instance.ch_in; - fprintf cfile " %s(" instance.node.fct_name; - fprintf cfile "%s" (List.hd instance.var_in).name; - List.iter (fun (x:variable) -> fprintf cfile ", %s" x.name) (List.tl instance.var_in); - List.iter (fun (x:variable) -> fprintf cfile ", &%s" x.name) instance.var_out; - if instance.node.ctx then fprintf cfile ", &ctx->%s[%i]" instance.node.ctx_tab instance.id; - fprintf cfile ");\n"; - List.iter (fprintf cfile " SEM_SIGNAL(channel%i);\n") instance.ch_out; - - fprintf cfile " }\n"; - fprintf cfile "}\n"; - fprintf cfile "\n"; + fprintf cfile "/* task structures */\n"; + let print_task_structs task = + fprintf cfile "typedef struct {\n"; + fprintf cfile "\t%s_ctx_type ctx;\n" task.name; + fprintf cfile "\tsem_t sem_start;\n"; + fprintf cfile "\tsem_t sem_join;\n"; + fprintf cfile "pthread_t thr;\n}"; + fprintf cfile "%s_TASK_struct;" task.name; + fprintf cfile "\n\n" in - List.iter print_instance_loop !instances; - - (* main function *) - fprintf cfile "/* Main function */\n"; - fprintf cfile "void main() { - int _s = 0;\n"; - - fprintf cfile " /* Initialize context */\n"; - fprintf cfile " ctx = %s(NULL);\n" main.ctx_new; - fprintf cfile "\n"; - - fprintf cfile " /* Initialize semaphores */\n"; - List.iter (fprintf cfile " SEM_INIT(channel%i, 0, 1);\n") !channels; - fprintf cfile " \n"; - fprintf cfile " /* Declare pthreads */\n"; - List.iter (fun x -> fprintf cfile " pthread_t pt_%s%i;\n" x.node.file_name x.id) !instances; - fprintf cfile " \n"; - fprintf cfile " /* Initialize pthreads */\n"; - List.iter (fun x -> fprintf cfile " pthread_create(&pt_%s%i, NULL, loop_%s%i, NULL);\n" x.node.file_name x.id x.node.file_name x.id) !instances; - fprintf cfile " \n"; - - (* main loop *) - fprintf cfile "print_rif_declaration();\n"; - output_string cfile " /* Main loop */ - while(true) { - if (ISATTY) printf(\"#step \\%d \\n\", _s+1); - else if(_s) printf(\"\\n\"); - fflush(stdout); - ++_s;\n"; - fprintf cfile " get_inputs(ctx"; - List.iter (fun (x:variable) -> fprintf cfile ", &%s" x.name) main.var_in; - fprintf cfile ");\n"; - - List.iter (fprintf cfile " SEM_SIGNAL(channel%i);\n") main.ch_in; - List.iter (fprintf cfile " SEM_WAIT(channel%i);\n") main.ch_out; - - fprintf cfile " print_outputs(%s" (List.hd main.var_out).name; - List.iter (fun (x:variable) -> fprintf cfile ", %s" x.name) (List.tl main.var_out); - fprintf cfile ");\n"; - - fprintf cfile " }\n"; - fprintf cfile " \n"; - - List.iter (fun x -> fprintf cfile " pthread_join(&pt_%s%i, NULL);\n" x.node.file_name x.id) !instances; - fprintf cfile "}\n"; - fprintf cfile "\n" + List.iter print_task_structs !tasks; + + (* fprintf cfile "/* runners */\n"; + *let print_task_runners task = + * fprintf cfile "void* %s_runner(void* cd) {\n" task.name; + * fprintf cfile "\t%s_TASK_struct* ts = (%s_TASK_struct*) cd" task.name task.name; + * fprintf cfile "\twhile(1){\n"; + * fprintf cfile "\t\tSEM_WAIT(ts->sem_start);\n"; + * fprintf cfile "\t\t%s_step(&ts->ctx);\n" task.name; + * fprintf cfile "\t\tSEM_SIGNAL(ts->sem_join);\n"; + * fprintf cfile "\t}\n"; + * fprintf cfile "}\n\n" + *in + *List.iter print_task_runners !tasks; +* + *fprintf cfile "/* task Initializers */\n"; + *let print_task_inits task = + * fprintf cfile "%s_TASK_type %s_TASK_init() {\n" task.name task.name; + * fprintf cfile "\t%s_TASK_struct* ts = (%s_TASK_struct*)calloc(1, sizeof(%s_TASK_struct));\n" task.name task.name task.name; + * fprintf cfile "\t%s_ctx_init(&ts->ctx);\n" task.name; + * fprintf cfile "SEM_INIT(ts->sem_start, 0, 1);\n"; + * fprintf cfile "SEM_INIT(ts->sem_join, 0, 1);\n"; + * fprintf cfile "pthread_create(&(ts->thr), NULL, %s_runner, ts);\n" task.name; + * fprintf cfile "return (void*) ts;\n"; + * fprintf cfile "}\n\n" + *in + *List.iter print_task_inits !tasks; + * + *fprintf cfile "/* task resets */\n"; + *let print_task_resets task = + * fprintf cfile "void %s_TASK_reset(%s_TASK_type tD) {\n" task.name task.name; + * fprintf cfile "%s_TASK_struct* ts = (%s_TASK_struct*) tD;\n" task.name task.name; + * fprintf cfile "%s_ctx_reset(&ts->ctx)\n;" task.name; + * fprintf cfile "}\n\n" + *in + *List.iter print_task_resets !tasks; + *) + + (* fprintf cfile "/* task inputs */\n"; + * let print_task_inputs task = + * List.iter print_var_inputs task.var_in task.name; + * task. + * in + * List.iter print_task_inputs !tasks; + *) + + + + + +(* +* (* main function *) +* fprintf cfile "/* Main function */\n"; +* fprintf cfile "void main() { +* int _s = 0;\n"; +* +* fprintf cfile " /* Initialize context */\n"; +* fprintf cfile " ctx = %s(NULL);\n" main.ctx_new; +* fprintf cfile "\n"; +* +* fprintf cfile " /* Initialize semaphores */\n"; +* List.iter (fprintf cfile " SEM_INIT(channel%i, 0, 1);\n") !channels; +* fprintf cfile " \n"; +* +* let x = List.length !instances in +* let i = x+1 in +* fprintf cfile "\t#pragma omp parallel num_threads(%i) default(shared)\n" i; +* fprintf cfile "\t{\n"; +* (* main loop *) +* fprintf cfile "\t\t#pragma omp single nowait\n{\n"; +* fprintf cfile "print_rif_declaration();\n"; +* output_string cfile " /* Main loop */ +* while(true) { +* if (ISATTY) printf(\"#step \\%d \\n\", _s+1); +* else if(_s) printf(\"\\n\"); +* fflush(stdout); +* ++_s;\n"; +* fprintf cfile " get_inputs(ctx"; +* List.iter (fun (x:variable) -> fprintf cfile ", &%s" x.name) main.var_in; +* fprintf cfile ");\n"; +* +* List.iter (fprintf cfile " SEM_SIGNAL(channel%i);\n") main.ch_in; +* List.iter (fprintf cfile " SEM_WAIT(channel%i);\n") main.ch_out; +* +* fprintf cfile " print_outputs(%s" (List.hd main.var_out).name; +* List.iter (fun (x:variable) -> fprintf cfile ", %s" x.name) (List.tl main.var_out); +* fprintf cfile ");\n"; +* +* fprintf cfile " }\n"; +* fprintf cfile "\t\t}\n"; +* fprintf cfile " \n"; +* +* let print_instance_thread instance = +* fprintf cfile "\n#pragma omp single nowait\n{\n"; +* fprintf cfile "loop_%s%i();" instance.node.file_name instance.id; +* fprintf cfile "}\n"; +* in +* List.iter print_instance_thread !instances; +* +* fprintf cfile "\t}\n"; +* fprintf cfile "\n" +* fprintf cfile "}\n"; +*) ;; let _ = main () diff --git a/bin/lustre-mt/parser.ml b/bin/lustre-mt/parser.ml new file mode 100644 index 0000000000000000000000000000000000000000..53ef982a45f4552281d692db1ae1c1c86ebf1d6b --- /dev/null +++ b/bin/lustre-mt/parser.ml @@ -0,0 +1,137 @@ +open Types + +let tasks = ref [] +let name = ref "" + +(** saves the data from the Yaml parser into references of type Types.t *) + +(* converts a `Float list into a int list *) +let (intlist : Yaml.value list -> int list) = + fun l -> + let (yv_to_int : Yaml.value -> int) = fun y -> + match y with + |`Float f -> int_of_float f + |_ -> assert false + in List.map yv_to_int l +;; + + + +(** let (save_channels : Yaml.value -> unit) = fun x -> +* match x with +* |`Float f -> channels := !channels @ [int_of_float f] +* |_ -> assert false +* +* +* let (save_main : (string * Yaml.value) -> unit) = +* fun (key, value) -> +* match key, value with +* |"name", `String s -> main.name <- s +* |"ctx_type", `String s -> main.ctx_type <- s +* |"ctx_new", `String s -> main.ctx_new <- s +* |"var_in", `A l -> main.var_in <- List.map (List.nth !variables) (intlist l) +* |"var_out", `A l -> main.var_out <- List.map (List.nth !variables) (intlist l) +* |"ch_in", `A l -> main.ch_in <- intlist l +* |"ch_out", `A l -> main.ch_out <- intlist l +* |_ -> assert false +* +* +*let (save_node_attributes : Types.node -> (string * Yaml.value) -> unit) = +* fun n (key, value) -> +* match key, value with +* |"id", `Float _f -> () +* |"file_name", `String s -> n.file_name <- s +* |"fct_name", `String s -> n.fct_name <- s +* |"ctx", `Bool b -> n.ctx <- b +* |"ctx_tab", `String s -> n.ctx_tab <- s +* |_ -> assert false +* +*let (save_nodes : Yaml.value -> unit) = fun x -> +* let t = {name = ""; fct_name = ""; ctx = false; ctx_tab = ""}::[] in +* match x with +* |`O l -> +* List.iter (save_node_attributes (List.hd n)) l; +* tasks := !tasks @ t +* |_ -> assert false +* +* +*let (save_instance_attributes : Types.instance -> (string * Yaml.value) -> unit) = +* fun i (key, value) -> +* match key, value with +* |"id", `Float f -> i.id <- int_of_float f +* |"node", `Float f -> i.node <- List.nth !nodes (int_of_float f) +* |"var_in", `A l -> i.var_in <- List.map (List.nth !variables) (intlist l) +* |"var_out", `A l -> i.var_out <- List.map (List.nth !variables) (intlist l) +* |"ch_in", `A l -> i.ch_in <- intlist l +* |"ch_out", `A l -> i.ch_out <- intlist l +* |_ -> assert false +* +*let (save_instances : Yaml.value -> unit) = fun x -> +* let i = {id = 0; node = List.nth !nodes 0; var_in = []; var_out = []; ch_in = []; ch_out = []}::[] +* in +* match x with +* |`O l -> +* List.iter (save_instance_attributes (List.hd i)) l; +* instances := !instances @ i +* |_ -> assert false *) + +let (save_variable_attributes : Types.variable -> (string * Yaml.value) -> unit) = + fun v (key, value) -> + match key, value with + |"name", `String s -> v.name <- s + |"type", `String s -> v.var_type <- s + |_ -> assert false + +let (save_variables_in : Types.task -> Yaml.value -> unit) = fun task x -> + let v = {name = ""; var_type = ""}::[] + in + match x with + |`O l -> + List.iter (save_variable_attributes (List.hd v)) l; + task.var_in <- v @ task.var_in + |_ -> assert false + +let (save_variables_out : Types.task -> Yaml.value -> unit) = fun task x -> + let v = {name = ""; var_type = ""}::[] + in + match x with + |`O l -> + List.iter (save_variable_attributes (List.hd v)) l; + task.var_out <- v @ task.var_out + |_ -> assert false + + +let (save_data_one_task : Types.task -> (string * Yaml.value) -> unit) = + fun task (key, value) -> + match key, value with + |"name", `String n -> task.name <- n + |"var_in" , `A l -> List.iter (save_variables_in task) l + |"var_out" , `A l -> List.iter (save_variables_out task) l + |_ -> () + +let (save_data_task : Types.task -> (string * Yaml.value) -> unit) = + fun task (key, value) -> + match (key, value) with + | "task", `O l -> List.iter (save_data_one_task task) l + |_ -> () +(*faire en sorte de match avec "task", `O pour aller ensuite sur le save_data_task*) + +let (save_data_tasks : Yaml.value -> unit) = fun x -> + let task = {name = ""; var_in = []; var_out = []}::[] in + match x with + |`O l -> + List.iter (save_data_task (List.hd task)) l; + tasks := !tasks @ task + |_ -> assert false + +let (save_data : (string * Yaml.value) -> unit) = fun (key, value) -> + match (key, value) with + |"tasks", `A l -> List.iter save_data_tasks l; + |"main_node", `String n -> name := n + |_ -> assert false + +let (get_data : Yaml.value -> unit) = fun x -> + match x with + |`O l -> List.iter save_data l + |_ -> assert false +;; \ No newline at end of file diff --git a/bin/lustre-mt/types.ml b/bin/lustre-mt/types.ml index 3477eae685a788421788d657565917bc23b20348..6309f68bab05be485648be388cdc0e2e52f56fed 100644 --- a/bin/lustre-mt/types.ml +++ b/bin/lustre-mt/types.ml @@ -32,3 +32,9 @@ type instance = { mutable ch_in: int list; mutable ch_out: int list } + +type task = { + mutable name: string; + mutable var_in: variable list; + mutable var_out: variable list; +}