2010-04-13 05:30:31 -04:00
|
|
|
! Copyright (C) 2010 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-04-18 19:33:18 -04:00
|
|
|
USING: accessors alien alien.data alien.parser alien.strings
|
|
|
|
alien.syntax arrays assocs byte-arrays classes.struct
|
2010-04-19 01:46:03 -04:00
|
|
|
combinators continuations cuda.ffi cuda.memory cuda.utils
|
2010-04-23 15:27:19 -04:00
|
|
|
destructors fry init io io.backend io.encodings.string
|
2010-04-19 01:46:03 -04:00
|
|
|
io.encodings.utf8 kernel lexer locals macros math math.parser
|
|
|
|
namespaces nested-comments opengl.gl.extensions parser
|
2010-04-23 15:43:13 -04:00
|
|
|
prettyprint quotations sequences words cuda.libraries ;
|
2010-04-18 19:33:18 -04:00
|
|
|
QUALIFIED-WITH: alien.c-types a
|
2010-04-13 05:30:31 -04:00
|
|
|
IN: cuda
|
|
|
|
|
2010-04-14 22:09:16 -04:00
|
|
|
TUPLE: launcher
|
|
|
|
{ device integer initial: 0 }
|
2010-04-19 01:46:03 -04:00
|
|
|
{ device-flags initial: 0 } ;
|
2010-04-18 19:33:18 -04:00
|
|
|
|
2010-04-23 15:27:19 -04:00
|
|
|
: <launcher> ( device-id -- launcher )
|
|
|
|
launcher new
|
|
|
|
swap >>device ; inline
|
|
|
|
|
2010-04-18 19:33:18 -04:00
|
|
|
TUPLE: function-launcher
|
2010-04-19 01:46:03 -04:00
|
|
|
dim-block dim-grid shared-size stream ;
|
2010-04-14 22:09:16 -04:00
|
|
|
|
|
|
|
: with-cuda-context ( flags device quot -- )
|
2010-04-19 01:46:03 -04:00
|
|
|
H{ } clone cuda-modules set-global
|
2010-04-18 19:33:18 -04:00
|
|
|
H{ } clone cuda-functions set
|
2010-04-19 01:46:03 -04:00
|
|
|
[ create-context ] dip
|
2010-04-14 22:09:16 -04:00
|
|
|
[ '[ _ @ ] ]
|
2010-04-19 01:46:03 -04:00
|
|
|
[ drop '[ _ destroy-context ] ] 2bi
|
2010-04-14 22:09:16 -04:00
|
|
|
[ ] cleanup ; inline
|
|
|
|
|
2010-04-19 01:46:03 -04:00
|
|
|
: with-cuda-program ( flags device quot -- )
|
2010-04-14 22:09:16 -04:00
|
|
|
[ dup cuda-device set ] 2dip
|
2010-04-19 01:46:03 -04:00
|
|
|
'[ cuda-context set _ call ] with-cuda-context ; inline
|
2010-04-14 22:09:16 -04:00
|
|
|
|
|
|
|
: with-cuda ( launcher quot -- )
|
2010-04-24 00:17:47 -04:00
|
|
|
init-cuda
|
2010-04-19 01:46:03 -04:00
|
|
|
[ H{ } clone cuda-memory-hashtable ] 2dip '[
|
2010-04-14 22:09:16 -04:00
|
|
|
_
|
|
|
|
[ cuda-launcher set ]
|
2010-04-19 01:46:03 -04:00
|
|
|
[ [ device>> ] [ device-flags>> ] bi ] bi
|
2010-04-14 22:09:16 -04:00
|
|
|
_ with-cuda-program
|
|
|
|
] with-variable ; inline
|
2010-04-13 05:30:31 -04:00
|
|
|
|
2010-04-18 19:33:18 -04:00
|
|
|
: c-type>cuda-setter ( c-type -- n cuda-type )
|
|
|
|
{
|
|
|
|
{ [ dup a:int = ] [ drop 4 [ cuda-int* ] ] }
|
|
|
|
{ [ dup a:uint = ] [ drop 4 [ cuda-int* ] ] }
|
|
|
|
{ [ dup a:float = ] [ drop 4 [ cuda-float* ] ] }
|
|
|
|
{ [ dup a:pointer? ] [ drop 4 [ ptr>> cuda-int* ] ] }
|
|
|
|
{ [ dup a:void* = ] [ drop 4 [ ptr>> cuda-int* ] ] }
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: run-function-launcher ( function-launcher function -- )
|
|
|
|
swap
|
|
|
|
{
|
|
|
|
[ dim-block>> first3 function-block-shape* ]
|
|
|
|
[ shared-size>> function-shared-size* ]
|
|
|
|
[
|
|
|
|
dim-grid>> [
|
|
|
|
launch-function*
|
|
|
|
] [
|
|
|
|
first2 launch-function-grid*
|
|
|
|
] if-empty
|
|
|
|
]
|
|
|
|
} 2cleave ;
|
|
|
|
|
|
|
|
: cuda-argument-setter ( offset c-type -- offset' quot )
|
|
|
|
c-type>cuda-setter
|
|
|
|
[ over [ + ] dip ] dip
|
|
|
|
'[ swap _ swap _ call ] ;
|
|
|
|
|
|
|
|
MACRO: cuda-arguments ( c-types -- quot: ( args... function -- ) )
|
|
|
|
[ 0 ] dip [ cuda-argument-setter ] map reverse
|
|
|
|
swap '[ _ param-size* ] suffix
|
|
|
|
'[ _ cleave ] ;
|
2010-04-14 22:09:16 -04:00
|
|
|
|
2010-04-19 01:46:03 -04:00
|
|
|
: define-cuda-word ( word module-name function-name arguments -- )
|
2010-04-18 19:33:18 -04:00
|
|
|
[
|
|
|
|
'[
|
2010-04-19 01:46:03 -04:00
|
|
|
_ _ cached-function
|
2010-04-18 19:33:18 -04:00
|
|
|
[ nip _ cuda-arguments ]
|
|
|
|
[ run-function-launcher ] 2bi
|
|
|
|
]
|
|
|
|
]
|
2010-04-19 01:46:03 -04:00
|
|
|
[ 2nip \ function-launcher suffix a:void function-effect ]
|
|
|
|
3bi define-declared ;
|