-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocm-rocblas.scm
504 lines (450 loc) · 15.7 KB
/
rocm-rocblas.scm
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
(define-module (guile-gpu rocm-rocblas)
#:use-module (ice-9 match)
#:use-module (rnrs bytevectors)
#:use-module (system foreign)
#:use-module (guile-gpu gpu)
#:use-module (guile-gpu common))
(eval-when (compile)
; since gpu.scm is GPU/CPU agnostic, we
; will replace parts of its internal API
; with GPU-using definitions using this 're-def' macro
; define will re-set or introduce a symbol in your current 'package'
; whereas set! will use the symbol in your current package
(define-syntax re-def
(syntax-rules ()
((_ (name . args) . body)
(set! name (lambda args . body)))))
(define-syntax re-def*
(syntax-rules ()
((_ (name . args) . body)
(set! name (lambda* args . body))))))
(define *rocm-dynlibfile*
(dynamic-link (let ((lpath (getenv "GUILE_FFI_ROCM_LIBPATH"))
(lname (or (getenv "GUILE_FFI_ROCM_LIBNAME") "librocm")))
(if (and lpath (not (string=? lpath "")))
(string-append lpath file-name-separator-string lname)
lname))))
(define *gpu-dynlibfile*
(dynamic-link (let ((lpath (getenv "GUILE_FFI_NNGPU_LIBPATH"))
(lname (or (getenv "GUILE_FFI_NNGPU_LIBNAME") "libnn_gpu")))
(if (and lpath (not (string=? lpath "")))
(string-append lpath file-name-separator-string lname)
lname))))
(define (pointer-to-first A)
(bytevector->pointer A ;(shared-array-root A)
;(* (shared-array-offset A) (srfi4-type-size (array-type A)))
))
(define (scalar->arg a)
(bytevector->pointer (shared-array-root (make-typed-array 'f32 a)))
;(f32vector-set! (gpu-array (%rocblas-v1)) 0 a)
;(bytevector->pointer (gpu-array (%rocblas-v1)))
)
;##################################################################
;;;; HIP
(define %hip-stream #f)
(begin
(define _c_hip-malloc
(pointer->procedure
int (dynamic-func "hipMalloc" *rocm-dynlibfile*)
(list '* ; pointer to store address to allocated device memory
int ; number of floats to allocate
)))
(define (hip-malloc numfloats)
(let* ((bv (make-bytevector 8))
(ptr (bytevector->pointer bv)))
(if (not (= (_c_hip-malloc ptr (* numfloats 4)) 0))
(error "FAIL:hipMalloc"))
(make-pointer (bytevector-u64-ref bv 0 (endianness little))))))
(begin
(define _c_hip-free
(pointer->procedure
int (dynamic-func "hipFree" *rocm-dynlibfile*)
(list '*))) ; pointer to allocated device memory
(define (hip-free dx)
(_c_hip-free dx)))
(begin
(define _c_hip-memcpy-to-device
(pointer->procedure
int (dynamic-func "hipMemcpy" *rocm-dynlibfile*)
(list '* ; pointer to device memory
'* ; pointer to host memory
int ; number of bytes to move
int))) ; host/device direction to move bytes in
(define (hip-memcpy-to-device dst src len)
(_c_hip-memcpy-to-device
dst (bytevector->pointer (array-contents src)) (* len 4) 1))) ; 1 = hipMemcpyHostToDevice
(begin
(define _c_hip-memcpy-from-device
(pointer->procedure
int (dynamic-func "hipMemcpy" *rocm-dynlibfile*)
(list '* ; pointer to host memory
'* ; pointer to device memory
int ; number of bytes to move
int))) ; host/device direction to move bytes in
(define (hip-memcpy-from-device dst src len)
(_c_hip-memcpy-from-device
(bytevector->pointer (array-contents dst)) src (* len 4) 2))) ; 2 = hipMemcpyDeviceToHost
;;;; ROCBLAS
(re-def (gpu-host) #:gpu-rocblas)
;;; device/host vector/matrix mappings
(re-def (gpu-free-array rv)
(let* ((addr (gpu-addr rv)))
(if addr (hip-free addr))))
; load an array onto GPU/device
(re-def (gpu-load-array rv)
(let* ((bv (gpu-array rv))
(addr (gpu-addr rv))
(rows (gpu-rows rv))
(len (if (= (gpu-type rv) 0)
rows
(* (gpu-cols rv) rows))))
(if (not addr) ; no memory on device
(begin
(struct-set! rv 3 (hip-malloc len))
(set! addr (gpu-addr rv))))
(hip-memcpy-to-device addr bv len)
(gpu-dirty-set! rv 0)))
; save into the rocm object from array at GPU/device
(re-def (gpu-save-array rv)
(let* ((bv (gpu-array rv))
(addr (gpu-addr rv))
(rows (gpu-rows rv))
(len (if (= (gpu-type rv) 0)
rows
(* (gpu-cols rv) rows))))
(if (not addr) ; no memory on device, user-error
(error "expected allocated memory-array on device"))
(hip-memcpy-from-device bv addr len)
(gpu-dirty-set! rv 0)))
(re-def (gpu-refresh rv)
(match (gpu-dirty rv)
(0 #t) ; not-dirty
(1 (gpu-load-array rv))
(2 (gpu-save-array rv)))
(gpu-dirty-set! rv 0))
(re-def (gpu-refresh-host rv)
(if (= (gpu-dirty rv) 2)
(begin
(gpu-save-array rv)
(gpu-dirty-set! rv 0))))
(re-def (gpu-refresh-device rv)
(if (= (gpu-dirty rv) 1)
(begin
(gpu-load-array rv)
(gpu-dirty-set! rv 0))))
(define (gpu-maybe-alloc rv)
(if (not (gpu-addr rv))
(let* (;(bv (gpu-array rv))
;(addr (gpu-addr rv))
(rows (gpu-rows rv))
(len (if (= (gpu-type rv) 0)
rows
(* (gpu-cols rv) rows))))
(struct-set! rv 3 (hip-malloc len)))))
;;;; NN-GPU
(begin
(define _c_gpu_sigmoid
(pointer->procedure
int (dynamic-func "_Z11f32_sigmoidPfS_iPv" *gpu-dynlibfile*) ; c++ mangled f32_sigmoid
(list '* ; dst
'* ; src
int ; length
'*))) ; hip-stream
(define (_gpu-sigmoid src dst)
(let ((len (if (= (gpu-type src) 0)
(gpu-rows src)
(* (gpu-rows src) (gpu-cols src)))))
(_c_gpu_sigmoid ; hip-stream
(gpu-addr dst)
(gpu-addr src)
len
(%hip-stream)))))
(re-def (gpu-array-sigmoid src dst)
(gpu-maybe-alloc dst)
(gpu-refresh-device src)
(gpu-dirty-set! dst 2)
(assert (gpu-addr src))
(assert (gpu-addr dst))
(_gpu-sigmoid src dst))
;;;; rocblas API (and some HIP functions)
(define %rocblas-handle #f)
(define %rocblas-v1 #f)
(re-def (gpu-init)
(set! %rocblas-handle (make-parameter #f))
(let ((bv (make-bytevector 8)))
((pointer->procedure
int (dynamic-func "rocblas_create_handle" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
))
(bytevector->pointer bv))
(%rocblas-handle (make-pointer (bytevector-u64-ref bv 0 (endianness little))))
;--------------------
;rocblas_status rocblas_set_pointer_mode(rocblas_handle handle, rocblas_pointer_mode pointer_mode)
((pointer->procedure
int (dynamic-func "rocblas_set_pointer_mode" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int))
(%rocblas-handle)
0) ; set pointer-mode to host
; setup 1-element-array (for cache)
(set! %rocblas-v1 (make-parameter #f))
(set! %hip-stream (make-parameter #f))))
(define (quit-rocblas)
;rocblas_status rocblas_destroy_handle(rocblas_handle handle)
((pointer->procedure
int (dynamic-func "rocblas_destroy_handle" *rocm-dynlibfile*)
(list '*)) ; rocblas_handle handle
(%rocblas-handle)))
(re-def (gpu-init-thread threadno)
(%rocblas-v1 (gpu-make-vector 1)) ; single-element vector
; setup the hip-stream
(let ((bv (make-bytevector 8)))
((pointer->procedure
int (dynamic-func "hipStreamCreate" *rocm-dynlibfile*)
(list '*)) ; pointer to hip stream object
(bytevector->pointer bv))
(%hip-stream (make-pointer (bytevector-u64-ref bv 0 (endianness little)))))
; bind hip-stream to rocblas-handle
; rocblas_set_stream(rocblas_handle handle, hipStream_tstream_id)
((pointer->procedure
int (dynamic-func "rocblas_set_stream" *rocm-dynlibfile*)
(list '* ; rocblas-handle
'*)) ; hip-stream
(%rocblas-handle)
(%hip-stream)))
; get the single-element-vector value
(define (gpu-get-v1)
(let ((rv (%rocblas-v1)))
(gpu-refresh rv)
(f32vector-ref (gpu-array rv) 0)))
;;;; rocblas
(define (rocblas-result-assert res)
(if (not (= res 0))
(error "rocblas result fail")))
;-----------------------------------------------------------------
;
; Level-1 BLAS
;
;-----------------------------------------------------------------
; -----------------------------
; copy x y
;
; rocblas_status
; rocblas_scopy(rocblas_handle handle,
; rocblas_int n,
; const float *x, rocblas_int incx,
; float *y, rocblas_int incy)
(begin
(define _rocblas_scopy
(pointer->procedure
int (dynamic-func "rocblas_scopy" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; const float *x
int ; rocblas_int incx
'* ; float *y
int))) ; rocblas_int incy
(re-def (gpu-scopy! x y)
(gpu-refresh-device x)
(gpu-dirty-set! y 2)
(rocblas-result-assert
(_rocblas_scopy (%rocblas-handle)
(gpu-rows x)
(gpu-addr x) 1 ; 1=stride
(gpu-addr y) 1))))
; -----------------------------
; swap x y
;
; rocblas_status
; rocblas_sswap(rocblas_handle handle,
; rocblas_int n,
; float *x, rocblas_int incx,
; float *y, rocblas_int incy)
(begin
(define _rocblas_sswap
(pointer->procedure
int (dynamic-func "rocblas_sswap" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; const float *x
int ; rocblas_int incx
'* ; float *y
int))) ; rocblas_int incy
(re-def (gpu-sswap! x y)
(gpu-refresh-device x)
(gpu-refresh-device y)
(gpu-dirty-set! x 2)
(gpu-dirty-set! y 2)
(rocblas-result-assert
(_rocblas_sswap (%rocblas-handle)
(gpu-rows x)
(gpu-addr x) 1
(gpu-addr y) 1))))
; -----------------------------
; axpy
;
; rocblas_status
; rocblas_saxpy(rocblas_handle handle,
; rocblas_int n,
; const float *alpha,
; const float *x, rocblas_int incx,
; float *y, rocblas_int incy)
(begin
(define _rocblas_saxpy
(pointer->procedure
int (dynamic-func "rocblas_saxpy" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; const float *alpha
'* ; const float *x
int ; rocblas_int incx
'* ; float *y
int))) ; rocblas_int incy
(define (rocblas-saxpy! N a x y)
(rocblas-result-assert
(_rocblas_saxpy (%rocblas-handle)
N
(scalar->arg a)
x 1
y 1))))
(re-def* (gpu-saxpy! alpha x y #:optional rox roy)
(gpu-refresh-device x)
(gpu-refresh-device y)
(gpu-dirty-set! y 2)
(cond
((or rox roy) ; row-offset
(let* ((x-cols (if rox (gpu-cols x) #f))
(y-cols (if roy (gpu-cols y) #f))
(xaddr (if rox (make-pointer (+ (pointer-address (gpu-addr x)) (* rox x-cols 4))) #f))
(yaddr (if roy (make-pointer (+ (pointer-address (gpu-addr y)) (* roy y-cols 4))) #f)))
(rocblas-saxpy! (or x-cols y-cols) alpha
(or xaddr (gpu-addr x))
(or yaddr (gpu-addr y)))))
(else
(rocblas-saxpy! (gpu-rows x) alpha (gpu-addr x) (gpu-addr y)))))
; -----------------------------
; scal
;
; rocblas_status
; rocblas_sscal (rocblas_handle handle,
; rocblas_int n,
; const float *alpha,
; float *x, rocblas_int incx)
(begin
(define _rocblas_sscal
(pointer->procedure
int (dynamic-func "rocblas_sscal" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; const float *alpha
'* ; float *x
int))) ; rocblas_int incx
(re-def (gpu-sscal! a x)
(gpu-refresh-device x)
(gpu-dirty-set! x 2)
(rocblas-result-assert
(_rocblas_sscal (%rocblas-handle)
(if (= (gpu-type x) 0)
(gpu-rows x)
(* (gpu-rows x) (gpu-cols x)))
(scalar->arg a)
(gpu-addr x) 1))))
(begin
(define _rocblas_sdot
(pointer->procedure
int (dynamic-func "rocblas_sdot" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; float *x
int ; rocblas_int incx
'* ; float *y
int ; rocblas_int incy
'*))) ; float *result
(re-def (gpu-sdot! x y r)
(gpu-refresh-device x)
(gpu-refresh-device y)
(gpu-refresh-device r)
(gpu-dirty-set! r 2)
(rocblas-result-assert
(_rocblas_sdot (%rocblas-handle)
(gpu-rows x)
(gpu-addr x) 1
(gpu-addr y) 1
(gpu-addr r)))))
; -----------------------------
; isamax
;
; rocblas_status
; rocblas_isamax(rocblas_handle handle,
; rocblas_int n,
; const float *x, rocblas_int incx,
; rocblas_int *result)
(begin
(define _rocblas_isamax
(pointer->procedure
int (dynamic-func "rocblas_isamax" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_int n
'* ; float *x
int ; rocblas_int incx
'*))) ; rocblas_int *result
(re-def (gpu-isamax x)
(gpu-refresh-device x)
(gpu-refresh-device (%rocblas-v1))
(rocblas-result-assert
(_rocblas_isamax (%rocblas-handle)
(gpu-rows x)
(gpu-addr x) 1
(gpu-addr (%rocblas-v1))))
(gpu-get-v1)))
;-----------------------------------------------------------------
;
; Level-2 BLAS
;
;-----------------------------------------------------------------
; -----------------------------
; sgemv alpha*A*x + beta*y -> y
; rocblas_status
; rocblas_sgemv(rocblas_handle handle,
; rocblas_operation trans,
; rocblas_int m, rocblas_int n,
; const float *alpha,
; const float *A, rocblas_int lda,
; const float *x, rocblas_int incx,
; const float *beta,
; float *y, rocblas_int incy)
(begin
(define _rocblas_sgemv
(pointer->procedure
int (dynamic-func "rocblas_sgemv" *rocm-dynlibfile*)
(list '* ; rocblas_handle handle
int ; rocblas_operation trans,
int int ; rocblas_int m, rocblas_int n,
'* ; const float *alpha,
'* int ; const float *A, rocblas_int lda,
'* int ; const float *x, rocblas_int incx,
'* ; const float *beta,
'* int))) ; float *y, rocblas_int incy
(define (rocblas-sgemv! alpha M N A TransA x beta y)
(rocblas-result-assert
(_rocblas_sgemv (%rocblas-handle)
(if TransA 111 112) ; convert to row-major+transpose
N M
(scalar->arg alpha)
A N
x 1
(scalar->arg beta)
y 1))))
(re-def (gpu-sgemv! alpha A transA x beta y)
(let ((M (gpu-rows A))
(N (gpu-cols A)))
(unless (= M (gpu-rows y)) (throw 'mismatched-Ay N (gpu-rows y)))
(unless (= N (gpu-rows x)) (throw 'mismatched-Ax N (gpu-rows x)))
(gpu-refresh-device A)
(gpu-refresh-device x)
(gpu-refresh-device y)
(gpu-dirty-set! y 2)
(assert (gpu-addr A))
(assert (gpu-addr x))
(assert (gpu-addr y))
(rocblas-sgemv! alpha M N (gpu-addr A) transA (gpu-addr x) beta (gpu-addr y))))