Skip to content

Commit

Permalink
Several updates to make using julia nicer.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Dec 27, 2020
1 parent 46374d7 commit b5c1029
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.nrepl-port
.hgignore
.hg/
julia-1.5.3*
julia
openj9
openj9
.cpcache
*.png
2 changes: 1 addition & 1 deletion examples/fractal/deps.edn
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{:deps {cnuernber/libjulia-clj {:mvn/version "0.01"}}}
{:deps {cnuernber/libjulia-clj {:mvn/version "0.05-SNAPSHOT"}}}
8 changes: 1 addition & 7 deletions examples/fractal/julia_fractals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,14 @@ end")


(def jl-fn* (delay (do (julia/initialize!)
(julia/eval-string julia-code))))
(julia/jl julia-code))))


(defn jl-fractal
[]
(let [jl-fn @jl-fn*]
(-> (jl-fn i1 i2 d zoom-factor fract-width fract-height)
(dtt/ensure-tensor)
;;Julia is column-major so our image comes out widthxheight
;;datatype is row major.
(dtt/transpose [1 0])
;;The tensor library *knows* the original was transposed so transposing the result
;;back into row-major means the memory can be read in order and thus
;;the copy operation below is one large memcopy into a jvm byte array.
(dtype/copy! (bufimg/new-image fract-height fract-width :byte-gray)))))


Expand Down
6 changes: 3 additions & 3 deletions src/libjulia_clj/impl/base.clj
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,15 @@
dtype-proto/PShape
(shape [this]
(let [rank (julia-jna/jl_array_rank handle)]
(mapv #(julia-jna/jl_array_size handle %) (range rank))))
(mapv #(julia-jna/jl_array_size handle %)
(reverse (range rank)))))
dtype-proto/PECount
(ecount [this]
(long (apply * (dtype-proto/shape this))))
dtype-proto/PToTensor
(as-tensor [this]
(-> (julia-array->nd-descriptor handle)
(dtt/nd-buffer-descriptor->tensor)
(dtt/transpose (vec (reverse (range (julia-jna/jl_array_rank handle)))))))
(dtt/nd-buffer-descriptor->tensor)))
dtype-proto/PToNDBufferDesc
(convertible-to-nd-buffer-desc? [this] true)
(->nd-buffer-descriptor [this] (julia-array->nd-descriptor handle))
Expand Down
19 changes: 11 additions & 8 deletions src/libjulia_clj/julia.clj
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,26 @@ user> jl-ary
(defonce ^{:doc "Resolves to the julia undef type"}
jl-undef* (delay (jl "Base.undef")))

(defn new-julia-array
"Create a new, uninitialized dense julia array"
([shape {:keys [datatype] :or {datatype :float64}}]

(defn new-array
"Create a new, uninitialized dense julia array. Because Julia is column major
while tech.v3.datatype is row major, the returned array's size will be the
reverse of dtype/shape as that keeps the same in memory alignment of data."
([shape datatype]
(let [jl-dtype (base/lookup-julia-type datatype)
ary-type (apply-type @base-ary-type* jl-dtype)]
(apply ary-type @jl-undef* shape)))
([shape]
(new-julia-array shape nil)))
(new-array shape :float64)))


(defn ->julia-array
(defn ->array
"Create a new dense julia array that is the 'transpose' of the input tensor.
Transposing ensures the memory alignment matches and as Julia is column-major
while datatype is row-major."
[tens]
(let [dtype (dtype/elemwise-datatype tens)
tens-shape (dtype/shape tens)
tens-rank (count tens-shape)
retval (new-julia-array tens-shape dtype)]
(dtype/copy! tens (dtt/transpose retval (reverse (range tens-rank))))
retval (new-array (reverse tens-shape) dtype)]
(dtype/copy! tens retval)
retval))
11 changes: 6 additions & 5 deletions test/libjulia_clj/julia_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
;;with each other.
(System/gc)
(jl/cycle-gc!)
(is (= [3 4] (dtype/shape jl-ary)))
(is (= [[25.0 25.0 25.0 25.0]
[1.0 1.0 1.0 1.0]
[1.0 1.0 1.0 1.0]]
;;datatype is transpose of julia
(is (= [4 3] (dtype/shape jl-ary)))
(is (= [[25.0 25.0 25.0]
[1.0 1.0 1.0]
[1.0 1.0 1.0]
[1.0 1.0 1.0]]
(mapv vec (dtt/as-tensor jl-ary)))))
(System/gc)
(jl/cycle-gc!))
Expand Down Expand Up @@ -80,5 +82,4 @@ end)")
[]
(let [doasync (jl "function doasync(cback, arg) @async cback(arg) end")
{:keys [ary-list raw-clj-fn before after wrapper]} (make-task-wrapper)]

[before,after,wrapper]))

0 comments on commit b5c1029

Please sign in to comment.