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 <c-object> [ cuDeviceGetCount cuda-error ] keep int deref ;
13 : n>cuda-device ( n -- device )
14 [ CUdevice <c-object> ] dip [ cuDeviceGet cuda-error ] 2keep
17 : enumerate-cuda-devices ( -- devices )
18 #cuda-devices iota [ n>cuda-device ] map ;
20 : with-each-cuda-device ( quot -- )
21 [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline
23 : cuda-device-properties ( n -- properties )
24 [ CUdevprop <struct> ] dip
25 [ cuDeviceGetProperties cuda-error ] 2keep drop ;
27 : cuda-devices ( -- assoc )
28 enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;
30 : cuda-device-name ( n -- string )
31 [ 256 [ <byte-array> ] keep ] dip
32 [ cuDeviceGetName cuda-error ]
33 [ 2drop utf8 alien>string ] 3bi ;
35 : cuda-device-capability ( n -- pair )
36 [ int <c-object> int <c-object> ] dip
37 [ cuDeviceComputeCapability cuda-error ]
38 [ drop [ int deref ] bi@ ] 3bi 2array ;
40 : cuda-device-memory ( n -- bytes )
41 [ uint <c-object> ] dip
42 [ cuDeviceTotalMem cuda-error ]
43 [ drop uint deref ] 2bi ;
45 : cuda-device-attribute ( attribute n -- n )
46 [ int <c-object> ] 2dip
47 [ cuDeviceGetAttribute cuda-error ]
48 [ 2drop int deref ] 3bi ;
50 : cuda-device. ( n -- )
52 [ "Device: " write number>string print ]
53 [ "Name: " write cuda-device-name print ]
54 [ "Memory: " write cuda-device-memory number>string print ]
57 cuda-device-capability [ number>string ] map " " join print
59 [ "Properties: " write cuda-device-properties . ]
61 "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
62 CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
63 cuda-device-attribute number>string print
69 "CUDA Version: " write cuda-version number>string print nl
70 #cuda-devices iota [ nl ] [ cuda-device. ] interleave ;
73 [ 1 - + ] keep /i ; inline
75 : context-device-properties ( -- props )
76 context-device cuda-device-properties ; inline
78 :: (distribute-jobs) ( job-count per-job-shared max-shared-size max-block-size
79 -- grid-size block-size per-block-shared )
80 per-job-shared [ max-block-size ] [ max-shared-size swap /i max-block-size min ] if-zero
81 job-count min :> job-max-block-size
82 job-count job-max-block-size up/i :> grid-size
83 job-count grid-size up/i :> block-size
84 block-size per-job-shared * :> per-block-shared
86 grid-size block-size per-block-shared ; inline
88 : distribute-jobs ( job-count per-job-shared -- launcher )
89 context-device-properties
90 [ sharedMemPerBlock>> ] [ maxThreadsPerBlock>> ] bi
91 (distribute-jobs) <grid-shared> ; inline