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 nested-comments opengl.gl.extensions parser
9 prettyprint quotations sequences words cuda.libraries ;
10 QUALIFIED-WITH: alien.c-types a
14 { device integer initial: 0 }
15 { device-flags initial: 0 } ;
17 : <launcher> ( device-id -- launcher )
19 swap >>device ; inline
21 TUPLE: function-launcher
22 dim-block dim-grid shared-size stream ;
24 : with-cuda-context ( flags device quot -- )
25 H{ } clone cuda-modules set-global
26 H{ } clone cuda-functions set
27 [ create-context ] dip
29 [ drop '[ _ destroy-context ] ] 2bi
32 : with-cuda-program ( flags device quot -- )
33 [ dup cuda-device set ] 2dip
34 '[ cuda-context set _ call ] with-cuda-context ; inline
36 : with-cuda ( launcher quot -- )
39 [ [ device>> ] [ device-flags>> ] bi ] bi
40 ] [ with-cuda-program ] bi* ; inline
42 : c-type>cuda-setter ( c-type -- n cuda-type )
44 { [ dup a:int = ] [ drop 4 [ cuda-int* ] ] }
45 { [ dup a:uint = ] [ drop 4 [ cuda-int* ] ] }
46 { [ dup a:float = ] [ drop 4 [ cuda-float* ] ] }
47 { [ dup a:pointer? ] [ drop 4 [ cuda-int* ] ] }
48 { [ dup a:void* = ] [ drop 4 [ cuda-int* ] ] }
51 : run-function-launcher ( function-launcher function -- )
54 [ dim-block>> first3 function-block-shape* ]
55 [ shared-size>> function-shared-size* ]
60 first2 launch-function-grid*
65 : cuda-argument-setter ( offset c-type -- offset' quot )
67 [ over [ + ] dip ] dip
68 '[ swap _ swap _ call ] ;
70 MACRO: cuda-arguments ( c-types -- quot: ( args... function -- ) )
71 [ 0 ] dip [ cuda-argument-setter ] map reverse
72 swap '[ _ param-size* ] suffix
75 : define-cuda-word ( word module-name function-name arguments -- )
79 [ nip _ cuda-arguments ]
80 [ run-function-launcher ] 2bi
83 [ 2nip \ function-launcher suffix a:void function-effect ]