PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/extra/cuda/memory/memory.factor

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