CUDA-FUNCTION: works, splitting up CUDA into more vocabs

db4
Doug Coleman 2010-04-18 18:33:18 -05:00
parent 0f2f54a195
commit 5c17e6ee98
11 changed files with 164 additions and 37 deletions

View File

@ -1,11 +1,13 @@
! Copyright (C) 2010 Doug Coleman. ! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien alien.c-types alien.data alien.parser USING: accessors alien alien.data alien.parser alien.strings
alien.strings arrays assocs byte-arrays classes.struct alien.syntax arrays assocs byte-arrays classes.struct
combinators continuations cuda.ffi destructors fry io combinators continuations cuda.ffi destructors fry io
io.backend io.encodings.string io.encodings.utf8 kernel lexer io.backend io.encodings.string io.encodings.utf8 kernel lexer
locals math math.parser namespaces opengl.gl.extensions locals macros math math.parser namespaces nested-comments
prettyprint quotations sequences ; opengl.gl.extensions parser prettyprint quotations sequences
words ;
QUALIFIED-WITH: alien.c-types a
IN: cuda IN: cuda
SYMBOL: cuda-device SYMBOL: cuda-device
@ -15,13 +17,32 @@ SYMBOL: cuda-function
SYMBOL: cuda-launcher SYMBOL: cuda-launcher
SYMBOL: cuda-memory-hashtable SYMBOL: cuda-memory-hashtable
SYMBOL: cuda-libraries
cuda-libraries [ H{ } clone ] initialize
SYMBOL: cuda-functions
TUPLE: cuda-library name path ;
: <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 set-at ;
: cuda-library ( name -- cuda-library )
cuda-libraries get at ;
ERROR: throw-cuda-error n ; ERROR: throw-cuda-error n ;
: cuda-error ( n -- ) : cuda-error ( n -- )
dup CUDA_SUCCESS = [ drop ] [ throw-cuda-error ] if ; dup CUDA_SUCCESS = [ drop ] [ throw-cuda-error ] if ;
: cuda-version ( -- n ) : cuda-version ( -- n )
int <c-object> [ cuDriverGetVersion cuda-error ] keep *int ; a:int <c-object> [ cuDriverGetVersion cuda-error ] keep a:*int ;
: init-cuda ( -- ) : init-cuda ( -- )
0 cuInit cuda-error ; 0 cuInit cuda-error ;
@ -29,12 +50,19 @@ ERROR: throw-cuda-error n ;
TUPLE: launcher TUPLE: launcher
{ device integer initial: 0 } { device integer initial: 0 }
{ device-flags initial: 0 } { device-flags initial: 0 }
path block-shape shared-size grid ; path ;
TUPLE: function-launcher
dim-block
dim-grid
shared-size
stream ;
: with-cuda-context ( flags device quot -- ) : with-cuda-context ( flags device quot -- )
H{ } clone cuda-functions set
[ [
[ CUcontext <c-object> ] 2dip [ CUcontext <c-object> ] 2dip
[ cuCtxCreate cuda-error ] 3keep 2drop *void* [ cuCtxCreate cuda-error ] 3keep 2drop a:*void*
] dip ] dip
[ '[ _ @ ] ] [ '[ _ @ ] ]
[ drop '[ _ cuCtxDestroy cuda-error ] ] 2bi [ drop '[ _ cuCtxDestroy cuda-error ] ] 2bi
@ -44,7 +72,7 @@ path block-shape shared-size grid ;
[ [
normalize-path normalize-path
[ CUmodule <c-object> ] dip [ CUmodule <c-object> ] dip
[ cuModuleLoad cuda-error ] 2keep drop *void* [ cuModuleLoad cuda-error ] 2keep drop a:*void*
] dip ] dip
[ '[ _ @ ] ] [ '[ _ @ ] ]
[ drop '[ _ cuModuleUnload cuda-error ] ] 2bi [ drop '[ _ cuModuleUnload cuda-error ] ] 2bi
@ -74,10 +102,10 @@ path block-shape shared-size grid ;
<PRIVATE <PRIVATE
: #cuda-devices ( -- n ) : #cuda-devices ( -- n )
int <c-object> [ cuDeviceGetCount cuda-error ] keep *int ; a:int <c-object> [ cuDeviceGetCount cuda-error ] keep a:*int ;
: n>cuda-device ( n -- device ) : n>cuda-device ( n -- device )
[ CUdevice <c-object> ] dip [ cuDeviceGet cuda-error ] 2keep drop *int ; [ CUdevice <c-object> ] dip [ cuDeviceGet cuda-error ] 2keep drop a:*int ;
: enumerate-cuda-devices ( -- devices ) : enumerate-cuda-devices ( -- devices )
#cuda-devices iota [ n>cuda-device ] map ; #cuda-devices iota [ n>cuda-device ] map ;
@ -98,27 +126,30 @@ PRIVATE>
[ 2drop utf8 alien>string ] 3bi ; [ 2drop utf8 alien>string ] 3bi ;
: cuda-device-capability ( n -- pair ) : cuda-device-capability ( n -- pair )
[ int <c-object> int <c-object> ] dip [ a:int <c-object> a:int <c-object> ] dip
[ cuDeviceComputeCapability cuda-error ] [ cuDeviceComputeCapability cuda-error ]
[ drop [ *int ] bi@ ] 3bi 2array ; [ drop [ a:*int ] bi@ ] 3bi 2array ;
: cuda-device-memory ( n -- bytes ) : cuda-device-memory ( n -- bytes )
[ uint <c-object> ] dip [ a:uint <c-object> ] dip
[ cuDeviceTotalMem cuda-error ] [ cuDeviceTotalMem cuda-error ]
[ drop *uint ] 2bi ; [ drop a:*uint ] 2bi ;
: get-cuda-function* ( module string -- function ) : get-function-ptr* ( module string -- function )
[ CUfunction <c-object> ] 2dip [ CUfunction <c-object> ] 2dip
[ cuModuleGetFunction cuda-error ] 3keep 2drop *void* ; [ cuModuleGetFunction cuda-error ] 3keep 2drop a:*void* ;
: get-cuda-function ( string -- function ) : get-function-ptr ( string -- function )
[ cuda-module get ] dip get-cuda-function* ; [ cuda-module get ] dip get-function-ptr* ;
: with-cuda-function ( string quot -- ) : with-cuda-function ( string quot -- )
[ [
get-cuda-function cuda-function set get-function-ptr* cuda-function set
] dip call ; inline ] dip call ; inline
: cached-cuda-function ( string -- alien )
cuda-functions get [ get-function-ptr ] cache ;
: launch-function* ( function -- ) cuLaunch cuda-error ; : launch-function* ( function -- ) cuLaunch cuda-error ;
: launch-function ( -- ) cuda-function get cuLaunch cuda-error ; : launch-function ( -- ) cuda-function get cuLaunch cuda-error ;
@ -157,7 +188,7 @@ M: cuda-memory byte-length length>> ;
: cuda-malloc ( n -- ptr ) : cuda-malloc ( n -- ptr )
[ CUdeviceptr <c-object> ] dip [ CUdeviceptr <c-object> ] dip
[ cuMemAlloc cuda-error ] 2keep [ cuMemAlloc cuda-error ] 2keep
[ *int ] dip <cuda-memory> add-cuda-memory ; [ a:*int ] dip <cuda-memory> add-cuda-memory ;
: cuda-free* ( ptr -- ) : cuda-free* ( ptr -- )
cuMemFree cuda-error ; cuMemFree cuda-error ;
@ -237,9 +268,9 @@ ERROR: bad-cuda-parameter parameter ;
offset param-size ; offset param-size ;
: cuda-device-attribute ( attribute dev -- n ) : cuda-device-attribute ( attribute dev -- n )
[ int <c-object> ] 2dip [ a:int <c-object> ] 2dip
[ cuDeviceGetAttribute cuda-error ] [ cuDeviceGetAttribute cuda-error ]
[ 2drop *int ] 3bi ; [ 2drop a:*int ] 3bi ;
: function-block-shape* ( function x y z -- ) : function-block-shape* ( function x y z -- )
cuFuncSetBlockShape cuda-error ; cuFuncSetBlockShape cuda-error ;
@ -289,20 +320,46 @@ ERROR: bad-cuda-parameter parameter ;
"CUDA Version: " write cuda-version number>string print nl "CUDA Version: " write cuda-version number>string print nl
#cuda-devices iota [ nl ] [ cuda-device. ] interleave ; #cuda-devices iota [ nl ] [ cuda-device. ] interleave ;
: 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 ;
: test-cuda0 ( -- ) : run-function-launcher ( function-launcher function -- )
T{ launcher swap
{ path "vocab:cuda/hello.ptx" } {
{ block-shape { 6 6 6 } } [ dim-block>> first3 function-block-shape* ]
{ shared-size 2 } [ shared-size>> function-shared-size* ]
{ grid { 2 6 } } [
} [ dim-grid>> [
"helloWorld" [ launch-function*
"Hello World!" [ - ] map-index ] [
malloc-device-string &dispose first2 launch-function-grid*
] if-empty
]
} 2cleave ;
[ 1array set-parameters ] : cuda-argument-setter ( offset c-type -- offset' quot )
[ drop launch ] c-type>cuda-setter
[ device>host utf8 alien>string . ] tri [ over [ + ] dip ] dip
] with-cuda-function '[ swap _ swap _ call ] ;
] with-cuda ;
MACRO: cuda-arguments ( c-types -- quot: ( args... function -- ) )
[ 0 ] dip [ cuda-argument-setter ] map reverse
swap '[ _ param-size* ] suffix
'[ _ cleave ] ;
: define-cuda-word ( word string arguments -- )
[
'[
_ get-function-ptr
[ nip _ cuda-arguments ]
[ run-function-launcher ] 2bi
]
]
[ nip \ function-launcher suffix a:void function-effect ]
2bi define-declared ;

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -0,0 +1,30 @@
! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types alien.strings cuda cuda.syntax destructors
io.encodings.utf8 kernel locals math prettyprint sequences ;
IN: cuda.hello-world
CUDA-LIBRARY: hello vocab:cuda/hello.ptx
CUDA-FUNCTION: helloWorld ( char* string-ptr ) ;
:: cuda-hello-world ( -- )
T{ launcher
{ device 0 }
{ path "vocab:cuda/hello.ptx" }
} [
"Hello World!" [ - ] map-index malloc-device-string &dispose dup :> str
T{ function-launcher
{ dim-block { 6 1 1 } }
{ dim-grid { 2 1 } }
{ shared-size 0 }
}
helloWorld
! <<< { 6 1 1 } { 2 1 } 1 >>> helloWorld
str device>host utf8 alien>string .
] with-cuda ;
MAIN: cuda-hello-world

View File

@ -0,0 +1,2 @@
Doug Coleman
Joe Groff

View File

@ -0,0 +1,21 @@
! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types cuda cuda.syntax locals ;
IN: cuda.demos.prefix-sum
CUDA-LIBRARY: prefix-sum vocab:cuda/demos/prefix-sum/prefix-sum.ptx
CUDA-FUNCTION: prefix_sum_block ( uint* in, uint* out, uint n ) ;
:: cuda-prefix-sum ( -- )
T{ launcher
{ device 0 }
{ path "vocab:cuda/demos/prefix-sum/prefix-sum.ptx" }
} [
! { 1 1 1 } { 2 1 } 0 3<<< prefix_sum_block
] with-cuda ;
MAIN: cuda-prefix-sum

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -0,0 +1,15 @@
! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: alien.parser cuda kernel lexer parser ;
IN: cuda.syntax
SYNTAX: CUDA-LIBRARY: scan scan add-cuda-library ;
SYNTAX: CUDA-FUNCTION:
scan [ create-in ] [ ] bi ";" scan-c-args drop define-cuda-word ;
: 3<<< ( dim-block dim-grid shared-size -- function-launcher )
f function-launcher boa ;
: 4<<< ( dim-block dim-grid shared-size stream -- function-launcher )
function-launcher boa ;