Source file Mem.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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
open Cuda
open OpenCL
let auto = ref true
let unsafe = ref false
let auto_transfers b = auto := b
let unsafe_rw b = unsafe := b
let unsafe_set vect idx value =
Vector.unsafe_set vect idx value
let unsafe_get vect idx =
Vector.unsafe_get vect idx
let rec flush_and_transfer_to_cpu d vect =
Devices.flush d ();
to_cpu vect ();
Devices.flush d ()
and set vect (idx : int) (value : 'a) =
if !unsafe then unsafe_set vect idx value
else
(if (idx < 0) || (idx >= Vector.length vect)
then raise (Invalid_argument "index out of bounds");
if !auto
then
(match Vector.dev vect with
| Vector.Dev d | Vector.Transferring d ->
(flush_and_transfer_to_cpu d vect)
| Vector.No_dev -> ());
match Vector.is_sub vect with
| Some (1, start, _, 0, v2) -> unsafe_set v2 (start + idx) value
| Some (1, start, ok_r, ko_r, v2) ->
unsafe_set v2
((start + ((idx / ok_r) * (ko_r + ok_r))) + (idx mod ok_r)) value
| Some (_, start, ok_r, ko_r, v2) ->
set v2 ((start + ((idx / ok_r) * (ko_r + ok_r))) + (idx mod ok_r))
value
| None -> unsafe_set vect idx value)
and get vect idx =
if !unsafe then unsafe_get vect idx
else
(if (idx < 0) || (idx >= Vector.length vect)
then raise (Invalid_argument "index out of bounds");
if !auto
then
(match Vector.dev vect with
| Vector.Dev d | Vector.Transferring d ->
(flush_and_transfer_to_cpu d vect)
| Vector.No_dev -> ());
match Vector.is_sub vect with
| Some (1, start, _, 0, v2) -> unsafe_get v2 (start + idx)
| Some (1, start, ok_r, ko_r, v2) ->
unsafe_get v2
((start + ((idx / ok_r) * (ko_r + ok_r))) + (idx mod ok_r))
| Some (_, start, ok_r, ko_r, v2) ->
get v2 ((start + ((idx / ok_r) * (ko_r + ok_r))) + (idx mod ok_r))
| None -> unsafe_get vect idx)
and temp_vector vect =
Vector.temp_vector vect
and basic_transfer_to_device new_vect q dev =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
let f () =
(match Vector.vector new_vect with
| Vector.CustomArray _ ->
(cuda_custom_cpu_to_device new_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info q;)
| _ ->
(cuda_cpu_to_device new_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev.Devices.gc_info q;)
)
in
(try f ()
with
| Cuda.ERROR_OUT_OF_MEMORY ->
(try (Devices.flush dev (); f ())
with | _ -> (Gc.full_major (); f ())))
| Devices.OpenCLInfo _ ->
let f () =
(match Vector.vector new_vect with
| Vector.CustomArray _ ->
(opencl_custom_cpu_to_device new_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info q;)
| _ ->
(opencl_cpu_to_device new_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info q;)
)
in
(try f ()
with
| _ ->
(try (Devices.flush dev (); f ())
with | _ -> (Gc.full_major (); f ())))
);
and basic_transfer_to_cpu new_vect q dev =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
let f () =
(match Vector.vector new_vect with
| Vector.CustomArray _ ->
(cuda_custom_device_to_cpu new_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info q;)
| _ ->
(cuda_device_to_cpu new_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev q;)
)
in
(try f ()
with
| Cuda.ERROR_OUT_OF_MEMORY ->
(try (Devices.flush dev (); f ())
with | _ -> (Gc.full_major (); f ())))
| Devices.OpenCLInfo _ ->
let f () =
(match Vector.vector new_vect with
| Vector.CustomArray _ ->
(opencl_custom_device_to_cpu new_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.specific_info q;)
| _ ->
(opencl_device_to_cpu new_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.specific_info q;)
)
in
(try f ()
with
| _ ->
(try (Devices.flush dev (); f ())
with | _ -> (Gc.full_major (); f ())))
);
and transfer_part_to_device vect sub_vect q (dev : Devices.device) host_offset guest_offset start size =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match Vector.vector vect with
| Vector.CustomArray _ ->
(cuda_custom_part_cpu_to_device vect sub_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
| _ -> (cuda_part_cpu_to_device vect sub_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
)
| Devices.OpenCLInfo _ ->
(match Vector.vector vect with
| Vector.CustomArray _ -> (opencl_custom_part_cpu_to_device vect sub_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
| _ -> (opencl_part_cpu_to_device vect sub_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
)
)
and transfer_part_to_cpu vect sub_vect q (dev : Devices.device) host_offset guest_offset start size =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match Vector.vector vect with
| Vector.CustomArray _ ->
(cuda_custom_part_device_to_cpu vect sub_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
| _ -> (cuda_part_device_to_cpu vect sub_vect dev.Devices.general_info.Devices.id
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
)
| Devices.OpenCLInfo _ ->
(match Vector.vector vect with
| Vector.CustomArray _ -> (opencl_custom_part_device_to_cpu vect sub_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
| _ -> (opencl_part_device_to_cpu vect sub_vect
(dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))
dev.Devices.general_info dev.Devices.gc_info q host_offset guest_offset start size;)
)
)
and to_device vect ?queue_id:(q = 0) (dev : Devices.device) =
is a sub_vector then *)
let aux_transfer_to_device vect q dev =
match Vector.is_sub vect with
| None -> basic_transfer_to_device vect q dev
| Some (1, _start, _ok, _ko, v) ->
if _ok = 0 then
(
transfer_part_to_device v vect q dev 0 0 _start (Vector.length vect) ;
)
else
if _ok > 512 then
(
let to_transfer = ref (Vector.length vect) in
let cpt = ref 0 in
while !to_transfer > _ok do
transfer_part_to_device v vect q dev (!cpt * (_ok + _ko)) (!cpt*_ok)_start _ok ;
to_transfer := !to_transfer - _ok;
incr cpt;
done;
if !to_transfer > 0 then
transfer_part_to_device v vect q dev (!cpt * (_ok + _ko)) (!cpt*_ok) _start !to_transfer;
)
else
(
let to_transfer = ref (Vector.length vect) in
let cpt = ref 0 in
let i = ref 0 in
while !to_transfer > 512 do
let temp = Vector.create (Vector.kind vect) 512 in
Vector.copy_sub vect temp;
for idx = !i to (!i + 511) do
set temp idx (get vect !i);
done;
i := !i + 512;
transfer_part_to_device v temp q dev (!cpt * (512 + _ko)) (!cpt*512) _start 512 ;
to_transfer := !to_transfer - 512;
incr cpt;
done;
if !to_transfer > 0 then
(
let temp = Vector.create (Vector.kind vect) !to_transfer in
for idx = !i to (!i + !to_transfer - 1) do
set temp idx (get vect !i);
done;
Vector.copy_sub vect temp;
transfer_part_to_device v temp q dev (!cpt * (512 + _ko)) (!cpt*512) _start !to_transfer;
)
)
| Some _ ->
( let new_vect = temp_vector vect in
for i = 0 to Vector.length vect - 1 do
unsafe_set new_vect i (get vect i)
done;
basic_transfer_to_device new_vect q dev;
Vector.update_device_array new_vect vect;
)
in
match Vector.dev vect with
| Vector.Transferring d ->
( if d <> dev then
(Devices.flush d ();
to_device vect ~queue_id: q dev))
| Vector.Dev d ->
if d <> dev then
(to_cpu vect ~queue_id: q ();
Devices.flush d ();
to_device vect ~queue_id: q dev)
else
(Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.Transferring dev);
aux_transfer_to_device vect q dev;
Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.Dev dev)
)
| Vector.No_dev ->
(Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.Transferring dev);
(try alloc_vect_on_device vect dev with
| Cuda.ERROR_OUT_OF_MEMORY | OpenCL.MEM_OBJECT_ALLOCATION_FAILURE | _ ->
(try (Devices.flush dev ~queue_id:q (); alloc_vect_on_device vect dev)
with
| Cuda.ERROR_OUT_OF_MEMORY | OpenCL.MEM_OBJECT_ALLOCATION_FAILURE ->
( Devices.flush dev (); Gc.compact ();
alloc_vect_on_device vect dev)
| e -> raise e));
);
aux_transfer_to_device vect q dev;
Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.Dev dev)
and alloc_vect_on_device vector dev =
match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match Vector.vector vector with
| Vector.Bigarray _ | Vector.Host_vec _ -> cuda_alloc_vect vector dev.Devices.general_info.Devices.id dev.Devices.general_info
| Vector.CustomArray _ -> cuda_custom_alloc_vect vector dev.Devices.general_info.Devices.id dev.Devices.general_info)
| Devices.OpenCLInfo _ ->
(match Vector.vector vector with
| Vector.Bigarray _ | Vector.Host_vec _ -> opencl_alloc_vect vector (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())) dev.Devices.general_info
| Vector.CustomArray _ -> opencl_custom_alloc_vect vector (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())) dev.Devices.general_info)
and to_cpu vect ?queue_id:(q = 0) () =
match Vector.dev vect with
| Vector.Transferring d ->
(Devices.flush d ();
to_cpu vect ~queue_id:q ())
| Vector.No_dev -> ()
| Vector.Dev dev ->
Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.Transferring dev);
(match Vector.is_sub vect with
| None -> basic_transfer_to_cpu vect q dev
| Some (1, _start, _ok, _ko, v) ->
if _ok = 0 then
(
transfer_part_to_cpu v vect q dev 0 0 _start (Vector.length vect) ;
)
else
if _ok > 512 then
(
let to_transfer = ref (Vector.length vect) in
let cpt = ref 0 in
while !to_transfer > _ok do
transfer_part_to_cpu v vect q dev (!cpt * (_ok + _ko)) (!cpt*_ok)_start _ok ;
to_transfer := !to_transfer - _ok;
incr cpt;
done;
if !to_transfer > 0 then
transfer_part_to_cpu v vect q dev (!cpt * (_ok + _ko)) (!cpt*_ok)_start !to_transfer ;
)
else
(
let to_transfer = ref (Vector.length vect) in
let cpt = ref 0 in
let i = ref 0 in
while !to_transfer > 512 do
let temp = Vector.create (Vector.kind vect) 512 in
Vector.copy_sub vect temp;
for idx = !i to (!i + 511) do
set temp idx (get vect !i);
done;
transfer_part_to_cpu v temp q dev (!cpt * (512 + _ko)) (!cpt*512) _start 512 ;
to_transfer := !to_transfer - 512;
incr cpt;
done;
if !to_transfer > 0 then
(
let temp = Vector.create (Vector.kind vect) !to_transfer in
for idx = !i to (!i + !to_transfer - 1) do
set temp idx (get vect !i);
done;
Vector.copy_sub vect temp;
transfer_part_to_cpu v temp q dev (!cpt * (512 + _ko)) (!cpt*512) _start !to_transfer;
)
)
| Some _ ->
let new_vect = temp_vector vect
in
(
Vector.update_device_array new_vect vect;
basic_transfer_to_cpu new_vect q dev;
for i = 0 to (Vector.length new_vect) - 1 do
set vect i (unsafe_get new_vect i)
done
));
Vector.set_device vect (dev.Devices.general_info.Devices.id) (Vector.No_dev)
let sub_vector (vect : ('a, 'b) Vector.vector) _start ?ok_rng:(_ok_r = 0)
?ko_rng:(_ko_r = 0) _len =
if (_ok_r > _len) || ((_ok_r < 0) || (_ko_r < 0))
then failwith "Wrong value: sub_vector"
else
(if (_start + _len) > Vector.length vect
then raise (Invalid_argument "index out of bounds")
else
(match Vector.dev vect with
| Vector.Dev d | Vector.Transferring d ->
(Devices.flush d ~queue_id: 0 ();
to_cpu vect ~queue_id: 0 ();
Devices.flush d ~queue_id: 0 ())
| Vector.No_dev -> ());
incr Vector.vec_id;
Vector.sub_vector vect _start _ok_r _ko_r _len)
let gpu_vector_copy vecA startA vecB startB size dev =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match Vector.vector vecA with
| Vector.CustomArray _ -> (
Cuda.cuda_custom_vector_copy vecA startA vecB startB size dev.Devices.general_info dev.Devices.general_info.Devices.id)
| _ -> (
Cuda.cuda_vector_copy vecA startA vecB startB size dev.Devices.general_info dev.Devices.general_info.Devices.id))
| Devices.OpenCLInfo _ ->
(match Vector.vector vecA with
| Vector.CustomArray _ -> (OpenCL.opencl_custom_vector_copy vecA startA vecB startB size dev.Devices.general_info (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())))
| _ -> (OpenCL.opencl_vector_copy vecA startA vecB startB size dev.Devices.general_info (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))))
)
let cpu_vector_copy vecA startA vecB startB size =
let sB = ref startB in
for i = startA to startA + size - 1 do
set vecB !sB (get vecA i);
incr sB;
done
let rec vector_copy (vecA: ('a,'b) Vector.vector) startA (vecB: ('a,'b) Vector.vector) startB size =
if (startA + size) > Vector.length vecA || (startB + size) > Vector.length vecB then
raise (Invalid_argument "index out of bounds" )
else
match Vector.dev vecA, Vector.dev vecB with
| Vector.Dev d1, Vector.Dev d2 ->
if d1 = d2 then
gpu_vector_copy vecA startA vecB startB size d1
else
(Devices.flush d2 ();
to_device vecB d1;
Devices.flush d1 ();
gpu_vector_copy vecA startA vecB startB size d1 )
| Vector.Dev d1 , Vector.Transferring d2 ->
if d1 = d2 then
(Devices.flush d1 ();
gpu_vector_copy vecA startA vecB startB size d1)
else
(Devices.flush d2 ();
to_device vecB d1;
Devices.flush d1 ();
gpu_vector_copy vecA startA vecB startB size d1)
| Vector.Dev d1, Vector.No_dev ->
(to_device vecB d1;
Devices.flush d1 ();
gpu_vector_copy vecA startA vecB startB size d1)
| Vector.Transferring d1 , _ ->
Devices.flush d1 ();
vector_copy vecA startA vecB startB size;
| Vector.No_dev, Vector.No_dev ->
cpu_vector_copy vecA startA vecB startB size
| Vector.No_dev, Vector.Dev d2
| Vector.No_dev, Vector.Transferring d2 ->
to_cpu vecB ();
Devices.flush d2 ();
cpu_vector_copy vecA startA vecB startB size
let gpu_matrix_copy (vecA: ('a,'b) Vector.vector) ldA start_rowA start_colA (vecB: ('a,'b) Vector.vector) ldB start_rowB start_colB rows cols dev =
(match dev.Devices.specific_info with
| Devices.CudaInfo _ ->
(match Vector.vector vecA with
| Vector.CustomArray _ -> (
Cuda.cuda_custom_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols dev.Devices.general_info dev.Devices.general_info.Devices.id)
| _ -> (
Cuda.cuda_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols dev.Devices.general_info dev.Devices.general_info.Devices.id))
| Devices.OpenCLInfo _ ->
(match Vector.vector vecA with
| Vector.CustomArray _ -> (OpenCL.opencl_custom_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols dev.Devices.general_info (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ())))
| _ -> (OpenCL.opencl_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols dev.Devices.general_info (dev.Devices.general_info.Devices.id - (Devices.cuda_devices ()))))
)
let cpu_matrix_copy (_vecA: ('a,'b) Vector.vector) _ldA _start_rowA _start_colA (_vecB: ('a,'b) Vector.vector) _ldB _start_rowB _start_colB _rows _cols =
()
let rec matrix_copy (vecA: ('a,'b) Vector.vector) ldA start_rowA start_colA
(vecB: ('a,'b) Vector.vector) ldB start_rowB start_colB rows cols =
if (start_rowA*ldA+start_colA) + (rows*cols) - 1 > Vector.length vecA then
raise (Invalid_argument
(Printf.sprintf "index out of bounds %d > %d"
((start_rowA*ldA+start_colA) + rows*cols - 1) (Vector.length vecA)) )
else
if (start_rowB*ldB+start_colB) + (rows*cols) - 1 > Vector.length vecB then
raise (Invalid_argument
(Printf.sprintf "index out of bounds %d > %d"
((start_rowB*ldA+start_colB) + rows*cols - 1) (Vector.length vecB)) )
else
match Vector.dev vecA, Vector.dev vecB with
| Vector.Dev d1, Vector.Dev d2 ->
if d1 = d2 then
gpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols d1
else
(Devices.flush d2 ();
to_device vecB d1;
Devices.flush d1 ();
gpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols d1 )
| Vector.Dev d1 , Vector.Transferring d2 ->
if d1 = d2 then
(Devices.flush d1 ();
gpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols d1)
else
(Devices.flush d2 ();
to_device vecB d1;
Devices.flush d1 ();
gpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols d1)
| Vector.Dev d1, Vector.No_dev ->
(to_device vecB d1;
Devices.flush d1 ();
gpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols d1)
| Vector.Transferring d1 , _ ->
Devices.flush d1 ();
matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols;
| Vector.No_dev, Vector.No_dev ->
cpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols;
| Vector.No_dev, Vector.Dev d2
| Vector.No_dev, Vector.Transferring d2 ->
to_cpu vecB ();
Devices.flush d2 ();
cpu_matrix_copy vecA ldA start_rowA start_colA vecB ldB start_rowB start_colB rows cols