]> gitweb.factorcode.org Git - factor.git/blob - extra/cuda/devices/devices.factor
Remove many uses of <int> and *int etc
[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 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 ;
8 IN: cuda.devices
9
10 : #cuda-devices ( -- n )
11     int <c-object> [ cuDeviceGetCount cuda-error ] keep int deref ;
12
13 : n>cuda-device ( n -- device )
14     [ CUdevice <c-object> ] dip [ cuDeviceGet cuda-error ] 2keep
15     drop int deref ;
16
17 : enumerate-cuda-devices ( -- devices )
18     #cuda-devices iota [ n>cuda-device ] map ;
19
20 : with-each-cuda-device ( quot -- )
21     [ enumerate-cuda-devices ] dip '[ 0 _ with-cuda-context ] each ; inline
22
23 : cuda-device-properties ( n -- properties )
24     [ CUdevprop <struct> ] dip
25     [ cuDeviceGetProperties cuda-error ] 2keep drop ;
26
27 : cuda-devices ( -- assoc )
28     enumerate-cuda-devices [ dup cuda-device-properties ] { } map>assoc ;
29
30 : cuda-device-name ( n -- string )
31     [ 256 [ <byte-array> ] keep ] dip
32     [ cuDeviceGetName cuda-error ]
33     [ 2drop utf8 alien>string ] 3bi ;
34
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 ;
39
40 : cuda-device-memory ( n -- bytes )
41     [ uint <c-object> ] dip
42     [ cuDeviceTotalMem cuda-error ]
43     [ drop uint deref ] 2bi ;
44
45 : cuda-device-attribute ( attribute n -- n )
46     [ int <c-object> ] 2dip
47     [ cuDeviceGetAttribute cuda-error ]
48     [ 2drop int deref ] 3bi ;
49
50 : cuda-device. ( n -- )
51     {
52         [ "Device: " write number>string print ]
53         [ "Name: " write cuda-device-name print ]
54         [ "Memory: " write cuda-device-memory number>string print ]
55         [
56             "Capability: " write
57             cuda-device-capability [ number>string ] map " " join print
58         ]
59         [ "Properties: " write cuda-device-properties . ]
60         [
61             "CU_DEVICE_ATTRIBUTE_GPU_OVERLAP: " write
62             CU_DEVICE_ATTRIBUTE_GPU_OVERLAP swap
63             cuda-device-attribute number>string print
64         ]
65     } cleave ;
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