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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
type device_vec
type customarray
type ('a,'b) custom =
{
size : int ;
get: customarray -> int -> 'a;
set: customarray -> int -> 'a -> unit
}
type ('a,'b) couple = 'a*'b
type ('a, 'b) kind =
Float32 of ('a, 'b) Bigarray.kind
| Char of ('a, 'b) Bigarray.kind
| Float64 of ('a, 'b) Bigarray.kind
| Int32 of ('a, 'b) Bigarray.kind
| Int64 of ('a, 'b) Bigarray.kind
| Complex32 of ('a, 'b) Bigarray.kind
| Custom of ('a,'b) custom
| Unit of ('a, 'b) couple
| Dummy of ('a, 'b) couple
let int = Int32 (Bigarray.int)
let char = Char (Bigarray.char)
let int32 = Int32 (Bigarray.int32)
let int64 = Int64 (Bigarray.int64)
let float32 = Float32 (Bigarray.float32)
let float64 = Float64 (Bigarray.float64)
let complex32 = Complex32 (Bigarray.complex32)
type ('a,'b) ptr
type ('a,'b) get_f = ('a,'b) ptr -> int -> 'a
type ('a,'b) set_f = ('a,'b) ptr -> int -> 'a -> unit
type ('a,'b) host_vec =
{
ptr : ('a,'b) ptr;
get : ('a,'b) get_f;
set : ('a,'b) set_f;
}
type ('a,'b) spoc_vec =
| Bigarray of ('a, 'b, Bigarray.c_layout)Bigarray.Array1.t
| CustomArray of (customarray * ('a,'b) custom)
| Host_vec of ('a,'b) host_vec
external float32_of_float : float -> float = "float32_of_float"
external float_of_float32 : float -> float = "float_of_float32"
type vec_device =
| No_dev
| Dev of Devices.device
| Transferring of Devices.device
type ('a,'b) sub = int * int * int * int * ('a,'b) vector
and ('a,'b) vector = {
mutable device : int;
vector : ('a,'b) spoc_vec;
cuda_device_vec : device_vec array;
opencl_device_vec : device_vec array;
length : int;
mutable dev: vec_device;
kind: ('a, 'b)kind;
mutable is_sub: (('a,'b) sub) option;
sub : ('a, 'b) vector list;
vec_id: int;
mutable seek:int
}
external init_cuda_device_vec: unit -> device_vec = "spoc_init_cuda_device_vec"
external init_opencl_device_vec : unit -> device_vec = "spoc_init_opencl_device_vec"
external create_custom : ('a,'b) custom -> int -> customarray = "spoc_create_custom"
external host_alloc :
int -> int -> ('a, 'b)ptr = "host_alloc"
external get_float32 :
('a,'b) ptr -> int -> 'a = "get_float32"
external set_float32 :
('a,'b) ptr -> int -> 'a -> unit = "set_float32"
external get_float64 :
('a,'b) ptr -> int -> 'a = "get_float64"
external set_float64 :
('a,'b) ptr -> int -> 'a -> unit = "set_float64"
external get_int32 :
('a,'b) ptr -> int -> 'a = "get_int32"
external set_int32 :
('a,'b) ptr -> int -> 'a -> unit = "set_int32"
external get_int64 :
('a,'b) ptr -> int -> 'a = "get_int64"
external set_int64 :
('a,'b) ptr -> int -> 'a -> unit = "set_int64"
external get_char :
('a,'b) ptr -> int -> 'a = "get_char"
external set_char :
('a,'b) ptr -> int -> 'a -> unit = "set_char"
external get_complex32 :
('a,'b) ptr -> int -> 'a = "get_complex32"
external set_complex32 :
('a,'b) ptr -> int -> 'a -> unit = "set_complex32"
external cuda_custom_alloc_vect :
('a, 'b) vector -> int -> Devices.generalInfo -> unit =
"spoc_cuda_custom_alloc_vect"
external cuda_alloc_vect :
('a, 'b) vector -> int -> Devices.generalInfo -> unit =
"spoc_cuda_alloc_vect"
external opencl_alloc_vect :
('a, 'b) vector -> int -> Devices.generalInfo -> unit =
"spoc_opencl_alloc_vect"
external opencl_custom_alloc_vect :
('a, 'b) vector -> int -> Devices.generalInfo -> unit =
"spoc_opencl_alloc_vect"
let vec_id = ref 0
let emitVect _ _ = () ;;
#ifdef SPOC_PROFILE
external printVector : int -> int -> int -> int -> string -> bool -> int -> int -> int -> int -> int -> unit = "print_vector_bytecode" "print_vector_native"
let emitVect (vect : ('a, 'b) vector) length =
let isSub = match vect.is_sub with
| None -> "false"
| Some x -> "true" in
let dev = vect.device in
let id = vect.vec_id in
let kindS = match vect.kind with
| Char x -> "char"
| Float32 x -> "float32"
| Int32 x -> "int32"
| Float64 x -> "float64"
| Int64 x -> "int64"
| Complex32 x -> "complex32"
| _ -> "unknown" in
let size = match vect.kind with
| Char x -> 1
| Float32 x | Int32 x -> 4
| Float64 x | Int64 x | Complex32 x -> 8
| _ -> 0 in
if isSub = "true" then begin
let (depth, start, ok_range, ko_range, parent) = match vect.is_sub with
| None -> failwith "Subvector not found"
| Some e -> e in
let parentId = parent.vec_id in
printVector id dev length size kindS true depth start ok_range ko_range parentId;
end
else
printVector id dev length size kindS false 1 1 1 1 1;
#endif
external sizeofFloat32 : unit -> int = "sizeofFloat32"
external sizeofFloat64 : unit -> int = "sizeofFloat64"
external sizeofChar : unit -> int = "sizeofChar"
external sizeofInt32 : unit -> int = "sizeofInt32"
external sizeofInt64 : unit -> int = "sizeofInt64"
external sizeofComplex32 : unit -> int = "sizeofComplex32"
let printEvent _ = () ;;
#ifdef SPOC_PROFILE
external printEvent : string -> unit = "print_event"
#endif
let create (kind: ('a,'b) kind) ?dev size =
printEvent "VectorCreation";
incr vec_id;
let vec =
{
device = -1;
vector =
(match kind with
| Unit _ | Dummy _-> assert false
| Float32 _ ->
Host_vec
{
ptr = (host_alloc (sizeofFloat32 ()) size);
get = get_float32;
set = set_float32;
}
| Char _ ->
Host_vec {
ptr = (host_alloc (sizeofChar ()) size);
get = get_char;
set = set_char;
}
| Float64 _ ->
Host_vec {
ptr = (host_alloc (sizeofFloat64 ()) size);
get = get_float64;
set = set_float64;
}
| Int32 _ ->
Host_vec
{
ptr = (host_alloc (sizeofInt32 ()) size);
get = get_int32;
set = set_int32;
}
| Int64 _ -> Host_vec
{
ptr = (host_alloc (sizeofInt64 ()) size);
get = get_int64;
set = set_int64;
}
| Complex32 _ -> Host_vec
{
ptr = (host_alloc (sizeofComplex32 ()) size);
get = get_complex32;
set = set_complex32;
}
| Custom c ->
CustomArray ((create_custom c size), c)
);
cuda_device_vec = Array.make (Devices.cuda_devices() +1) (init_cuda_device_vec ());
opencl_device_vec = Array.make (Devices.opencl_devices() +1) (init_opencl_device_vec ());
length = size;
dev = No_dev;
kind = kind;
is_sub = None;
sub = [];
vec_id = !vec_id;
seek = 0; }
in
(match dev with
| None -> ()
| Some dev ->
let alloc_on_dev () =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match kind with
| Custom _ ->
cuda_custom_alloc_vect vec dev.Devices.general_info.Devices.id dev.Devices.general_info
| _ -> cuda_alloc_vect vec dev.Devices.general_info.Devices.id dev.Devices.general_info)
| Devices.OpenCLInfo _ ->
(match kind with
| Custom _ ->
opencl_custom_alloc_vect vec (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())) dev.Devices.general_info
| _ -> opencl_alloc_vect vec (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())) dev.Devices.general_info)
)
in
(try
alloc_on_dev ()
with
| _ -> Gc.full_major (); alloc_on_dev ());
vec.dev <- Dev dev;
);
emitVect vec size;
vec
let length v =
v.length
let dev v =
v.dev
let is_sub v =
v.is_sub
let kind v =
v.kind
let device v =
v.device
let get_vec_id v =
v.vec_id
let vector v =
v.vector
let set_device v device dev =
v.device <- device;
v.dev <- dev
let equals v1 v2 =
v1.vec_id = v2.vec_id
let vseek v s =
v.seek <- s
let get_seek v =
v.seek
let device_vec v framework id =
match framework with
| `Cuda -> v.cuda_device_vec.(id)
| `OpenCL -> v.opencl_device_vec.(id)
let update_device_array vect new_vect =
(for i = 0 to (Array.length new_vect.cuda_device_vec) - 2 do
new_vect.cuda_device_vec.(i) <- vect.cuda_device_vec.(i)
done;
for i = 0 to (Array.length new_vect.opencl_device_vec) - 2 do
new_vect.opencl_device_vec.(i) <-
vect.opencl_device_vec.(i)
done)
let unsafe_set vect idx value =
match vect.vector with
| Bigarray v -> Bigarray.Array1.set v idx value
| CustomArray c -> (snd c).set (fst c) idx value
| Host_vec v -> v.set v.ptr idx value
let unsafe_get vect idx =
match vect.vector with
| Bigarray v -> Bigarray.Array1.get v idx
| CustomArray c -> (snd c).get (fst c) idx
| Host_vec v -> v.get v.ptr idx
let temp_vector vect =
match vect.is_sub with
| Some _ ->
let new_v = create (vect.kind) (vect.length)
in
(new_v.device <- vect.device;
new_v.dev <- vect.dev;
update_device_array vect new_v;
new_v)
| None -> vect
let copy_sub vect1 vect2 =
vect2.is_sub <- vect1.is_sub
let dep = function | None -> 0 | Some (a, _, _, _, _) -> a
let sub_vector (vect : ('a, 'b) vector) _start _ok_r
_ko_r _len =
incr vec_id;
{
device = (-1);
vector = vect.vector;
cuda_device_vec =
Array.make ((Devices.cuda_devices ()) + 1) (init_cuda_device_vec ());
opencl_device_vec =
Array.make ((Devices.opencl_devices ()) + 1)
(init_opencl_device_vec ());
length = _len;
dev = No_dev;
kind = vect.kind;
is_sub =
Some (((dep vect.is_sub) + 1), _start, _ok_r, _ko_r, vect);
sub = [];
vec_id = !vec_id;
seek = 0;
}
external bigarray_adress : 'c -> int -> int -> ('a,'b) ptr = "spoc_bigarray_adress"
let of_bigarray_shr kind b =
incr vec_id;
let open Devices in
{
device = (-1);
vector =
Host_vec
(
match kind with
| Float32 _ ->
{
ptr = (bigarray_adress b (sizeofFloat32 ()) (Bigarray.Array1.dim b));
get = get_float32;
set = set_float32;
}
| Char _ ->
{
ptr = (bigarray_adress b (sizeofChar ()) (Bigarray.Array1.dim b));
get = get_char;
set = set_char;
}
| Float64 _ ->
{
ptr = (bigarray_adress b (sizeofFloat64 ()) (Bigarray.Array1.dim b));
get = get_float64;
set = set_float64;
}
| Int32 _ ->
{
ptr = (bigarray_adress b (sizeofInt32 ()) (Bigarray.Array1.dim b));
get = get_int32;
set = set_int32;
}
| Int64 _ ->
{
ptr = (bigarray_adress b (sizeofInt64 ()) (Bigarray.Array1.dim b));
get = get_int64;
set = set_int64;
}
| Complex32 _ ->
{
ptr = (bigarray_adress b (sizeofComplex32 ()) (Bigarray.Array1.dim b));
get = get_complex32;
set = set_complex32;
}
| Custom _ | Unit _ | Dummy _ -> assert false
);
cuda_device_vec = Array.make (cuda_devices() +1) (init_cuda_device_vec ());
opencl_device_vec = Array.make (opencl_devices() +1) (init_opencl_device_vec ());
length = Bigarray.Array1.dim b;
dev = No_dev;
kind = kind;
is_sub = None;
sub = [];
vec_id = !vec_id;
seek = 0 }
let to_bigarray_shr v =
match v.vector with
| Bigarray b -> b
| _ -> raise (Invalid_argument "v")