1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.data alien.strings arrays assocs
4 byte-arrays classes.struct combinators cuda cuda.ffi cuda.utils
5 fry io io.encodings.utf8 kernel math.parser prettyprint
9 : #cuda-devices ( -- n )
11 int <c-object> [ cuDeviceGetCount cuda-error ] keep *int ;
13 : n>cuda-device ( n -- device )
15 [ CUdevice <c-object> ] dip [ cuDeviceGet cuda-error ] 2keep drop *int ;
17 : enumerate-cuda-devices ( -- devices )
18 #cuda-devices iota [ n>cuda-device ] map ;
20 : with-each-cuda-device ( quot -- )
21 [ enumerate-cuda-devices ] dip '[ <launcher> _ with-cuda ] each ; inline
23 : cuda-device-properties ( n -- properties )
25 [ CUdevprop <c-object> ] dip
26 [ cuDeviceGetProperties cuda-error ] 2keep drop
27 CUdevprop memory>struct ;
29 : cuda-devices ( -- assoc )
30 enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;
32 : cuda-device-name ( n -- string )
34 [ 256 [ <byte-array> ] keep ] dip
35 [ cuDeviceGetName cuda-error ]
36 [ 2drop utf8 alien>string ] 3bi ;
38 : cuda-device-capability ( n -- pair )
40 [ int <c-object> int <c-object> ] dip
41 [ cuDeviceComputeCapability cuda-error ]
42 [ drop [ *int ] bi@ ] 3bi 2array ;
44 : cuda-device-memory ( n -- bytes )
46 [ uint <c-object> ] dip
47 [ cuDeviceTotalMem cuda-error ]
50 : cuda-device-attribute ( attribute n -- n )
52 [ int <c-object> ] 2dip
53 [ cuDeviceGetAttribute cuda-error ]
56 : cuda-device. ( n -- )
59 [ "Device: " write number>string print ]
60 [ "Name: " write cuda-device-name print ]
61 [ "Memory: " write cuda-device-memory number>string print ]
64 cuda-device-capability [ number>string ] map " " join print
66 [ "Properties: " write cuda-device-properties . ]
68 "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
69 CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
70 cuda-device-attribute number>string print
76 "CUDA Version: " write cuda-version number>string print nl
77 #cuda-devices iota [ nl ] [ cuda-device. ] interleave ;