1 ! Copyright (C) 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.data continuations cuda cuda.ffi
4 cuda.libraries alien.destructors fry kernel namespaces ;
7 : set-up-cuda-context ( -- )
8 H{ } clone cuda-modules set-global
9 H{ } clone cuda-functions set-global ; inline
11 : create-context ( device flags -- context )
13 [ { CUcontext } ] 2dip
14 '[ _ _ cuCtxCreate cuda-error ] with-out-parameters ; inline
17 cuCtxSynchronize cuda-error ; inline
19 : context-device ( -- n )
20 { CUdevice } [ cuCtxGetDevice cuda-error ] with-out-parameters ; inline
22 : destroy-context ( context -- ) cuCtxDestroy cuda-error ; inline
24 : clean-up-context ( context -- )
25 [ sync-context ] ignore-errors destroy-context ; inline
27 DESTRUCTOR: destroy-context
28 DESTRUCTOR: clean-up-context
30 : (with-cuda-context) ( context quot -- )
31 swap '[ _ clean-up-context ] [ ] cleanup ; inline
33 : with-cuda-context ( device flags quot -- )
34 [ set-up-cuda-context create-context ] dip (with-cuda-context) ; inline