-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.clj
107 lines (94 loc) · 2.88 KB
/
dev.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(ns lexref.dev
(:gen-class)
(:require
[lexref.core :refer [with-lexref]]
[lexref.lexref :refer [lex-ref? lex-ref-create]]
[lexref.apply :refer [lex-ref-apply]]
[lexref.tree :refer [tree? leaf-map]]
[lexref.resource :refer [release!]]
[libpython-clj2.python :as py]
[libpython-clj2.python.np-array]
[libpython-clj2.require :refer [require-python]]
[libpython-clj2.python.ffi :as py-ffi]
[libpython-clj2.python.gc :as py-gc]))
(require-python '[builtins :as pyb])
(require-python '[numpy :as np])
(defmethod release! java.lang.Number [this]
(println "release number " this))
(defn- lex-ref->map [x]
(cond (lex-ref? x) {:value (:value x)
:count @(:count x)
:released? @(:released? x)}
(tree? x) (leaf-map lex-ref->map x)
:else x))
(println "Test return temporary argument")
(let [temp (lex-ref-create 13)
bound (lex-ref-create 37 1)]
(println)
(println "temp before: " (lex-ref->map temp))
(println "bound before: " (lex-ref->map bound))
(let [result (lex-ref-apply (fn [a _] a) [temp bound])]
(println "result " (lex-ref->map result))
(println "temp after: " (lex-ref->map temp))
(println "bound after: " (lex-ref->map bound))))
(println)
(println "Test return bound argument")
(let [temp (lex-ref-create 13)
bound (lex-ref-create 37 1)]
(println)
(println "temp before: " (lex-ref->map temp))
(println "bound before: " (lex-ref->map bound))
(let [result (lex-ref-apply (fn [_ b] [b b]) [temp bound])]
(println "result " (lex-ref->map result))
(println "temp after: " (lex-ref->map temp))
(println "bound after: " (lex-ref->map bound))))
(println "Test with-lexref with external var")
(def outer-var 3)
(pprint
(with-lexref [outer-var]
(let [inner-var (* outer-var outer-var)]
(+ outer-var inner-var))))
(println)
(println "Test with-lexref reduce")
(pprint
(with-lexref
(let [x 1 y 2 z 4]
(reduce + [x y z]))))
(println)
(defmethod release! :pyobject [x]
(when (pyb/isinstance x np/ndarray)
(println "release numpy array: " (np/shape x))
(py-ffi/Py_DecRef x)))
(defmacro with-python
([vars body]
`(py-gc/with-disabled-gc
(py/with-gil
(with-lexref ~vars
~body))))
([body]
`(with-python [] ~body)))
(defn -main [& args]
(def n 10)
(def shape [n n n])
(println "create x")
(def xs
(time
(with-python
[(np/add (np/ones shape :dtype :uint8)
(np/ones shape :dtype :uint8))
(np/multiply 3 (np/ones shape :dtype :uint8))])))
(println "move x, create y")
(def y
(time
(with-python [xs]
(np/add (first xs) (second xs)))))
(println "release y")
(time (release! y))
(def zs
(time
(with-python
(let [xs (np/ones shape :dtype :uint8)
ys (np/ones shape :dtype :uint8)]
(np/add xs ys)))))
;; (System/exit 0)
)