Source file owl_parallel.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# 1 "src/owl/working/owl_parallel.ml"
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2020 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)

(** Parallel & distributed computing: engine interface *)

(* Experimental module, do not use now *)

module type Mapre_Engine = sig
  val map : ('a -> 'b) -> string -> string

  val map_partition : ('a list -> 'b list) -> string -> string

  val union : string -> string -> string

  val reduce : ('a -> 'a -> 'a) -> string -> 'a option

  val collect : string -> 'a list

  val workers : unit -> string list

  val myself : unit -> string

  val load : string -> string

  val save : string -> string -> int
end

module type Ndarray = sig
  type arr

  type elt

  val shape : arr -> int array

  val empty : int array -> arr

  val create : int array -> elt -> arr

  val zeros : int array -> arr

  val ones : int array -> arr

  val uniform : ?a:float -> ?b:float -> int array -> arr

  val numel : arr -> int

  val get : arr -> int array -> elt

  val set : arr -> int array -> elt -> unit

  val fill : arr -> elt -> unit

  val concatenate : ?axis:int -> arr array -> arr

  val reshape : arr -> int array -> arr

  val map : (elt -> elt) -> arr -> arr

  val map2 : (elt -> elt -> elt) -> arr -> arr -> arr

  val sin : arr -> arr

  val cos : arr -> arr

  val add : arr -> arr -> arr

  val sub : arr -> arr -> arr

  val mul : arr -> arr -> arr

  val div : arr -> arr -> arr

  val sum' : arr -> elt
end

module Make_Distributed (M : Ndarray) (E : Mapre_Engine) = struct
  type distr_arr =
    { mutable id : string
    ; mutable shape : int array
    ; mutable c_start : int array
    ; mutable c_len : int array
    ; mutable workers : string array
    }

  let make_distr_arr id shape c_start c_len workers =
    { id; shape; c_start; c_len; workers }


  let shape x = x.shape

  let num_dims x = Array.length x.shape

  let numel x = Array.fold_left (fun a b -> a * b) 1 x.shape

  let divide_to_chunks shape n =
    let total_sz = Array.fold_left (fun a b -> a * b) 1 shape in
    let chunk_sz = total_sz / n in
    match chunk_sz = 0 with
    | true  -> [| 0, total_sz |]
    | false ->
      Array.init n (fun i ->
          let c_start = i * chunk_sz in
          let c_len =
            match i = n - 1 with
            | true  -> total_sz - c_start
            | false -> chunk_sz
          in
          c_start, c_len)


  (* make a distributed version of [create_fun d], the elements will be
     distributed among the working nodes.

     [create_fun] receives three parameters: shape, starting pos (1d), and
     length of the chunk (1d).
  *)
  let distributed_create_basic create_fun d =
    let workers = E.workers () in
    let chunks = divide_to_chunks d (List.length workers) in
    let c_start = Array.map fst chunks in
    let c_len = Array.map snd chunks in
    let id =
      E.map
        (fun _ ->
          let me = E.myself () in
          let pos = Owl_utils.list_search me workers in
          me, create_fun d c_start.(pos) c_len.(pos))
        ""
    in
    make_distr_arr id d c_start c_len (Array.of_list workers)


  let distributed_create create_fun d =
    let f _ _ len = create_fun [| len |] in
    distributed_create_basic f d


  (* init function [f] receives 1d index *)
  let init d f =
    let create_fun _ c_start c_len =
      let x = M.empty [| c_len |] in
      for i = 0 to c_len - 1 do
        let j = c_start + i in
        M.set x [| i |] (f j)
      done;
      x
    in
    distributed_create_basic create_fun d


  let create d a =
    let create_fun d = M.create d a in
    distributed_create create_fun d


  let zeros d =
    let create_fun d = M.zeros d in
    distributed_create create_fun d


  let ones d =
    let create_fun d = M.ones d in
    distributed_create create_fun d


  let sequential ?_a ?_step _d = None

  let uniform ?a ?b d =
    let create_fun d = M.uniform ?a ?b d in
    distributed_create create_fun d


  let gaussian _d = None

  (* given 1d index, calculate its owner *)
  let calc_index_owner i_1d c_start c_len =
    let i = ref 0 in
    try
      for j = 0 to Array.length c_start - 1 do
        i := j;
        let a = c_start.(j) in
        let b = c_start.(j) + c_len.(j) in
        if a <= i_1d && i_1d < b then failwith "found"
      done;
      !i
    with
    | _exn -> !i


  let get x i_nd =
    let stride = Owl_utils.calc_stride x.shape in
    let i_1d = Owl_utils.index_nd_1d i_nd stride in
    assert (numel x > i_1d);
    let pos = calc_index_owner i_1d x.c_start x.c_len in
    let owner_id = x.workers.(pos) in
    (* the offset in the owner's chunk *)
    let j_1d = i_1d - x.c_start.(pos) in
    let y_id =
      E.map_partition
        (fun l ->
          match E.myself () = owner_id with
          | true  ->
            let arr = List.nth l 0 |> snd in
            [ M.get arr [| j_1d |] ]
          | false -> [])
        x.id
    in
    let l = E.collect y_id |> List.filter (fun l -> List.length l > 0) in
    List.(nth (nth l 0) 0)


  let set x i_nd a =
    let stride = Owl_utils.calc_stride x.shape in
    let i_1d = Owl_utils.index_nd_1d i_nd stride in
    assert (numel x > i_1d);
    let pos = calc_index_owner i_1d x.c_start x.c_len in
    let owner_id = x.workers.(pos) in
    (* the offset in the owner's chunk *)
    let j_1d = i_1d - x.c_start.(pos) in
    let y_id =
      E.map_partition
        (fun l ->
          match E.myself () = owner_id with
          | true  ->
            let arr = List.nth l 0 |> snd in
            [ M.set arr [| j_1d |] a ]
          | false -> [])
        x.id
    in
    E.collect y_id |> ignore


  let map_chunk f x =
    let y_id = E.map (fun (node_id, arr) -> node_id, f arr) x.id in
    let shape = Array.copy x.shape in
    let c_start = Array.copy x.c_start in
    let c_len = Array.copy x.c_len in
    let workers = Array.copy x.workers in
    make_distr_arr y_id shape c_start c_len workers


  let map f x = map_chunk (M.map f) x

  let map2_chunk f x y =
    assert (x.shape = y.shape);
    let z_id = E.union x.id y.id in
    let z_id =
      E.map_partition
        (fun l ->
          let x_node_id, x_arr = List.nth l 0 in
          let _y_node_id, y_arr = List.nth l 1 in
          [ x_node_id, f x_arr y_arr ])
        z_id
    in
    let shape = Array.copy x.shape in
    let c_start = Array.copy x.c_start in
    let c_len = Array.copy x.c_len in
    let workers = Array.copy x.workers in
    make_distr_arr z_id shape c_start c_len workers


  let map2 f x y = map2_chunk (M.map2 f) x y

  let fold f x a =
    let y_id =
      E.map
        (fun (_node_id, arr) ->
          let b = ref M.(get arr [| 0 |]) in
          for i = 1 to M.numel arr - 1 do
            b := f !b M.(get arr [| i |])
          done;
          !b)
        x.id
    in
    E.collect y_id |> List.fold_left (fun b c -> f b (List.nth c 0)) a


  let fill _x a = map_chunk (fun y -> M.fill y a) |> ignore

  (* of_ndarray and to_ndarray convert between distributed ndarray and local
     ndarray. They are equivalent to [distribute] and [collect] in some other
     distributed data processing frameworks. *)

  let of_ndarray _x = None

  let to_ndarray x =
    let l =
      E.collect x.id |> List.map (fun l' -> List.nth l' 0 |> snd) |> Array.of_list
    in
    let y = M.concatenate ~axis:0 l in
    M.reshape y x.shape


  let sin x = map_chunk M.sin x

  let cos x = map_chunk M.cos x

  (* TODO: need to implement add_elt to support complex number *)
  let sum x =
    let y = map_chunk M.sum' x in
    let l = E.collect y.id |> List.map (fun l' -> List.nth l' 0 |> snd) in
    let a = ref (List.nth l 0) in
    for i = 1 to List.length l - 1 do
      a := !a +. List.nth l i
    done;
    !a


  let min _x = None

  let max _x = None

  let add x y = map2_chunk M.add x y

  let sub x y = map2_chunk M.sub x y

  let mul x y = map2_chunk M.mul x y

  let div x y = map2_chunk M.div x y

  (* TODO: distributed garbage collection is not implemented yet. *)
end

module Make_Shared (M : Ndarray) (E : Mapre_Engine) = struct end

module type Ndarray_Any = sig
  type 'a arr

  val shape : 'a arr -> int array

  val numel : 'a arr -> int

  val create : int array -> 'a -> 'a arr
end

module Make_Distributed_Any (M : Ndarray_Any) (E : Mapre_Engine) = struct
  type distr_arr =
    { mutable id : string
    ; mutable shape : int array
    ; mutable c_start : int array
    ; mutable c_len : int array
    ; mutable workers : string array
    }

  let make_distr_arr id shape c_start c_len workers =
    { id; shape; c_start; c_len; workers }


  let divide_to_chunks shape n =
    let total_sz = Array.fold_left (fun a b -> a * b) 1 shape in
    let chunk_sz = total_sz / n in
    match chunk_sz = 0 with
    | true  -> [| 0, total_sz |]
    | false ->
      Array.init n (fun i ->
          let c_start = i * chunk_sz in
          let c_len =
            match i = n - 1 with
            | true  -> total_sz - c_start
            | false -> chunk_sz
          in
          c_start, c_len)


  let distributed_create create_fun d =
    let workers = E.workers () in
    let chunks = divide_to_chunks d (List.length workers) in
    let c_start = Array.map fst chunks in
    let c_len = Array.map snd chunks in
    let id =
      E.map
        (fun _ ->
          let me = E.myself () in
          let pos = Owl_utils.list_search me workers in
          me, create_fun [| c_len.(pos) |])
        ""
    in
    make_distr_arr id d c_start c_len (Array.of_list workers)
end