]> gitweb.factorcode.org Git - factor.git/blob - extra/cuda/devices/devices.factor
factor: trim using lists
[factor.git] / extra / cuda / devices / devices.factor
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 combinators cuda cuda.contexts cuda.ffi
5 cuda.libraries io io.encodings.utf8 kernel math math.order
6 math.parser prettyprint sequences splitting ;
7 IN: cuda.devices
8
9 : #cuda-devices ( -- n )
10     { int } [ cuDeviceGetCount cuda-error ] with-out-parameters ;
11
12 : n>cuda-device ( n -- device )
13     [ { CUdevice } ] dip '[ _ cuDeviceGet cuda-error ] with-out-parameters ;
14
15 : enumerate-cuda-devices ( -- devices )
16     #cuda-devices <iota> [ n>cuda-device ] map ;
17
18 : with-each-cuda-device ( quot -- )
19     [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline
20
21 : cuda-device-properties ( n -- properties )
22     [ CUdevprop new ] dip
23     [ cuDeviceGetProperties cuda-error ] keepd ;
24
25 : cuda-devices ( -- assoc )
26     enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;
27
28 : cuda-device-name ( n -- string )
29     [ 256 [ <byte-array> ] keep ] dip
30     [ cuDeviceGetName cuda-error ]
31     [ 2drop utf8 alien>string ] 3bi ;
32
33 : cuda-device-capability ( n -- pair )
34     [ { int int } ] dip
35     '[ _ cuDeviceComputeCapability cuda-error ] with-out-parameters
36     2array ;
37
38 : cuda-device-memory ( n -- bytes )
39     [ { ulonglong } ] dip
40     '[ _ cuDeviceTotalMem_v2 cuda-error ] with-out-parameters ;
41
42 : cuda-device-attribute ( attribute n -- n )
43     [ { int } ] 2dip
44     '[ _ _ cuDeviceGetAttribute cuda-error ] with-out-parameters ;
45
46 : cuda-device. ( n -- )
47     {
48         [ "Device: " write number>string print ]
49         [ "Name: " write cuda-device-name print ]
50         [ "Memory: " write cuda-device-memory number>string print ]
51         [
52             "Capability: " write
53             cuda-device-capability [ number>string ] map join-words print
54         ]
55         [ "Properties: " write cuda-device-properties . ]
56         [
57             "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
58             CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
59             cuda-device-attribute number>string print
60         ]
61     } cleave ;
62
63 : cuda-devices. ( -- )
64     init-cuda
65     enumerate-cuda-devices [ cuda-device. ] each ;
66
67 : cuda. ( -- )
68     init-cuda
69     "CUDA Version: " write cuda-version number>string print nl
70     #cuda-devices <iota> [ nl ] [ cuda-device. ] interleave ;
71
72 : up/i ( x y -- z )
73     [ 1 - + ] keep /i ; inline
74
75 : context-device-properties ( -- props )
76     context-device cuda-device-properties ; inline
77
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
85
86     grid-size block-size per-block-shared ; inline
87
88 : distribute-jobs ( job-count per-job-shared -- launcher )
89     context-device-properties
90     [ sharedMemPerBlock>> ] [ maxThreadsPerBlock>> ] bi
91     (distribute-jobs) <grid-shared> ; inline