diff --git a/extra/cuda/cuda.factor b/extra/cuda/cuda.factor index 94e10a96dd..b2687d1cb6 100644 --- a/extra/cuda/cuda.factor +++ b/extra/cuda/cuda.factor @@ -3,7 +3,7 @@ USING: accessors alien alien.data alien.parser alien.strings alien.syntax arrays assocs byte-arrays classes.struct combinators continuations cuda.ffi cuda.memory cuda.utils -destructors fry io io.backend io.encodings.string +destructors fry init io io.backend io.encodings.string io.encodings.utf8 kernel lexer locals macros math math.parser namespaces nested-comments opengl.gl.extensions parser prettyprint quotations sequences words ; @@ -14,6 +14,10 @@ TUPLE: launcher { device integer initial: 0 } { device-flags initial: 0 } ; +: ( device-id -- launcher ) + launcher new + swap >>device ; inline + TUPLE: function-launcher dim-block dim-grid shared-size stream ; @@ -81,3 +85,5 @@ MACRO: cuda-arguments ( c-types -- quot: ( args... function -- ) ) ] [ 2nip \ function-launcher suffix a:void function-effect ] 3bi define-declared ; + +[ init-cuda ] "cuda-init" add-startup-hook diff --git a/extra/cuda/demos/hello-world/hello-world.factor b/extra/cuda/demos/hello-world/hello-world.factor index 19951c709c..789948be68 100644 --- a/extra/cuda/demos/hello-world/hello-world.factor +++ b/extra/cuda/demos/hello-world/hello-world.factor @@ -1,20 +1,23 @@ ! Copyright (C) 2010 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.c-types alien.strings cuda cuda.memory cuda.syntax -destructors io io.encodings.utf8 kernel locals math sequences ; +USING: accessors alien.c-types alien.strings cuda cuda.devices +cuda.memory cuda.syntax cuda.utils destructors io +io.encodings.string io.encodings.utf8 kernel locals math +math.parser namespaces sequences ; IN: cuda.demos.hello-world CUDA-LIBRARY: hello vocab:cuda/demos/hello-world/hello.ptx CUDA-FUNCTION: helloWorld ( char* string-ptr ) ; -:: cuda-hello-world ( -- ) - T{ launcher { device 0 } } [ - "Hello World!" [ - ] map-index host>device &dispose :> str +: cuda-hello-world ( -- ) + [ + cuda-launcher get device>> number>string + "CUDA device " ": " surround write + "Hello World!" [ - ] map-index host>device - str { 6 1 1 } { 2 1 } 2<<< helloWorld - - str device>host utf8 alien>string print - ] with-cuda ; + [ { 6 1 1 } { 2 1 } 2<<< helloWorld ] + [ device>host utf8 decode print ] bi + ] with-each-cuda-device ; MAIN: cuda-hello-world diff --git a/extra/cuda/utils/utils.factor b/extra/cuda/utils/utils.factor index 912b9e2e92..32e8bf2fac 100644 --- a/extra/cuda/utils/utils.factor +++ b/extra/cuda/utils/utils.factor @@ -141,3 +141,6 @@ ERROR: no-cuda-library name ; : function-shared-size ( n -- ) [ cuda-function get ] dip cuFuncSetSharedSize cuda-error ; + +: with-each-cuda-device ( quot -- ) + [ enumerate-cuda-devices ] dip '[ _ with-cuda ] each ; inline