]> gitweb.factorcode.org Git - factor.git/blob - extra/cuda/cuda.factor
Merge branch 'master' of factorcode.org:/git/factor
[factor.git] / extra / cuda / cuda.factor
1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.data alien.parser alien.strings
4 alien.syntax arrays assocs byte-arrays classes.struct
5 combinators continuations cuda.ffi cuda.memory cuda.utils
6 destructors fry init io io.backend io.encodings.string
7 io.encodings.utf8 kernel lexer locals macros math math.parser
8 namespaces opengl.gl.extensions parser prettyprint quotations
9 sequences words cuda.libraries ;
10 QUALIFIED-WITH: alien.c-types c
11 IN: cuda
12
13 TUPLE: launcher
14 { device integer initial: 0 }
15 { device-flags initial: 0 } ;
16
17 : <launcher> ( device-id -- launcher )
18     launcher new
19         swap >>device ; inline
20
21 TUPLE: function-launcher
22 dim-grid dim-block shared-size stream ;
23
24 : with-cuda-context ( flags device quot -- )
25     H{ } clone cuda-modules set-global
26     H{ } clone cuda-functions set
27     [ create-context ] dip 
28     [ '[ _ @ ] ]
29     [ drop '[ _ destroy-context ] ] 2bi
30     [ ] cleanup ; inline
31
32 : with-cuda-program ( flags device quot -- )
33     [ dup cuda-device set ] 2dip
34     '[ cuda-context set _ call ] with-cuda-context ; inline
35
36 : with-cuda ( launcher quot -- )
37     init-cuda [
38         [ cuda-launcher set ]
39         [ [ device>> ] [ device-flags>> ] bi ] bi
40     ] [ with-cuda-program ] bi* ; inline
41
42 : c-type>cuda-setter ( c-type -- n cuda-type )
43     {
44         { [ dup c:int = ] [ drop 4 [ cuda-int* ] ] }
45         { [ dup c:uint = ] [ drop 4 [ cuda-int* ] ] }
46         { [ dup c:float = ] [ drop 4 [ cuda-float* ] ] }
47         { [ dup c:pointer? ] [ drop 4 [ cuda-int* ] ] }
48         { [ dup c:void* = ] [ drop 4 [ cuda-int* ] ] }
49     } cond ;
50
51 <PRIVATE
52 : block-dim ( block -- x y z )
53     dup sequence? [ 3 1 pad-tail first3 ] [ 1 1 ] if ; inline
54 : grid-dim ( block -- x y )
55     dup sequence? [ 2 1 pad-tail first2 ] [ 1 ] if ; inline
56 PRIVATE>
57
58 : run-function-launcher ( function-launcher function -- )
59     swap
60     {
61         [ dim-block>> block-dim function-block-shape* ]
62         [ shared-size>> function-shared-size* ]
63         [
64             dim-grid>>
65             [ grid-dim launch-function-grid* ]
66             [ launch-function* ] if*
67         ]
68     } 2cleave ;
69
70 : cuda-argument-setter ( offset c-type -- offset' quot )
71     c-type>cuda-setter
72     [ over [ + ] dip ] dip
73     '[ swap _ swap _ call ] ;
74
75 MACRO: cuda-arguments ( c-types -- quot: ( args... function -- ) )
76     [ 0 ] dip [ cuda-argument-setter ] map reverse
77     swap '[ _ param-size* ] suffix
78     '[ _ cleave ] ;
79
80 : define-cuda-word ( word module-name function-name arguments -- )
81     [
82         '[
83             _ _ cached-function
84             [ nip _ cuda-arguments ]
85             [ run-function-launcher ] 2bi
86         ]
87     ]
88     [ 2nip \ function-launcher suffix c:void function-effect ]
89     3bi define-declared ;