2010-04-23 15:43:13 -04:00
|
|
|
! Copyright (C) 2010 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-05-20 17:32:35 -04:00
|
|
|
USING: accessors alien.data alien.parser arrays
|
|
|
|
assocs combinators cuda cuda.ffi fry io.backend kernel macros
|
|
|
|
math namespaces sequences words ;
|
|
|
|
QUALIFIED-WITH: alien.c-types c
|
2010-04-23 15:43:13 -04:00
|
|
|
IN: cuda.libraries
|
|
|
|
|
2010-05-20 17:32:35 -04:00
|
|
|
SYMBOL: cuda-modules
|
|
|
|
SYMBOL: cuda-functions
|
|
|
|
|
2010-04-23 15:43:13 -04:00
|
|
|
SYMBOL: cuda-libraries
|
|
|
|
cuda-libraries [ H{ } clone ] initialize
|
|
|
|
|
|
|
|
SYMBOL: current-cuda-library
|
|
|
|
|
2010-05-20 17:32:35 -04:00
|
|
|
: ?delete-at ( key assoc -- old/key ? )
|
|
|
|
2dup delete-at* [ 2nip t ] [ 2drop f ] if ; inline
|
2010-04-23 15:43:13 -04:00
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: cuda-int ( function offset value -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuParamSeti cuda-error ; inline
|
2010-04-23 15:43:13 -04:00
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: cuda-float ( function offset value -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuParamSetf cuda-error ; inline
|
2010-04-23 15:43:13 -04:00
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: cuda-vector ( function offset ptr n -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuParamSetv cuda-error ; inline
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: param-size ( function n -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuParamSetSize cuda-error ; inline
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: launch-function-grid ( function width height -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuLaunchGrid cuda-error ; inline
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: function-block-shape ( function x y z -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuFuncSetBlockShape cuda-error ; inline
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: function-shared-size ( function n -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
cuFuncSetSharedSize cuda-error ; inline
|
|
|
|
|
2010-05-20 17:51:47 -04:00
|
|
|
TUPLE: grid
|
2010-05-20 17:32:35 -04:00
|
|
|
dim-grid dim-block shared-size stream ;
|
|
|
|
|
2010-05-20 17:51:47 -04:00
|
|
|
: <grid> ( dim-grid dim-block -- grid )
|
|
|
|
0 f grid boa ; inline
|
|
|
|
|
|
|
|
: <grid-shared> ( dim-grid dim-block shared-size -- grid )
|
|
|
|
f grid boa ; inline
|
|
|
|
|
|
|
|
: <grid-shared-stream> ( dim-grid dim-block shared-size stream -- grid )
|
|
|
|
grid boa ; inline
|
|
|
|
|
2010-05-20 17:32:35 -04:00
|
|
|
: c-type>cuda-setter ( c-type -- n cuda-type )
|
|
|
|
{
|
2010-05-20 18:57:23 -04:00
|
|
|
{ [ dup c:int = ] [ drop 4 [ cuda-int ] ] }
|
|
|
|
{ [ dup c:uint = ] [ drop 4 [ cuda-int ] ] }
|
|
|
|
{ [ dup c:float = ] [ drop 4 [ cuda-float ] ] }
|
|
|
|
{ [ dup c:pointer? ] [ drop 4 [ cuda-int ] ] }
|
|
|
|
{ [ dup c:void* = ] [ drop 4 [ cuda-int ] ] }
|
2010-05-20 17:32:35 -04:00
|
|
|
} cond ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
: block-dim ( block -- x y z )
|
|
|
|
dup sequence? [ 3 1 pad-tail first3 ] [ 1 1 ] if ; inline
|
|
|
|
: grid-dim ( block -- x y )
|
|
|
|
dup sequence? [ 2 1 pad-tail first2 ] [ 1 ] if ; inline
|
|
|
|
PRIVATE>
|
2010-04-23 15:43:13 -04:00
|
|
|
|
|
|
|
: load-module ( path -- module )
|
|
|
|
[ CUmodule <c-object> ] dip
|
2010-05-20 17:32:35 -04:00
|
|
|
[ cuModuleLoad cuda-error ] 2keep drop c:*void* ;
|
2010-04-23 15:43:13 -04:00
|
|
|
|
|
|
|
: unload-module ( module -- )
|
|
|
|
cuModuleUnload cuda-error ;
|
|
|
|
|
|
|
|
: load-cuda-library ( library -- handle )
|
|
|
|
path>> load-module ;
|
|
|
|
|
2010-05-20 17:32:35 -04:00
|
|
|
ERROR: no-cuda-library name ;
|
|
|
|
|
2010-04-23 15:43:13 -04:00
|
|
|
: lookup-cuda-library ( name -- cuda-library )
|
|
|
|
cuda-libraries get ?at [ no-cuda-library ] unless ;
|
|
|
|
|
|
|
|
: remove-cuda-library ( name -- library )
|
|
|
|
cuda-libraries get ?delete-at [ no-cuda-library ] unless ;
|
|
|
|
|
|
|
|
: unload-cuda-library ( name -- )
|
|
|
|
remove-cuda-library handle>> unload-module ;
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: launch-function ( function -- ) cuLaunch cuda-error ; inline
|
2010-05-20 17:32:35 -04:00
|
|
|
|
2010-05-20 17:51:47 -04:00
|
|
|
: run-grid ( grid function -- )
|
2010-05-20 17:32:35 -04:00
|
|
|
swap
|
|
|
|
{
|
2010-05-20 18:57:23 -04:00
|
|
|
[ dim-block>> block-dim function-block-shape ]
|
|
|
|
[ shared-size>> function-shared-size ]
|
2010-05-20 17:32:35 -04:00
|
|
|
[
|
|
|
|
dim-grid>>
|
2010-05-20 18:57:23 -04:00
|
|
|
[ grid-dim launch-function-grid ]
|
|
|
|
[ launch-function ] if*
|
2010-05-20 17:32:35 -04:00
|
|
|
]
|
|
|
|
} 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
|
2010-05-20 18:57:23 -04:00
|
|
|
swap '[ _ param-size ] suffix
|
2010-05-20 17:32:35 -04:00
|
|
|
'[ _ cleave ] ;
|
|
|
|
|
2010-05-20 18:57:23 -04:00
|
|
|
: get-function-ptr ( module string -- function )
|
2010-05-20 17:32:35 -04:00
|
|
|
[ CUfunction <c-object> ] 2dip
|
|
|
|
[ cuModuleGetFunction cuda-error ] 3keep 2drop c:*void* ;
|
|
|
|
|
2010-04-23 15:43:13 -04:00
|
|
|
: cached-module ( module-name -- alien )
|
|
|
|
lookup-cuda-library
|
|
|
|
cuda-modules get-global [ load-cuda-library ] cache ;
|
|
|
|
|
|
|
|
: cached-function ( module-name function-name -- alien )
|
|
|
|
[ cached-module ] dip
|
2010-05-20 18:57:23 -04:00
|
|
|
2array cuda-functions get [ first2 get-function-ptr ] cache ;
|
2010-05-20 17:32:35 -04:00
|
|
|
|
2010-05-20 18:45:35 -04:00
|
|
|
MACRO: cuda-invoke ( module-name function-name arguments -- )
|
|
|
|
'[
|
|
|
|
_ _ cached-function
|
|
|
|
[ nip _ cuda-arguments ]
|
|
|
|
[ run-grid ] 2bi
|
|
|
|
] ;
|
|
|
|
|
|
|
|
: cuda-global* ( module-name symbol-name -- device-ptr size )
|
|
|
|
[ CUdeviceptr <c-object> c:uint <c-object> ] 2dip
|
|
|
|
[ cached-module ] dip
|
|
|
|
'[ _ _ cuModuleGetGlobal cuda-error ] 2keep [ c:*uint ] bi@ ; inline
|
|
|
|
|
|
|
|
: cuda-global ( module-name symbol-name -- device-ptr )
|
|
|
|
cuda-global* drop ; inline
|
|
|
|
|
|
|
|
: define-cuda-function ( word module-name function-name arguments -- )
|
|
|
|
[ '[ _ _ _ cuda-invoke ] ]
|
2010-05-20 17:51:47 -04:00
|
|
|
[ 2nip \ grid suffix c:void function-effect ]
|
2010-05-20 17:32:35 -04:00
|
|
|
3bi define-declared ;
|
|
|
|
|
2010-05-20 18:45:35 -04:00
|
|
|
: define-cuda-global ( word module-name symbol-name -- )
|
|
|
|
'[ _ _ cuda-global ] (( -- device-ptr )) define-declared ;
|
|
|
|
|
2010-05-20 17:32:35 -04:00
|
|
|
TUPLE: cuda-library name path handle ;
|
|
|
|
|
|
|
|
: <cuda-library> ( name path -- obj )
|
|
|
|
\ cuda-library new
|
|
|
|
swap >>path
|
|
|
|
swap >>name ;
|
|
|
|
|
|
|
|
: add-cuda-library ( name path -- )
|
|
|
|
normalize-path <cuda-library>
|
|
|
|
dup name>> cuda-libraries get-global set-at ;
|
|
|
|
|