]> gitweb.factorcode.org Git - factor.git/blob - extra/cuda/memory/memory.factor
f3c452093a7ea044e2e2d6e732c82406698bd527
[factor.git] / extra / cuda / memory / memory.factor
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
10 : cuda-malloc ( n -- ptr )
11     [ CUdeviceptr <c-object> ] dip
12     '[ _ cuMemAlloc cuda-error ] keep
13     c:*int ; inline
14
15 : cuda-malloc-type ( n type -- ptr )
16     c:heap-size * cuda-malloc ; inline
17
18 : cuda-free ( ptr -- )
19     cuMemFree cuda-error ; inline
20
21 DESTRUCTOR: cuda-free
22
23 : memcpy-device>device ( dest-ptr src-ptr count -- )
24     cuMemcpyDtoD cuda-error ; inline
25
26 : memcpy-device>array ( dest-array dest-index src-ptr count -- )
27     cuMemcpyDtoA cuda-error ; inline
28
29 : memcpy-array>device ( dest-ptr src-array src-index count -- )
30     cuMemcpyAtoD cuda-error ; inline
31
32 : memcpy-array>host ( dest-ptr src-array src-index count -- )
33     cuMemcpyAtoH cuda-error ; inline
34
35 : memcpy-host>array ( dest-array dest-index src-ptr count -- )
36     cuMemcpyHtoA cuda-error ; inline
37
38 : memcpy-array>array ( dest-array dest-index src-array src-ptr count -- )
39     cuMemcpyAtoA cuda-error ; inline
40
41 : memcpy-host>device ( dest-ptr src-ptr count -- )
42     cuMemcpyHtoD cuda-error ; inline
43
44 : memcpy-device>host ( dest-ptr src-ptr count -- )
45     cuMemcpyDtoH cuda-error ; inline
46
47 : host>device ( data -- ptr )
48     [ >c-ptr ] [ byte-length ] bi
49     [ nip cuda-malloc dup ] [ memcpy-host>device ] 2bi ; inline
50
51 : device>host ( ptr len -- byte-array )
52     [ nip <byte-array> dup ] [ memcpy-device>host ] 2bi ; inline