/extra/cuda/memory/memory.factor

http://github.com/abeaumont/factor · Factor · 51 lines · 35 code · 14 blank · 2 comment · 0 complexity · 45cff9ac302a651c537c3a7ef85510c9 MD5 · raw file

  1. ! Copyright (C) 2010 Doug Coleman.
  2. ! See http://factorcode.org/license.txt for BSD license.
  3. USING: accessors alien alien.data alien.destructors assocs
  4. byte-arrays cuda cuda.ffi destructors fry io.encodings.string
  5. io.encodings.utf8 kernel locals math namespaces sequences
  6. strings ;
  7. QUALIFIED-WITH: alien.c-types c
  8. IN: cuda.memory
  9. : cuda-malloc ( n -- ptr )
  10. [ { CUdeviceptr } ] dip
  11. '[ _ cuMemAlloc cuda-error ] with-out-parameters ; inline
  12. : cuda-malloc-type ( n type -- ptr )
  13. c:heap-size * cuda-malloc ; inline
  14. : cuda-free ( ptr -- )
  15. cuMemFree cuda-error ; inline
  16. DESTRUCTOR: cuda-free
  17. : memcpy-device>device ( dest-ptr src-ptr count -- )
  18. cuMemcpyDtoD cuda-error ; inline
  19. : memcpy-device>array ( dest-array dest-index src-ptr count -- )
  20. cuMemcpyDtoA cuda-error ; inline
  21. : memcpy-array>device ( dest-ptr src-array src-index count -- )
  22. cuMemcpyAtoD cuda-error ; inline
  23. : memcpy-array>host ( dest-ptr src-array src-index count -- )
  24. cuMemcpyAtoH cuda-error ; inline
  25. : memcpy-host>array ( dest-array dest-index src-ptr count -- )
  26. cuMemcpyHtoA cuda-error ; inline
  27. : memcpy-array>array ( dest-array dest-index src-array src-ptr count -- )
  28. cuMemcpyAtoA cuda-error ; inline
  29. : memcpy-host>device ( dest-ptr src-ptr count -- )
  30. cuMemcpyHtoD cuda-error ; inline
  31. : memcpy-device>host ( dest-ptr src-ptr count -- )
  32. cuMemcpyDtoH cuda-error ; inline
  33. : host>device ( data -- ptr )
  34. [ >c-ptr ] [ byte-length ] bi
  35. [ nip cuda-malloc dup ] [ memcpy-host>device ] 2bi ; inline
  36. : device>host ( ptr len -- byte-array )
  37. [ nip <byte-array> dup ] [ memcpy-device>host ] 2bi ; inline