factor/extra/cuda/memory/memory.factor

49 lines
1.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
2010-05-03 21:18:10 -04:00
USING: accessors alien alien.data alien.destructors assocs
byte-arrays cuda.ffi cuda.utils destructors fry io.encodings.string
io.encodings.utf8 kernel locals namespaces sequences strings ;
QUALIFIED-WITH: alien.c-types c
IN: cuda.memory
: cuda-malloc ( n -- ptr )
[ CUdeviceptr <c-object> ] dip
2010-05-03 21:18:10 -04:00
'[ _ cuMemAlloc cuda-error ] keep
c:*int ;
2010-05-03 21:18:10 -04:00
: cuda-free ( ptr -- )
cuMemFree cuda-error ;
2010-05-03 21:18:10 -04:00
DESTRUCTOR: cuda-free
: memcpy-device>device ( dest-ptr src-ptr count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyDtoD cuda-error ; inline
: memcpy-device>array ( dest-array dest-index src-ptr count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyDtoA cuda-error ; inline
: memcpy-array>device ( dest-ptr src-array src-index count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyAtoD cuda-error ; inline
: memcpy-array>host ( dest-ptr src-array src-index count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyAtoH cuda-error ; inline
: memcpy-host>array ( dest-array dest-index src-ptr count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyHtoA cuda-error ; inline
: memcpy-array>array ( dest-array dest-index src-array src-ptr count -- )
2010-05-03 21:18:10 -04:00
cuMemcpyAtoA cuda-error ; inline
2010-05-03 21:18:10 -04:00
: memcpy-host>device ( dest-ptr src-ptr count -- )
cuMemcpyHtoD cuda-error ; inline
2010-05-03 21:18:10 -04:00
: memcpy-device>host ( dest-ptr src-ptr count -- )
cuMemcpyDtoH cuda-error ; inline
2010-05-03 21:18:10 -04:00
: host>device ( data -- ptr )
[ >c-ptr ] [ byte-length ] bi
[ nip cuda-malloc dup ] [ memcpy-host>device ] 2bi ; inline
2010-05-03 21:18:10 -04:00
: device>host ( ptr len -- byte-array )
[ nip <byte-array> dup ] [ memcpy-device>host ] 2bi ; inline