1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.strings arrays
4 assocs byte-arrays classes.struct combinators cuda
5 cuda.contexts cuda.ffi cuda.libraries fry io io.encodings.utf8
6 kernel locals math math.order math.parser namespaces
7 prettyprint sequences ;
10 : #cuda-devices ( -- n )
11 { int } [ cuDeviceGetCount cuda-error ] with-out-parameters ;
13 : n>cuda-device ( n -- device )
14 [ { CUdevice } ] dip '[ _ cuDeviceGet cuda-error ] with-out-parameters ;
16 : enumerate-cuda-devices ( -- devices )
17 #cuda-devices iota [ n>cuda-device ] map ;
19 : with-each-cuda-device ( quot -- )
20 [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline
22 : cuda-device-properties ( n -- properties )
23 [ CUdevprop <struct> ] dip
24 [ cuDeviceGetProperties cuda-error ] 2keep drop ;
26 : cuda-devices ( -- assoc )
27 enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;
29 : cuda-device-name ( n -- string )
30 [ 256 [ <byte-array> ] keep ] dip
31 [ cuDeviceGetName cuda-error ]
32 [ 2drop utf8 alien>string ] 3bi ;
34 : cuda-device-capability ( n -- pair )
36 '[ _ cuDeviceComputeCapability cuda-error ] with-out-parameters
39 : cuda-device-memory ( n -- bytes )
41 '[ _ cuDeviceTotalMem cuda-error ] with-out-parameters ;
43 : cuda-device-attribute ( attribute n -- n )
45 '[ _ _ cuDeviceGetAttribute cuda-error ] with-out-parameters ;
47 : cuda-device. ( n -- )
49 [ "Device: " write number>string print ]
50 [ "Name: " write cuda-device-name print ]
51 [ "Memory: " write cuda-device-memory number>string print ]
54 cuda-device-capability [ number>string ] map " " join print
56 [ "Properties: " write cuda-device-properties . ]
58 "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
59 CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
60 cuda-device-attribute number>string print
66 "CUDA Version: " write cuda-version number>string print nl
67 #cuda-devices iota [ nl ] [ cuda-device. ] interleave ;
70 [ 1 - + ] keep /i ; inline
72 : context-device-properties ( -- props )
73 context-device cuda-device-properties ; inline
75 :: (distribute-jobs) ( job-count per-job-shared max-shared-size max-block-size
76 -- grid-size block-size per-block-shared )
77 per-job-shared [ max-block-size ] [ max-shared-size swap /i max-block-size min ] if-zero
78 job-count min :> job-max-block-size
79 job-count job-max-block-size up/i :> grid-size
80 job-count grid-size up/i :> block-size
81 block-size per-job-shared * :> per-block-shared
83 grid-size block-size per-block-shared ; inline
85 : distribute-jobs ( job-count per-job-shared -- launcher )
86 context-device-properties
87 [ sharedMemPerBlock>> ] [ maxThreadsPerBlock>> ] bi
88 (distribute-jobs) <grid-shared> ; inline