package sarek

  1. Overview
  2. Docs

Source file Profile.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(******************************************************************************
 * Mathias Bourgoin, Université Pierre et Marie Curie (2013)
 *
 * Mathias.Bourgoin@gmail.com
 *
 * This software is a computer program whose purpose is to allow
 * GPU programming with the OCaml language.
 *
 * This software is governed by the CeCILL-B license under French law and
 * abiding by the rules of distribution of free software.  You can  use,
 * modify and/ or redistribute the software under the terms of the CeCILL-B
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info".
 *
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability.
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or
 * data to be ensured and,  more generally, to use and operate it in the
 * same conditions as regards security.
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL-B license and that you accept its terms.
 *******************************************************************************)
let target_name = "OpenCL"

let global_function = ""
let device_function = ""
let host_function = ""

let global_parameter = ""

let global_variable = ""
let local_variable = ""
let shared_variable = ""

let kern_start  = ""
let kern_end = ""


let parse_intrinsics (_cuda,opencl) =
  match opencl with
  | "(get_local_id (0))" -> "thread_idx_x"
  | "(get_local_id (1))" -> "thread_idx_y"
  | "(get_local_id (2))" -> "thread_idx_z"
  | "(get_group_id (0))" -> "block_idx_x"
  | "(get_group_id (1))" -> "block_idx_y"
  | "(get_group_id (2))" -> "block_idx_z"
  | "(get_local_size (0))" -> "block_dim_x"
  | "(get_local_size (1))" -> "block_dim_y"
  | "(get_local_size (2))" -> "block_dim_z"
  | "(get_num_groups (0))" -> "grid_dim_x"
  | "(get_num_groups (1))" -> "grid_dim_y"
  | "(get_num_groups (2))" -> "grid_dim_z"
  | "(get_global_id (0))" -> "global_thread_id"

  | "(float)" -> "float"

  | "return" -> "return"

  | a -> "(* could not translate back to Sarek intrinsic using OpenCL instead*)"^a


let space i =
  String.make i ' '

and  indent i =
  let rec aux acc = function
    | 0 -> acc
    | i -> aux (acc^"  ") (i-1)
  in aux "" i

let default_parser = false
let prof_counter = ref 5

let generated_functions = Hashtbl.create 10

let gmem_store ()  =
  "(** ### global_memory stores : "^(Int64.to_string (Spoc.Mem.get !Gen.profile_vect 0))^" **)\n"

let gmem_load () =
  "(** ### global_memory loads : "^(Int64.to_string (Spoc.Mem.get !Gen.profile_vect 1))^" **)\n"

let smem_store ()  =""
let smem_load  ()  =""

let prof_string ()=
  "(**  ### visits : "^(Int64.to_string (Spoc.Mem.get !Gen.profile_vect !prof_counter))^" **)\n"

let prof_time_string()=
  let cycles = Spoc.Mem.get !Gen.profile_vect !prof_counter
  in
  "(** ### total_#_cycles : "^(Int64.to_string cycles)^" **)\n"


let branches_string () =
  let nb_branches = Int64.to_string (Spoc.Mem.get !Gen.profile_vect 4)
  and nb_divergents = Int64.to_string (Spoc.Mem.get !Gen.profile_vect 5)
  in
  "(** ### Total branches : "^nb_branches^" **)\n"^
  "(** ### Divergent branches : "^nb_divergents^" **)\n"

let branch_analysis_string c =
  let numActive = Int64.to_string (Spoc.Mem.get !Gen.profile_vect (c+1))
  and numTaken = Int64.to_string (Spoc.Mem.get !Gen.profile_vect (c+2))
  and numNotTaken = Int64.to_string (Spoc.Mem.get !Gen.profile_vect (c+3))
  in
  "(** ### Active threads : "^numActive^" **)\n"^
  "(** ### Branch taken : "^numTaken^" **)\n" ^
  "(** ### Branch not taken : "^numNotTaken^" **)\n"

let memory_div_string () =
  let memDiv = Int64.to_string (Spoc.Mem.get !Gen.profile_vect 6) in
  "(** ### Global Memory divergence : "^memDiv^" **)\n"

let profile_head () =
  gmem_store ()^
  gmem_load ()^
  smem_store ()^
  smem_load ()^
  branches_string ()

let fun_counter = ref 0

let rec parse_fun i a _b n dev =
  let open Kirc_Ast in
  let aux _name a =
    let rec aux2 i a =
      match a with
      | Kern (args,body) ->
        (let pargs = aux2 i args  in
         let pbody =
           let s = prof_string() in
           incr prof_counter;
           s^

           match body with
           | Seq (_,_)  -> (aux2 (i) body)
           | _  ->  ((aux2 (i) body )^"\n")

         in
         (pargs ^ " ->\n"^
          indent (i+1) ^ pbody)^indent (i+1)^ "in")
      | Params k -> parse (i+1) k dev
      | a -> parse (i+1) a dev
    in
    aux2 i a

  in
  let name =
    try snd (Hashtbl.find generated_functions a) with
    | Not_found ->
      let gen_name =
        if n <> "" then
          n
        else ("spoc_fun"^string_of_int !fun_counter) in
      incr fun_counter;
      let fun_src = aux gen_name a
      in
      Hashtbl.add generated_functions a (fun_src,gen_name);
      "\n"^indent (i+1) ^"let "^gen_name^" = fun "^fun_src ^"\n"^indent (i+1)^gen_name
  in
  name


(*Profiling counters info
   pc[0] <- gmem_store
   pc[1] <- gmem_load
   pc[2] <- smem_store
   pc[3] <- smem_load
   pc[4] <- nb_branches
   pc[5] <- nb_divergent
*)



and  parse i a  dev =
  let open Kirc_Ast in
  let aux  = function
    | Kern (args, body) -> ("kern "^(parse i args dev)^" ->\n"^
                            profile_head ()^
                            (match dev.Spoc.Devices.specific_info with
                             | Spoc.Devices.CudaInfo _ ->
                               memory_div_string()
                             | _ -> "")
                            ^"\n"^(parse (i) body dev))
    | Local (_x,y) -> (*"let mutable " ^ (parse i x)^" in\n"^
                        (indent (i))^(parse i y)^
                          "\n"^(indent (i))*)
      parse i y dev
    | VecVar (_t, _i, s) ->
      global_parameter^s
    | Params k -> parse i k dev
    | Concat (a,b) ->
      (match b with
       | Empty -> parse i a dev
       | Concat (_c,_d) -> parse i a dev ^ " " ^parse i b dev
       | _ -> assert false )
    | Seq (a,b) ->
      let a = parse i a dev in
      let b = parse i b dev in
      a^" \n"^(indent i)^b
    | SetV(vecacc, value) -> parse i vecacc dev ^" <- " ^(parse i value dev)^";"
    | IntId(s,_) -> s
    | IntVecAcc (s,v) -> ((parse i s dev)^".[<"^(parse i v dev)^">]")
    | Plus  (a,b) ->
      let a = parse i a dev in
      let b = parse i b dev in
      ("("^ a^" + "^b^")")
    | Plusf  (a,b) ->
      let a = parse i a dev in
      let b = parse i b dev in
      ("("^ a^" +. "^b^")")
    | Min  (a,b) -> ("("^(parse i a dev)^" - "^(parse i b dev)^")")
    | Minf  (a,b) -> ("("^(parse i a dev)^" -. "^(parse i b dev)^")")
    | Mul  (a,b) -> ("("^(parse i a dev)^" * "^(parse i b dev)^")")
    | Mulf  (a,b) -> ("("^(parse i a dev)^" *. "^(parse i b dev)^")")
    | Div  (a,b) -> ("("^(parse i a dev)^" / "^(parse i b dev)^")")
    | Divf  (a,b) -> ("("^(parse i a dev)^" /. "^(parse i b dev)^")")
    | Int i -> (string_of_int i)
    | GInt i  ->  Int32.to_string  (i ())
    | Set (var,value) -> (indent i )^"let mutable "^(parse i var dev)^" = " ^(parse i value dev)^" in"
    | Acc (var, value) -> (parse i var dev)^" := " ^(parse i value dev)^";"
    | Float f -> (string_of_float f)^"f"
    | GFloat  a  -> (string_of_float (a ()))^"f"
    | Double f -> string_of_float f

    | Ife(a,b,c) ->
      (*Printf.printf "Ife %d\n%!" !prof_counter;*)
      let a = parse i a dev in
      let b_anal = branch_analysis_string !prof_counter in
      prof_counter := !prof_counter + 4;
      let b = parse (i+1) b dev in
      let c = parse (i+1) c dev in
      (* let iff = *)
      (* (indent (i+1))^prof_string()^ *)
      (*           (indent (i+1))^b^"\n"^(indent i)^"\n"^(indent i) *)
      (* in *)
      (* prof_counter := !prof_counter + 3; *)
      (* let elsee = "else \n"^ *)
      (* (\* (indent (i+1))^prof_string()^ *\) *)
      (* (\*             (indent (i+1))^c^";\n"^(indent i)^"\n"^(indent i) *\) *)
      (* (\* in *\) *)
      b_anal ^
      "if ("^a^") then \n"^ b ^"\n"^(indent i)^"else\n"^indent (i+1)^c
    | If (a,b) ->
      (*Printf.printf "If %d\n%!" !prof_counter;*)
      let a = parse i a dev in
      let b_anal = branch_analysis_string !prof_counter in
      prof_counter := !prof_counter + 4;
      let s =
        "if ("^a^")"^" then \n"^(indent (i+1))^
        (indent (i+1))^(parse (i+1) b dev)^";\n"^(indent i)^""^(indent i)
      in
      b_anal ^ s
    | Or (a,b) -> (parse i a dev)^" || "^(parse i b dev)
    | And (a,b) -> (parse i a dev)^" && "^(parse i b dev)
    | Not (a) -> "!"^(parse i a dev)
    | EqBool (a,b) -> (parse i a dev)^" = "^(parse i b dev)
    | LtBool (a,b) -> (parse i a dev)^" < "^(parse i b dev)
    | GtBool (a,b) -> (parse i a dev)^" > "^(parse i b dev)
    | LtEBool (a,b) -> (parse i a dev)^" <= "^(parse i b dev)
    | GtEBool (a,b) -> (parse i a dev)^" >= "^(parse i b dev)
    | DoLoop (a,b,c,d) ->
      let id = parse i a dev in
      let min = parse i b dev in
      let max = parse i c dev in
      let body = parse (i+1) d dev in
      "for (int "^id^" = "^min^" to  "^max^"do\n"^(indent (i+1))^body^"done;"

    | While (a,b) ->
      (*Printf.printf "While %d\n%!" !prof_counter;*)
      let cond = parse i a dev in
      let b_anal = branch_analysis_string !prof_counter in
      prof_counter := !prof_counter + 4;
      let body = indent (i+1)^parse (i+1) b dev in
      let s = b_anal^"while " ^ cond ^" do\n"^
              body^"\n"^
              indent i^"done;"
      in
      s
    | Unit -> "()"
    | GlobalFun (a,b,n) ->
      let s = (parse_fun i a b n dev) in

      s
    | Intrinsics gv -> parse_intrinsics gv
    | FloatVar (_,s) | IntVar (_,s) -> s
    | Decl (var) ->
      (parse i var dev)
    | App (a,b) ->
      let f = parse i a dev in
      let rec aux = function
        | t::[] -> parse i t dev
        | t::q -> (parse i t dev)^" "^(aux q)
        | [] -> assert false
      in
      (match a with
       | Intrinsics ("return","return") -> f^" "^(aux (Array.to_list b))^" "
       |  _ -> f^" "^(aux (Array.to_list b))^" ")
    | Return k ->
      let s  =
        parse i k dev
      in
      let ss =
        (match dev.Spoc.Devices.specific_info with
         |  Spoc.Devices.CudaInfo _ ->
           let s = indent (i) ^
                   prof_time_string()^ indent (i) in
           incr prof_counter;
           s
         | _ -> "")
      in

      ss^s
    | _ -> Kirc_Ast.print_ast a; ""
  in aux  a

let parse _i a dev =
  prof_counter := 6;
  Hashtbl.clear generated_functions;
  let header =
    "(* Profile Kernel *)"
  in
  let footer = ";;"
  in
  (header^"\n" ^ parse 0  a dev ^"\n"^footer^"\n")