package sarek

  1. Overview
  2. Docs

Source file fastflow.ml

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
open Camlp4.PreCast
open Syntax
open Ast

open Sarek_types

let fastflow=false

let get_ff_type p =
  match p with
  | PaId (_loc, IdLid (_loc2, x) ) ->
    (let var = (Hashtbl.find !current_args x) in
     match var.var_type with
     | TInt32  ->
       <:str_item<let $lid:("task_"^x)$ = field task  $str:"task_"^x$ int>>
     | TInt64 ->
       <:str_item<let $lid:("task_"^x)$ = field task  $str:"task_"^x$ int>>
     | TVec k ->
       (match k with
        | TFloat32 ->
          <:str_item<let $lid:("task_"^x)$ = field task  $str:"task_"^x$ (ptr float)>>
        | TInt32 ->
          <:str_item<let $lid:("task_"^x)$ = field task  $str:"task_"^x$ (ptr int)>>

        | _ ->  failwith ("gfft : unimplemented yet " ^ ktyp_to_string k)
       )
     | _  -> failwith "error get_ff_type")
  | _ -> assert false

let f p =
  match p with
  | PaId (_loc, IdLid (_loc2, x) ) -> x
  | _ -> assert false


let rec get_ff_type_str p =
  match p with
  | PaId (_loc, IdLid (_loc2, x) ) ->
    (let var = (Hashtbl.find !current_args x) in
     Printf.sprintf "let task_%s = field task \"task_%s\" %s" x x
       (
         match var.var_type with
         | TFloat32 -> "float"
         | TInt32  ->
           "int"
         | TInt64 ->
           "int"
         | TVec k ->
           (match k with
            | TFloat32 ->
              "(ptr float)"
            | TInt32 -> "(ptr int)"
            | _ ->  failwith "gfft : unimplemented yet"
           )
         | _  -> failwith ("error get_ff_type_str -> "^ (ktyp_to_string var.var_type))))
  | PaTyc (_, x, _ ) -> get_ff_type_str x
  | _ -> assert false


let rec string_of_patt = function
  | PaId(_,x) ->string_of_ident x
  | PaTyc(_,x,_) -> string_of_patt x
  | _ -> assert false


let print_task args nameid _loc =
  let moduleName = (String.capitalize_ascii (string_of_ident nameid)) in
  let res = <:expr<
                object (self)
                method offloadTask = $lid:moduleName$.offloadTask
                method noMoreTasks = $lid:moduleName$.accNomoretasks
                method getResult = $lid:moduleName$.getResult
                end
  >> in
  Printf.printf "open Ctypes\n";
  Printf.printf "\tmodule %s = struct\n" moduleName;

  Printf.printf "\ttype task\n\t
  let task : task structure typ = structure \"TASK\";;
  %s
  let () = seal task\n"
    (List.fold_left
       (fun a b -> Printf.sprintf "%s;;\n\t%s" a b)
       (get_ff_type_str (List.hd args))
       ((List.map get_ff_type_str (List.tl args))));

  let param_as_tuple_str = (List.fold_left
                              (fun a b -> Printf.sprintf "%s, %s" a b)
                              (string_of_patt (List.hd args))
                              (List.tl (List.map string_of_patt args)))
  in
  Printf.printf "\tlet create_task (%s) =\n"
    param_as_tuple_str;

  Printf.printf "\t\tlet t = allocate_n task 1 in\n%s\t\tt"
    (List.fold_left
       (fun a b -> Printf.sprintf "%s\t\tCtypes.setf !@t task_%s %s;\n" a (string_of_patt b) (string_of_patt b))
       ""
       (args));

  (* TODO : replace fun with functor *)
  Printf.printf"
  let accGetResult () =   let fflib = FastFlow.fflib () in
    Foreign.foreign  ~check_errno:true ~release_runtime_lock:true ~from:fflib \"loadresacc\" (ptr void @-> (ptr (ptr void)) @-> returning void)
  let accOffload () =   let fflib = FastFlow.fflib () in
    Foreign.foreign ~check_errno:true ~release_runtime_lock:true ~from:fflib \"offloadacc\" (ptr void @-> (ptr task) @-> returning void)
  let accNomoretasks () =   let fflib = FastFlow.fflib () in
    Foreign.foreign  ~from:fflib \"nomoretasks\" (ptr void @-> returning void)";

  let rec adapt_string_of_patt = function
    | PaId (_loc, IdLid (_loc2, x) ) ->
      (let var = (Hashtbl.find !current_args x) in
       (
         match var.var_type with
         | TInt32
         | TInt64
         | TFloat32
         | TFloat64  -> x
         | TVec k ->
           let rec t  = function
             | TInt32 -> "(int, Bigarray.int32_elt)"
             | TInt64 -> "(int, Bigarray.int64_elt)"
             | TFloat32 -> "(float, Bigarray.float32_elt)"
             | TFloat64  -> "(float, Bigarray.float64_elt)"
             | TVec k ->
               ((t k)^" Spoc.Vector.vector")
             | _ -> assert false
           in
           Printf.sprintf "(Ctypes.bigarray_start array1
                           (Spoc.Vector.to_bigarray_shr (%s : %s)))"
             x (t var.var_type)
         | _  -> failwith "error get_ff_type_str"))
    | PaTyc (_,x,_ ) -> adapt_string_of_patt x
    | _ -> assert false
  in
  let adapted_params_as_tuple =
    (List.fold_left (fun a b -> Printf.sprintf "%s, %s" a b)
       (adapt_string_of_patt (List.hd args))
       (List.tl (List.map adapt_string_of_patt args)))
  in
  Printf.printf "
  let offloadTask acc (%s) =
    let t = create_task (%s) in
    (accOffload ()) acc t"
    param_as_tuple_str
    adapted_params_as_tuple;

  Printf.printf "
  let getResult acc (%s) =
  let t = create_task (%s) in
  let t_ptr = allocate (ptr void) (to_voidp t) in
  (accGetResult ()) acc t_ptr
" param_as_tuple_str adapted_params_as_tuple;
  Printf.printf "
end\n\n";
  res
OCaml

Innovation. Community. Security.